Skip to content

Commit b3d744b

Browse files
committed
wip windows
1 parent d5c777f commit b3d744b

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

workspaces/libnpmexec/lib/index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,19 +132,20 @@ const exec = async (opts) => {
132132
path: tmpNmPath,
133133
pkg: { bin: { [args[0]]: manifestBinPath } },
134134
}
135-
const [binPath] = await binLinks.getPaths(binOpts)
136-
const [createdBin] = await binLinks.linkBins(binOpts).catch(() => [false])
137135
// binLinks returns null if it was created and false if not so if we have
138136
// a valid path here then we can keep going. if we did not create a bin
139137
// here then keep trying the next steps below, since there is probably
140138
// a bin that is already linked there which we will run.
141-
const linkedBin = createdBin === null && binPath
139+
const linkedBins = await binLinks.linkBins(binOpts)
140+
.then((r) => r[0] === null ? binLinks.getPaths(binOpts) : [])
141+
// hard to force an error here on windows
142+
.catch(/* istanbul ignore next */ () => [])
142143

143144
const cleanupLinks = async () => {
144145
// always unlink symlinks when we are done
145146
await unlink(tmpNmPath)
146-
if (linkedBin) {
147-
await unlink(linkedBin)
147+
if (linkedBins.length) {
148+
await Promise.all(linkedBins.map(b => unlink(b)))
148149
}
149150
// Only if mkdir indicated that it created a dir should we cleanup
150151
// that directory
@@ -153,7 +154,7 @@ const exec = async (opts) => {
153154
}
154155
}
155156

156-
if (linkedBin) {
157+
if (linkedBins.length) {
157158
binPaths.push(path)
158159
return await run().finally(cleanupLinks)
159160
} else {

workspaces/libnpmexec/test/local.js

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,86 @@
11
const log = require('proc-log')
22
const { resolve } = require('path')
33
const t = require('tap')
4+
const fs = require('fs/promises')
45
const { setup, createPkg, merge } = require('./fixtures/setup.js')
56

67
t.test('bin in local pkg', async t => {
78
const { pkg, fixtures } = createPkg({
8-
versions: ['1.0.0'],
9+
version: '1.0.0',
910
name: '@npmcli/local-pkg-bin-test',
1011
bin: {
1112
b: 'does-not-exist.js',
1213
a: 'local-bin-test.js',
1314
'a-nested': 'bin-dir/nested-bin-test.js',
14-
'conflicting-bin': 'conflicting-bin.js',
15+
'conflicting-bin': 'local-bin-test.js',
1516
},
1617
files: {
1718
'local-bin-test.js': { key: 'local-bin', value: 'LOCAL PKG' },
18-
'conflicting-bin.js': { key: 'conflicting-bin', value: 'LOCAL PKG' },
1919
'bin-dir': {
2020
'nested-bin-test.js': { key: 'nested-bin', value: 'LOCAL PKG' },
2121
},
2222
},
2323
})
2424

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, {
2637
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+
},
3546
},
36-
},
37-
},
47+
}
48+
),
3849
})
3950

4051
const localBin = resolve(path, 'node_modules', '.bin')
4152

4253
await chmod('local-bin-test.js')
4354
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')
4656

4757
await exec({ localBin, args: ['a', 'argument-a'] })
4858

4959
t.match(await readOutput('local-bin'), {
5060
value: 'LOCAL PKG',
5161
args: ['argument-a'],
5262
})
63+
t.strictSame(await fs.readdir(resolve(path, 'node_modules', '.bin')), [])
5364

5465
// remove the existing scope dir from node_modules so that the next run
5566
// 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 })
5768

5869
await exec({ localBin, args: ['a-nested', 'argument-a-nested'] })
70+
t.strictSame(await fs.readdir(resolve(path, 'node_modules', '.bin')), [])
5971

6072
t.match(await readOutput('nested-bin'), {
6173
value: 'LOCAL PKG',
6274
args: ['argument-a-nested'],
6375
})
6476

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'])
6580
await exec({ localBin, args: ['conflicting-bin'] })
6681

6782
t.match(await readOutput('existing-bin'), {
68-
value: 'NODEMODULES PKG',
83+
value: 'NODE_MODULES PKG',
6984
})
7085

7186
// this will hit the registry because the file does not exist

0 commit comments

Comments
 (0)