<commit_before>var _ = require('lodash');
var status = require('./status');
var GridRunner = require('./runner/grid');

module.exports  = function run(config, scenarios, done) {
    // ensure configuration meets requirements
    config = _.merge({}, require("./config.json"), config);
    done || (done = function () {});

    // check wich browsers are available
    checkBrowsersCapabilities(config, runForBrowsers);

    function runForBrowsers(err, browsers) {
        var grid, reporter;
        if (err) {
            return done(err);
        }

        // override browser configuration
        config.browsers = browsers;

        grid = new GridRunner(config, scenarios);

        // run !
        grid.run(done);
    }
};

function checkBrowsersCapabilities(configuration, callback) {
    var browsers = configuration.browsers;

    if (configuration.skipCapabilitiesCheck) {
        // invoke with the full browser stack
        return callback(null, browsers);
    }

    status.available(
        configuration.remoteCfg,
        function (err, availableBrowsers) {
            if (err !== null) {
                return callback(new Error([
                    'Could not connect to selenium grid, ',
                    'did you started it?'
                ].join('')));
            }

            var foundBrowsers = browsers.filter(function(desired) {
                return availableBrowsers.some(function(available) {
                    return available.browserName === desired.browserName && (
                            !desired.version ||
                            available.version === desired.version);
                });
            });

            if (foundBrowsers.length === 0) {
                return callback(new Error([
                    'No matching browsers found in the grid, ',
                    'did you started your VMs?'
                ].join('')));
            }

            callback(err, foundBrowsers);
    });
}<commit_msg>Browser capabilities are filtered on every capabilities.<commit_after>var _ = require('lodash');
var status = require('./status');
var GridRunner = require('./runner/grid');

module.exports  = function run(config, scenarios, done) {
    // ensure configuration meets requirements
    config = _.merge({}, require("./config.json"), config);
    done || (done = function () {});

    // check wich browsers are available
    checkBrowsersCapabilities(config, runForBrowsers);

    function runForBrowsers(err, browsers) {
        var grid, reporter;
        if (err) {
            return done(err);
        }

        // override browser configuration
        config.browsers = browsers;
        logAvailableBrowsers(browsers);

        grid = new GridRunner(config, scenarios);

        // run !
        grid.run(done);
    }
};

function checkBrowsersCapabilities(configuration, callback) {
    var browsers = configuration.browsers;

    if (configuration.skipCapabilitiesCheck) {
        // invoke with the full browser stack
        return callback(null, browsers);
    }

    status.available(
        configuration.remoteCfg,
        function (err, availableBrowsers) {
            if (err !== null) {
                return callback(new Error([
                    'Could not connect to selenium grid, ',
                    'did you started it?'
                ].join('')));
            }

            var foundBrowsers = browsers.filter(function(desired) {
                return availableBrowsers.some(function(available) {
                    var keys = _.keys(desired);
                    for (var i in keys) {
                        var key = keys[i];
                        // is the key the same ?
                        if (available[key] !== desired[key]) {
                            return false;
                        }
                    }
                    // no diverging.
                    return true;
                });
            });

            if (foundBrowsers.length === 0) {
                return callback(new Error([
                    'No matching browsers found in the grid, ',
                    'did you started your VMs?'
                ].join('')));
            }

            callback(err, foundBrowsers);
    });
}

function logAvailableBrowsers(browsers) {
    console.log('Will execute tests on following browsers:\n');
    _.each(browsers, function (browser) {
        browser = _.defaults(browser, {
            browserName: 'ANY',
            platform: 'ANY',
            version: 'ANY'
        });

        console.log('  -> browser: %s - version: %s - platform: %s', browser.browserName, browser.version, browser.platform);
    });
    console.log('\n');
}
{'fraction_non_alphanumeric': 0.11311928556240697, 'fraction_numerical': 0.00042526047203912394, 'mean_word_length': 1.6941580756013745, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '710128', 'n_tokens_mistral': 1199, 'n_tokens_neox': 1128, 'n_words': 302}