Skip to content

Commit

Permalink
refactor: use commander implies() syntax for flag
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Dec 2, 2023
1 parent f3378be commit 82eded4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .changeset/flat-ducks-double.md
@@ -0,0 +1,2 @@
---
---
9 changes: 5 additions & 4 deletions bin/lint-staged.js
Expand Up @@ -68,7 +68,9 @@ cli
new Option(
'--no-stash',
'disable the backup stash, and do not revert in case of errors. Implies "--no-hide-partially-staged".'
).default(false)
)
.default(false)
.implies({ hidePartiallyStaged: false })
)

/**
Expand All @@ -79,7 +81,7 @@ cli
cli
.addOption(
new Option('--hide-partially-staged', 'hide unstaged changes from partially staged files')
.default(null)
.default(true)
.hideHelp()
)
.addOption(
Expand Down Expand Up @@ -122,8 +124,7 @@ const options = {
relative: !!cliOptions.relative,
shell: cliOptions.shell /* Either a boolean or a string pointing to the shell */,
stash: !!cliOptions.stash, // commander inverts `no-<x>` flags to `!x`
hidePartiallyStaged:
cliOptions.hidePartiallyStaged == null ? !!cliOptions.stash : !!cliOptions.hidePartiallyStaged, // commander inverts `no-<x>` flags to `!x`
hidePartiallyStaged: !!cliOptions.hidePartiallyStaged, // commander inverts `no-<x>` flags to `!x`
verbose: !!cliOptions.verbose,
}

Expand Down
36 changes: 31 additions & 5 deletions test/e2e/no-stash.test.js
Expand Up @@ -35,14 +35,40 @@ describe('lint-staged', () => {
res = await lintStaged('--diff=master...my-branch')
expect(res.stderr).toMatch('Skipping backup because `--diff` was used.')

try {
await lintStaged('--diff=master...my-branch --stash')
} catch (err) {
expect(err.stderr).toMatch('lint-staged failed due to a git error.')
}
await expect(lintStaged('--diff=master...my-branch --stash')).rejects.toThrowError(
expect.objectContaining({
stderr: expect.stringContaining('lint-staged failed due to a git error.'),
})
)

res = await lintStaged('--diff=master...my-branch --no-stash')
expect(res.stderr).toMatch('Skipping backup because `--diff` was used.')
})
)

test(
'--no-stash implies --no-hide-partially-staged',
withGitIntegration(async ({ execGit, readFile, writeFile, cwd }) => {
const lintStaged = getLintStagedExecutor(cwd)

await writeFile('.lintstagedrc.json', JSON.stringify(configFixtures.prettierListDifferent))

// Stage ugly file
await writeFile('test.js', fileFixtures.uglyJS)
await execGit(['add', 'test.js'])

// modify file with unstaged changes
await writeFile('test.js', fileFixtures.uglyJSWithChanges)

// lint-staged fails because file is ugly
await expect(lintStaged('--no-stash')).rejects.toMatchObject({
stderr: expect.stringContaining(
'Skipping hiding unstaged changes from partially staged files because `--no-stash` was used'
),
})

// unstaged changes were not touched
expect(await readFile('test.js')).toEqual(fileFixtures.uglyJSWithChanges)
})
)
})

0 comments on commit 82eded4

Please sign in to comment.