Skip to content

Commit

Permalink
add --grep option
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jun 23, 2017
1 parent e3c17e4 commit 7e94cfc
Show file tree
Hide file tree
Showing 11 changed files with 572 additions and 1 deletion.
35 changes: 35 additions & 0 deletions bin/run.js
Expand Up @@ -123,6 +123,7 @@ function constructDefaultArgs () {
color: !!colorSupport.level,
reporter: null,
files: [],
grep: [],
bail: false,
saveFile: null,
pipeToService: false,
Expand Down Expand Up @@ -150,6 +151,8 @@ function parseArgs (args, defaults) {
var singleFlags = {
b: 'bail',
B: 'no-bail',
i: 'invert',
I: 'no-invert',
c: 'color',
C: 'no-color',
T: 'no-timeout',
Expand All @@ -161,6 +164,7 @@ function parseArgs (args, defaults) {

var singleOpts = {
j: 'jobs',
g: 'grep',
R: 'reporter',
t: 'timeout',
s: 'save',
Expand Down Expand Up @@ -367,6 +371,20 @@ function parseArgs (args, defaults) {
options.timeout = +val
continue

case '--invert':
options.grepInvert = true
continue

case '--no-invert':
options.grepInvert = false
continue

case '--grep':
val = val || args[++i]
if (val !== undefined)
options.grep.push(strToRegExp(val))
continue

case '--bail':
options.bail = true
continue
Expand Down Expand Up @@ -576,6 +594,14 @@ function setupTapEnv (options) {

if (options.bail)
process.env.TAP_BAIL = '1'

if (options.grepInvert)
process.env.TAP_GREP_INVERT = '1'

if (options.grep.length)
process.env.TAP_GREP = options.grep.map(function (pattern) {
return pattern.toString()
}).join('\n')
}

function globFiles (files) {
Expand Down Expand Up @@ -734,6 +760,8 @@ function runTests (options) {
// because there are 1 or more files to spawn.
var tap = require('../lib/tap.js')

// greps are passed to children, but not the runner itself
tap.grep = []
tap.jobs = options.jobs
tap.patchProcess()

Expand Down Expand Up @@ -762,3 +790,10 @@ function parseRcFile (path) {
return {}
}
}

function strToRegExp (g) {
var p = g.match(/^\/(.*)\/([a-z]*)$/)
g = p ? p[1] : g
var flags = p ? p[2] : ''
return new RegExp(g, flags)
}
16 changes: 16 additions & 0 deletions bin/usage.txt
Expand Up @@ -29,6 +29,22 @@ Options:
cannot be reported, and older TAP
parsers may get upset.

-g<pattern> Only run subtests tests matching the specified
--grep=<pattern> pattern.

Patterns are matched against top-level
subtests in each file. To filter tests
at subsequent levels, specify this
option multiple times.

To specify regular expression flags,
format pattern like a JavaScript RegExp
literal. For example: `/xyz/i` for
case-insensitive matching.

-i --invert Invert the matches to --grep patterns.
(Like grep -v)

-c --color Use colors (Default for TTY)

-C --no-color Do not use colors (Default for non-TTY)
Expand Down
2 changes: 2 additions & 0 deletions lib/base.js
Expand Up @@ -25,6 +25,8 @@ function Base (options) {
this.time = null
this.readyToProcess = false
this.options = options
this.grep = ownOr(options, 'grep', [])
this.grepInvert = ownOr(options, 'grepInvert', [])
this.parent = ownOr(options, 'parent', null)
this.bail = ownOrEnv(options, 'bail', 'TAP_BAIL', true)
this.name = ownOr(options, 'name', '')
Expand Down
2 changes: 2 additions & 0 deletions lib/clean-yaml-object.js
Expand Up @@ -59,6 +59,8 @@ function yamlFilter (propertyName, isRoot, source, target) {
propertyName === 'indent' ||
propertyName === 'skip' ||
propertyName === 'bail' ||
propertyName === 'grep' ||
propertyName === 'grepInvert' ||
propertyName === 'diagnostic' ||
propertyName === 'buffered' ||
propertyName === 'parent' ||
Expand Down
13 changes: 13 additions & 0 deletions lib/tap.js
Expand Up @@ -118,6 +118,19 @@ if (process.env.TAP_DEBUG === '1' ||
/\btap\b/.test(process.env.NODE_DEBUG || ''))
opt.debug = true

if (process.env.TAP_GREP) {
opt.grep = process.env.TAP_GREP.split('\n').map(function (g) {
var p = g.match(/^\/(.*)\/([a-z]*)$/)
g = p ? p[1] : g
var flags = p ? p[2] : ''
return new RegExp(g, flags)
})
}

if (process.env.TAP_GREP_INVERT === '1') {
opt.grepInvert = true
}

var tap = new TAP(opt)
module.exports = tap
tap.mocha = require('./mocha.js')
Expand Down
16 changes: 15 additions & 1 deletion lib/test.js
Expand Up @@ -110,11 +110,25 @@ Test.prototype.spawn = function spawn (cmd, args, options, name) {
}

Test.prototype.sub = function (Class, extra, caller) {
if (extra && (extra.todo || extra.skip)) {
extra = extra || {}
if (!extra.skip && this.grep.length) {
var match = this.grep[0].test(extra.name)
if (this.grepInvert) {
match = !match
}
if (!match) {
var p = 'filter' + (this.grepInvert ? ' out' : '') + ': '
extra.skip = p + this.grep[0]
}
}

if (extra.todo || extra.skip) {
this.pass(extra.name, extra)
return Promise.resolve(this)
}

if (!extra.grep)
extra.grep = this.grep.slice(1)
extra.indent = ' '
if (this.jobs > 1 && process.env.TAP_BUFFER === undefined)
extra.buffered = ownOr(extra, 'buffered', true)
Expand Down
112 changes: 112 additions & 0 deletions test/test/grep--bail--buffer.tap
@@ -0,0 +1,112 @@
TAP version 13
ok 1 - ___/.*(node|iojs)(.exe)?/~~~ ./bin/run.js ./test/test/ok.js -C --grep=nesting --grep=/[ASDF].*d$/gi ___/# time=[0-9.]+(ms)?/~~~ {
ok 1 - ___/.*/~~~ok.js ___/# time=[0-9.]+(ms)?/~~~ {
ok 1 - nesting # SKIP filter out: /nesting/
ok 2 - this passes
ok 3 - this passes too
ok 4 - async kid ___/# time=[0-9.]+(ms)?/~~~ {
1..2
ok 1 - timeout
ok 2 - timeout
}

ok 5 - pass after async kid
1..5
# skip: 1
___/# time=[0-9.]+(ms)?/~~~
}

1..1
___/# time=[0-9.]+(ms)?/~~~
}

ok 2 - ___/.*(node|iojs)(.exe)?/~~~ ./bin/run.js ./test/test/ok.js -C -i --grep=nesting --grep=/[ASDF].*d$/gi ___/# time=[0-9.]+(ms)?/~~~ {
ok 1 - ___/.*/~~~ok.js ___/# time=[0-9.]+(ms)?/~~~ {
ok 1 - nesting # SKIP filter out: /nesting/
ok 2 - this passes
ok 3 - this passes too
ok 4 - async kid ___/# time=[0-9.]+(ms)?/~~~ {
1..2
ok 1 - timeout
ok 2 - timeout
}

ok 5 - pass after async kid
1..5
# skip: 1
___/# time=[0-9.]+(ms)?/~~~
}

1..1
___/# time=[0-9.]+(ms)?/~~~
}

# invert=false
ok 3 - a ___/# time=[0-9.]+(ms)?/~~~ {
ok 1 - x ___/# time=[0-9.]+(ms)?/~~~ {
ok 1 - abc # SKIP filter out: /b/
ok 2 - xyz ___/# time=[0-9.]+(ms)?/~~~ {
ok 1 - 123 # SKIP filter out: /[246]+/
ok 2 - ijk ___/# time=[0-9.]+(ms)?/~~~ {
ok 1 - this is fine
1..1
}

1..2
# skip: 1
}

1..2
# skip: 1
}

ok 2 - y ___/# time=[0-9.]+(ms)?/~~~ {
ok 1 - abc # SKIP filter out: /b/
ok 2 - xyz ___/# time=[0-9.]+(ms)?/~~~ {
ok 1 - 123 # SKIP filter out: /[246]+/
ok 2 - ijk ___/# time=[0-9.]+(ms)?/~~~ {
ok 1 - this is fine
1..1
}

1..2
# skip: 1
}

1..2
# skip: 1
}

ok 3 - z # SKIP filter: /[^z]/
1..3
# skip: 1
}

# invert=true
ok 4 - a ___/# time=[0-9.]+(ms)?/~~~ {
ok 1 - x # SKIP filter out: /[^z]/
ok 2 - y # SKIP filter out: /[^z]/
ok 3 - z ___/# time=[0-9.]+(ms)?/~~~ {
ok 1 - abc # SKIP filter out: /b/
ok 2 - xyz ___/# time=[0-9.]+(ms)?/~~~ {
ok 1 - 123 # SKIP filter out: /[246]+/
ok 2 - ijk ___/# time=[0-9.]+(ms)?/~~~ {
ok 1 - this is fine
1..1
}

1..2
# skip: 1
}

1..2
# skip: 1
}

1..3
# skip: 2
}

1..4
___/# time=[0-9.]+(ms)?/~~~

112 changes: 112 additions & 0 deletions test/test/grep--bail.tap
@@ -0,0 +1,112 @@
TAP version 13
# Subtest: ___/.*(node|iojs)(.exe)?/~~~ ./bin/run.js ./test/test/ok.js -C --grep=nesting --grep=/[ASDF].*d$/gi
# Subtest: ___/.*/~~~ok.js
ok 1 - nesting # SKIP filter out: /nesting/
ok 2 - this passes
ok 3 - this passes too
# Subtest: async kid
1..2
ok 1 - timeout
ok 2 - timeout
ok 4 - async kid ___/# time=[0-9.]+(ms)?/~~~

ok 5 - pass after async kid
1..5
# skip: 1
___/# time=[0-9.]+(ms)?/~~~
ok 1 - ___/.*/~~~ok.js ___/# time=[0-9.]+(ms)?/~~~

1..1
___/# time=[0-9.]+(ms)?/~~~
ok 1 - ___/.*(node|iojs)(.exe)?/~~~ ./bin/run.js ./test/test/ok.js -C --grep=nesting --grep=/[ASDF].*d$/gi ___/# time=[0-9.]+(ms)?/~~~

# Subtest: ___/.*(node|iojs)(.exe)?/~~~ ./bin/run.js ./test/test/ok.js -C -i --grep=nesting --grep=/[ASDF].*d$/gi
# Subtest: ___/.*/~~~ok.js
ok 1 - nesting # SKIP filter out: /nesting/
ok 2 - this passes
ok 3 - this passes too
# Subtest: async kid
1..2
ok 1 - timeout
ok 2 - timeout
ok 4 - async kid ___/# time=[0-9.]+(ms)?/~~~

ok 5 - pass after async kid
1..5
# skip: 1
___/# time=[0-9.]+(ms)?/~~~
ok 1 - ___/.*/~~~ok.js ___/# time=[0-9.]+(ms)?/~~~

1..1
___/# time=[0-9.]+(ms)?/~~~
ok 2 - ___/.*(node|iojs)(.exe)?/~~~ ./bin/run.js ./test/test/ok.js -C -i --grep=nesting --grep=/[ASDF].*d$/gi ___/# time=[0-9.]+(ms)?/~~~

# invert=false
# Subtest: a
# Subtest: x
ok 1 - abc # SKIP filter out: /b/
# Subtest: xyz
ok 1 - 123 # SKIP filter out: /[246]+/
# Subtest: ijk
ok 1 - this is fine
1..1
ok 2 - ijk ___/# time=[0-9.]+(ms)?/~~~

1..2
# skip: 1
ok 2 - xyz ___/# time=[0-9.]+(ms)?/~~~

1..2
# skip: 1
ok 1 - x ___/# time=[0-9.]+(ms)?/~~~

# Subtest: y
ok 1 - abc # SKIP filter out: /b/
# Subtest: xyz
ok 1 - 123 # SKIP filter out: /[246]+/
# Subtest: ijk
ok 1 - this is fine
1..1
ok 2 - ijk ___/# time=[0-9.]+(ms)?/~~~

1..2
# skip: 1
ok 2 - xyz ___/# time=[0-9.]+(ms)?/~~~

1..2
# skip: 1
ok 2 - y ___/# time=[0-9.]+(ms)?/~~~

ok 3 - z # SKIP filter: /[^z]/
1..3
# skip: 1
ok 3 - a ___/# time=[0-9.]+(ms)?/~~~

# invert=true
# Subtest: a
ok 1 - x # SKIP filter out: /[^z]/
ok 2 - y # SKIP filter out: /[^z]/
# Subtest: z
ok 1 - abc # SKIP filter out: /b/
# Subtest: xyz
ok 1 - 123 # SKIP filter out: /[246]+/
# Subtest: ijk
ok 1 - this is fine
1..1
ok 2 - ijk ___/# time=[0-9.]+(ms)?/~~~

1..2
# skip: 1
ok 2 - xyz ___/# time=[0-9.]+(ms)?/~~~

1..2
# skip: 1
ok 3 - z ___/# time=[0-9.]+(ms)?/~~~

1..3
# skip: 2
ok 4 - a ___/# time=[0-9.]+(ms)?/~~~

1..4
___/# time=[0-9.]+(ms)?/~~~

0 comments on commit 7e94cfc

Please sign in to comment.