Skip to content

Commit

Permalink
more runner tests
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Nov 15, 2017
1 parent 9ca96eb commit 27f59fe
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 47 deletions.
43 changes: 21 additions & 22 deletions bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ const main = _ => {
const rcOptions = parseRcFile(rcFile)

// supplement defaults with parsed rc options
if (rcOptions)
Object.keys(rcOptions).forEach(function (k) {
defaults[k] = rcOptions[k]
})
Object.keys(rcOptions).forEach(function (k) {
defaults[k] = rcOptions[k]
})

defaults.rcFile = rcFile

Expand Down Expand Up @@ -97,6 +96,7 @@ const main = _ => {
if (options.files.length === 1 && options.files[0] === '-') {
if (options.coverage)
console.error('Coverage disabled because stdin cannot be instrumented')
setupTapEnv(options)
stdinOnly(options)
return
}
Expand All @@ -109,7 +109,6 @@ const main = _ => {
return respawnWithCoverage(options)

setupTapEnv(options)

runTests(options)
}

Expand Down Expand Up @@ -417,6 +416,8 @@ const parseArgs = (args, options) => {
return options
}

// Obviously, this bit isn't going to ever be covered, because
// it only runs when we DON'T have coverage enabled, to enable it.
/* istanbul ignore next */
const respawnWithCoverage = options => {
// console.error('respawn with coverage')
Expand Down Expand Up @@ -448,7 +449,6 @@ const pipeToCoverageServices = (options, child) => {
}
}

/* istanbul ignore if */
if (!piped)
throw new Error('unknown service, internal error')
}
Expand Down Expand Up @@ -579,9 +579,9 @@ const getReporters = _ => {
const setupTapEnv = options => {
process.env.TAP_TIMEOUT = options.timeout
if (options.color)
process.env.TAP_COLORS = 1
process.env.TAP_COLORS = '1'
else
process.env.TAP_COLORS = 0
process.env.TAP_COLORS = '0'

if (options.bail)
process.env.TAP_BAIL = '1'
Expand All @@ -598,7 +598,7 @@ const setupTapEnv = options => {
}

const globFiles = files => {
return files.reduce(function (acc, f) {
return files.reduce((acc, f) => {
if (f === '-') {
acc.push(f)
return acc
Expand Down Expand Up @@ -642,7 +642,7 @@ const saveFails = (options, tap) => {

let fails = []
const successes = []
tap.on('result', function (res) {
tap.on('result', res => {
// we will continue to re-run todo tests, even though they're
// not technically "failures".
if (!res.ok && !res.extra.skip)
Expand All @@ -651,17 +651,7 @@ const saveFails = (options, tap) => {
successes.push(res.extra.file)
})

tap.on('bailout', function (reason) {
// add any pending test files to the fails list.
fails.push.apply(fails, options.files.filter(function (file) {
return successes.indexOf(file) === -1
}))
save()
})

tap.on('end', save)

function save () {
const save = () => {
fails = fails.reduce(function (set, f) {
if (set.indexOf(f) === -1)
set.push(f)
Expand All @@ -677,10 +667,19 @@ const saveFails = (options, tap) => {
fs.writeFileSync(options.saveFile, fails.join('\n') + '\n')
} catch (er) {}
}

tap.on('bailout', reason => {
// add any pending test files to the fails list.
fails.push.apply(fails, options.files.filter(file =>
successes.indexOf(file) === -1))
save()
})

tap.on('end', save)
}

const filterFiles = (files, saved, parallelOk) => {
return files.filter(function (file) {
return files.filter(file => {
if (path.basename(file) === 'tap-parallel-ok')
parallelOk[path.resolve(path.dirname(file))] = true
else if (path.basename(file) === 'tap-parallel-not-ok')
Expand Down
22 changes: 22 additions & 0 deletions tap-snapshots/test-run.js-TAP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,25 @@ ok 1 - test/cli-tests/ok.js # {time}
# {time}
`

exports[`test/run.js TAP stdin with file > undefined 1`] = `
TAP version 13
ok 1 - foo.js # {time} {
ok 1 - child # {time} {
ok 1 - this is fine
1..1
}
1..1
# {time}
}
ok 2 - /dev/stdin # {time} {
1..1
ok
}
1..2
# {time}
`
166 changes: 141 additions & 25 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ const run = (args, options, cb) => {
return execFile(node, [bin].concat(args), options, cb)
}

const tmpfile = (t, filename, program) => {
const tmpfile = (t, filename, content) => {
filename = path.join(dir, filename)
t.teardown(() => rimraf.sync(filename))
fs.writeFileSync(filename, program)
fs.writeFileSync(filename, content)
return path.relative('', filename)
}

Expand Down Expand Up @@ -102,15 +102,15 @@ t.test('dump config stuff', t => {
t.test('shotgun a bunch of option parsing junk', t => {
run([
'--dump-config', '-J', '--jobs', '4',
'--no-browser', '--no-coverage-report', '--coverage-report', 'html',
'--no-cov', '--cov', '--save', 'foo.txt', '--reporter=spec', '--gc',
'--strict', '--debug', '--debug-brk', '--harmony', '--node-arg=xyz',
'--check-coverage', '--test-arg=xyz', '--test-arg', 'abc', '--100',
'--branches=99', '--lines', '100', '--color', '--no-color',
'--output-file=out.txt', '--no-timeout', '--timeout', '99', '--invert',
'--no-invert', '--grep', 'x', '--grep=/y/i', '--bail', '--no-bail',
'--only', '-R', 'spec', '--node-arg', 'abc', '--nyc-arg', 'abc',
'-o', 'out.txt'
'--no-browser', '--no-coverage-report', '--coverage-report', 'json',
'--coverage-report=html', '--no-cov', '--cov', '--save', 'foo.txt',
'--reporter=spec', '--gc', '--strict', '--debug', '--debug-brk',
'--harmony', '--node-arg=xyz', '--check-coverage', '--test-arg=xyz',
'--test-arg', 'abc', '--100', '--branches=99', '--lines', '100',
'--color', '--no-color', '--output-file=out.txt', '--no-timeout',
'--timeout', '99', '--invert', '--no-invert', '--grep', 'x',
'--grep=/y/i', '--bail', '--no-bail', '--only', '-R', 'spec',
'--node-arg', 'abc', '--nyc-arg', 'abc', '-o', 'out.txt'
], null, (er, o, e) => {
t.equal(er, null)
t.same(JSON.parse(o), {
Expand Down Expand Up @@ -149,26 +149,142 @@ t.test('dump config stuff', t => {
})
})

t.test('short options as well as short flags')
t.test('undefined trailing value opts')
t.test('short options as well as short flags', t => {
run(['--dump-config','-j2','-Cb','-t=0' ], { env: {
TAP: '0'
}}, (er, o, e) => {
t.equal(er, null)
t.match(JSON.parse(o), {
bail: true,
color: false,
reporter: 'tap',
jobs: 2,
timeout: 0
})
t.end()
})
})

t.test('unknown short opt', t => {
run(['-bCx'], null, (er, o, e) => {
t.match(er, { code: 1 })
t.match(e, 'Unknown argument: -x')
t.end()
})
})

t.test('undefined trailing value opts', t => {
const opts = [
'--node-arg',
'--test-arg',
'--nyc-arg',
'--output-file',
'--grep'
]
const expect = {
nodeArgs: [],
testArgs: [],
nycArgs: [],
outputFile: null,
grep: []
}
t.plan(opts.length)
opts.forEach(opt => t.test(opt, t => run([
'--dump-config', opt
], null, (er, o, e) => {
t.equal(er, null)
t.match(JSON.parse(o), expect)
t.end()
})))
})

t.test('good rc file', t => {
const rc = tmpfile(t, 'taprc', `
reporter: spec
jobs: 3
`)
run(['--dump-config'], { env: {
TAP_RCFILE: rc,
TAP: 0
}}, (er, o, e) => {
t.equal(er, null)
t.match(JSON.parse(o), {
reporter: 'spec',
jobs: 3
})
t.end()
})
})

t.test('empty rc file', t => {
const rc = tmpfile(t, 'taprc', '')
run(['--dump-config'], { env: {
TAP_RCFILE: rc,
TAP: 0
}}, (er, o, e) => {
t.equal(er, null)
t.match(JSON.parse(o), {
reporter: 'classic',
jobs: 1
})
t.end()
})
})

t.end()
})

t.test('stdin only', t => {
t.test('stdin', t => {
const tapcode = 'TAP version 13\n1..1\nok\n'
const c = run(['-', '-C', '-Rspec', '-ofoo.txt'], { env: {
_TAP_IS_TTY: '1',
TAP: '0'
}}, (er, o, e) => {
t.equal(er, null)
t.equal(e, '')
t.match(o, '✓')
t.equal(fs.readFileSync('foo.txt', 'utf8'), tapcode)
fs.unlinkSync('foo.txt')
t.end()

t.test('with output file', t => {
const c = run(['-', '-C', '-Rspec', '-ofoo.txt'], { env: {
_TAP_IS_TTY: '1',
TAP: '0'
}}, (er, o, e) => {
t.equal(er, null)
t.equal(e, '')
t.match(o, '✓')
t.equal(fs.readFileSync('foo.txt', 'utf8'), tapcode)
fs.unlinkSync('foo.txt')
t.end()
})
c.stdin.end(tapcode)
})
c.stdin.end(tapcode)

t.test('no output file', t => {
const c = run(['-', '--only', '-gx', '-iC', '-Rclassic'], { env: {
_TAP_IS_TTY: '1',
TAP: '0'
}}, (er, o, e) => {
t.equal(er, null)
t.equal(e, '')
t.match(o, /total \.+ 1\/1/)
t.throws(() => fs.statSync('foo.txt'))
t.end()
})
c.stdin.end(tapcode)
})

t.test('with file', t => {
const foo = tmpfile(t, 'foo.js', `
'use strict'
require(${tap}).test('child', t => {
t.pass('this is fine')
t.end()
})
`)
const args = ['-', 'foo.js', '-CRclassic', '-ofoo.txt']
const c = run(args, { env: { TAP: 0, TAP_BUFFER: 1 }}, (er, o, e) => {
t.equal(er, null)
t.matchSnapshot(clean(fs.readFileSync('foo.txt', 'utf8')))
t.match(o, /foo.js \.+ 1\/1.*\n\/dev\/stdin \.+ 1\/1\n/)
t.end()
})
c.stdin.end(tapcode)
})

t.end()
})

t.test('unknown arg throws', t => {
Expand Down

0 comments on commit 27f59fe

Please sign in to comment.