Skip to content

Commit

Permalink
test: add test for error during merge state restoration
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Apr 25, 2020
1 parent b565481 commit 6aede38
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 24 deletions.
11 changes: 8 additions & 3 deletions lib/gitWorkflow.js
Expand Up @@ -12,6 +12,7 @@ const {
ApplyEmptyCommitError,
GetBackupStashError,
HideUnstagedChangesError,
RestoreMergeStatusError,
RestoreUnstagedChangesError
} = require('./symbols')

Expand Down Expand Up @@ -132,7 +133,7 @@ class GitWorkflow {
/**
* Restore meta information about ongoing git merge
*/
async restoreMergeStatus() {
async restoreMergeStatus(ctx) {
debug('Restoring merge state...')
try {
await Promise.all([
Expand All @@ -144,7 +145,11 @@ class GitWorkflow {
} catch (error) {
debug('Failed restoring merge state with error:')
debug(error)
throw new Error('Merge state could not be restored due to an error!')
handleError(
new Error('Merge state could not be restored due to an error!'),
ctx,
RestoreMergeStatusError
)
}
}

Expand Down Expand Up @@ -305,7 +310,7 @@ class GitWorkflow {
await this.execGit(['stash', 'apply', '--quiet', '--index', await this.getBackupStash(ctx)])

// Restore meta information about ongoing git merge
await this.restoreMergeStatus()
await this.restoreMergeStatus(ctx)

// If stashing resurrected deleted files, clean them out
await Promise.all(this.deletedFiles.map((file) => unlink(file)))
Expand Down
24 changes: 9 additions & 15 deletions lib/symbols.js
@@ -1,30 +1,24 @@
'use strict'

const GitRepoError = Symbol('GitRepoError')

const ApplyEmptyCommitError = Symbol('ApplyEmptyCommitError')
const GetBackupStashError = Symbol('GetBackupStashError')
const GetStagedFilesError = Symbol('GetStagedFilesError')

const TaskError = Symbol('TaskError')

const GitError = Symbol('GitError')

const GetBackupStashError = Symbol('GetBackupStashError')

const GitRepoError = Symbol('GitRepoError')
const HideUnstagedChangesError = Symbol('HideUnstagedChangesError')

const ApplyEmptyCommitError = Symbol('ApplyEmptyCommitError')

const RestoreUnstagedChangesError = Symbol('RestoreUnstagedChangesError')

const RestoreMergeStatusError = Symbol('RestoreMergeStatusError')
const RestoreOriginalStateError = Symbol('RestoreOriginalStateError')
const RestoreUnstagedChangesError = Symbol('RestoreUnstagedChangesError')
const TaskError = Symbol('TaskError')

module.exports = {
GitRepoError,
GetStagedFilesError,
ApplyEmptyCommitError,
GetBackupStashError,
GetStagedFilesError,
GitError,
GitRepoError,
HideUnstagedChangesError,
RestoreMergeStatusError,
RestoreOriginalStateError,
RestoreUnstagedChangesError,
TaskError
Expand Down
47 changes: 41 additions & 6 deletions test/gitWorkflow.spec.js
Expand Up @@ -5,8 +5,11 @@ import os from 'os'
import path from 'path'

import execGitBase from '../lib/execGit'
import { writeFile } from '../lib/file'
import GitWorkflow from '../lib/gitWorkflow'
import { getInitialState } from '../lib/state'

jest.mock('../lib/file.js')
jest.unmock('execa')

jest.setTimeout(20000)
Expand Down Expand Up @@ -72,7 +75,7 @@ describe('gitWorkflow', () => {
gitConfigDir: path.resolve(cwd, './.git')
})
jest.doMock('execa', () => Promise.reject({}))
const ctx = { hasPartiallyStagedFiles: false, errors: new Set() }
const ctx = getInitialState()
// mock a simple failure
gitWorkflow.getPartiallyStagedFiles = () => ['foo']
gitWorkflow.getHiddenFilepath = () => {
Expand All @@ -87,6 +90,8 @@ describe('gitWorkflow', () => {
Symbol(GitError),
},
"hasPartiallyStagedFiles": true,
"output": Array [],
"shouldBackup": null,
}
`)
})
Expand All @@ -98,7 +103,7 @@ describe('gitWorkflow', () => {
gitDir: cwd,
gitConfigDir: path.resolve(cwd, './.git')
})
const ctx = { hasPartiallyStagedFiles: false, errors: new Set() }
const ctx = getInitialState()
await expect(gitWorkflow.cleanup(ctx)).rejects.toThrowErrorMatchingInlineSnapshot(
`"lint-staged automatic backup is missing!"`
)
Expand All @@ -108,7 +113,9 @@ describe('gitWorkflow', () => {
Symbol(GetBackupStashError),
Symbol(GitError),
},
"hasPartiallyStagedFiles": false,
"hasPartiallyStagedFiles": null,
"output": Array [],
"shouldBackup": null,
}
`)
})
Expand All @@ -122,17 +129,45 @@ describe('gitWorkflow', () => {
})
const totallyRandom = `totally_random_file-${Date.now().toString()}`
gitWorkflow.partiallyStagedFiles = [totallyRandom]
const ctx = { hasPartiallyStagedFiles: false, errors: new Set() }
const ctx = getInitialState()
await expect(gitWorkflow.hideUnstagedChanges(ctx)).rejects.toThrowErrorMatchingInlineSnapshot(
`"error: pathspec '${totallyRandom}' did not match any file(s) known to git"`
`"error: pathspec 'totally_random_file-1587728648348' did not match any file(s) known to git"`
)
expect(ctx).toMatchInlineSnapshot(`
Object {
"errors": Set {
Symbol(GitError),
Symbol(HideUnstagedChangesError),
},
"hasPartiallyStagedFiles": false,
"hasPartiallyStagedFiles": null,
"output": Array [],
"shouldBackup": null,
}
`)
})
})

describe('restoreMergeStatus', () => {
it('should handle error when restoring merge state fails', async () => {
const gitWorkflow = new GitWorkflow({
gitDir: cwd,
gitConfigDir: path.resolve(cwd, './.git')
})
gitWorkflow.mergeHeadBuffer = true
writeFile.mockImplementation(() => Promise.reject('test'))
const ctx = getInitialState()
await expect(gitWorkflow.restoreMergeStatus(ctx)).rejects.toThrowErrorMatchingInlineSnapshot(
`"Merge state could not be restored due to an error!"`
)
expect(ctx).toMatchInlineSnapshot(`
Object {
"errors": Set {
Symbol(GitError),
Symbol(RestoreMergeStatusError),
},
"hasPartiallyStagedFiles": null,
"output": Array [],
"shouldBackup": null,
}
`)
})
Expand Down

0 comments on commit 6aede38

Please sign in to comment.