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

Commit 37c6a51

Browse files
isaacsiarna
authored andcommitted
file-completion: Ensure that 'npm cache ls' outputs real filenames
Credit: @isaacs Reviewed-By: @iarna PR-URL: #12150
1 parent 60b9a05 commit 37c6a51

2 files changed

Lines changed: 52 additions & 3 deletions

File tree

lib/utils/completion/file-completion.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module.exports = fileCompletion
22

33
var mkdir = require('mkdirp')
4-
var path = require('path')
54
var glob = require('glob')
65

76
function fileCompletion (root, req, depth, cb) {
@@ -18,8 +17,7 @@ function fileCompletion (root, req, depth, cb) {
1817
glob(pattern, opts, function (er, files) {
1918
if (er) return cb(er)
2019
return cb(null, (files || []).map(function (f) {
21-
var tail = f.substr(root.length + 1).replace(/^\//, '')
22-
return path.join(req, tail)
20+
return f.substr(root.length + 1).replace(/^\/|\/$/g, '')
2321
}))
2422
})
2523
})

test/tap/cache-ls-filenames.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
var t = require('tap')
2+
var path = require('path')
3+
var fs = require('fs')
4+
var mkdirp = require('mkdirp')
5+
var rimraf = require('rimraf')
6+
var spawn = require('child_process').spawn
7+
var npm = require.resolve('../../bin/npm-cli.js')
8+
var dir = path.resolve(__dirname, 'cache-ls-filenames')
9+
var node = process.execPath
10+
11+
t.test('setup', function (t) {
12+
rimraf.sync(dir)
13+
mkdirp.sync(dir + '/a/b/c/d')
14+
for (var i = 0; i < 5; i++) {
15+
fs.writeFileSync(dir + '/file-' + i, 'x\n')
16+
fs.writeFileSync(dir + '/a/b/file-' + i, 'x\n')
17+
}
18+
t.end()
19+
})
20+
21+
function test (t, args) {
22+
var child = spawn(node, [npm, 'cache', 'ls', '--cache=' + dir].concat(args))
23+
var out = ''
24+
child.stdout.on('data', function (c) {
25+
out += c
26+
})
27+
child.on('close', function (code, signal) {
28+
t.equal(code, 0)
29+
t.equal(signal, null)
30+
out.trim().split(/[\n\r]+/).map(function (filename) {
31+
return filename.replace(/^~/, process.env.HOME)
32+
}).forEach(function (file) {
33+
// verify that all exist
34+
t.ok(fs.existsSync(file), 'exists: ' + file)
35+
})
36+
t.end()
37+
})
38+
}
39+
40+
t.test('without path arg', function (t) {
41+
test(t, [])
42+
})
43+
44+
t.test('with path arg', function (t) {
45+
test(t, ['a'])
46+
})
47+
48+
t.test('cleanup', function (t) {
49+
rimraf.sync(dir)
50+
t.end()
51+
})

0 commit comments

Comments
 (0)