Skip to content

Commit

Permalink
fix: added check for dry-run (#7274)
Browse files Browse the repository at this point in the history
* fix: added check for dry-run
  added a check for dry-run for `npm ci` so that node_modules won't be
  deleted

  closes #7239

* chore: added test for ci --dry-run check
  • Loading branch information
cod1r authored Mar 13, 2024
1 parent 7f1ab88 commit bdb3c28
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/commands/ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,18 @@ class CI extends ArboristWorkspaceCmd {
)
}

// Only remove node_modules after we've successfully loaded the virtual
// tree and validated the lockfile
await this.npm.time('npm-ci:rm', async () => {
const path = `${where}/node_modules`
// get the list of entries so we can skip the glob for performance
const entries = await fs.readdir(path, null).catch(er => [])
return Promise.all(entries.map(f => fs.rm(`${path}/${f}`, { force: true, recursive: true })))
})
const dryRun = this.npm.config.get('dry-run')
if (!dryRun) {
// Only remove node_modules after we've successfully loaded the virtual
// tree and validated the lockfile
await this.npm.time('npm-ci:rm', async () => {
const path = `${where}/node_modules`
// get the list of entries so we can skip the glob for performance
const entries = await fs.readdir(path, null).catch(er => [])
return Promise.all(entries.map(f => fs.rm(`${path}/${f}`,
{ force: true, recursive: true })))
})
}

await arb.reify(opts)

Expand Down
20 changes: 20 additions & 0 deletions test/lib/commands/ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ const abbrev = {
test: 'test file',
}

t.test('reifies, but doesn\'t remove node_modules because --dry-run', async t => {
const { npm, joinedOutput } = await loadMockNpm(t, {
config: {
'dry-run': true,
},
prefixDir: {
abbrev: abbrev,
'package.json': JSON.stringify(packageJson),
'package-lock.json': JSON.stringify(packageLock),
node_modules: { test: 'test file that will not be removed' },
},
})
await npm.exec('ci', [])
t.match(joinedOutput(), 'added 1 package, and removed 1 package in')
const nmTest = path.join(npm.prefix, 'node_modules', 'test')
t.equal(fs.existsSync(nmTest), true, 'existing node_modules is not removed')
const nmAbbrev = path.join(npm.prefix, 'node_modules', 'abbrev')
t.equal(fs.existsSync(nmAbbrev), false, 'does not install abbrev')
})

t.test('reifies, audits, removes node_modules', async t => {
const { npm, joinedOutput, registry } = await loadMockNpm(t, {
prefixDir: {
Expand Down

0 comments on commit bdb3c28

Please sign in to comment.