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

Commit

Permalink
refactor(test-reader): fix up
Browse files Browse the repository at this point in the history
  • Loading branch information
rostik404 committed Nov 28, 2016
1 parent 5c9f0ed commit 7a0062f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 101 deletions.
3 changes: 1 addition & 2 deletions lib/config/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const GeminiError = require('../errors/gemini-error');
const util = require('./util');

const browserOptions = require('./browser-options');
const geminiCoreOptions = require('gemini-core/lib/config-options');
const geminiCoreOptions = require('gemini-core').options;

const is = util.is;
const anyObject = util.anyObject;
Expand All @@ -19,7 +19,6 @@ const section = configparser.section;
const option = configparser.option;
const map = configparser.map;


module.exports = root(
section(_.extend(browserOptions.getTopLevel(), {
system: section({
Expand Down
33 changes: 5 additions & 28 deletions lib/test-reader.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
'use strict';

const path = require('path');

const _ = require('lodash');
const Promise = require('bluebird');

const SetBuilder = require('gemini-core/lib/test-reader/set-builder');
const CoreError = require('gemini-core/lib/errors/core-error');
const validateUnknownSets = require('gemini-core/lib/utils/unknown-sets-validator');
const GeminiError = require('./errors/gemini-error');
const SetsBuilder = require('gemini-core').SetsBuilder;
const Suite = require('./suite');
const Events = require('./constants/events');
const testsApi = require('./tests-api');
const utils = require('./utils');

const PROJECT_TITLE = 'gemini';
const PROJECT_PATH = 'gemini';

const loadSuites = (sets, emitter) => {
const rootSuite = Suite.create('');
Expand All @@ -32,29 +25,13 @@ const loadSuites = (sets, emitter) => {
return rootSuite;
};

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

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

module.exports = (opts, config, emitter) => {
const filesToUse = filesExist(config.sets, opts.paths)
? opts.paths
: [getGeminiPath(config.system.projectRoot)];
const globOpts = {ignore: config.system.exclude};

validateUnknownSets(_.keys(config.sets), opts.sets);

return SetBuilder
return SetsBuilder
.create(config.sets, config.getBrowserIds())
.useSets(opts.sets)
.useFiles(filesToUse)
.useFiles(opts.paths, PROJECT_PATH)
.build(config.system.projectRoot, globOpts)
.then((setCollection) => loadSuites(setCollection, emitter))
.catch((e) => {
return (e instanceof CoreError)
? Promise.reject(new GeminiError(e.message))
: Promise.reject(e);
});
.then((setCollection) => loadSuites(setCollection, emitter));
};
1 change: 0 additions & 1 deletion test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
--require ./test/setup
-t 0
85 changes: 15 additions & 70 deletions test/unit/test-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@ const _ = require('lodash');
const proxyquire = require('proxyquire');
const EventEmitter = require('events').EventEmitter;
const globExtra = require('glob-extra');
const SetCollection = require('gemini-core/lib/test-reader/set-collection');
const SetBuilder = require('gemini-core/lib/test-reader/set-builder');
const TestSet = require('gemini-core/lib/test-reader/test-set');
const CoreError = require('gemini-core/lib/errors/core-error');
const GeminiError = require('lib/errors/gemini-error');
const SetsBuilder = require('gemini-core').SetsBuilder;

const utils = require('lib/utils');

describe('test-reader', () => {
const sandbox = sinon.sandbox.create();
const testsApi = sandbox.stub();
const validateUnknownSets = sandbox.stub();

let readTests;

Expand Down Expand Up @@ -49,20 +44,22 @@ describe('test-reader', () => {
return readTests({paths: opts.paths, sets: opts.sets}, opts.config, opts.emitter);
};

const mkSetStub = (opts) => TestSet.create(opts);

beforeEach(() => {
sandbox.stub(utils, 'requireWithNoCache');
sandbox.stub(globExtra, 'expandPaths').returns(Promise.resolve([]));
sandbox.stub(SetBuilder.prototype, 'useFiles').returnsThis();
sandbox.stub(SetBuilder.prototype, 'useSets').returnsThis();
sandbox.stub(SetsBuilder.prototype, 'useFiles').returnsThis();
sandbox.stub(SetsBuilder.prototype, 'useSets').returnsThis();

const setCollection = sinon.createStubInstance(SetCollection);
sandbox.stub(SetBuilder.prototype, 'build').returns(Promise.resolve(setCollection));
const groupByFile = () => {
return {
'/some/path/file1.js': [],
'/other/path/file2.js': []
};
};
sandbox.stub(SetsBuilder.prototype, 'build').returns(Promise.resolve({groupByFile}));

readTests = proxyquire('lib/test-reader', {
'./tests-api': testsApi,
'gemini-core/lib/utils/unknown-sets-validator': validateUnknownSets
'./tests-api': testsApi
});
});

Expand All @@ -73,7 +70,7 @@ describe('test-reader', () => {

describe('read tests', () => {
it('should create set-builder using passed options and browsers from config', () => {
const create = sandbox.spy(SetBuilder, 'create');
const create = sandbox.spy(SetsBuilder, 'create');
const opts = {
config: mkConfigStub({
sets: {
Expand All @@ -87,58 +84,16 @@ describe('test-reader', () => {
.then(() => assert.calledWith(create, {all: {}}, ['bro1', 'bro2']));
});

it('should use gemini folder if sets are not specified in config and paths are not passed', () => {
const config = mkConfigStub({
system: {
projectRoot: '/project/root'
}
});

return readTests_({config})
.then(() => assert.calledWith(SetBuilder.prototype.useFiles, ['/project/root/gemini']));
});

it('should use paths passed from cli', () => {
return readTests_({paths: ['some/path'], config: mkConfigStub()})
.then(() => assert.calledWith(SetBuilder.prototype.useFiles, ['some/path']));
});

it('should validate unknown sets', () => {
const config = mkConfigStub({
sets: {
set1: {}
}
});

return readTests_({sets: ['set2'], config})
.then(() => assert.calledWith(validateUnknownSets, ['set1'], ['set2']));
});

it('should be rejected with gemini-error, if core error was thrown', () => {
SetBuilder.prototype.build.returns(Promise.reject(new CoreError()));

return assert.isRejected(readTests_({config: mkConfigStub()}), GeminiError);
});

it('should be rejected with native error, if native error was thrown', () => {
SetBuilder.prototype.build.returns(Promise.reject(new CoreError()));

return assert.isRejected(readTests_({config: mkConfigStub()}), Error);
.then(() => assert.calledWith(SetsBuilder.prototype.useFiles, ['some/path']));
});
});

describe('global "gemini" variable', () => {
let gemini;

beforeEach(() => {
const sets = {
all: mkSetStub({
files: ['some/files', 'other/files/']
})
};
const setCollection = SetCollection.create(sets);
SetBuilder.prototype.build.returns(Promise.resolve(setCollection));

utils.requireWithNoCache.restore();
});

Expand Down Expand Up @@ -179,16 +134,6 @@ describe('test-reader', () => {
});

describe('events', () => {
beforeEach(() => {
const sets = {
all: mkSetStub({
files: ['/some/path/file.js']
})
};
const setCollection = SetCollection.create(sets);
SetBuilder.prototype.build.returns(Promise.resolve(setCollection));
});

it('should emit "beforeFileRead" before reading each file', () => {
const beforeReadSpy = sandbox.spy().named('OnBeforeFileRead');

Expand All @@ -197,7 +142,7 @@ describe('test-reader', () => {

return readTests_({config: mkConfigStub(), emitter})
.then(() => {
assert.calledWithExactly(beforeReadSpy, '/some/path/file.js');
assert.calledWithExactly(beforeReadSpy, '/some/path/file1.js');
assert.callOrder(beforeReadSpy, utils.requireWithNoCache);
});
});
Expand All @@ -210,7 +155,7 @@ describe('test-reader', () => {

return readTests_({config: mkConfigStub(), emitter})
.then(() => {
assert.calledWithExactly(afterReadSpy, '/some/path/file.js');
assert.calledWithExactly(afterReadSpy, '/some/path/file1.js');
assert.callOrder(utils.requireWithNoCache, afterReadSpy);
});
});
Expand Down

0 comments on commit 7a0062f

Please sign in to comment.