Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Commit

Permalink
Merge pull request #568 from gemini-testing/fix-sets
Browse files Browse the repository at this point in the history
Fix up sets logic
  • Loading branch information
rostik404 authored Aug 30, 2016
2 parents 09e15e6 + c627c71 commit 114eae5
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 91 deletions.
5 changes: 1 addition & 4 deletions lib/config/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ module.exports = root(

sets: map(section({
files: option({
defaultValue: 'gemini',
validate: function(value) {
if (!_.isArray(value) && !_.isString(value)) {
throw new GeminiError('"sets.files" must be an array or string');
Expand Down Expand Up @@ -116,9 +115,7 @@ module.exports = root(
}
}
})
}), {
all: {} // Use `all` set with default values if no set specified in config
}),
})),

browsers: map(section(browserOptions.getPerBrowser()))
}))
Expand Down
17 changes: 15 additions & 2 deletions lib/test-reader/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
'use strict';

const q = require('q');
const path = require('path');

const _ = require('lodash');
const globExtra = require('glob-extra');
const q = require('q');

const SetCollection = require('./set-collection');
const Suite = require('../suite');
const testsApi = require('../tests-api');
Expand All @@ -24,10 +27,20 @@ const loadSuites = (sets, emitter) => {
return rootSuite;
};

const filesExist = (configSets, cliPaths) => {
return !_.isEmpty(configSets) || !_.isEmpty(cliPaths);
};

const getGeminiPath = (projectRoot) => path.resolve(projectRoot, 'gemini');

module.exports = (cli, config, emitter) => {
const files = filesExist(config.sets, cli.paths)
? cli.paths
: [getGeminiPath(config.system.projectRoot)];

return q.all([
SetCollection.create(config, cli.sets),
globExtra.expandPaths(cli.paths, {formats: ['.js']})
globExtra.expandPaths(files, {formats: ['.js']})
])
.spread((sets, paths) => {
sets.filterFiles(paths);
Expand Down
21 changes: 14 additions & 7 deletions lib/test-reader/set-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ const q = require('q');

const GeminiError = require('../errors/gemini-error');
const globExtra = require('glob-extra');
const Set = require('./set');
const TestSet = require('./test-set');

module.exports = class SetCollection {
static create(config, sets) {
const filteredSets = SetCollection._filter(config.sets, sets);
let filteredSets = SetCollection._filter(config.sets, sets);

if (_.isEmpty(filteredSets)) {
filteredSets = [{files: {}, browsers: config.getBrowserIds()}];
}

return SetCollection._expand(filteredSets, config.system.projectRoot)
.then((sets) => new SetCollection(sets));
Expand All @@ -29,10 +33,13 @@ module.exports = class SetCollection {
const unknownSets = _.difference(setsToUse, _.keys(sets));

if (!_.isEmpty(unknownSets)) {
throw new GeminiError(
`No such sets: ${unknownSets.join(', ')}. Use one of the sets, specified ` +
`in config file: ${_.keys(sets).join(', ')}`
);
let error = `No such sets: ${unknownSets.join(', ')}.`;

if (!_.isEmpty(sets)) {
error += ` Use one of the sets, specified in config file: ${_.keys(sets).join(', ')}`;
}

throw new GeminiError(error);
}
}

Expand All @@ -47,7 +54,7 @@ module.exports = class SetCollection {
}

constructor(sets) {
this._sets = _.map(sets, (set) => Set.create(set));
this._sets = _.map(sets, (set) => TestSet.create(set));
}

filterFiles(files) {
Expand Down
27 changes: 0 additions & 27 deletions lib/test-reader/set.js

This file was deleted.

35 changes: 35 additions & 0 deletions lib/test-reader/test-set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const _ = require('lodash');

module.exports = class TestSet {
static create(set) {
return new TestSet(set);
}

constructor(set) {
this._set = set;
}

getFiles() {
return this._set.files;
}

getBrowsers(path) {
return _.includes(this._set.files, path) ? this._set.browsers : [];
}

filterFiles(files) {
this._set.files = this._getFilteredFiles(files);
}

_getFilteredFiles(files) {
if (_.isEmpty(this._set.files)) {
return files;
}

return _.isEmpty(files)
? this._set.files
: _.intersection(files, this._set.files);
}
};
83 changes: 45 additions & 38 deletions test/unit/config-options/sets.test.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
'use strict';
var parser = require('lib/config/options'),
GeminiError = require('lib/errors/gemini-error'),
_ = require('lodash');

describe('config.sets', function() {
///
function createConfig(opts) {
var REQUIRED_OPTIONS = {

const MissingOptionError = require('gemini-configparser').MissingOptionError;
const _ = require('lodash');

const parser = require('lib/config/options');
const GeminiError = require('lib/errors/gemini-error');

describe('config.sets', () => {
const createConfig = (opts) => {
const REQUIRED_OPTIONS = {
system: {
projectRoot: '/some/path'
},
rootUrl: 'http://example.com/root',
desiredCapabilities: {}
};

var options = _.extend({}, REQUIRED_OPTIONS, opts);
const options = _.extend({}, REQUIRED_OPTIONS, opts);

return parser({
options: options,
env: {},
argv: []
});
}
};

describe('files', function() {
it('should be `gemini` by default', function() {
var config = createConfig({
sets: {
someSet: {}
}
});

assert.deepEqual(config.sets.someSet.files, ['gemini']);
describe('files', () => {
it('should throw an error if files are not specified', () => {
assert.throws(() => {
createConfig({
sets: {
someSet: {}
}
});
}, MissingOptionError);
});

it('should convert string to array of strings', function() {
var config = createConfig({
it('should convert string to array of strings', () => {
const config = createConfig({
sets: {
someSet: {
files: 'some/path'
Expand All @@ -46,8 +48,8 @@ describe('config.sets', function() {
assert.deepEqual(config.sets.someSet.files, ['some/path']);
});

it('should not accept non-string arrays', function() {
assert.throws(function() {
it('should not accept non-string arrays', () => {
assert.throws(() => {
createConfig({
sets: {
someSet: {
Expand All @@ -58,8 +60,8 @@ describe('config.sets', function() {
}, GeminiError);
});

it('should accept array with strings', function() {
var config = createConfig({
it('should accept array with strings', () => {
const config = createConfig({
sets: {
someSet: {
files: [
Expand All @@ -77,60 +79,66 @@ describe('config.sets', function() {
});
});

describe('browsers', function() {
it('should contain all browsers by default', function() {
describe('browsers', () => {
it('should contain all browsers by default', () => {
var config = createConfig({
browsers: {
b1: {},
b2: {}
},
sets: {
someSet: {}
someSet: {
files: ['some/path']
}
}
});

assert.deepEqual(config.sets.someSet.browsers, ['b1', 'b2']);
});

it('should not accept non-arrays', function() {
assert.throws(function() {
it('should not accept non-arrays', () => {
assert.throws(() => {
createConfig({
sets: {
someSet: {
files: ['some/path'],
browsers: 'something'
}
}
});
}, GeminiError);
});

it('should not accept unknown browsers', function() {
assert.throws(function() {
it('should not accept unknown browsers', () => {
assert.throws(() => {
createConfig({
browsers: {
b1: {},
b2: {}
},
sets: {
someSet: {
files: ['some/path'],
browsers: ['b3']
}
}
});
}, GeminiError);
});

it('should accept configured browsers', function() {
var config = createConfig({
it('should accept configured browsers', () => {
const config = createConfig({
browsers: {
b1: {},
b2: {}
},
sets: {
set1: {
files: ['some/path'],
browsers: ['b1']
},
set2: {
files: ['other/path'],
browsers: ['b2']
}
}
Expand All @@ -141,15 +149,14 @@ describe('config.sets', function() {
});
});

it('should have `all` set with default values if no set specified', function() {
var config = createConfig({
it('should not have default set if sets are not specified', () => {
const config = createConfig({
browsers: {
b1: {},
b2: {}
}
});

assert.deepEqual(config.sets.all.files, ['gemini']);
assert.deepEqual(config.sets.all.browsers, ['b1', 'b2']);
assert.deepEqual(config.sets, {});
});
});
11 changes: 11 additions & 0 deletions test/unit/test-reader/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ describe('test-reader', () => {
});
});

it('should use gemini folder if sets are not specified in config and paths are not passed', () => {
const config = {
getBrowserIds: () => []
};

globExtra.expandPaths.withArgs(['/root/gemini']).returns(q(['/root/gemini/file.js']));

return readTests_({config})
.then(() => assert.calledWith(utils.requireWithNoCache, '/root/gemini/file.js'));
});

it('should load suites related to sets from config', () => {
const config = {
sets: {
Expand Down
Loading

0 comments on commit 114eae5

Please sign in to comment.