-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgit.test.ts
More file actions
363 lines (316 loc) · 11.1 KB
/
git.test.ts
File metadata and controls
363 lines (316 loc) · 11.1 KB
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
353
354
355
356
357
358
359
360
361
362
363
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import { describe, jest, beforeEach, afterAll, it, expect } from '@jest/globals'
import * as cwd from '../src/utils/cwd'
import {
addFileChanges,
getFileChanges,
switchBranch,
pushCurrentBranch,
} from '../src/git'
describe('Git CLI', () => {
beforeEach(() => {
jest.restoreAllMocks()
})
describe('git checkout', () => {
it('should create new branch', async () => {
const execMock = jest.spyOn(exec, 'exec').mockResolvedValue(0)
await switchBranch('new-branch')
expect(execMock).toHaveBeenCalledWith(
'git',
['checkout', '-b', 'new-branch'],
expect.objectContaining({
listeners: { stdline: expect.anything(), errline: expect.anything() },
})
)
})
it('should log debug', async () => {
const execMock = jest
.spyOn(exec, 'exec')
.mockImplementationOnce(async (cmd, args, options) => {
const io = options?.listeners?.stdline
if (io) {
io.call(this, 'git checkout debug log message')
return 0
}
return 0
})
const debugMock = jest.spyOn(core, 'debug').mockReturnValue()
await switchBranch('new-branch')
expect(execMock).toHaveBeenCalled()
expect(debugMock).toHaveBeenCalledWith('git checkout debug log message')
})
it('should log warning', async () => {
const execMock = jest
.spyOn(exec, 'exec')
.mockImplementationOnce(async (cmd, args, options) => {
const io = options?.listeners?.errline
if (io) {
io.call(this, "Switched to a new branch 'new-branch'")
return 1
}
return 0
})
const warningMock = jest.spyOn(core, 'warning').mockReturnValue()
await switchBranch('new-branch')
expect(execMock).toHaveBeenCalled()
expect(warningMock).toHaveBeenCalledWith(
"Switched to a new branch 'new-branch'"
)
})
it('should log error', async () => {
const execMock = jest
.spyOn(exec, 'exec')
.mockImplementationOnce(async (cmd, args, options) => {
const io = options?.listeners?.errline
if (io) {
io.call(this, "fatal: 'new-branch' is invalid")
return 1
}
return 0
})
const errorMock = jest.spyOn(core, 'error').mockReturnValue()
await switchBranch('new-branch')
expect(execMock).toHaveBeenCalled()
expect(errorMock).toHaveBeenCalledWith("fatal: 'new-branch' is invalid")
})
})
describe('git push', () => {
beforeEach(() => {
jest.spyOn(core, 'getBooleanInput').mockReturnValue(false)
})
it('should push new branch', async () => {
const execMock = jest.spyOn(exec, 'exec').mockResolvedValue(0)
await pushCurrentBranch()
expect(execMock).toHaveBeenCalledWith(
'git',
['push', '--porcelain', '--set-upstream', 'origin', 'HEAD'],
expect.objectContaining({
listeners: { stdline: expect.anything(), errline: expect.anything() },
})
)
})
it('should force push new branch', async () => {
const execMock = jest.spyOn(exec, 'exec').mockResolvedValue(0)
const getInput = jest.spyOn(core, 'getBooleanInput').mockReturnValue(true)
await pushCurrentBranch()
expect(execMock).toHaveBeenCalled()
expect(execMock).toHaveBeenCalledWith(
'git',
['push', '--force', '--porcelain', '--set-upstream', 'origin', 'HEAD'],
expect.objectContaining({
listeners: { stdline: expect.anything(), errline: expect.anything() },
})
)
expect(getInput).toHaveBeenCalledWith('branch-push-force')
})
it('should log debug', async () => {
const execMock = jest
.spyOn(exec, 'exec')
.mockImplementationOnce(async (cmd, args, options) => {
const io = options?.listeners?.stdline
if (io) {
io.call(this, 'git push debug log message')
return 0
}
return 0
})
const debugMock = jest.spyOn(core, 'debug').mockReturnValue()
await pushCurrentBranch()
expect(execMock).toHaveBeenCalled()
expect(debugMock).toHaveBeenCalledWith('git push debug log message')
})
it('should log warning', async () => {
const execMock = jest
.spyOn(exec, 'exec')
.mockImplementationOnce(async (cmd, args, options) => {
const io = options?.listeners?.errline
if (io) {
io.call(this, 'git push warning log message')
return 1
}
return 0
})
const warningMock = jest.spyOn(core, 'warning').mockReturnValue()
await pushCurrentBranch()
expect(execMock).toHaveBeenCalled()
expect(warningMock).toHaveBeenCalledWith('git push warning log message')
})
it('should log error', async () => {
const execMock = jest
.spyOn(exec, 'exec')
.mockImplementationOnce(async (cmd, args, options) => {
const io = options?.listeners?.errline
if (io) {
io.call(this, "fatal: 'new-branch' is rejected")
return 1
}
return 0
})
const errorMock = jest.spyOn(core, 'error').mockReturnValue()
await pushCurrentBranch()
expect(execMock).toHaveBeenCalled()
expect(errorMock).toHaveBeenCalledWith("fatal: 'new-branch' is rejected")
})
})
describe('git add', () => {
beforeEach(() => {
jest.spyOn(cwd, 'getWorkspace').mockReturnValue('/test-workspace')
})
it('should ensure file paths are within curent working directory', async () => {
const execMock = jest.spyOn(exec, 'exec').mockResolvedValue(0)
await addFileChanges(['*.ts', '~/.bashrc'])
expect(execMock).toHaveBeenCalledWith(
'git',
['add', '--', '/test-workspace/*.ts', '/test-workspace/~/.bashrc'],
expect.objectContaining({
listeners: { stdline: expect.anything(), errline: expect.anything() },
})
)
})
it('should log debug', async () => {
const execMock = jest
.spyOn(exec, 'exec')
.mockImplementationOnce(async (cmd, args, options) => {
const io = options?.listeners?.stdline
if (io) {
io.call(this, 'git add debug log message')
return 0
}
return 0
})
const debugMock = jest.spyOn(core, 'debug').mockReturnValue()
await addFileChanges(['*.ts'])
expect(execMock).toHaveBeenCalled()
expect(debugMock).toHaveBeenCalledWith('git add debug log message')
})
it('should log warning', async () => {
const execMock = jest
.spyOn(exec, 'exec')
.mockImplementationOnce(async (cmd, args, options) => {
const io = options?.listeners?.errline
if (io) {
io.call(this, 'git add warning log message')
return 1
}
return 0
})
const warningMock = jest.spyOn(core, 'warning').mockReturnValue()
await addFileChanges(['*.ts'])
expect(execMock).toHaveBeenCalled()
expect(warningMock).toHaveBeenCalledWith('git add warning log message')
})
it('should log error', async () => {
const execMock = jest
.spyOn(exec, 'exec')
.mockImplementationOnce(async (cmd, args, options) => {
const io = options?.listeners?.errline
if (io) {
io.call(this, "fatal: pathspec 'main.ts' did not match any files")
return 1
}
return 0
})
const errorMock = jest.spyOn(core, 'error').mockReturnValue()
await addFileChanges(['*.ts'])
expect(execMock).toHaveBeenCalled()
expect(errorMock).toHaveBeenCalledWith(
"fatal: pathspec 'main.ts' did not match any files"
)
})
})
describe('git status', () => {
const gitStatus = [
' D src/index.ts',
'DA src/indices.ts',
'AM src/main.ts',
'A src/run.ts',
'?? src/errors.ts',
'RM tests/main.test.ts -> tests/program.test.ts',
'D tests/runner.test.ts',
'A tests/run.test.ts',
]
it('should parse ouput into file changes', async () => {
const execMock = jest
.spyOn(exec, 'exec')
.mockImplementationOnce(async (cmd, args, options) => {
const io = options?.listeners?.stdline
if (io) {
gitStatus.forEach((o) => io.call(this, o))
}
return 0
})
const changes = await getFileChanges()
expect(execMock).toHaveBeenCalledWith(
'git',
['status', '-suno', '--porcelain'],
expect.objectContaining({
listeners: { stdline: expect.anything(), errline: expect.anything() },
})
)
expect(changes).toBeDefined()
expect(changes.additions).toBeDefined()
expect(changes.additions).toHaveLength(5)
expect(changes.additions).toContainEqual(
expect.objectContaining({ path: 'src/main.ts' })
)
expect(changes.additions).toContainEqual(
expect.objectContaining({ path: 'src/run.ts' })
)
expect(changes.additions).toContainEqual(
expect.objectContaining({ path: 'src/errors.ts' })
)
expect(changes.additions).toContainEqual(
expect.objectContaining({ path: 'tests/program.test.ts' })
)
expect(changes.additions).toContainEqual(
expect.objectContaining({ path: 'tests/run.test.ts' })
)
expect(changes.deletions).toBeDefined()
expect(changes.deletions).toHaveLength(3)
expect(changes.deletions).toContainEqual(
expect.objectContaining({ path: 'src/indices.ts' })
)
expect(changes.deletions).toContainEqual(
expect.objectContaining({ path: 'tests/main.test.ts' })
)
expect(changes.deletions).toContainEqual(
expect.objectContaining({ path: 'tests/runner.test.ts' })
)
})
it('should log warning', async () => {
const execMock = jest
.spyOn(exec, 'exec')
.mockImplementationOnce(async (cmd, args, options) => {
const io = options?.listeners?.errline
if (io) {
io.call(this, 'git status warning log message')
return 1
}
return 0
})
const warningMock = jest.spyOn(core, 'warning').mockReturnValue()
await getFileChanges()
expect(execMock).toHaveBeenCalled()
expect(warningMock).toHaveBeenCalledWith('git status warning log message')
})
it('should log error', async () => {
const execMock = jest
.spyOn(exec, 'exec')
.mockImplementationOnce(async (cmd, args, options) => {
const io = options?.listeners?.errline
if (io) {
io.call(this, 'fatal: git status error log message')
return 1
}
return 0
})
const errorMock = jest.spyOn(core, 'error').mockReturnValue()
await getFileChanges()
expect(execMock).toHaveBeenCalled()
expect(errorMock).toHaveBeenCalledWith(
'fatal: git status error log message'
)
})
})
})