/
gitWorkflow.spec.js
158 lines (144 loc) Β· 4.56 KB
/
gitWorkflow.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import fs from 'fs-extra'
import normalize from 'normalize-path'
import path from 'path'
import execGitBase from '../lib/execGit'
import { writeFile } from '../lib/file'
import GitWorkflow from '../lib/gitWorkflow'
import { getInitialState } from '../lib/state'
import { createTempDir } from './utils/tempDir'
jest.mock('../lib/file.js')
jest.unmock('execa')
jest.setTimeout(20000)
let tmpDir, cwd
/** Append to file, creating if it doesn't exist */
const appendFile = async (filename, content, dir = cwd) =>
fs.appendFile(path.resolve(dir, filename), content)
/** Wrap execGit to always pass `gitOps` */
const execGit = async (args) => execGitBase(args, { cwd })
/** Initialize git repo for test */
const initGitRepo = async () => {
await execGit('init')
await execGit(['config', 'user.name', '"test"'])
await execGit(['config', 'user.email', '"test@test.com"'])
await appendFile('README.md', '# Test\n')
await execGit(['add', 'README.md'])
await execGit(['commit', '-m initial commit'])
}
const isAppveyor = !!process.env.APPVEYOR
describe('gitWorkflow', () => {
beforeEach(async () => {
tmpDir = await createTempDir()
cwd = normalize(tmpDir)
await initGitRepo()
})
afterEach(async () => {
if (!isAppveyor) {
await fs.remove(tmpDir)
}
})
describe('prepare', () => {
it('should handle errors', async () => {
const gitWorkflow = new GitWorkflow({
gitDir: cwd,
gitConfigDir: path.resolve(cwd, './.git'),
})
jest.doMock('execa', () => Promise.reject({}))
const ctx = getInitialState()
// mock a simple failure
gitWorkflow.getPartiallyStagedFiles = () => ['foo']
gitWorkflow.getHiddenFilepath = () => {
throw new Error('test')
}
await expect(gitWorkflow.prepare(ctx, false)).rejects.toThrowErrorMatchingInlineSnapshot(
`"test"`
)
expect(ctx).toMatchInlineSnapshot(`
Object {
"errors": Set {
Symbol(GitError),
},
"hasPartiallyStagedFiles": true,
"output": Array [],
"quiet": false,
"shouldBackup": null,
}
`)
})
})
describe('cleanup', () => {
it('should handle errors', async () => {
const gitWorkflow = new GitWorkflow({
gitDir: cwd,
gitConfigDir: path.resolve(cwd, './.git'),
})
const ctx = getInitialState()
await expect(gitWorkflow.cleanup(ctx)).rejects.toThrowErrorMatchingInlineSnapshot(
`"lint-staged automatic backup is missing!"`
)
expect(ctx).toMatchInlineSnapshot(`
Object {
"errors": Set {
Symbol(GetBackupStashError),
Symbol(GitError),
},
"hasPartiallyStagedFiles": null,
"output": Array [],
"quiet": false,
"shouldBackup": null,
}
`)
})
})
describe('hideUnstagedChanges', () => {
it('should handle errors', async () => {
const gitWorkflow = new GitWorkflow({
gitDir: cwd,
gitConfigDir: path.resolve(cwd, './.git'),
})
const totallyRandom = `totally_random_file-${Date.now().toString()}`
gitWorkflow.partiallyStagedFiles = [totallyRandom]
const ctx = getInitialState()
await expect(gitWorkflow.hideUnstagedChanges(ctx)).rejects.toThrowError(
`pathspec '${totallyRandom}' did not match any file(s) known to git`
)
expect(ctx).toMatchInlineSnapshot(`
Object {
"errors": Set {
Symbol(GitError),
Symbol(HideUnstagedChangesError),
},
"hasPartiallyStagedFiles": null,
"output": Array [],
"quiet": false,
"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 [],
"quiet": false,
"shouldBackup": null,
}
`)
})
})
})