Skip to content

Commit

Permalink
wip windows
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Nov 13, 2022
1 parent d5c777f commit b3d744b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
13 changes: 7 additions & 6 deletions workspaces/libnpmexec/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,20 @@ const exec = async (opts) => {
path: tmpNmPath,
pkg: { bin: { [args[0]]: manifestBinPath } },
}
const [binPath] = await binLinks.getPaths(binOpts)
const [createdBin] = await binLinks.linkBins(binOpts).catch(() => [false])
// binLinks returns null if it was created and false if not so if we have
// a valid path here then we can keep going. if we did not create a bin
// here then keep trying the next steps below, since there is probably
// a bin that is already linked there which we will run.
const linkedBin = createdBin === null && binPath
const linkedBins = await binLinks.linkBins(binOpts)
.then((r) => r[0] === null ? binLinks.getPaths(binOpts) : [])
// hard to force an error here on windows
.catch(/* istanbul ignore next */ () => [])

const cleanupLinks = async () => {
// always unlink symlinks when we are done
await unlink(tmpNmPath)
if (linkedBin) {
await unlink(linkedBin)
if (linkedBins.length) {
await Promise.all(linkedBins.map(b => unlink(b)))
}
// Only if mkdir indicated that it created a dir should we cleanup
// that directory
Expand All @@ -153,7 +154,7 @@ const exec = async (opts) => {
}
}

if (linkedBin) {
if (linkedBins.length) {
binPaths.push(path)
return await run().finally(cleanupLinks)
} else {
Expand Down
51 changes: 33 additions & 18 deletions workspaces/libnpmexec/test/local.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,86 @@
const log = require('proc-log')
const { resolve } = require('path')
const t = require('tap')
const fs = require('fs/promises')
const { setup, createPkg, merge } = require('./fixtures/setup.js')

t.test('bin in local pkg', async t => {
const { pkg, fixtures } = createPkg({
versions: ['1.0.0'],
version: '1.0.0',
name: '@npmcli/local-pkg-bin-test',
bin: {
b: 'does-not-exist.js',
a: 'local-bin-test.js',
'a-nested': 'bin-dir/nested-bin-test.js',
'conflicting-bin': 'conflicting-bin.js',
'conflicting-bin': 'local-bin-test.js',
},
files: {
'local-bin-test.js': { key: 'local-bin', value: 'LOCAL PKG' },
'conflicting-bin.js': { key: 'conflicting-bin', value: 'LOCAL PKG' },
'bin-dir': {
'nested-bin-test.js': { key: 'nested-bin', value: 'LOCAL PKG' },
},
},
})

const { exec, chmod, readOutput, rimraf, registry, path } = setup(t, {
const existingPkg = createPkg({
name: 'pkg-with-conflicting-bin',
localVersion: '1.0.0',
bin: {
'conflicting-bin': 'index.js',
},
files: {
'index.js': { key: 'existing-bin', value: 'NODE_MODULES PKG' },
},
})

const { exec, chmod, readOutput, binLinks, registry, path } = setup(t, {
pkg,
testdir: {
...fixtures.packages[`@npmcli-local-pkg-bin-test-1.0.0`],
node_modules: {
'.bin': {
'conflicting-bin': { key: 'existing-bin', value: 'NODEMODULES PKG' },
},
'@npmcli': {
'some-other-pkg-with-same-scope': {},
testdir: merge(
existingPkg.fixtures,
fixtures.packages[`@npmcli-local-pkg-bin-test-1.0.0`],
{
node_modules: {
'@npmcli': {
'some-other-pkg-with-same-scope': {},
},
},
},
},
}
),
})

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

await chmod('local-bin-test.js')
await chmod('bin-dir/nested-bin-test.js')
await chmod('conflicting-bin.js')
await chmod('node_modules/.bin/conflicting-bin')
await chmod('node_modules/pkg-with-conflicting-bin/index.js')

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

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

// remove the existing scope dir from node_modules so that the next run
// will have to create and cleanup that directory
await rimraf('node_modules/@npmcli')
await fs.rm(resolve(path, 'node_modules/@npmcli'), { recursive: true, force: true })

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

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

// now link a bin which will conflict with the one we try to run next
await binLinks(existingPkg.pkg)
t.match(await fs.readdir(resolve(path, 'node_modules', '.bin')), ['conflicting-bin'])
await exec({ localBin, args: ['conflicting-bin'] })

t.match(await readOutput('existing-bin'), {
value: 'NODEMODULES PKG',
value: 'NODE_MODULES PKG',
})

// this will hit the registry because the file does not exist
Expand Down

0 comments on commit b3d744b

Please sign in to comment.