Skip to content

Commit

Permalink
[npm#5280] Detect default save, improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ficocelliguy committed Mar 5, 2024
1 parent 1f3c284 commit f7e4d7d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 31 deletions.
5 changes: 3 additions & 2 deletions workspaces/config/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,9 @@ class Config {
this.#flatten(data, this.#flatOptions)
}

// Ensure 'save' is true if a saveType has been specified
if (this.#flatOptions.saveType) {
// Only set 'save' to true if a saveType has been specified
// and if 'save' is false due to non-default config
if (!this.isDefault('save') && this.#flatOptions.saveType) {
this.#flatOptions.save = true
}
this.#flatOptions.nodeBin = this.execPath
Expand Down
52 changes: 23 additions & 29 deletions workspaces/config/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -910,48 +910,42 @@ t.test('finding the global prefix', t => {

t.test('manages the save flag when flat is retrieved', t => {
const npmPath = __dirname
t.test('does not set save to true if a save flag is not passed', async t => {
const buildConfig = async (args = [], envSave = false) => {
const c = new Config({
argv: [process.execPath, __filename],
argv: [process.execPath, __filename, ...args],
shorthands,
definitions,
npmPath,
flatten,
env: {
save: envSave,
},
})
await c.load()
// Ensure test runner environment's npm settings do not change test outcomes
c.set('save', envSave, 'user')
c.set('save', envSave, 'global')
c.set('save', envSave, 'project')
return c
}
t.test('does not override save to true if a save flag is not passed', async t => {
const c = await buildConfig([], false)
t.equal(c.flat.save, false)
})
t.test('does not set save to true if flag is passed that does not efffect saveType', async t => {
const c = new Config({
argv: [process.execPath, __filename, '--save-exact'],
shorthands,
definitions,
npmPath,
flatten,
})
await c.load()
t.test('does not override save to true if a negative save flag is passed', async t => {
const c = await buildConfig(['--save-dev=false'], false)
t.equal(c.flat.save, false)
})
t.test('does not set save to true if a negative save flag is passed', async t => {
const c = new Config({
argv: [process.execPath, __filename, '--save-dev=false'],
shorthands,
definitions,
npmPath,
flatten,
})
await c.load()
t.test('overrides save to true if a save flag is passed', async t => {
const c = await buildConfig(['--save-prod'], false)
t.equal(c.flat.save, true)
})
t.test('does not overwrite save if --no-save is present', async t => {
const c = await buildConfig(['--no-save'], true)
t.equal(c.flat.save, false)
})
t.test('sets save to true if a save flag is passed', async t => {
const c = new Config({
argv: [process.execPath, __filename, '--save-prod'],
shorthands,
definitions,
npmPath,
flatten,
})
await c.load()
t.test('overwrites save if --no-save is present and also a save flag', async t => {
const c = await buildConfig(['--save-prod', '--no-save'], false)
t.equal(c.flat.save, true)
})
t.end()
Expand Down

0 comments on commit f7e4d7d

Please sign in to comment.