Skip to content
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

Revert grep to original functionality with strings converted to regex's.... #632

Closed
wants to merge 6 commits into from
Closed
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
10 changes: 5 additions & 5 deletions Makefile
Expand Up @@ -30,7 +30,7 @@ lib-cov:

test: test-unit

test-all: test-bdd test-tdd test-qunit test-exports test-unit test-grep test-jsapi test-compilers
test-all: test-bdd test-tdd test-qunit test-exports test-unit test-only test-jsapi test-compilers

test-jsapi:
@node test/jsapi
Expand Down Expand Up @@ -72,16 +72,16 @@ test-exports:
--ui exports \
test/acceptance/interfaces/exports

test-grep:
test-only:
@./bin/mocha \
--reporter $(REPORTER) \
--grep fast \
--only fast \
test/acceptance/misc/grep

test-invert:
@./bin/mocha \
--reporter $(REPORTER) \
--grep slow \
--only slow \
--invert \
test/acceptance/misc/grep

Expand Down Expand Up @@ -123,4 +123,4 @@ tm:
mkdir -p $(TM_DEST)
cp -fr editors/$(TM_BUNDLE) $(TM_DEST)

.PHONY: test-cov test-jsapi test-compilers watch test test-all test-bdd test-tdd test-qunit test-exports test-unit non-tty test-grep tm clean
.PHONY: test-cov test-jsapi test-compilers watch test test-all test-bdd test-tdd test-qunit test-exports test-unit non-tty test-only tm clean
13 changes: 9 additions & 4 deletions bin/_mocha
Expand Up @@ -60,8 +60,8 @@ program
.option('-r, --require <name>', 'require the given module')
.option('-R, --reporter <name>', 'specify the reporter to use', 'dot')
.option('-u, --ui <name>', 'specify user-interface (bdd|tdd|exports)', 'bdd')
.option('-g, --grep <pattern>', 'only run tests matching <pattern>')
.option('-i, --invert', 'inverts --grep matches')
.option('-o, --only <string>', 'only run tests containing <string>')
.option('-i, --invert', 'inverts --only and --only-regexp matches')
.option('-t, --timeout <ms>', 'set test-case timeout in milliseconds [2000]')
.option('-s, --slow <ms>', '"slow" test threshold in milliseconds [75]')
.option('-w, --watch', 'watch files for changes')
Expand All @@ -78,6 +78,7 @@ program
.option('--interfaces', 'display available interfaces')
.option('--reporters', 'display available reporters')
.option('--compilers <ext>:<module>,...', 'use the given module(s) to compile files', list, [])
.option('--only-regexp <pattern>', 'only run tests matching <pattern>')

program.name = 'mocha';

Expand Down Expand Up @@ -217,9 +218,13 @@ if (program.timeout) mocha.suite.timeout(program.timeout);

mocha.suite.bail(program.bail);

// --grep
// --only

if (program.grep) mocha.grep(new RegExp(program.grep));
if (program.only) mocha.only(program.only);

// --only-regexp

if (program.onlyRegexp) mocha.only(new RegExp(program.onlyRegexp));

// --invert

Expand Down
15 changes: 8 additions & 7 deletions lib/mocha.js
Expand Up @@ -64,7 +64,8 @@ function Mocha(options) {
options = options || {};
this.files = [];
this.options = options;
this.grep(options.grep);
if (options.only) this.only(options.only);
if (options['only-regexp']) this.only(new RegExp(options['only-regexp']));
this.suite = new exports.Suite('', new exports.Context);
this.ui(options.ui);
this.reporter(options.reporter);
Expand Down Expand Up @@ -165,17 +166,17 @@ Mocha.prototype._growl = function(runner, reporter) {
};

/**
* Add regexp to grep, if `re` is a string it is escaped.
* Add a pattern to grep, if `pattern` is a string, it is escaped.
*
* @param {RegExp|String} re
* @param {RegExp|String} pattern
* @return {Mocha}
* @api public
*/

Mocha.prototype.grep = function(re){
this.options.grep = 'string' == typeof re
? new RegExp(utils.escapeRegexp(re))
: re;
Mocha.prototype.only = function(pattern){
this.options.grep = 'string' == typeof pattern
? new RegExp(utils.escapeRegexp(pattern))
: pattern;
return this;
};

Expand Down
3 changes: 2 additions & 1 deletion support/tail.js
Expand Up @@ -100,7 +100,8 @@ process.on = function(e, fn){
mocha.globals('location');

var query = Mocha.utils.parseQuery(window.location.search || '');
if (query.grep) mocha.grep(query.grep);
if (query.only) mocha.only(query.only);
if (query['only-regexp']) mocha.only(new RegExp(query['only-regexp']));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd remove this, I wouldn't expect anyone to quote an option, we can just pass it through { only: 'string' } or { only: /stuff/ }

if (query.invert) mocha.invert();

return Mocha.prototype.run.call(mocha, function(){
Expand Down
38 changes: 25 additions & 13 deletions test/grep.js
Expand Up @@ -2,34 +2,46 @@
var Mocha = require('../');

describe('Mocha', function(){
describe('"grep" option', function(){
describe('"only-regexp" option', function(){
it('should add a RegExp to the mocha.options object', function(){
var mocha = new Mocha({ grep: /foo/ });
mocha.options.grep.toString().should.equal('/foo/');
var mocha = new Mocha({ 'only-regexp': /foo()/ });
mocha.options.grep.toString().should.equal('/foo()/');
})

it('should convert grep string to a RegExp', function(){
var mocha = new Mocha({ grep: 'foo' });
mocha.options.grep.toString().should.equal('/foo/');
it('should convert string to a RegExp', function(){
var mocha = new Mocha({ 'only-regexp': 'foo()' });
mocha.options.grep.toString().should.equal('/foo()/');
})
})

describe('.grep()', function(){
describe('.only()', function(){
it('should add a RegExp to the mocha.options object', function(){
var mocha = new Mocha;
mocha.grep(/foo/);
mocha.options.grep.toString().should.equal('/foo/');
mocha.only(/foo()/);
mocha.options.grep.toString().should.equal('/foo()/');
})

it('should convert grep string to a RegExp', function(){
it('should convert string to an escaped RegExp', function(){
var mocha = new Mocha;
mocha.grep('foo');
mocha.options.grep.toString().should.equal('/foo/');
mocha.only('foo()');
mocha.options.grep.toString().should.equal('/foo\\(\\)/');
})

it('should return it\'s parent Mocha object for chainability', function(){
var mocha = new Mocha;
mocha.grep().should.equal(mocha);
mocha.only().should.equal(mocha);
})
})

describe('"only" option', function(){
it('should add a RegExp to the mocha.options object', function(){
var mocha = new Mocha({ 'only': /foo()/ });
mocha.options.grep.toString().should.equal('/foo()/');
})

it('should convert string to an escaped RegExp', function(){
var mocha = new Mocha({ 'only': 'foo()' });
mocha.options.grep.toString().should.equal('/foo\\(\\)/');
})
})

Expand Down