/
getBackupStash.spec.js
93 lines (71 loc) · 2.78 KB
/
getBackupStash.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
import { execGit } from '../../lib/execGit.js'
import { GitWorkflow, STASH } from '../../lib/gitWorkflow.js'
import { getInitialState } from '../../lib/state.js'
import { GetBackupStashError } from '../../lib/symbols'
jest.mock('../../lib/execGit.js', () => ({
execGit: jest.fn(async () => ''),
}))
describe('gitWorkflow', () => {
const options = { gitConfigDir: '.' }
describe('getBackupStash', () => {
it('should throw when stash not found', async () => {
const gitWorkflow = new GitWorkflow(options)
const ctx = getInitialState()
await expect(gitWorkflow.getBackupStash(ctx)).rejects.toThrowError(
'lint-staged automatic backup is missing!'
)
expect(ctx.errors.has(GetBackupStashError)).toEqual(true)
})
it('should throw when stash not found even when other stashes are', async () => {
const gitWorkflow = new GitWorkflow(options)
const ctx = getInitialState()
execGit.mockResolvedValueOnce('stash@{0}: some random stuff')
await expect(gitWorkflow.getBackupStash(ctx)).rejects.toThrowError(
'lint-staged automatic backup is missing!'
)
expect(ctx.errors.has(GetBackupStashError)).toEqual(true)
})
it('should return ref to the backup stash', async () => {
const gitWorkflow = new GitWorkflow(options)
const ctx = getInitialState()
execGit.mockResolvedValueOnce(
[
'stash@{0}: some random stuff',
`stash@{1}: ${STASH}`,
'stash@{2}: other random stuff',
].join('\n')
)
await expect(gitWorkflow.getBackupStash(ctx)).resolves.toEqual('refs/stash@{1}')
})
it('should return unescaped ref to the backup stash when using MSYS2 without login shell', async () => {
const gitWorkflow = new GitWorkflow(options)
const ctx = getInitialState()
process.env.MSYSTEM = 'MSYS'
execGit.mockResolvedValueOnce(
[
'stash@{0}: some random stuff',
`stash@{1}: ${STASH}`,
'stash@{2}: other random stuff',
].join('\n')
)
await expect(gitWorkflow.getBackupStash(ctx)).resolves.toEqual('refs/stash@{1}')
delete process.env.MSYSTEM
})
it('should return escaped ref to the backup stash when using MSYS2 with login shell', async () => {
const gitWorkflow = new GitWorkflow(options)
const ctx = getInitialState()
process.env.MSYSTEM = 'MSYS'
process.env.LOGINSHELL = 'bash'
execGit.mockResolvedValueOnce(
[
'stash@{0}: some random stuff',
`stash@{1}: ${STASH}`,
'stash@{2}: other random stuff',
].join('\n')
)
await expect(gitWorkflow.getBackupStash(ctx)).resolves.toEqual('refs/stash@\\{1\\}')
delete process.env.MSYSTEM
delete process.env.LOGINSHELL
})
})
})