diff --git a/lib/gitWorkflow.js b/lib/gitWorkflow.js index 921597161..b3a13d86a 100644 --- a/lib/gitWorkflow.js +++ b/lib/gitWorkflow.js @@ -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}}` } @@ -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) + } } } diff --git a/lib/runAll.js b/lib/runAll.js index e4b3ba4a8..8b930174f 100644 --- a/lib/runAll.js +++ b/lib/runAll.js @@ -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 diff --git a/test/runAll.unmocked.spec.js b/test/runAll.unmocked.spec.js index 70026fe73..bb37f12dd 100644 --- a/test/runAll.unmocked.spec.js +++ b/test/runAll.unmocked.spec.js @@ -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!" + `) + }) })