<commit_before>/**
 * Copyright Kitware Inc.
 *
 * This is the PhantomJS runtime script that invokes the Girder app in test
 * mode. The test mode page is built with grunt and lives at:
 * clients/web/static/built/testing/testEnv.html. It then executes a Jasmine spec within
 * the context of that test application, and afterwards runs our custom coverage
 * handler on the coverage data.
 */
/* globals phantom, WebPage, jasmine, girderTest */

var system = require('system');
var args = phantom.args ? phantom.args : system.args.slice(1);

if (args && args.length < 2) {
    console.error('Usage: phantomjs phantom_jasmine_runner.js <page> <spec> [<covg_output> [<default jasmine timeout>]');
    console.error('  <page> is the path to the HTML page to load');
    console.error('  <spec> is the path to the Jasmine spec to run.');
    console.error('  <covg_output> is the path to a file to write coverage into.');
    console.error('  <default Jasmine timeout> is in milliseconds.');
    phantom.exit(2);
}

var env = system.env;

var pageUrl = args[0];
var spec = args[1];
var coverageOutput = args[2] || null;
var page = new WebPage();

var fs = require('fs');
require('event-source/global');

try {
    fs.remove(coverageOutput);
} catch (e) {
}

// write coverage results to a file
var reportCoverage = function () {
    if (!coverageOutput) {
        return;
    }

    var cov = page.evaluate(function () {
        return window.__coverage__;
    });

    cov = cov || {};
    fs.write(coverageOutput, JSON.stringify(cov), 'w');
};

// Set decent viewport size for screenshots.
page.viewportSize = {
    width: 1024,
    height: 769
};

page.onConsoleMessage = function (msg) {
    if (msg.indexOf('__SCREENSHOT__') === 0) {
        var imageFile = msg.substring('__SCREENSHOT__'.length) || 'phantom_screenshot.png';
        page.render(imageFile);
        console.log('Created screenshot: ' + imageFile);

        console.log('<DartMeasurementFile name="PhantomScreenshot" type="image/png">' +
            fs.workingDirectory + fs.separator + imageFile + '</DartMeasurementFile>');

        if (env['PHANTOMJS_OUTPUT_AJAX_TRACE'] === undefined ||
            env['PHANTOMJS_OUTPUT_AJAX_TRACE'] === 1 ||
            env['PHANTOMJS_OUTPUT_AJAX_TRACE'] === true) {
            console.log('Dumping ajax trace:');
            console.log(page.evaluate(function () {
                return JSON.stringify(girderTest.ajaxLog(true), null, '  ');
            }));
        }
        return;
    } else if (msg === 'ConsoleReporter finished') {
        var success = this.page.evaluate(function () {
            return window.jasmine_phantom_reporter.status === 'success';
        });
        if (success) {
            reportCoverage();
        }<commit_msg>Adding a necessary console.log because reasons<commit_after>/**
 * Copyright Kitware Inc.
 *
 * This is the PhantomJS runtime script that invokes the Girder app in test
 * mode. The test mode page is built with grunt and lives at:
 * clients/web/static/built/testing/testEnv.html. It then executes a Jasmine spec within
 * the context of that test application, and afterwards runs our custom coverage
 * handler on the coverage data.
 */
/* globals phantom, WebPage, jasmine, girderTest */

var system = require('system');
var args = phantom.args ? phantom.args : system.args.slice(1);

if (args && args.length < 2) {
    console.error('Usage: phantomjs phantom_jasmine_runner.js <page> <spec> [<covg_output> [<default jasmine timeout>]');
    console.error('  <page> is the path to the HTML page to load');
    console.error('  <spec> is the path to the Jasmine spec to run.');
    console.error('  <covg_output> is the path to a file to write coverage into.');
    console.error('  <default Jasmine timeout> is in milliseconds.');
    phantom.exit(2);
}

var env = system.env;

var pageUrl = args[0];
var spec = args[1];
var coverageOutput = args[2] || null;
var page = new WebPage();

var fs = require('fs');
require('event-source/global');

try {
    fs.remove(coverageOutput);
} catch (e) {
}

// write coverage results to a file
var reportCoverage = function () {
    if (!coverageOutput) {
        return;
    }

    var cov = page.evaluate(function () {
        return window.__coverage__;
    });

    cov = cov || {};
    fs.write(coverageOutput, JSON.stringify(cov), 'w');
};

// Set decent viewport size for screenshots.
page.viewportSize = {
    width: 1024,
    height: 769
};

page.onConsoleMessage = function (msg) {
    console.log(msg); // this needs to be here or the tests fail...
    if (msg.indexOf('__SCREENSHOT__') === 0) {
        var imageFile = msg.substring('__SCREENSHOT__'.length) || 'phantom_screenshot.png';
        page.render(imageFile);
        console.log('Created screenshot: ' + imageFile);

        console.log('<DartMeasurementFile name="PhantomScreenshot" type="image/png">' +
            fs.workingDirectory + fs.separator + imageFile + '</DartMeasurementFile>');

        if (env['PHANTOMJS_OUTPUT_AJAX_TRACE'] === undefined ||
            env['PHANTOMJS_OUTPUT_AJAX_TRACE'] === 1 ||
            env['PHANTOMJS_OUTPUT_AJAX_TRACE'] === true) {
            console.log('Dumping ajax trace:');
            console.log(page.evaluate(function () {
                return JSON.stringify(girderTest.ajaxLog(true), null, '  ');
            }));
        }
        return;
    } else if (msg === 'ConsoleReporter finished') {
        var success = this.page.evaluate(function () {
            return window.jasmine_phantom_reporter.status === 'success';
        });
        if (success) {
            reportCoverage();
        }
{'fraction_non_alphanumeric': 0.13449564134495642, 'fraction_numerical': 0.005337128624799858, 'mean_word_length': 3.0272206303724927, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2243559', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1667, 'n_words': 475}