Skip to content

Commit

Permalink
Merge pull request #9 from gemini-testing/config/sets
Browse files Browse the repository at this point in the history
Support configuration of 'sets'
  • Loading branch information
eGavr committed Mar 30, 2016
2 parents 9792f61 + ae148a2 commit 079a8b6
Show file tree
Hide file tree
Showing 7 changed files with 426 additions and 49 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,14 @@ module.exports = {
For example,
```javascript
specs: [
'tests/desktop',
'tests/touch'
{ // run tests associated with this path in all browsers
files: 'tests/desktop' // which are configured in option `browsers`
},
'tests/deskpad', // the alias for the previous case
{
files: 'tests/touch', // run tests associated with this path in a browser with id `browser`
browsers: ['browser'] // which is configured in option `browsers`
}
]
```

Expand Down
39 changes: 4 additions & 35 deletions lib/hermione.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
var Runner = require('./runner'),
HermioneFacade = require('./hermione-facade'),
RunnerEvents = require('./constants/runner-events'),
pathUtils = require('./path-utils'),
logger = require('./utils').logger,
readTests = require('./tests-reader'),
_ = require('lodash'),
inherit = require('inherit'),
util = require('util'),
chalk = require('chalk');
inherit = require('inherit');

// Hack for [email protected] and lower
// Remove restriction for maximum open concurrent sockets
Expand All @@ -25,8 +22,6 @@ module.exports = inherit({
},

run: function(testPaths, browsers) {
browsers = this._filterBrowsers(browsers, _.keys(this._config.browsers));

var runner = Runner.create(this._config);
runner.on(RunnerEvents.TEST_FAIL, this._fail.bind(this));
runner.on(RunnerEvents.ERROR, this._fail.bind(this));
Expand All @@ -35,10 +30,8 @@ module.exports = inherit({

this._loadPlugins(runner);

return this._getTests(testPaths)
.then(function(tests) {
return runner.run(tests, browsers);
})
return readTests(testPaths, browsers, this._config)
.then(runner.run.bind(runner))
.then(function() {
return !this._failed;
}.bind(this));
Expand All @@ -52,30 +45,6 @@ module.exports = inherit({

_fail: function() {
this._failed = true;
},

_filterBrowsers: function(browsers, allBrowsers) {
if (!browsers) {
return allBrowsers;
}

var unknownBrowsers = _.difference(browsers, allBrowsers);
if (unknownBrowsers.length) {
logger.warn(util.format(
'%s Unknown browsers id: %s. Use one of the browser ids specified in config file: %s',
chalk.yellow('WARNING:'), unknownBrowsers.join(', '), allBrowsers.join(', ')
));
}

return _.intersection(browsers, allBrowsers);
},

_getTests: function(paths) {
if (_.isEmpty(paths)) {
paths = this._config.specs;
}

return pathUtils.expandPaths(paths);
}
});

Expand Down
16 changes: 8 additions & 8 deletions lib/runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@ var MainRunner = inherit(QEmitter, {
this._pool = new BrowserPool(this._config);
},

run: function(suites, browsers) {
run: function(tests) {
var _this = this,
anyTest = _.identity.bind(null, true);

return this.emitAndWait(RunnerEvents.RUNNER_START)
.then(function() {
return _this._runTestSession(suites, browsers, anyTest);
return _this._runTestSession(tests, anyTest);
})
.finally(function() {
return _this.emitAndWait(RunnerEvents.RUNNER_END)
.catch(logger.warn);
});
},

_runTestSession: function(suites, browsers, filterFn) {
_runTestSession: function(tests, filterFn) {
var _this = this;

return _(browsers)
.map(function(browserId) {
return _this._runInBrowser(browserId, suites, filterFn);
return _(tests)
.map(function(files, browserId) {
return _this._runInBrowser(browserId, files, filterFn);
})
.thru(utils.waitForResults)
.value()
Expand All @@ -55,7 +55,7 @@ var MainRunner = inherit(QEmitter, {
});
},

_runInBrowser: function(browserId, suites, filterFn) {
_runInBrowser: function(browserId, files, filterFn) {
var browserAgent = new BrowserAgent(browserId, this._pool),
mochaRunner = MochaRunner.create(this._config, browserAgent);

Expand All @@ -76,7 +76,7 @@ var MainRunner = inherit(QEmitter, {
mochaRunner.on(RunnerEvents.TEST_FAIL, this._retryMgr.handleTestFail.bind(this._retryMgr));
mochaRunner.on(RunnerEvents.ERROR, this._retryMgr.handleError.bind(this._retryMgr));

return mochaRunner.run(suites, filterFn);
return mochaRunner.run(files, filterFn);
}
}, {
create: function(config) {
Expand Down
117 changes: 117 additions & 0 deletions lib/tests-reader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
'use strict';

var pathUtils = require('./path-utils'),
logger = require('./utils').logger,
chalk = require('chalk'),
_ = require('lodash'),
q = require('q'),
format = require('util').format;

module.exports = function(testPaths, browsers, config) {
var specs = config.specs,
configBrowsers = _.keys(config.browsers);

validateUnknownBrowsers(getBrowsersFromSpecs(specs), browsers, configBrowsers);

return q.all([
expandSpecs(specs, configBrowsers),
pathUtils.expandPaths(testPaths)
])
.spread(function(specs, testFiles) {
return filterSpecs(specs, testFiles, browsers);
})
.then(assignBrowsersToTestFiles);
};

function validateUnknownBrowsers(specsBrowsers, cliBrowsers, configBrowsers) {
var unknownBrowsers = getUnknownBrowsers_();

if (_.isEmpty(unknownBrowsers)) {
return;
}

logger.warn(format(
'%s Unknown browsers id: %s. Use one of the browser ids specified in config file: %s',
chalk.yellow('WARNING:'), unknownBrowsers.join(', '), configBrowsers.join(', ')
));

function getUnknownBrowsers_() {
return _(specsBrowsers)
.concat(cliBrowsers)
.compact()
.uniq()
.difference(configBrowsers)
.value();
}
}

function expandSpecs(specs, configBrowsers) {
return _(specs)
.map(revealSpec_)
.thru(q.all)
.value();

function revealSpec_(spec) {
if (!_.isString(spec) && !_.isPlainObject(spec)) {
throw new TypeError('config.specs must be an array of strings or/and plain objects');
}

var paths = _.isString(spec) ? [spec] : spec.files;

return pathUtils.expandPaths(paths)
.then(function(files) {
return {
files: files,
browsers: spec.browsers ? _.intersection(spec.browsers, configBrowsers) : configBrowsers
};
});
}
}

function filterSpecs(specs, testFiles, browsers) {
return specs.map(function(spec) {
return {
files: filterSpec_(spec.files, testFiles),
browsers: filterSpec_(spec.browsers, browsers)
};
});

function filterSpec_(specValue, value) {
return _.isEmpty(value) ? specValue : _.intersection(specValue, value);
}
}

function assignBrowsersToTestFiles(specs) {
var browsers = getBrowsersFromSpecs(specs);

return _(browsers)
.map(getTestFilesForBrowser_)
.thru(_.zipObject.bind(null, browsers))
.omit(_.isEmpty)
.value();

function getTestFilesForBrowser_(browser) {
return _(specs)
.filter(function(spec) {
return _.contains(spec.browsers, browser);
})
.thru(getFilesFromSpecs)
.value();
}
}

function getFilesFromSpecs(specs) {
return getDataFromSpecs(specs, 'files');
}

function getBrowsersFromSpecs(specs) {
return getDataFromSpecs(specs, 'browsers');
}

function getDataFromSpecs(specs, prop) {
return _(specs)
.map(prop)
.flatten()
.uniq()
.value();
}
10 changes: 6 additions & 4 deletions test/lib/runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ describe('Runner', function() {
function run_(opts) {
opts = _.defaults(opts || {}, {
browsers: ['default-browser'],
tests: []
files: ['default-file']
});

var runner = opts.runner || new Runner(makeConfigStub({browsers: opts.browsers}));
return runner.run(opts.tests, opts.browsers);
var tests = _.zipObject(opts.browsers, _.fill(Array(opts.browsers.length), opts.files)),
runner = opts.runner || new Runner(makeConfigStub({browsers: opts.browsers}));

return runner.run(tests);
}

beforeEach(function() {
Expand Down Expand Up @@ -103,7 +105,7 @@ describe('Runner', function() {
});

it('should run mocha runner with passed tests and filter function', function() {
return run_({tests: ['test1', 'test2']})
return run_({files: ['test1', 'test2']})
.then(function() {
assert.calledWith(MochaRunner.prototype.run, ['test1', 'test2'], sinon.match.func);
});
Expand Down
Loading

0 comments on commit 079a8b6

Please sign in to comment.