<commit_before>// runner module.
var mocha = require("mocha");
var should = require("chai").should();
var path = require("path");
var fs = require("fs");
var looksSame = require('looks-same');

exports.run = function(url) {
    var Test = require("mocha/lib/test");
    var phantom = require("phantom");
    describe("Phantom", function() {
        var ph;
        var page;
        var tests = {};
        var suite = this;
        before(function(done) {
            this.timeout(15000);
            phantom.create(function(ph2) {
                ph = ph2;
                ph.createPage(function(p) {
                    page = p;
                    page.set("onError", function(data) {
                        throw new Error(data);
                    });
                    page.set("onResourceError", function(resourceError) {
                        suite.emit("error", new Error(resourceError.errorString));
                    });
                    page.set("onCallback", function(data) {
                        if (!data) return;
                        if (data.event == "mochaReady") {
                            page.evaluate(function() {
                                mocha.run();
                            });
                        } else if (data.event == "mochaPass") {
                            var test = new Test(data.title, function(done) {
                                done();
                            });
                            suite.addTest(test);

                        } else if (data.event == "mochaPending") {
                            var test = new Test(data.title);
                            suite.addTest(test);
                        } else if (data.event == "mochaFail") {
                            var test = new Test(data.title, function(done) {
                                throw (new Error(data.message));
                            });
                            suite.addTest(test);
                        } else if (data.event == "mochaDone") {
                            done();
                        } else if (data.event == "screenshotCheck") {
                            // todo: save/check screenshots
                            var basepath = "./screenshots/" + data.title + "";

                            var goodImage = basepath + " - good.png";
                            var badImage = basepath + " - bad.png";
                            var currentImage = basepath + " - current.png";
                            var diffImage = basepath + " - diff.png";
                            page.set("clipRect", data.clipRect);
                            if (fs.existsSync(currentImage)) fs.unlinkSync(currentImage);
                            page.render(currentImage, function() {
<commit_msg>support running in gulp task<commit_after>// runner module.
var Mocha = require('mocha');

var should = require("chai").should();
var path = require("path");
var fs = require("fs");
var looksSame = require('looks-same');

exports.run = function(url, options, callback) {
    if (typeof(describe) === "undefined") {
        // must be running in gulp - need to set up new mocha instance
        runInGulp(url, options, callback);
    } else {
        // running in mocha
        runInternal(url);
    }
}

function runInGulp(url, options, done) {
    var mocha = new Mocha(options);

    mocha.suite.emit('pre-require', global, "not a file", mocha);
    //    mocha.suite.emit('require', require(file), file, self);
    //    mocha.suite.emit('post-require', global, file, self);

    runInternal(url);
    mocha.run(function(e) {
        if (done) done(e);
    });
}

function runInternal(url) {
    var phantom = require("phantom");
    describe("Phantom", function() {
        var ph;
        var page;
        var tests = {};
        var suite = this;
        before(function(done) {
            this.timeout(600000);
            phantom.create(function(ph2) {
                ph = ph2;
                ph.createPage(function(p) {
                    page = p;
                    page.set("viewportSize", {
                        width: 1024,
                        height: 1024
                    })
                    page.set("onError", function(data) {
                        throw new Error(data);
                    });
                    page.set("onResourceError", function(resourceError) {                        
                        // ignore resource errors for now
                        // todo: add an option to fail on all resource errors
                        console.log(resourceError.errorString);
                        // suite.emit("error", new Error(resourceError.errorString));
                    });
                    page.set("onCallback", function(data) {
                        if (!data) return;
                        if (data.event == "mochaReady") {
                            page.evaluate(function() {
                                mocha.run();
                            });
                        } else if (data.event == "mochaPass") {
                            var test = new Mocha.Test(data.title, function(done) {
                                done();
                            });
                            suite.addTest(test);

                        } else if (data.event == "mochaPending") {
                            var test = new Mocha.Test(data.title);
                            suite.addTest(test);
                        } else if (data.event == "mochaFail") {
                            var test = new Mocha.Test(data.title, function(done) {
                                throw (new Error(data.message));
                            });
                            suite.addTest(test);
                        } else if (data.event == "mochaDone") {
                            done();
                        } else if (data.event == "screenshotCheck") {
                            // todo: save/check screenshots
                            var basepath = path.resolve("./screenshots/" + data.title + "");

                            var goodImage = basepath + " - good.png";
                            var badImage = basepath + " - bad.png";
                            var currentImage = basepath + " - current.png";
                            var diffImage = basepath + " - diff.png";
                            page.set("clipRect", data.clipRect);
                            if (fs.existsSync(currentImage)) fs.unlinkSync(currentImage);
                            page.render(currentImage, function() {
                            console.log(currentImage);
                            console.log(fs.existsSync(currentImage));

                            console.log(data.clipRect);

{'fraction_non_alphanumeric': 0.11541863207547169, 'fraction_numerical': 0.0033903301886792454, 'mean_word_length': 1.1104199066874028, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '755841', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1585, 'n_words': 353}