diff options
author | isaacs <i@izs.me> | 2014-04-30 23:21:05 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2014-04-30 23:21:20 +0400 |
commit | 44ed5943f152190808f804daf4b6940b1500bdd3 (patch) | |
tree | a2571921fd9fa0de882771ef86174415b6ddd72f /test | |
parent | 1053c97891a53c92358b75d943e7d01aed54f83a (diff) |
test: simplify common.run() to common.npm()
Diffstat (limited to 'test')
-rw-r--r-- | test/common-tap.js | 29 | ||||
-rw-r--r-- | test/tap/invalid-cmd-exit-code.js | 26 | ||||
-rw-r--r-- | test/tap/ls-depth-cli.js | 34 | ||||
-rw-r--r-- | test/tap/startstop.js | 27 |
4 files changed, 61 insertions, 55 deletions
diff --git a/test/common-tap.js b/test/common-tap.js index 2c7dee4ec..d6d09ed9b 100644 --- a/test/common-tap.js +++ b/test/common-tap.js @@ -1,35 +1,32 @@ -var spawn = require('child_process').spawn +var spawn = require("child_process").spawn var port = exports.port = 1337 exports.registry = "http://localhost:" + port process.env.npm_config_loglevel = "error" -exports.run = run -function run (cmd, t, opts, cb) { - if (!opts) - opts = {} - if (!Array.isArray(cmd)) - throw new Error("cmd must be an Array") - if (!t || !t.end) - throw new Error("node-tap instance is missing") +var bin = exports.bin = require.resolve("../bin/npm-cli.js") +var once = require("once") +exports.npm = function (cmd, opts, cb) { + cb = once(cb) + cmd = [bin].concat(cmd) + opts = opts || {} var stdout = "" , stderr = "" , node = process.execPath , child = spawn(node, cmd, opts) - child.stderr.on("data", function (chunk) { + if (child.stderr) child.stderr.on("data", function (chunk) { stderr += chunk }) - child.stdout.on("data", function (chunk) { + if (child.stdout) child.stdout.on("data", function (chunk) { stdout += chunk }) - child.on("close", function (code) { - if (cb) - cb(t, stdout, stderr, code, { cmd: cmd, opts: opts }) - else - t.end() + child.on("error", cb) + + child.on("close", function (code, signal) { + cb(null, code, stdout, stderr) }) } diff --git a/test/tap/invalid-cmd-exit-code.js b/test/tap/invalid-cmd-exit-code.js index c3dabb85a..14db8669e 100644 --- a/test/tap/invalid-cmd-exit-code.js +++ b/test/tap/invalid-cmd-exit-code.js @@ -1,27 +1,29 @@ -var test = require('tap').test -var npm = require.resolve('../../bin/npm-cli.js') +var test = require("tap").test var node = process.execPath -var common = require('../common-tap.js') +var common = require("../common-tap.js") var opts = { cwd: process.cwd() } -test('npm asdf should return exit code 1', function(t) { - common.run([npm, 'asdf'], t, opts, function (t, _, __, c) { - t.equal(c, 1, 'exit code should be 1') +test("npm asdf should return exit code 1", function(t) { + common.npm(["asdf"], opts, function (er, c) { + if (er) throw er + t.ok(c, "exit code should not be zero") t.end() }) }) -test('npm help should return exit code 0', function(t) { - common.run([npm, 'help'], t, opts, function (t, _, __, c) { - t.equal(c, 0, 'exit code should be 0') +test("npm help should return exit code 0", function(t) { + common.npm(["help"], opts, function (er, c) { + if (er) throw er + t.equal(c, 0, "exit code should be 0") t.end() }) }) -test('npm help fadf should return exit code 0', function(t) { - common.run([npm, 'help', 'fadf'], t, opts, function (t, _, __, c) { - t.equal(c, 0, 'exit code should be 0') +test("npm help fadf should return exit code 0", function(t) { + common.npm(["help", "fadf"], opts, function (er, c) { + if (er) throw er + t.equal(c, 0, "exit code should be 0") t.end() }) }) diff --git a/test/tap/ls-depth-cli.js b/test/tap/ls-depth-cli.js index b6fbb939e..fcbc4364f 100644 --- a/test/tap/ls-depth-cli.js +++ b/test/tap/ls-depth-cli.js @@ -25,12 +25,10 @@ test('setup', function (t) { mkdirp.sync(pkg + '/cache') mkdirp.sync(pkg + '/tmp') mr(common.port, function (s) { - common.run([ - npm - , 'install' - , '--registry=' + common.registry - ], t, opts - , function (t, c) { + var cmd = ['install', '--registry=' + common.registry] + common.npm(cmd, opts, function (er, c) { + if (er) throw er + t.equal(c, 0) s.close() t.end() }) @@ -38,20 +36,24 @@ test('setup', function (t) { }) test('npm ls --depth=0', function (t) { - common.run([npm, 'ls', '--depth=0'], t, opts, function (t, c) { - t.has(c, /test-package-with-one-dep@0\.0\.0/ + common.npm(['ls', '--depth=0'], opts, function (er, c, out) { + if (er) throw er + t.equal(c, 0) + t.has(out, /test-package-with-one-dep@0\.0\.0/ , "output contains test-package-with-one-dep@0.0.0") - t.doesNotHave(c, /test-package@0\.0\.0/ + t.doesNotHave(out, /test-package@0\.0\.0/ , "output not contains test-package@0.0.0") t.end() }) }) test('npm ls --depth=1', function (t) { - common.run([npm, 'ls', '--depth=1'], t, opts, function (t, c) { - t.has(c, /test-package-with-one-dep@0\.0\.0/ + common.npm(['ls', '--depth=1'], opts, function (er, c, out) { + if (er) throw er + t.equal(c, 0) + t.has(out, /test-package-with-one-dep@0\.0\.0/ , "output contains test-package-with-one-dep@0.0.0") - t.has(c, /test-package@0\.0\.0/ + t.has(out, /test-package@0\.0\.0/ , "output contains test-package@0.0.0") t.end() }) @@ -60,10 +62,12 @@ test('npm ls --depth=1', function (t) { test('npm ls --depth=Infinity', function (t) { // travis has a preconfigured depth=0, in general we can not depend // on the default value in all environments, so explictly set it here - common.run([npm, 'ls', '--depth=Infinity'], t, opts, function (t, c) { - t.has(c, /test-package-with-one-dep@0\.0\.0/ + common.npm(['ls', '--depth=Infinity'], opts, function (er, c, out) { + if (er) throw er + t.equal(c, 0) + t.has(out, /test-package-with-one-dep@0\.0\.0/ , "output contains test-package-with-one-dep@0.0.0") - t.has(c, /test-package@0\.0\.0/ + t.has(out, /test-package@0\.0\.0/ , "output contains test-package@0.0.0") t.end() }) diff --git a/test/tap/startstop.js b/test/tap/startstop.js index cf7ec9f91..4ac8fd000 100644 --- a/test/tap/startstop.js +++ b/test/tap/startstop.js @@ -11,13 +11,16 @@ var common = require('../common-tap') , npm = path.resolve(__dirname, '../../cli.js') , opts = { cwd: pkg } -function testOutput (t, c, e, code, o) { - if (e) - throw new Error('npm ' + command + ' stderr: ' + e.toString()) +function testOutput (t, command, er, code, stdout, stderr) { + if (er) + throw er - c = c.trim().split('\n') - c = c[c.length - 1] - t.equal(c, o.cmd[1]) + if (stderr) + throw new Error('npm ' + command + ' stderr: ' + stderr.toString()) + + stdout = stdout.trim().split('\n') + stdout = stdout[stdout.length - 1] + t.equal(stdout, command) t.end() } @@ -34,19 +37,19 @@ test('setup', function (t) { }) test('npm start', function (t) { - common.run([npm, 'start'], t, opts, testOutput) + common.npm(['start'], opts, testOutput.bind(null, t, "start")) }) test('npm stop', function (t) { - common.run([npm, 'stop'], t, opts, testOutput) + common.npm(['stop'], opts, testOutput.bind(null, t, "stop")) }) test('npm restart', function (t) { - common.run([npm, 'restart'], t, opts, function (t, c, e) { - if (e) - throw new Error('npm ' + command + ' stderr: ' + e.toString()) + common.npm(['restart'], opts, function (er, c, stdout, stderr) { + if (er) + throw er - var output = c.split('\n').filter(function (val) { + var output = stdout.split('\n').filter(function (val) { return val.match(/^s/) }) |