[Pkg-javascript-commits] [node-tap] 88/186: more runner tests

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 1 16:40:47 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-tap.

commit 27f59fe3be4dfc330d16cae8c319ce0b8ab5c0e8
Author: isaacs <i at izs.me>
Date:   Tue Nov 14 17:07:44 2017 -0800

    more runner tests
---
 bin/run.js                            |  43 +++++----
 tap-snapshots/test-run.js-TAP.test.js |  22 +++++
 test/run.js                           | 166 +++++++++++++++++++++++++++++-----
 3 files changed, 184 insertions(+), 47 deletions(-)

diff --git a/bin/run.js b/bin/run.js
index a363fe1..eaca0c3 100755
--- a/bin/run.js
+++ b/bin/run.js
@@ -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
 
@@ -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
   }
@@ -109,7 +109,6 @@ const main = _ => {
     return respawnWithCoverage(options)
 
   setupTapEnv(options)
-
   runTests(options)
 }
 
@@ -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')
@@ -448,7 +449,6 @@ const pipeToCoverageServices = (options, child) => {
     }
   }
 
-  /* istanbul ignore if */
   if (!piped)
     throw new Error('unknown service, internal error')
 }
@@ -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'
@@ -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
@@ -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)
@@ -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)
@@ -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')
diff --git a/tap-snapshots/test-run.js-TAP.test.js b/tap-snapshots/test-run.js-TAP.test.js
index c2c99ff..277bb22 100644
--- a/tap-snapshots/test-run.js-TAP.test.js
+++ b/tap-snapshots/test-run.js-TAP.test.js
@@ -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}
+
+`
diff --git a/test/run.js b/test/run.js
index 63650f4..8e763e8 100644
--- a/test/run.js
+++ b/test/run.js
@@ -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)
 }
 
@@ -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), {
@@ -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 => {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-tap.git



More information about the Pkg-javascript-commits mailing list