Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add failing regression test for npm ls --depth=0 #4757

Closed
wants to merge 2 commits into from

4 participants

@robertkowalski
Collaborator

This test should turn green if read-installed is fixed and should
avoid regressions in the future.

This tests the cli, as it turned out that the programatical version
of npm has a bug where depth=0 is not working

#4733

test/tap/ls-depth-cli.js
@@ -0,0 +1,76 @@
+var common = require('../common-tap')
+ , test = require('tap').test
+ , path = require('path')
+ , spawn = require('child_process').spawn
+ , rimraf = require('rimraf')
+ , mkdirp = require('mkdirp')
+ , pkg = __dirname + '/ls-depth'
+ , cache = pkg + '/cache'
+ , tmp = pkg + '/tmp'
+ , node = process.execPath
+ , npm = path.resolve(__dirname, '../../cli.js')
+
+function run (command, t, test) {
@domenic Collaborator
domenic added a note

I wonder if we can put this in common-tap by now?

@robertkowalski Collaborator

Good idea!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
test/tap/ls-depth-cli.js
((33 lines not shown))
+ rimraf.sync(pkg + '/tmp')
+}
+
+test('setup', function (t) {
+ cleanup()
+ mkdirp.sync(pkg + '/cache')
+ mkdirp.sync(pkg + '/tmp')
+ run([npm, 'install'], t)
+})
+
+test('npm ls --depth=0', function (t) {
+ run([npm, 'ls', '--depth=0'], t, function (t, c) {
+ t.has(c, /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/
+ , "output not contains test-package@0.0.0")
@domenic Collaborator
domenic added a note

"does not contain" is slightly better English, FWIW :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@domenic
Collaborator

LGTM although at some point we should figure out a way to deduplicate code in the tap tests generally. Maybe factoring out the spawn helper would be good in that regard.

robertkowalski added some commits
@robertkowalski robertkowalski Add failing regression test for npm ls --depth=0
This test should turn green if read-installed is fixed and should
avoid regressions in the future.

This tests the cli, as it turned out that the programatical version
of npm has a bug where depth=0 is not working
742dc24
@robertkowalski robertkowalski Factor test-helper out of tests
Bonus:
  - Fix test in `startstop.js`
871ef65
@robertkowalski
Collaborator

Yep, that's a good point. Introduced the helper and found a bug in the startstop.js test -- the helper returned the same results:

return {actual: output, expected: output}

// later

t.equal(c.actual, c.expected)
@isaacs isaacs commented on the diff
test/common-tap.js
((3 lines not shown))
var port = exports.port = 1337
exports.registry = "http://localhost:" + port
+
+exports.run = run
+function run (cmd, t, opts, cb) {
@isaacs Owner
isaacs added a note

Why not common.npm(['help', 'args', 'etc'], opts, cb)? Most of the time we do this, it's all npm cli stuff anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@isaacs
Owner

I like adding a common thing to test npm cli stuff. That's long overdue.

The other thing is also good, but I dont' want to just land a failing test without the code to fix it.

@domenic
Collaborator

This test no longer fails, merging.

@domenic domenic closed this
@othiym23 othiym23 added the bug label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 22, 2014
  1. @robertkowalski

    Add failing regression test for npm ls --depth=0

    robertkowalski authored
    This test should turn green if read-installed is fixed and should
    avoid regressions in the future.
    
    This tests the cli, as it turned out that the programatical version
    of npm has a bug where depth=0 is not working
  2. @robertkowalski

    Factor test-helper out of tests

    robertkowalski authored
    Bonus:
      - Fix test in `startstop.js`
This page is out of date. Refresh to see the latest.
View
32 test/common-tap.js
@@ -1,2 +1,34 @@
+var spawn = require('child_process').spawn
+
var port = exports.port = 1337
exports.registry = "http://localhost:" + port
+
+exports.run = run
+function run (cmd, t, opts, cb) {
@isaacs Owner
isaacs added a note

Why not common.npm(['help', 'args', 'etc'], opts, cb)? Most of the time we do this, it's all npm cli stuff anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ 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 c = ""
+ , e = ""
+ , node = process.execPath
+ , child = spawn(node, cmd, opts)
+
+ child.stderr.on("data", function (chunk) {
+ e += chunk
+ })
+
+ child.stdout.on("data", function (chunk) {
+ c += chunk
+ })
+
+ child.stdout.on("end", function () {
+ if (cb)
+ cb(t, c, e, { cmd: cmd, opts: opts })
+ else
+ t.end()
+ })
+}
View
71 test/tap/ls-depth-cli.js
@@ -0,0 +1,71 @@
+var common = require('../common-tap')
+ , test = require('tap').test
+ , path = require('path')
+ , rimraf = require('rimraf')
+ , mkdirp = require('mkdirp')
+ , pkg = __dirname + '/ls-depth'
+ , cache = pkg + '/cache'
+ , tmp = pkg + '/tmp'
+ , node = process.execPath
+ , npm = path.resolve(__dirname, '../../cli.js')
+ , mr = require('npm-registry-mock')
+ , opts = {cwd: pkg}
+
+
+function cleanup () {
+ rimraf.sync(pkg + '/cache')
+ rimraf.sync(pkg + '/tmp')
+ rimraf.sync(pkg + '/node_modules')
+}
+
+test('setup', function (t) {
+ cleanup()
+ 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) {
+ s.close()
+ t.end()
+ })
+ })
+})
+
+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/
+ , "output contains test-package-with-one-dep@0.0.0")
+ t.doesNotHave(c, /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/
+ , "output contains test-package-with-one-dep@0.0.0")
+ t.has(c, /test-package@0\.0\.0/
+ , "output contains test-package@0.0.0")
+ t.end()
+ })
+})
+
+test('npm ls (no depth defined)', function (t) {
+ common.run([npm, 'ls'], t, opts, function (t, c) {
+ t.has(c, /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/
+ , "output contains test-package@0.0.0")
+ t.end()
+ })
+})
+
+test('cleanup', function (t) {
+ cleanup()
+ t.end()
+})
View
8 test/tap/ls-depth/package.json
@@ -0,0 +1,8 @@
+{
+ "author": "Rocko Artischocko",
+ "name": "ls-depth",
+ "version": "0.0.0",
+ "dependencies": {
+ "test-package-with-one-dep": "0.0.0"
+ }
+}
View
57 test/tap/startstop.js
@@ -9,41 +9,16 @@ var common = require('../common-tap')
, tmp = pkg + '/tmp'
, node = process.execPath
, npm = path.resolve(__dirname, '../../cli.js')
+ , opts = { cwd: pkg }
-function run (command, t, parse) {
- var c = ''
- , e = ''
- , node = process.execPath
- , child = spawn(node, [npm, command], {
- cwd: pkg
- })
-
- child.stderr.on('data', function (chunk) {
- e += chunk
- })
-
- child.stdout.on('data', function (chunk) {
- c += chunk
- })
-
- child.stdout.on('end', function () {
- if (e) {
- throw new Error('npm ' + command + ' stderr: ' + e.toString())
- }
- if (parse) {
- // custom parsing function
- c = parse(c)
- t.equal(c.actual, c.expected)
- t.end()
- return
- }
-
- c = c.trim().split('\n')
- c = c[c.length - 1]
- t.equal(c, command)
- t.end()
- })
+function testOutput (t, c, e, o) {
+ if (e)
+ throw new Error('npm ' + command + ' stderr: ' + e.toString())
+ c = c.trim().split('\n')
+ c = c[c.length - 1]
+ t.equal(c, o.cmd[1])
+ t.end()
}
function cleanup () {
@@ -56,23 +31,27 @@ test('setup', function (t) {
mkdirp.sync(pkg + '/cache')
mkdirp.sync(pkg + '/tmp')
t.end()
-
})
test('npm start', function (t) {
- run('start', t)
+ common.run([npm, 'start'], t, opts, testOutput)
})
test('npm stop', function (t) {
- run('stop', t)
+ common.run([npm, 'stop'], t, opts, testOutput)
})
test('npm restart', function (t) {
- run ('restart', t, function (output) {
- output = output.split('\n').filter(function (val) {
+ common.run([npm, 'restart'], t, opts, function (t, c, e) {
+ if (e)
+ throw new Error('npm ' + command + ' stderr: ' + e.toString())
+
+ var output = c.split('\n').filter(function (val) {
return val.match(/^s/)
})
- return {actual: output, expected: output}
+
+ t.same(output.sort(), ['start', 'stop'].sort())
+ t.end()
})
})
Something went wrong with that request. Please try again.