-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for --tags to include/exclude tests based on some optional tagging #1445
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
var utils = require('./utils'); | ||
|
||
module.exports = Filter; | ||
|
||
function Filter() { | ||
this.grep = /.*/; | ||
this.invertGrep = false; | ||
this.tags = []; | ||
this.skipTags = []; | ||
} | ||
|
||
Filter.prototype.count = function(suite) { | ||
var self = this; | ||
var total = 0; | ||
suite.eachTest(function(test){ | ||
if (self.shouldRun(test)) total++; | ||
}); | ||
return total; | ||
}; | ||
|
||
Filter.prototype.shouldRun = function(test) { | ||
// --grep | ||
var grepMatch = this.grep.test(test.fullTitle()); | ||
if (this.invertGrep) grepMatch = !grepMatch; | ||
// --tags --skip-tags | ||
var include = (!this.tags.length) || matchTags(test.ctx._tags, this.tags); | ||
var exclude = this.skipTags.length && matchTags(test.ctx._tags, this.skipTags); | ||
// final decision | ||
return grepMatch && include && !exclude; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could early return once we calculate a condition that fails so that we skip unnecessary calculus. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's true. I thought There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From my testing, the performance gain is negligible, and this reads easier. LGTM, good call. (like 12ms per 100 000 tests) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sharing so that someone points out my test is bad or wrong: const filterTags = ['ci']
const testTags = ['slow', 'vendor', 'thing', 'ci']
function matchTags (actualTags, against) {
return against.some((tag) => {
return actualTags.indexOf(tag) !== -1
})
}
console.time('shouldRun')
let tests = 100 * 1000
while (--tests) matchTags(testTags, filterTags)
console.timeEnd('shouldRun') |
||
}; | ||
|
||
function matchTags(actualTags, against) { | ||
return utils.some(against, function(tag) { | ||
return utils.indexOf(actualTags, tag) !== -1; | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
var EventEmitter = require('events').EventEmitter | ||
, debug = require('debug')('mocha:runner') | ||
, Test = require('./test') | ||
, Filter = require('./filter') | ||
, utils = require('./utils') | ||
, filter = utils.filter | ||
, keys = utils.keys; | ||
|
@@ -54,12 +55,12 @@ function Runner(suite) { | |
var self = this; | ||
this._globals = []; | ||
this._abort = false; | ||
this._filter = new Filter(); | ||
this.suite = suite; | ||
this.total = suite.total(); | ||
this.failures = 0; | ||
this.on('test end', function(test){ self.checkGlobals(test); }); | ||
this.on('hook end', function(hook){ self.checkGlobals(hook); }); | ||
this.grep(/.*/); | ||
this.globals(this.globalProps().concat(extraGlobals())); | ||
} | ||
|
||
|
@@ -88,34 +89,42 @@ Runner.prototype.__proto__ = EventEmitter.prototype; | |
* @api public | ||
*/ | ||
|
||
Runner.prototype.grep = function(re, invert){ | ||
debug('grep %s', re); | ||
this._grep = re; | ||
this._invert = invert; | ||
this.total = this.grepTotal(this.suite); | ||
Runner.prototype.grep = function(re, invert) { | ||
debug('grep %s (%s)', re, invert); | ||
this._filter.grep = re; | ||
this._filter.invertGrep = invert; | ||
this.total = this._filter.count(this.suite); | ||
return this; | ||
}; | ||
|
||
/** | ||
* Returns the number of tests matching the grep search for the | ||
* given suite. | ||
* Only run tests matching the given tags | ||
* | ||
* @param {Suite} suite | ||
* @return {Number} | ||
* @param {Array} tags | ||
* @return {Runner} for chaining | ||
* @api public | ||
*/ | ||
|
||
Runner.prototype.grepTotal = function(suite) { | ||
var self = this; | ||
var total = 0; | ||
Runner.prototype.tags = function(tags){ | ||
debug('include tags: %s', tags); | ||
this._filter.tags = tags; | ||
this.total = this._filter.count(this.suite); | ||
return this; | ||
}; | ||
|
||
suite.eachTest(function(test){ | ||
var match = self._grep.test(test.fullTitle()); | ||
if (self._invert) match = !match; | ||
if (match) total++; | ||
}); | ||
/** | ||
* Exclude tests matching the given tags | ||
* | ||
* @param {Array} tags | ||
* @return {Runner} for chaining | ||
* @api public | ||
*/ | ||
|
||
return total; | ||
Runner.prototype.skipTags = function(tags){ | ||
debug('skip tags: %s', tags); | ||
this._filter.skipTags = tags; | ||
this.total = this._filter.count(this.suite); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seeing this line quite a few times – might be worth refactoring into
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point thanks, I'll change extract & for a more descriptive name. |
||
return this; | ||
}; | ||
|
||
/** | ||
|
@@ -430,10 +439,8 @@ Runner.prototype.runTests = function(suite, fn){ | |
// all done | ||
if (!test) return fn(); | ||
|
||
// grep | ||
var match = self._grep.test(test.fullTitle()); | ||
if (self._invert) match = !match; | ||
if (!match) return next(); | ||
// filter | ||
if (!self._filter.shouldRun(test)) return next(); | ||
|
||
// pending | ||
if (test.pending) { | ||
|
@@ -480,7 +487,7 @@ Runner.prototype.runTests = function(suite, fn){ | |
*/ | ||
|
||
Runner.prototype.runSuite = function(suite, fn){ | ||
var total = this.grepTotal(suite) | ||
var total = this._filter.count(suite) | ||
, self = this | ||
, i = 0; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably mention the
a+b
anda,b
syntax.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This version doesn't actually have the
+
syntax, that was just a suggestion in thegh-pages
branch. But 👍 to the,
, I'll amend that.