Skip to content

Commit 0a957f5

Browse files
wraithgarlukekarrys
authored andcommitted
fix: consolidate path delimiter logic
1 parent 57d8f75 commit 0a957f5

File tree

5 files changed

+26
-70
lines changed

5 files changed

+26
-70
lines changed

lib/commands/bin.js

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

59
class Bin extends BaseCommand {
610
static description = 'Display npm bin folder'
@@ -11,7 +15,7 @@ class Bin extends BaseCommand {
1115
async exec (args) {
1216
const b = this.npm.bin
1317
this.npm.output(b)
14-
if (this.npm.config.get('global') && !envPath.includes(b)) {
18+
if (this.npm.config.get('global') && !path.split(delimiter).includes(b)) {
1519
log.error('bin', '(not in PATH env variable)')
1620
}
1721
}

lib/utils/path.js

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

test/lib/commands/bin.js

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,32 @@
11
const t = require('tap')
2-
const { relative, join } = require('path')
32
const { load: loadMockNpm } = require('../../fixtures/mock-npm')
43
const mockGlobals = require('../../fixtures/mock-globals')
54

6-
const mockBin = async (t, { args = [], config = {} } = {}) => {
7-
const { npm, outputs, ...rest } = await loadMockNpm(t, {
8-
config,
9-
})
10-
const cmd = await npm.cmd('bin')
11-
await npm.exec('bin', args)
12-
13-
return {
14-
npm,
15-
cmd,
16-
bin: outputs[0][0],
17-
...rest,
18-
}
19-
}
20-
21-
t.test('bin', async t => {
22-
const { cmd, bin, prefix, outputErrors } = await mockBin(t, {
23-
config: { global: false },
24-
})
25-
26-
t.match(cmd.usage, 'bin', 'usage has command name in it')
27-
t.equal(relative(prefix, bin), join('node_modules/.bin'), 'prints the correct directory')
28-
t.strictSame(outputErrors, [])
5+
t.test('bin not global', async t => {
6+
const { npm, joinedOutput, logs } = await loadMockNpm(t)
7+
await npm.exec('bin', [])
8+
t.match(joinedOutput(), npm.localBin)
9+
t.match(logs.error, [])
2910
})
3011

31-
t.test('bin -g', async t => {
32-
mockGlobals(t, { 'process.platform': 'posix' })
33-
const { globalPrefix, bin, outputErrors } = await mockBin(t, {
12+
t.test('bin global in env.path', async t => {
13+
const { npm, joinedOutput, logs } = await loadMockNpm(t, {
3414
config: { global: true },
3515
})
3616

37-
t.equal(relative(globalPrefix, bin), 'bin', 'prints the correct directory')
38-
t.strictSame(outputErrors, [])
39-
})
40-
41-
t.test('bin -g win32', async t => {
42-
mockGlobals(t, { 'process.platform': 'win32' })
43-
const { globalPrefix, bin, outputErrors } = await mockBin(t, {
44-
config: { global: true },
17+
mockGlobals(t, {
18+
'process.env': { path: npm.globalBin },
4519
})
46-
47-
t.equal(relative(globalPrefix, bin), '', 'prints the correct directory')
48-
t.strictSame(outputErrors, [])
20+
await npm.exec('bin', [])
21+
t.match(joinedOutput(), npm.globalBin)
22+
t.match(logs.error, [])
4923
})
5024

51-
t.test('bin -g (not in path)', async t => {
52-
const { logs } = await mockBin(t, {
25+
t.test('bin not in path', async t => {
26+
const { npm, joinedOutput, logs } = await loadMockNpm(t, {
5327
config: { global: true },
54-
globals: {
55-
'process.env.PATH': 'emptypath',
56-
},
5728
})
58-
59-
t.strictSame(logs.error[0], ['bin', '(not in PATH env variable)'])
29+
await npm.exec('bin', [])
30+
t.match(joinedOutput(), npm.globalBin)
31+
t.match(logs.error, [['bin', '(not in PATH env variable)']])
6032
})

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)