<commit_before>
var phantomcss = require('../../node_modules/phantomcss/phantomcss.js');

function prepareScreenshots() {
    var tests = document.querySelectorAll('.c-test');
    var testCount = 1;
    var caseCount = 1;
    var shotCount = 0;

    [].forEach.call(tests, function(test, index) {
        var targets = test.querySelectorAll('.c-test__run');

        [].forEach.call(targets, function(target) {
            target.setAttribute('data-shot-id', shotCount);
            target.setAttribute('data-shot-counter', testCount + '.' + caseCount);

            caseCount++;
            shotCount++;
        });

        caseCount = 1;
        testCount++;
    });

    return document.querySelectorAll('[data-shot-id]').length;
}

function getScreenshotName(index) {
    var subject = document.querySelector('[data-shot-id="' + index + '"]');
    var prefix = subject.getAttribute('data-shot-counter');
    var suffix = subject.getAttribute('data-shot-name') || false;

    return prefix + '-' + (suffix ? suffix : 'arrange');
}

phantomcss.init({
    libraryRoot: '../../node_modules/phantomcss',
    screenshotRoot: './screenshots',
    failedComparisonsRoot: './failures',
    fileNameGetter: function(root, fileName) {
        var name;

        fileName = fileName || 'screenshot';
        name = root + fs.separator + fileName;

        if ( fs.isFile(name + '.png') ) {
            return name + '.diff.png';
        } else {
            return name + '.png';
        }
    }
});

casper.on('remote.message', function(message) {
    this.echo('Remote console.log: ' + message);
});

casper
    .start()
    .zoom(2)
    .viewport(1200, 1200);

casper.thenOpen('../../tests/visual/components/arrange/index.html');

casper.then(function() {
    var shotCount = this.evaluate(prepareScreenshots);

    for (var i = 0; i < shotCount; i++) {
        var shotName = this.evaluate(getScreenshotName, i);

        phantomcss.screenshot('[data-shot-id="' + i + '"]', shotName);
    };
});

casper.then(function compare() {
    phantomcss.compareAll();
});

casper.then(function done() {
    casper.test.done();
});

casper.run(function() {
    console.log('\nDone.');
    phantom.exit( phantomcss.getExitStatus() );
});
<commit_msg>Automate running regression over all visual tests.<commit_after>
var fs = require('fs');
var utils = require('utils');
var phantomcss = require('../../node_modules/phantomcss/phantomcss.js');

var visualTestsDir = '../visual';
var clientHelpers = {};

function getParentDir(filePath) {
    if ( fs.exists(filePath) && fs.isFile(filePath) ) {
        var segments = filePath.split(fs.separator);
        return segments[segments.length - 2];
    }
}

function getTestFiles(path) {
    var files = [];

    if ( fs.exists(path) && fs.isFile(path) && utils.fileExt(path) === 'html' ) {
        files.push(path);
    } else if ( fs.isDirectory(path) ) {
        fs.list(path).forEach(function(file) {
            if (file !== '.' && file !== '..') { // Avoid loops.
                files = files.concat( getTestFiles(path + '/' + file) );
            }
        });
    }

    return files;
};

clientHelpers.prepareScreenshots = function prepareScreenshots() {
    var tests = document.querySelectorAll('.c-test');
    var testCount = 1;
    var caseCount = 1;
    var shotCount = 0;

    [].forEach.call(tests, function(test, index) {
        var targets = test.querySelectorAll('.c-test__run');

        [].forEach.call(targets, function(target) {
            target.setAttribute('data-shot-id', shotCount);
            target.setAttribute('data-shot-counter', testCount + '.' + caseCount);

            caseCount++;
            shotCount++;
        });

        caseCount = 1;
        testCount++;
    });

    return document.querySelectorAll('[data-shot-id]').length;
}

clientHelpers.getScreenshotName = function getScreenshotName(index) {
    var subject = document.querySelector('[data-shot-id="' + index + '"]');
    var prefix = subject.getAttribute('data-shot-counter');
    var suffix = subject.getAttribute('data-shot-name') || false;

    return prefix + '-' + (suffix ? suffix : 'arrange');
}

phantomcss.init({
    libraryRoot: '../../node_modules/phantomcss',
    screenshotRoot: './screenshots',
    failedComparisonsRoot: './failures',
    fileNameGetter: function(root, fileName) {
        var name;

        fileName = fileName || 'screenshot';
        name = root + fs.separator + fileName;

        if ( fs.isFile(name + '.png') ) {
            return name + '.diff.png';
        } else {
            return name + '.png';
        }
    }
});

casper.on('remote.message', function clientLog(message) {
    this.echo('Remote console.log: ' + message);
});

casper
    .start()
    .zoom(2)
    .viewport(1200, 1200);

// var testFiles = getTestFiles(visualTestsDir);
var testFiles = ['../visual/components/arrange/index.html'];

testFiles.forEach(function(file) {
    casper.log( getParentDir(file) , 'error');

    casper.thenOpen(file);

    casper.then(function takeScreenshots() {
        var shotCount = this.evaluate(clientHelpers.prepareScreenshots);

        for (var i = 0; i < shotCount; i++) {
            var shotName = this.evaluate(clientHelpers.getScreenshotName, i);

            phantomcss.screenshot('[data-shot-id="' + i + '"]', shotName);
        };
    });
});

casper.then(function compare() {
    phantomcss.compareAll();
});

casper.then(function done() {
    casper.test.done();
});

casper.run(function() {
    console.log('\nDone.');
    phantom.exit( phantomcss.getExitStatus() );
});

{'fraction_non_alphanumeric': 0.16885216454104546, 'fraction_numerical': 0.0052092689060535295, 'mean_word_length': 2.757085020242915, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4203866', 'n_tokens_mistral': 1788, 'n_tokens_neox': 1713, 'n_words': 315}