-
-
Notifications
You must be signed in to change notification settings - Fork 365
/
git.test.ts
473 lines (416 loc) · 12.2 KB
/
git.test.ts
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// Initial env variable setup for tests.
process.env['INPUT_FOLDER'] = 'build'
process.env['GITHUB_SHA'] = '123'
import {mkdirP, rmRF} from '@actions/io'
import {action, Status, TestFlag} from '../src/constants'
import {execute} from '../src/execute'
import {deploy, init} from '../src/git'
import fs from 'fs'
const originalAction = JSON.stringify(action)
jest.mock('fs', () => ({
existsSync: jest.fn()
}))
jest.mock('@actions/core', () => ({
setFailed: jest.fn(),
getInput: jest.fn(),
setOutput: jest.fn(),
isDebug: jest.fn(),
info: jest.fn()
}))
jest.mock('@actions/io', () => ({
rmRF: jest.fn(),
mkdirP: jest.fn()
}))
jest.mock('../src/execute', () => ({
__esModule: true,
execute: jest.fn(() => ({stdout: '', stderr: ''}))
}))
describe('git', () => {
afterEach(() => {
Object.assign(action, JSON.parse(originalAction))
})
describe('init', () => {
it('should execute commands', async () => {
Object.assign(action, {
hostname: 'github.com',
silent: false,
repositoryPath: 'JamesIves/github-pages-deploy-action',
token: '123',
branch: 'branch',
folder: '.',
pusher: {
name: 'asd',
email: 'as@cat'
},
isTest: TestFlag.HAS_CHANGED_FILES
})
await init(action)
expect(execute).toHaveBeenCalledTimes(7)
})
it('should catch when a function throws an error', async () => {
;(execute as jest.Mock).mockImplementationOnce(() => {
throw new Error('Mocked throw')
})
Object.assign(action, {
hostname: 'github.com',
silent: false,
repositoryPath: 'JamesIves/github-pages-deploy-action',
token: '123',
branch: 'branch',
folder: '.',
pusher: {
name: 'asd',
email: 'as@cat'
},
isTest: TestFlag.HAS_CHANGED_FILES
})
try {
await init(action)
} catch (error) {
expect(error instanceof Error && error.message).toBe(
'There was an error initializing the repository: Mocked throw ❌'
)
}
})
it('should correctly continue when it cannot unset a git config value', async () => {
Object.assign(action, {
hostname: 'github.com',
silent: false,
repositoryPath: 'JamesIves/github-pages-deploy-action',
token: '123',
branch: 'branch',
folder: '.',
pusher: {
name: 'asd',
email: 'as@cat'
},
isTest: TestFlag.UNABLE_TO_UNSET_GIT_CONFIG
})
await init(action)
expect(execute).toHaveBeenCalledTimes(7)
})
it('should not unset git config if a user is using ssh', async () => {
// Sets and unsets the CI condition.
process.env.CI = 'true'
Object.assign(action, {
hostname: 'github.com',
silent: false,
repositoryPath: 'JamesIves/github-pages-deploy-action',
sshKey: true,
branch: 'branch',
folder: '.',
pusher: {
name: 'asd',
email: 'as@cat'
},
isTest: false
})
await init(action)
expect(execute).toHaveBeenCalledTimes(6)
process.env.CI = undefined
})
it('should correctly continue when it cannot remove origin', async () => {
Object.assign(action, {
hostname: 'github.com',
silent: false,
repositoryPath: 'JamesIves/github-pages-deploy-action',
token: '123',
branch: 'branch',
folder: '.',
pusher: {
name: 'asd',
email: 'as@cat'
},
isTest: TestFlag.UNABLE_TO_REMOVE_ORIGIN
})
await init(action)
expect(execute).toHaveBeenCalledTimes(7)
})
})
describe('deploy', () => {
it('should execute commands', async () => {
Object.assign(action, {
hostname: 'github.com',
silent: false,
folder: 'assets',
branch: 'branch',
token: '123',
repositoryName: 'JamesIves/montezuma',
pusher: {
name: 'asd',
email: 'as@cat'
},
isTest: TestFlag.HAS_CHANGED_FILES
})
const response = await deploy(action)
// Includes the call to generateWorktree
expect(execute).toHaveBeenCalledTimes(15)
expect(rmRF).toHaveBeenCalledTimes(1)
expect(response).toBe(Status.SUCCESS)
})
it('should not push when asked to dryRun', async () => {
Object.assign(action, {
hostname: 'github.com',
silent: false,
dryRun: true,
folder: 'assets',
branch: 'branch',
token: '123',
pusher: {
name: 'asd',
email: 'as@cat'
},
isTest: TestFlag.HAS_CHANGED_FILES
})
const response = await deploy(action)
// Includes the call to generateWorktree
expect(execute).toHaveBeenCalledTimes(14)
expect(rmRF).toHaveBeenCalledTimes(1)
expect(response).toBe(Status.SUCCESS)
})
it('should execute commands with single commit toggled', async () => {
Object.assign(action, {
hostname: 'github.com',
silent: false,
folder: 'other',
folderPath: 'other',
branch: 'branch',
token: '123',
singleCommit: true,
pusher: {
name: 'asd',
email: 'as@cat'
},
clean: true,
isTest: TestFlag.HAS_CHANGED_FILES
})
await deploy(action)
// Includes the call to generateWorktree
expect(execute).toHaveBeenCalledTimes(15)
expect(rmRF).toHaveBeenCalledTimes(1)
})
it('should execute commands with single commit toggled and existing branch', async () => {
Object.assign(action, {
hostname: 'github.com',
silent: false,
folder: 'other',
folderPath: 'other',
branch: 'branch',
token: '123',
singleCommit: true,
pusher: {
name: 'asd',
email: 'as@cat'
},
clean: true,
isTest: TestFlag.HAS_CHANGED_FILES | TestFlag.HAS_REMOTE_BRANCH
})
await deploy(action)
// Includes the call to generateWorktree
expect(execute).toHaveBeenCalledTimes(14)
expect(rmRF).toHaveBeenCalledTimes(1)
})
it('should execute commands with single commit and dryRun toggled', async () => {
Object.assign(action, {
hostname: 'github.com',
silent: false,
folder: 'other',
folderPath: 'other',
branch: 'branch',
gitHubToken: '123',
singleCommit: true,
dryRun: true,
pusher: {
name: 'asd',
email: 'as@cat'
},
clean: true,
isTest: TestFlag.HAS_CHANGED_FILES
})
await deploy(action)
// Includes the call to generateWorktree
expect(execute).toHaveBeenCalledTimes(14)
expect(rmRF).toHaveBeenCalledTimes(1)
})
it('should not ignore CNAME or nojekyll if they exist in the deployment folder', async () => {
;(fs.existsSync as jest.Mock)
.mockImplementationOnce(() => {
return true
})
.mockImplementationOnce(() => {
return true
})
Object.assign(action, {
hostname: 'github.com',
silent: false,
folder: 'assets',
folderPath: 'assets',
branch: 'branch',
token: '123',
pusher: {
name: 'asd',
email: 'as@cat'
},
clean: true,
isTest: TestFlag.HAS_CHANGED_FILES
})
const response = await deploy(action)
// Includes the call to generateWorktree
expect(execute).toHaveBeenCalledTimes(15)
expect(rmRF).toHaveBeenCalledTimes(1)
expect(fs.existsSync).toHaveBeenCalledTimes(2)
expect(response).toBe(Status.SUCCESS)
})
describe('with empty GITHUB_SHA', () => {
const oldSha = process.env.GITHUB_SHA
afterAll(() => {
process.env.GITHUB_SHA = oldSha
})
it('should execute commands with clean options', async () => {
process.env.GITHUB_SHA = ''
Object.assign(action, {
hostname: 'github.com',
silent: false,
folder: 'other',
folderPath: 'other',
branch: 'branch',
token: '123',
pusher: {
name: 'asd',
email: 'as@cat'
},
clean: true,
workspace: 'other',
isTest: TestFlag.NONE
})
await deploy(action)
// Includes the call to generateWorktree
expect(execute).toHaveBeenCalledTimes(12)
expect(rmRF).toHaveBeenCalledTimes(1)
})
})
it('should execute commands with clean options stored as an array', async () => {
Object.assign(action, {
hostname: 'github.com',
silent: false,
folder: 'assets',
folderPath: 'assets',
branch: 'branch',
token: '123',
pusher: {
name: 'asd',
email: 'as@cat'
},
clean: true,
cleanExclude: ['cat', 'montezuma'],
isTest: TestFlag.NONE
})
await deploy(action)
// Includes the call to generateWorktree
expect(execute).toHaveBeenCalledTimes(12)
expect(rmRF).toHaveBeenCalledTimes(1)
})
it('should gracefully handle target folder', async () => {
Object.assign(action, {
hostname: 'github.com',
silent: false,
folder: '.',
branch: 'branch',
token: '123',
pusher: {},
clean: true,
targetFolder: 'new_folder',
commitMessage: 'Hello!',
isTest: TestFlag.NONE
})
await deploy(action)
expect(execute).toHaveBeenCalledTimes(12)
expect(rmRF).toHaveBeenCalledTimes(1)
expect(mkdirP).toHaveBeenCalledTimes(1)
})
it('should stop early if there is nothing to commit', async () => {
Object.assign(action, {
hostname: 'github.com',
silent: false,
folder: 'assets',
branch: 'branch',
token: '123',
pusher: {
name: 'asd',
email: 'as@cat'
},
isTest: TestFlag.NONE // Setting this flag to None means there will never be anything to commit and the action will exit early.
})
const response = await deploy(action)
expect(execute).toHaveBeenCalledTimes(12)
expect(rmRF).toHaveBeenCalledTimes(1)
expect(response).toBe(Status.SKIPPED)
})
it('should catch when a function throws an error', async () => {
;(execute as jest.Mock).mockImplementationOnce(() => {
throw new Error('Mocked throw')
})
Object.assign(action, {
hostname: 'github.com',
silent: false,
folder: 'assets',
branch: 'branch',
token: '123',
pusher: {
name: 'asd',
email: 'as@cat'
},
isTest: TestFlag.HAS_CHANGED_FILES
})
try {
await deploy(action)
} catch (error) {
expect(error instanceof Error && error.message).toBe(
'The deploy step encountered an error: Mocked throw ❌'
)
}
})
it('should execute commands if force is false and retry until limit is exceeded', async () => {
Object.assign(action, {
hostname: 'github.com',
silent: false,
folder: 'assets',
branch: 'branch',
force: false,
token: '123',
repositoryName: 'JamesIves/montezuma',
pusher: {
name: 'asd',
email: 'as@cat'
},
isTest: TestFlag.HAS_CHANGED_FILES
})
try {
await deploy(action)
} catch (error) {
expect(error instanceof Error && error.message).toBe(
'The deploy step encountered an error: Attempt limit exceeded ❌'
)
}
})
it('should add a tag to the commit', async () => {
Object.assign(action, {
hostname: 'github.com',
silent: false,
folder: 'assets',
branch: 'branch',
token: '123',
repositoryName: 'JamesIves/montezuma',
tag: 'v0.1',
pusher: {
name: 'asd',
email: 'as@cat'
},
isTest: TestFlag.HAS_CHANGED_FILES
})
const response = await deploy(action)
expect(execute).toHaveBeenCalledTimes(17)
expect(response).toBe(Status.SUCCESS)
})
})
})