Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions test/common-tap.js
Original file line number Diff line number Diff line change
@@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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()
})
}
71 changes: 71 additions & 0 deletions test/tap/ls-depth-cli.js
Original file line number Diff line number Diff line change
@@ -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()
})
8 changes: 8 additions & 0 deletions test/tap/ls-depth/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"author": "Rocko Artischocko",
"name": "ls-depth",
"version": "0.0.0",
"dependencies": {
"test-package-with-one-dep": "0.0.0"
}
}
57 changes: 18 additions & 39 deletions test/tap/startstop.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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()
})
})

Expand Down