Skip to content

Commit

Permalink
fix: fail with a message when backup stash is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Dec 17, 2019
1 parent 9913bb2 commit 1b64239
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
15 changes: 10 additions & 5 deletions lib/gitWorkflow.js
Expand Up @@ -61,6 +61,7 @@ class GitWorkflow {
async getBackupStash() {
const stashes = await this.execGit(['stash', 'list'])
const index = stashes.split('\n').findIndex(line => line.includes(STASH))
if (index === -1) throw new Error('lint-staged automatic backup is missing!')
return `stash@{${index}}`
}

Expand Down Expand Up @@ -208,11 +209,15 @@ class GitWorkflow {
/**
* Drop the created stashes after everything has run
*/
async dropBackup() {
debug('Dropping backup stash...')
const backupStash = await this.getBackupStash()
await this.execGit(['stash', 'drop', '--quiet', backupStash])
debug('Done dropping backup stash!')
async dropBackup(ctx) {
try {
debug('Dropping backup stash...')
const backupStash = await this.getBackupStash()
await this.execGit(['stash', 'drop', '--quiet', backupStash])
debug('Done dropping backup stash!')
} catch (error) {
handleGitLockError(error, ctx)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/runAll.js
Expand Up @@ -162,7 +162,7 @@ module.exports = async function runAll(
{
title: 'Cleaning up...',
skip: ctx => ctx.hasGitLockError && 'Skipped because of previous git error',
task: () => git.dropBackup()
task: ctx => git.dropBackup(ctx)
}
],
listrOptions
Expand Down
32 changes: 32 additions & 0 deletions test/runAll.unmocked.spec.js
Expand Up @@ -646,4 +646,36 @@ describe('runAll', () => {
expect(await readFile('test.js')).toEqual(testJsFilePretty)
expect(await readFile('test2.js')).toEqual(testJsFilePretty)
})

it('should fail when backup stash is missing', async () => {
await appendFile('test.js', testJsFilePretty)
await execGit(['add', 'test.js'])

// Remove backup stash during run
await expect(
gitCommit({
config: { '*.js': () => 'git stash drop' },
shell: true,
debug: true,
quiet: false
})
).rejects.toThrowError()

expect(console.printHistory()).toMatchInlineSnapshot(`
"
LOG Preparing... [started]
LOG Preparing... [completed]
LOG Running tasks... [started]
LOG Running tasks for *.js [started]
LOG git stash drop [started]
LOG git stash drop [completed]
LOG Running tasks for *.js [completed]
LOG Running tasks... [completed]
LOG Applying modifications... [started]
LOG Applying modifications... [completed]
LOG Cleaning up... [started]
LOG Cleaning up... [failed]
LOG → lint-staged automatic backup is missing!"
`)
})
})

0 comments on commit 1b64239

Please sign in to comment.