Skip to content

Commit cd9e5d2

Browse files
committed
fix: consolidate path delimiter logic
1 parent 20f5281 commit cd9e5d2

File tree

6 files changed

+41
-89
lines changed

6 files changed

+41
-89
lines changed

lib/commands/bin.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
const envPath = require('../utils/path.js')
21
const BaseCommand = require('../base-command.js')
2+
// TODO this may not be needed at all. Ignoring because our tests normalize
3+
// process.env.path already
4+
/* istanbul ignore next */
5+
const path = process.env.path || process.env.Path || process.env.PATH
6+
const { delimiter } = require('path')
37

48
class Bin extends BaseCommand {
59
static description = 'Display npm bin folder'
@@ -10,7 +14,7 @@ class Bin extends BaseCommand {
1014
async exec (args) {
1115
const b = this.npm.bin
1216
this.npm.output(b)
13-
if (this.npm.config.get('global') && !envPath.includes(b)) {
17+
if (this.npm.config.get('global') && !path.split(delimiter).includes(b)) {
1418
// XXX: does this need to be console?
1519
console.error('(not in PATH env variable)')
1620
}

lib/utils/path.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/fixtures/mock-npm.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ const LoadMockNpm = async (t, {
115115
Npm,
116116
npm,
117117
prefix,
118+
globalPrefix,
118119
testdir: dir,
119120
cache,
120121
debugFile: async () => {

test/lib/commands/bin.js

Lines changed: 31 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,43 @@
11
const t = require('tap')
2-
const { fake: mockNpm } = require('../../fixtures/mock-npm')
3-
4-
t.test('bin', async t => {
5-
t.plan(2)
6-
const dir = '/bin/dir'
7-
8-
const Bin = require('../../../lib/commands/bin.js')
9-
10-
const npm = mockNpm({
11-
bin: dir,
12-
config: { global: false },
13-
output: (output) => {
14-
t.equal(output, dir, 'prints the correct directory')
15-
},
2+
const { load: loadMockNpm } = require('../../fixtures/mock-npm')
3+
const mockGlobals = require('../../fixtures/mock-globals')
4+
5+
t.test('bin not global', async t => {
6+
let error
7+
const { npm, joinedOutput } = await loadMockNpm(t)
8+
mockGlobals(t, {
9+
'console.error': (err) => error = err,
1610
})
17-
const bin = new Bin(npm)
18-
t.match(bin.usage, 'bin', 'usage has command name in it')
19-
20-
await bin.exec([])
11+
await npm.exec('bin', [])
12+
t.match(joinedOutput(), npm.localBin)
13+
t.notOk(error, 'should not error')
2114
})
2215

23-
t.test('bin -g', async t => {
24-
t.plan(1)
25-
const consoleError = console.error
26-
t.teardown(() => {
27-
console.error = consoleError
28-
})
29-
30-
console.error = (output) => {
31-
t.fail('should not have printed to console.error')
32-
}
33-
const dir = '/bin/dir'
34-
35-
const Bin = t.mock('../../../lib/commands/bin.js', {
36-
'../../../lib/utils/path.js': [dir],
37-
})
38-
39-
const npm = mockNpm({
40-
bin: dir,
16+
t.test('bin global in env.path', async t => {
17+
let error
18+
const { npm, joinedOutput } = await loadMockNpm(t, {
4119
config: { global: true },
42-
output: (output) => {
43-
t.equal(output, dir, 'prints the correct directory')
44-
},
4520
})
46-
const bin = new Bin(npm)
47-
48-
await bin.exec([])
49-
})
5021

51-
t.test('bin -g (not in path)', async t => {
52-
t.plan(2)
53-
const consoleError = console.error
54-
t.teardown(() => {
55-
console.error = consoleError
22+
mockGlobals(t, {
23+
'console.error': (err) => error = err,
24+
'process.env': { path: npm.globalBin },
5625
})
26+
await npm.exec('bin', [])
27+
t.match(joinedOutput(), npm.globalBin)
28+
t.notOk(error, 'should not error')
29+
})
5730

58-
console.error = (output) => {
59-
t.equal(output, '(not in PATH env variable)', 'prints env warning')
60-
}
61-
const dir = '/bin/dir'
62-
63-
const Bin = t.mock('../../../lib/commands/bin.js', {
64-
'../../../lib/utils/path.js': ['/not/my/dir'],
65-
})
66-
const npm = mockNpm({
67-
bin: dir,
68-
config: { global: true },
69-
output: (output) => {
70-
t.equal(output, dir, 'prints the correct directory')
31+
t.test('bin not in path', async t => {
32+
let error
33+
const { npm } = await loadMockNpm(t, {
34+
config: {
35+
global: true,
36+
},
37+
globals: {
38+
'console.error': (err) => error = err,
7139
},
7240
})
73-
const bin = new Bin(npm)
74-
75-
await bin.exec([])
41+
await npm.exec('bin', [])
42+
t.match(error, '(not in PATH env variable)')
7643
})

test/lib/commands/exec.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ const read = (options, cb) => {
7272
process.nextTick(() => cb(READ_ERROR, READ_RESULT))
7373
}
7474

75-
const PATH = require('../../../lib/utils/path.js')
76-
7775
let CI_NAME = 'travis-ci'
7876

7977
const log = {
@@ -154,7 +152,7 @@ t.test('npx foo, bin already exists locally', async t => {
154152
stdioString: true,
155153
event: 'npx',
156154
env: {
157-
PATH: [npm.localBin, ...PATH].join(delimiter),
155+
PATH: [npm.localBin, process.env.PATH].join(delimiter),
158156
},
159157
stdio: 'inherit',
160158
},
@@ -183,7 +181,7 @@ t.test('npx foo, bin already exists globally', async t => {
183181
stdioString: true,
184182
event: 'npx',
185183
env: {
186-
PATH: [npm.globalBin, ...PATH].join(delimiter),
184+
PATH: [npm.globalBin, process.env.PATH].join(delimiter),
187185
},
188186
stdio: 'inherit',
189187
},
@@ -1175,7 +1173,7 @@ t.test('workspaces', t => {
11751173
stdioString: true,
11761174
event: 'npx',
11771175
env: {
1178-
PATH: [npm.localBin, ...PATH].join(delimiter),
1176+
PATH: [npm.localBin, process.env.PATH].join(delimiter),
11791177
},
11801178
stdio: 'inherit',
11811179
},

test/lib/utils/path.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)