/
runAll.unmocked.spec.js
352 lines (291 loc) Β· 12.2 KB
/
runAll.unmocked.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import fs from 'fs-extra'
import path from 'path'
import tmp from 'tmp'
import execGitBase from '../src/execGit'
import runAll from '../src/runAll'
tmp.setGracefulCleanup()
jest.unmock('execa')
const testJsFilePretty = `module.exports = {
foo: "bar"
};
`
const testJsFileUgly = `module.exports = {
'foo': 'bar',
}
`
const testJsFileUnfixable = `const obj = {
'foo': 'bar'
`
let wcDir
let cwd
// Get file content
const readFile = async (filename, dir = cwd) =>
fs.readFile(path.join(dir, filename), { encoding: 'utf-8' })
// Append to file, creating if it doesn't exist
const appendFile = async (filename, content, dir = cwd) =>
fs.appendFile(path.join(dir, filename), content)
// Wrap execGit to always pass `gitOps`
const execGit = async args => execGitBase(args, { cwd })
// Execute runAll before git commit to emulate lint-staged
const gitCommit = async options => {
try {
await runAll({ ...options, cwd, quiet: true })
await execGit(['commit', '-m "test"'])
return true
} catch (error) {
return false
}
}
describe('runAll', () => {
it('should throw when not in a git directory', async () => {
const nonGitDir = tmp.dirSync({ unsafeCleanup: true })
await expect(runAll({ cwd: nonGitDir })).rejects.toThrowErrorMatchingInlineSnapshot(
`"Current directory is not a git directory!"`
)
nonGitDir.removeCallback()
})
})
describe('runAll', () => {
beforeEach(async () => {
wcDir = tmp.dirSync({ unsafeCleanup: true })
cwd = await fs.realpath(wcDir.name)
// Init repository with initial commit
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'])
})
it('Should commit entire staged file when no errors from linter', async () => {
// Stage pretty file
await appendFile('test.js', testJsFilePretty)
await execGit(['add', 'test.js'])
// Run lint-staged with `prettier --list-different` and commit pretty file
const success = await gitCommit({ config: { '*.js': 'prettier --list-different' } })
expect(success).toEqual(true)
// Nothing is wrong, so a new commit is created
expect(await execGit(['rev-list', '--count', 'HEAD'])).toEqual('2')
expect(await execGit(['log', '-1', '--pretty=%B'])).toMatchInlineSnapshot(`
" \\"test\\"
"
`)
expect(await readFile('test.js')).toEqual(testJsFilePretty)
})
it('Should commit entire staged file when no errors and linter modifies file', async () => {
// Stage ugly file
await appendFile('test.js', testJsFileUgly)
await execGit(['add', 'test.js'])
// Run lint-staged with `prettier --write` and commit pretty file
const success = await gitCommit({ config: { '*.js': ['prettier --write', 'git add'] } })
expect(success).toEqual(true)
// Nothing is wrong, so a new commit is created and file is pretty
expect(await execGit(['rev-list', '--count', 'HEAD'])).toEqual('2')
expect(await execGit(['log', '-1', '--pretty=%B'])).toMatchInlineSnapshot(`
" \\"test\\"
"
`)
expect(await readFile('test.js')).toEqual(testJsFilePretty)
})
it('Should fail to commit entire staged file when errors from linter', async () => {
// Stage ugly file
await appendFile('test.js', testJsFileUgly)
await execGit(['add', 'test.js'])
const status = await execGit(['status'])
// Run lint-staged with `prettier --list-different` to break the linter
const success = await gitCommit({ config: { '*.js': 'prettier --list-different' } })
expect(success).toEqual(false)
// Something was wrong so the repo is returned to original state
expect(await execGit(['rev-list', '--count', 'HEAD'])).toEqual('1')
expect(await execGit(['log', '-1', '--pretty=%B'])).toMatchInlineSnapshot(`
" initial commit
"
`)
expect(await execGit(['status'])).toEqual(status)
expect(await readFile('test.js')).toEqual(testJsFileUgly)
})
it('Should fail to commit entire staged file when errors from linter and linter modifies files', async () => {
// Add unfixable file to commit so `prettier --write` breaks
await appendFile('test.js', testJsFileUnfixable)
await execGit(['add', 'test.js'])
const status = await execGit(['status'])
// Run lint-staged with `prettier --write` to break the linter
const success = await gitCommit({ config: { '*.js': ['prettier --write', 'git add'] } })
expect(success).toEqual(false)
// Something was wrong so the repo is returned to original state
expect(await execGit(['rev-list', '--count', 'HEAD'])).toEqual('1')
expect(await execGit(['log', '-1', '--pretty=%B'])).toMatchInlineSnapshot(`
" initial commit
"
`)
expect(await execGit(['status'])).toEqual(status)
expect(await readFile('test.js')).toEqual(testJsFileUnfixable)
})
it('Should commit partial change from partially staged file when no errors from linter', async () => {
// Stage pretty file
await appendFile('test.js', testJsFilePretty)
await execGit(['add', 'test.js'])
// Edit pretty file but do not stage changes
const appended = '\nconsole.log("test");\n'
await appendFile('test.js', appended)
// Run lint-staged with `prettier --list-different` and commit pretty file
const success = await gitCommit({ config: { '*.js': 'prettier --list-different' } })
expect(success).toEqual(true)
// Nothing is wrong, so a new commit is created and file is pretty
expect(await execGit(['rev-list', '--count', 'HEAD'])).toEqual('2')
expect(await execGit(['log', '-1', '--pretty=%B'])).toMatchInlineSnapshot(`
" \\"test\\"
"
`)
// Latest commit contains pretty file
// `git show` strips empty line from here here
expect(await execGit(['show', 'HEAD:test.js'])).toEqual(testJsFilePretty.replace(/\n$/, ''))
// Since edit was not staged, the file is still modified
const status = await execGit(['status'])
expect(status).toMatch('modified: test.js')
expect(status).toMatch('no changes added to commit')
expect(await readFile('test.js')).toEqual(testJsFilePretty + appended)
})
it('Should commit partial change from partially staged file when no errors from linter and linter modifies file', async () => {
// Stage ugly file
await appendFile('test.js', testJsFileUgly)
await execGit(['add', 'test.js'])
// Edit ugly file but do not stage changes
const appended = '\n\nconsole.log("test");\n'
await appendFile('test.js', appended)
// Run lint-staged with `prettier --write` and commit pretty file
const success = await gitCommit({ config: { '*.js': ['prettier --write', 'git add'] } })
expect(success).toEqual(true)
// Nothing is wrong, so a new commit is created and file is pretty
expect(await execGit(['rev-list', '--count', 'HEAD'])).toEqual('2')
expect(await execGit(['log', '-1', '--pretty=%B'])).toMatchInlineSnapshot(`
" \\"test\\"
"
`)
// Latest commit contains pretty file
// `git show` strips empty line from here here
expect(await execGit(['show', 'HEAD:test.js'])).toEqual(testJsFilePretty.replace(/\n$/, ''))
// Nothing is staged
const status = await execGit(['status'])
expect(status).toMatch('modified: test.js')
expect(status).toMatch('no changes added to commit')
// File is pretty, and has been edited
expect(await readFile('test.js')).toEqual(testJsFilePretty + appended)
})
it('Should fail to commit partial change from partially staged file when errors from linter', async () => {
// Stage ugly file
await appendFile('test.js', testJsFileUgly)
await execGit(['add', 'test.js'])
// Edit ugly file but do not stage changes
const appended = '\nconsole.log("test");\n'
await appendFile('test.js', appended)
const status = await execGit(['status'])
// Run lint-staged with `prettier --list-different` to break the linter
const success = await gitCommit({ config: { '*.js': 'prettier --list-different' } })
expect(success).toEqual(false)
// Something was wrong so the repo is returned to original state
expect(await execGit(['rev-list', '--count', 'HEAD'])).toEqual('1')
expect(await execGit(['log', '-1', '--pretty=%B'])).toMatchInlineSnapshot(`
" initial commit
"
`)
expect(await execGit(['status'])).toEqual(status)
expect(await readFile('test.js')).toEqual(testJsFileUgly + appended)
})
it('Should fail to commit partial change from partially staged file when errors from linter and linter modifies files', async () => {
// Add unfixable file to commit so `prettier --write` breaks
await appendFile('test.js', testJsFileUnfixable)
await execGit(['add', 'test.js'])
// Edit unfixable file but do not stage changes
const appended = '\nconsole.log("test");\n'
await appendFile('test.js', appended)
const status = await execGit(['status'])
// Run lint-staged with `prettier --write` to break the linter
const success = await gitCommit({ config: { '*.js': ['prettier --write', 'git add'] } })
expect(success).toEqual(false)
// Something was wrong so the repo is returned to original state
expect(await execGit(['rev-list', '--count', 'HEAD'])).toEqual('1')
expect(await execGit(['log', '-1', '--pretty=%B'])).toMatchInlineSnapshot(`
" initial commit
"
`)
expect(await execGit(['status'])).toEqual(status)
expect(await readFile('test.js')).toEqual(testJsFileUnfixable + appended)
})
it('Should clear unstaged changes when linter applies same changes', async () => {
// Stage ugly file
await appendFile('test.js', testJsFileUgly)
await execGit(['add', 'test.js'])
// Replace ugly file with pretty but do not stage changes
await fs.remove(path.join(cwd, 'test.js'))
await appendFile('test.js', testJsFilePretty)
// Run lint-staged with `prettier --write` and commit pretty file
const success = await gitCommit({ config: { '*.js': ['prettier --write', 'git add'] } })
expect(success).toEqual(true)
// Nothing is wrong, so a new commit is created and file is pretty
expect(await execGit(['rev-list', '--count', 'HEAD'])).toEqual('2')
expect(await execGit(['log', '-1', '--pretty=%B'])).toMatchInlineSnapshot(`
" \\"test\\"
"
`)
// Latest commit contains pretty file
// `git show` strips empty line from here here
expect(await execGit(['show', 'HEAD:test.js'])).toEqual(testJsFilePretty.replace(/\n$/, ''))
// Nothing is staged
expect(await execGit(['status'])).toMatchInlineSnapshot(`
"On branch master
nothing to commit, working tree clean"
`)
// File is pretty, and has been edited
expect(await readFile('test.js')).toEqual(testJsFilePretty)
})
it('Should fail when linter creates a .git/index.lock', async () => {
// Stage ugly file
await appendFile('test.js', testJsFileUgly)
await execGit(['add', 'test.js'])
// Edit ugly file but do not stage changes
const appended = '\n\nconsole.log("test");\n'
await appendFile('test.js', appended)
expect(await readFile('test.js')).toEqual(testJsFileUgly + appended)
const diff = await execGit(['diff'])
// Run lint-staged with `prettier --write` and commit pretty file
// The task creates a git lock file to simulate failure
const success = await gitCommit({
config: {
'*.js': files => [
`touch ${cwd}/.git/index.lock`,
`prettier --write ${files.join(' ')}`,
`git add ${files.join(' ')}`
]
}
})
expect(success).toEqual(false)
// Something was wrong so new commit wasn't created
expect(await execGit(['rev-list', '--count', 'HEAD'])).toEqual('1')
expect(await execGit(['log', '-1', '--pretty=%B'])).toMatchInlineSnapshot(`
" initial commit
"
`)
// But local modifications are gone
expect(await execGit(['diff'])).not.toEqual(diff)
expect(await execGit(['diff'])).toMatchInlineSnapshot(`
"diff --git a/test.js b/test.js
index f80f875..1c5643c 100644
--- a/test.js
+++ b/test.js
@@ -1,3 +1,3 @@
module.exports = {
- 'foo': 'bar',
-}
+ foo: \\"bar\\"
+};"
`)
expect(await readFile('test.js')).not.toEqual(testJsFileUgly + appended)
expect(await readFile('test.js')).toEqual(testJsFilePretty)
// Remove lock file
await fs.remove(`${cwd}/.git/index.lock`)
})
afterEach(async () => {
wcDir.removeCallback()
})
})