|
1 | 1 | const log = require('proc-log') |
2 | 2 | const { resolve } = require('path') |
3 | 3 | const t = require('tap') |
| 4 | +const fs = require('fs/promises') |
4 | 5 | const { setup, createPkg, merge } = require('./fixtures/setup.js') |
5 | 6 |
|
6 | 7 | t.test('bin in local pkg', async t => { |
7 | 8 | const { pkg, fixtures } = createPkg({ |
8 | | - versions: ['1.0.0'], |
| 9 | + version: '1.0.0', |
9 | 10 | name: '@npmcli/local-pkg-bin-test', |
10 | 11 | bin: { |
11 | 12 | b: 'does-not-exist.js', |
12 | 13 | a: 'local-bin-test.js', |
13 | 14 | 'a-nested': 'bin-dir/nested-bin-test.js', |
14 | | - 'conflicting-bin': 'conflicting-bin.js', |
| 15 | + 'conflicting-bin': 'local-bin-test.js', |
15 | 16 | }, |
16 | 17 | files: { |
17 | 18 | 'local-bin-test.js': { key: 'local-bin', value: 'LOCAL PKG' }, |
18 | | - 'conflicting-bin.js': { key: 'conflicting-bin', value: 'LOCAL PKG' }, |
19 | 19 | 'bin-dir': { |
20 | 20 | 'nested-bin-test.js': { key: 'nested-bin', value: 'LOCAL PKG' }, |
21 | 21 | }, |
22 | 22 | }, |
23 | 23 | }) |
24 | 24 |
|
25 | | - const { exec, chmod, readOutput, rimraf, registry, path } = setup(t, { |
| 25 | + const existingPkg = createPkg({ |
| 26 | + name: 'pkg-with-conflicting-bin', |
| 27 | + localVersion: '1.0.0', |
| 28 | + bin: { |
| 29 | + 'conflicting-bin': 'index.js', |
| 30 | + }, |
| 31 | + files: { |
| 32 | + 'index.js': { key: 'existing-bin', value: 'NODE_MODULES PKG' }, |
| 33 | + }, |
| 34 | + }) |
| 35 | + |
| 36 | + const { exec, chmod, readOutput, binLinks, registry, path } = setup(t, { |
26 | 37 | pkg, |
27 | | - testdir: { |
28 | | - ...fixtures.packages[`@npmcli-local-pkg-bin-test-1.0.0`], |
29 | | - node_modules: { |
30 | | - '.bin': { |
31 | | - 'conflicting-bin': { key: 'existing-bin', value: 'NODEMODULES PKG' }, |
32 | | - }, |
33 | | - '@npmcli': { |
34 | | - 'some-other-pkg-with-same-scope': {}, |
| 38 | + testdir: merge( |
| 39 | + existingPkg.fixtures, |
| 40 | + fixtures.packages[`@npmcli-local-pkg-bin-test-1.0.0`], |
| 41 | + { |
| 42 | + node_modules: { |
| 43 | + '@npmcli': { |
| 44 | + 'some-other-pkg-with-same-scope': {}, |
| 45 | + }, |
35 | 46 | }, |
36 | | - }, |
37 | | - }, |
| 47 | + } |
| 48 | + ), |
38 | 49 | }) |
39 | 50 |
|
40 | 51 | const localBin = resolve(path, 'node_modules', '.bin') |
41 | 52 |
|
42 | 53 | await chmod('local-bin-test.js') |
43 | 54 | await chmod('bin-dir/nested-bin-test.js') |
44 | | - await chmod('conflicting-bin.js') |
45 | | - await chmod('node_modules/.bin/conflicting-bin') |
| 55 | + await chmod('node_modules/pkg-with-conflicting-bin/index.js') |
46 | 56 |
|
47 | 57 | await exec({ localBin, args: ['a', 'argument-a'] }) |
48 | 58 |
|
49 | 59 | t.match(await readOutput('local-bin'), { |
50 | 60 | value: 'LOCAL PKG', |
51 | 61 | args: ['argument-a'], |
52 | 62 | }) |
| 63 | + t.strictSame(await fs.readdir(resolve(path, 'node_modules', '.bin')), []) |
53 | 64 |
|
54 | 65 | // remove the existing scope dir from node_modules so that the next run |
55 | 66 | // will have to create and cleanup that directory |
56 | | - await rimraf('node_modules/@npmcli') |
| 67 | + await fs.rm(resolve(path, 'node_modules/@npmcli'), { recursive: true, force: true }) |
57 | 68 |
|
58 | 69 | await exec({ localBin, args: ['a-nested', 'argument-a-nested'] }) |
| 70 | + t.strictSame(await fs.readdir(resolve(path, 'node_modules', '.bin')), []) |
59 | 71 |
|
60 | 72 | t.match(await readOutput('nested-bin'), { |
61 | 73 | value: 'LOCAL PKG', |
62 | 74 | args: ['argument-a-nested'], |
63 | 75 | }) |
64 | 76 |
|
| 77 | + // now link a bin which will conflict with the one we try to run next |
| 78 | + await binLinks(existingPkg.pkg) |
| 79 | + t.match(await fs.readdir(resolve(path, 'node_modules', '.bin')), ['conflicting-bin']) |
65 | 80 | await exec({ localBin, args: ['conflicting-bin'] }) |
66 | 81 |
|
67 | 82 | t.match(await readOutput('existing-bin'), { |
68 | | - value: 'NODEMODULES PKG', |
| 83 | + value: 'NODE_MODULES PKG', |
69 | 84 | }) |
70 | 85 |
|
71 | 86 | // this will hit the registry because the file does not exist |
|
0 commit comments