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

Some refactoring to run grep before tests run #146

Merged
merged 2 commits into from
Apr 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
238 changes: 122 additions & 116 deletions lib/gemini.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
'use strict';
var ChainedEmitter = require('chained-emitter').EventEmitter,
inherit = require('inherit'),

path = require('path'),
util = require('util'),

_ = require('lodash'),
inherit = require('inherit'),
chalk = require('chalk'),
ScreenShooter = require('./screen-shooter'),
Tester = require('./tester'),
Config = require('./config'),
Suite = require('./suite'),
suiteUtils = require('./suite-util'),

pathUtils = require('./path-utils'),
publicApi = require('./public-api'),

exposeTestsApi = require('./tests-api'),

GeminiError = require('./errors/gemini-error'),
Image = require('./image');
Image = require('./image'),

var DEFAULT_CFG_NAME = '.gemini.yml',
DEFAULT_CFG_NAME = '.gemini.yml',
DEFAULT_SPECS_DIR = 'gemini';

function requireWithNoCache(moduleName) {
Expand All @@ -30,121 +31,126 @@ function requireWithNoCache(moduleName) {

module.exports = inherit(ChainedEmitter, {
__constructor: function(config, overrides) {
config = config || DEFAULT_CFG_NAME;
this.config = new Config(config, overrides);
var _this = this;

require('./plugins').load(_this, this.config);

function getDefaultSpecsDir() {
return path.join(_this.config.projectRoot, DEFAULT_SPECS_DIR);
}

function executeRunner(runnerInstance, paths, options) {
if (!options) {
//if there are only two arguments, they are
//(runnerInstance, options) and paths are
//the default.
options = paths;
paths = [getDefaultSpecsDir()];
}
options = options || {};
options.reporters = options.reporters || [];

var envBrowsers = process.env.GEMINI_BROWSERS? process.env.GEMINI_BROWSERS.split(',') : null;
options.browsers = options.browsers || envBrowsers;

if (options.grep) {
runnerInstance.setGrepPattern(options.grep);
}
if (options.browsers) {
validateBrowsers(options.browsers);
runnerInstance.setTestBrowsers(options.browsers);
}

return _this.readTests(paths)
.then(function(rootSuite) {
return _this.emit('startRunner', runnerInstance)
.then(function() {
return rootSuite;
});
})
.then(function(rootSuite) {
options.reporters.forEach(applyReporter.bind(null, runnerInstance));
return runnerInstance.run(rootSuite);
})
.then(function(data) {
return _this.emit('endRunner', runnerInstance, data)
.then(function() {
return data;
});
});
}

function validateBrowsers(browsers) {
var browserIds = _this.browserIds;
browsers.forEach(function(id) {
if (browserIds.indexOf(id) === -1) {
throw new GeminiError('Unknown browser id: ' + id,
'Use one of the browser ids specified in config file: ' +
browserIds.join(', '));
}
});
}

this.readTests = function(paths) {
paths = paths || [getDefaultSpecsDir()];
return pathUtils.expandPaths(paths)
.then(function(expanded) {
var rootSuite = Suite.create('');
exposeTestsApi(publicApi, rootSuite);

expanded.forEach(requireWithNoCache);
return rootSuite;
config = config || DEFAULT_CFG_NAME;
this.config = new Config(config, overrides);
var _this = this;

require('./plugins').load(_this, this.config);

function getDefaultSpecsDir() {
return path.join(_this.config.projectRoot, DEFAULT_SPECS_DIR);
}

function executeRunner(runnerInstance, paths, options) {
if (!options) {
//if there are only two arguments, they are
//(runnerInstance, options) and paths are
//the default.
options = paths;
paths = [getDefaultSpecsDir()];
}
options = options || {};
options.reporters = options.reporters || [];

var envBrowsers = process.env.GEMINI_BROWSERS? process.env.GEMINI_BROWSERS.split(',') : null;
options.browsers = options.browsers || envBrowsers;

if (options.browsers) {
validateBrowsers(options.browsers);
runnerInstance.setTestBrowsers(options.browsers);
}

return _this.readTests(paths)
.then(function(rootSuite) {
return _this.emit('startRunner', runnerInstance)
.then(function() {
return rootSuite;
});
})
.then(function(rootSuite) {
var suites = suiteUtils.flattenSuites(rootSuite);

if (options.grep) {
suites = suites.filter(function(suite) {
return options.grep.test(suite.fullName);
});
}

options.reporters.forEach(_.partial(applyReporter, runnerInstance));
return runnerInstance.run(suites);
})
.then(function(data) {
return _this.emit('endRunner', runnerInstance, data)
.then(function() {
return data;
});
});
}

function validateBrowsers(browsers) {
var browserIds = _this.browserIds;
browsers.forEach(function(id) {
if (browserIds.indexOf(id) === -1) {
throw new GeminiError('Unknown browser id: ' + id,
'Use one of the browser ids specified in config file: ' +
browserIds.join(', '));
}
});
};

this.gather = function(paths, options) {
return executeRunner(new ScreenShooter(this.config), paths, options);
};

this.test = function(paths, options) {
return executeRunner(
new Tester(this.config, {tempDir: options.tempDir}),
paths,
options
);
};

this.getScreenshotPath = function(suite, stateName, browserId) {
return this.config.getScreenshotPath(suite, stateName, browserId);
};

this.buildDiff = util.deprecate(function(referencePath, currentPath, diffPath) {
return Image.buildDiff({
reference: referencePath,
current: currentPath,
diff: diffPath,
diffColor: this.config.diffColor,
strictComparison: this.config.strictComparison,
tolerance: this.config.tolerance
});
}, 'gemini.buildDiff' + chalk.red(' is deprecated.\n') +
}

this.readTests = function(paths) {
paths = paths || [getDefaultSpecsDir()];
return pathUtils.expandPaths(paths)
.then(function(expanded) {
var rootSuite = Suite.create('');
exposeTestsApi(publicApi, rootSuite);

expanded.forEach(requireWithNoCache);
return rootSuite;
});
};

this.gather = function(paths, options) {
return executeRunner(new ScreenShooter(this.config), paths, options);
};

this.test = function(paths, options) {
return executeRunner(
new Tester(this.config, {tempDir: options.tempDir}),
paths,
options
);
};

this.getScreenshotPath = function(suite, stateName, browserId) {
return this.config.getScreenshotPath(suite, stateName, browserId);
};

this.buildDiff = util.deprecate(function(referencePath, currentPath, diffPath) {
return Image.buildDiff({
reference: referencePath,
current: currentPath,
diff: diffPath,
diffColor: this.config.diffColor,
strictComparison: this.config.strictComparison,
tolerance: this.config.tolerance
});
}, 'gemini.buildDiff' + chalk.red(' is deprecated.\n') +
'Use ' + chalk.green('testResult.saveDiffTo(path)') +
' in ' + chalk.green('endTest') + ' event handler for more accurate diff'
);

this.getBrowserCapabilites = function(browserId) {
return this.config.browsers[browserId];
};

Object.defineProperty(this, 'browserIds', {
enumerable: true,
get: function() {
return Object.keys(this.config.browsers);
}
});
}
);

this.getBrowserCapabilites = function(browserId) {
return this.config.browsers[browserId];
};

Object.defineProperty(this, 'browserIds', {
enumerable: true,
get: function() {
return Object.keys(this.config.browsers);
}
});
}
});

function applyReporter(runner, reporter) {
Expand Down
30 changes: 11 additions & 19 deletions lib/runner.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
var EventEmitter = require('events').EventEmitter,

_ = require('lodash'),
q = require('q'),
inherit = require('inherit'),
promiseUtils = require('./promise-util'),
Expand All @@ -21,29 +22,31 @@ module.exports = inherit(EventEmitter, {
this.coverage = new Coverage(config);
},

setGrepPattern: function(pattern) {
this._grepPattern = pattern;
},

setTestBrowsers: function(browsers) {
this._testBrowsers = browsers;
},

run: function(rootSuite) {
/**
* @param {Suite[]} suites
* @returns {*}
*/
run: function(suites) {
var _this = this;
this._stats = new Stats();

return q.fcall(function() {
_this.emit('begin', {
config: _this.config,
totalStates: rootSuite.deepStatesCount,
totalStates: _.reduce(suites, function(result, suite) {
return result + suite.states.length;
}, 0),
browserIds: Object.keys(_this.config.browsers)
});
}).then(function() {
return _this._prepare();
})
.then(function() {
return _this._runBrowsers(rootSuite.children);
return _this._runBrowsers(suites);
})
.then(function() {
if (_this.config.coverage) {
Expand Down Expand Up @@ -113,23 +116,12 @@ module.exports = inherit(EventEmitter, {

this.emit('beginSuite', eventData);

return this._runSuiteStateIfMatches(suite, browser)
.then(function() {
return _this._runSuitesInBrowser(suite.children, browser);
})
return this._runSuiteStates(suite, browser)
.then(function() {
_this.emit('endSuite', eventData);
});
},

_runSuiteStateIfMatches: function(suite, browser) {
if (this._grepPattern && !this._grepPattern.test(suite.fullName)) {
return q.resolve();
}

return this._runSuiteStates(suite, browser);
},

_runSuiteStates: function(suite, browser) {
if (!suite.hasStates) {
return q.resolve();
Expand Down
12 changes: 12 additions & 0 deletions lib/suite-util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var _ = require('lodash');

exports.shouldSkip = function(skipped, browser) {
if (typeof skipped === 'boolean') {
return skipped;
Expand All @@ -21,3 +23,13 @@ exports.shouldSkip = function(skipped, browser) {
return true;
});
};

exports.flattenSuites = function flat(suiteNode) {
if (!suiteNode) {
return [];
}

return _.reduce(suiteNode.children, function(result, suite) {
return result.concat(flat(suite));
}, [suiteNode]);
};
6 changes: 0 additions & 6 deletions lib/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,6 @@ var Suite = inherit({
return !this.parent;
},

get deepStatesCount() {
return this._children.reduce(function(sum, child) {
return sum + child.deepStatesCount;
}, this._states.length);
},

get fullName() {
if (!this.parent) {
return this.name;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"wd": "^0.3.4"
},
"devDependencies": {
"chai": "^2.2.0",
"coveralls": "^2.11.2",
"istanbul": "^0.3.5",
"jscs": "^1.10.0",
Expand Down