Skip to content

Commit

Permalink
grep. Closes #90
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed May 20, 2014
1 parent b38df71 commit be294a2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
40 changes: 35 additions & 5 deletions lib/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ exports.execute = function (skipTraverse) {
.describe('l', 'disable global variable leaks detection')
.boolean('l')

.alias('g', 'grep')
.describe('g', 'only run tests matching the given pattern which is internally compiled to a RegExp')

.alias('G', 'use-global')
.default('G', false)
.describe('G', 'export Lab as a global')
Expand Down Expand Up @@ -144,13 +147,17 @@ exports.execute = function (skipTraverse) {
// Execute experiments

var idsFilter = argv.i ? [].concat(argv.i) : []
reporter.start({ count: idsFilter.length || Lab.count });
var grep = argv.g ? new RegExp(argv.g) : null;

console.log(internals.count(Lab.root, idsFilter, grep))
reporter.start({ count: internals.count(Lab.root, idsFilter, grep) });

var startTime = Date.now();
var state = {
report: report,
reporter: reporter,
ids: idsFilter
ids: idsFilter,
grep: grep
};

internals.executeExperiments(Lab.root, state, argv.d, function (err) {
Expand Down Expand Up @@ -348,8 +355,8 @@ internals.executeTests = function (experiment, state, skip, callback) {

var execute = function (test, nextTest) {

if (state.ids.length &&
state.ids.indexOf(test.id) === -1) {
if ((state.ids.length && state.ids.indexOf(test.id) === -1) ||
(state.grep && !state.grep.test(test.title))) {

return nextTest();
}
Expand All @@ -365,7 +372,6 @@ internals.executeTests = function (experiment, state, skip, callback) {

// Unit


if (!test.fn ||
skip ||
test.options.skip) {
Expand Down Expand Up @@ -497,3 +503,27 @@ internals.output = function (err) {

process.stderr.write(err.message + '\n' + err.stack + '\n');
};


internals.count = function (experiments, ids, grep) {

var counter = 0;

if (experiments) {
for (var e = 0, el = experiments.length; e < el; ++e) {
var experiment = experiments[e];

if (experiment.tests) {
for (var i = 0, il = experiment.tests.length; i < il; ++i) {
var test = experiment.tests[i];
counter += (ids.length && ids.indexOf(test.id) === -1) || (grep && !grep.test(test.title)) ? 0 : 1;
}
}

counter += internals.count(experiment.experiments, ids, grep);
}
}

return counter;
};

21 changes: 12 additions & 9 deletions lib/reporters/tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ exports = module.exports = internals.Reporter = function (options) {
this.failures = 0;
this.skipped = 0;
this.todo = 0;
this.counter = 0;
};


Expand All @@ -25,25 +26,27 @@ internals.Reporter.prototype.start = function (notebook) {

internals.Reporter.prototype.test = function (test) {

var title = test.title.replace(/#/g, '');
var title = '(' + test.id + ') ' + test.title.replace(/#/g, '');
var id = ++this.counter;

if (test.err) {
this.failures++;
this.print('not ok ' + test.id + ' ' + title + '\n');
++this.failures;
this.print('not ok ' + id + ' ' + title + '\n');
if (test.err.stack) {
this.print(test.err.stack.replace(/^/gm, ' ') + '\n');
}
}
else if (test.skipped) {
this.skipped++;
this.print('ok ' + test.id + ' SKIP ' + title + '\n');
++this.skipped;
this.print('ok ' + id + ' SKIP ' + title + '\n');
}
else if (test.todo) {
this.todo++;
this.print('ok ' + test.id + ' TODO ' + title + '\n');
++this.todo;
this.print('ok ' + id + ' TODO ' + title + '\n');
}
else {
this.passes++;
this.print('ok ' + test.id + ' ' + title + '\n');
++this.passes;
this.print('ok ' + id + ' ' + title + '\n');
}
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "lab",
"description": "Test utility",
"version": "3.1.4",
"version": "3.2.0",
"author": "Eran Hammer <eran@hueniverse.com> (http://hueniverse.com)",
"contributors": [],
"repository": "git://github.com/spumko/lab",
Expand Down

0 comments on commit be294a2

Please sign in to comment.