Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions lib/commands/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ class Exec extends BaseCommand {
return libexec({
...flatOptions,
allowScripts: allowScriptsPolicy,
// we explicitly set packageLockOnly to false because if it's true when we try to install a missing package, we won't actually install it
packageLockOnly: false,
// what the user asked to run args[0] is run by default
args: [...args], // copy args so they don't get mutated
// specify a custom command to be run instead of args[0]
Expand Down
36 changes: 36 additions & 0 deletions test/lib/commands/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,42 @@ t.test('registry package', async t => {
t.ok(exists.isFile(), 'bin ran, creating file')
})

t.test('registry package with dry-run set', async t => {
const registry = new MockRegistry({
tap: t,
registry: 'https://registry.npmjs.org/',
})

const manifest = registry.manifest({ name: '@npmcli/npx-dry-run-test' })
manifest.versions['1.0.0'].bin = { 'npx-dry-run-test': 'index.js' }

const { npm } = await loadMockNpm(t, {
config: {
audit: false,
yes: true,
'dry-run': true,
},
prefixDir: {
'npm-exec-test': {
'package.json': JSON.stringify(manifest),
'index.js': `#!/usr/bin/env node
require('fs').writeFileSync('npm-exec-test-success', '')`,
},
},
})

await registry.package({
times: 2,
manifest,
tarballs: {
'1.0.0': path.join(npm.prefix, 'npm-exec-test'),
} })

await npm.exec('exec', ['@npmcli/npx-dry-run-test'])
const exists = await fs.stat(path.join(npm.prefix, 'npm-exec-test-success'))
t.ok(exists.isFile(), 'package was installed and bin ran, creating file')
})

t.test('--prefix', async t => {
const registry = new MockRegistry({
tap: t,
Expand Down
4 changes: 4 additions & 0 deletions workspaces/libnpmexec/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ const exec = async (opts) => {
await mkdir(installDir, { recursive: true })
const npxArb = new Arborist({
...flatOptions,
// a missing package must actually be installed to the npx cache, so it can be run,
// even when these are set: eg, a lifecycle script inherits npm_config_dry_run from `npm publish --dry-run`
dryRun: false,
packageLockOnly: false,
path: installDir,
})
const lockPath = join(installDir, 'concurrency.lock')
Expand Down
26 changes: 26 additions & 0 deletions workspaces/libnpmexec/test/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,29 @@ t.test('override save to true when installing to npx cache', async t => {
value: 'packages-2.0.0',
})
})

t.test('install to npx cache when dryRun or packageLockOnly is set', async t => {
for (const flag of ['dryRun', 'packageLockOnly']) {
t.test(flag, async t => {
const { fixtures, package } = createPkg({ versions: ['2.0.0'] })

const { exec, path, registry, readOutput } = setup(t, {
testdir: merge(fixtures, {
global: {},
}),
})

await package({ registry, path })

await exec({
args: ['@npmcli/create-index'],
globalPath: resolve(path, 'global'),
[flag]: true,
})

t.match(await readOutput('@npmcli-create-index'), {
value: 'packages-2.0.0',
}, 'package was installed and its bin ran')
})
}
})
Loading