|
1 | 1 | 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, |
16 | 10 | }) |
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') |
21 | 14 | }) |
22 | 15 |
|
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, { |
41 | 19 | config: { global: true }, |
42 | | - output: (output) => { |
43 | | - t.equal(output, dir, 'prints the correct directory') |
44 | | - }, |
45 | 20 | }) |
46 | | - const bin = new Bin(npm) |
47 | | - |
48 | | - await bin.exec([]) |
49 | | -}) |
50 | 21 |
|
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 }, |
56 | 25 | }) |
| 26 | + await npm.exec('bin', []) |
| 27 | + t.match(joinedOutput(), npm.globalBin) |
| 28 | + t.notOk(error, 'should not error') |
| 29 | +}) |
57 | 30 |
|
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, |
71 | 39 | }, |
72 | 40 | }) |
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)') |
76 | 43 | }) |
0 commit comments