-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgit-diff.test.js
More file actions
910 lines (738 loc) · 29.9 KB
/
git-diff.test.js
File metadata and controls
910 lines (738 loc) · 29.9 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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
import {gitDiff} from '../../src/functions/git-diff'
import * as execAsync from '../../src/functions/exec-async'
import * as core from '@actions/core'
import fs from 'fs'
const infoMock = core.info
const debugMock = core.debug
const setFailedMock = core.setFailed
const warningMock = core.warning
function buildGitArgs(
gitOptions = ['--no-color', '--full-index'],
baseBranch = 'HEAD^1',
searchPath = '.'
) {
return ['--no-pager', 'diff', ...gitOptions, baseBranch, '--', searchPath]
}
function gitArgvDebug(args) {
return `running git diff argv: ${JSON.stringify(['git', ...args])}`
}
beforeEach(() => {
jest.clearAllMocks()
process.env.INPUT_BASE_BRANCH = 'HEAD^1'
process.env.INPUT_SEARCH_PATH = '.'
process.env.INPUT_MAX_BUFFER_SIZE = '1000000'
process.env.INPUT_FILE_OUTPUT_ONLY = 'false'
process.env.INPUT_GIT_OPTIONS = '--no-color --full-index'
process.env.INPUT_GIT_DIFF_FILE = '__tests__/fixtures/main.diff'
process.env.GITHUB_WORKSPACE = process.cwd()
})
test('executes gitDiff', async () => {
const results = await gitDiff()
expect(results.files.length).toBe(5)
const firstFile = results.files[0]
expect(firstFile.path).toBe('custom-endpoints/nightbot.mjs')
const lastFile = results.files[results.files.length - 1]
expect(lastFile.path).toBe('utils/cache-machine.mjs')
expect(lastFile.type).toBe('DeletedFile')
expect(lastFile.chunks[0].changes[0].type).toBe('DeletedLine')
expect(lastFile.chunks[0].changes[0].content).toBe(`// cache url`)
expect(lastFile.chunks[0].changes[0].lineBefore).toBe(1)
expect(lastFile.chunks[0].changes[0]?.lineAfter).toBe(undefined)
expect(results.files[0].type).toBe('ChangedFile')
expect(results.files[0].chunks[0].changes[0].content).toBe(
`import { v4 as uuidv4 } from 'uuid';`
)
expect(results.files[0].chunks[0].changes[0].type).toBe('UnchangedLine')
expect(results.files[0].chunks[0].changes[2].content).toBe(
`import cacheMachine from '../utils/cache-machine.mjs';`
)
expect(results.files[0].chunks[0].changes[2].type).toBe('DeletedLine')
expect(infoMock).toHaveBeenCalledWith('🏃 starting the git-diff-action')
expect(infoMock).toHaveBeenCalledWith(
'🧮 total detected files changed (raw diff): 5'
)
expect(infoMock).toHaveBeenCalledWith(
'🧮 total detected files changed (json diff): 5'
)
})
test('executes gitDiff with binary files', async () => {
process.env.INPUT_GIT_DIFF_FILE = '__tests__/fixtures/with-binary-files.diff'
const results = await gitDiff()
const firstFile = results.files[0]
expect(firstFile.path).toBe('custom-endpoints/nightbot.mjs')
const lastFile = results.files[results.files.length - 1]
expect(lastFile.path).toBe('utils/cache-machine.mjs')
expect(results.files[0].type).toBe('ChangedFile')
expect(results.files[0].chunks[0].changes[0].content).toBe(
`import { v4 as uuidv4 } from 'uuid';`
)
expect(results.files[0].chunks[0].changes[0].type).toBe('UnchangedLine')
expect(results.files[0].chunks[0].changes[2].content).toBe(
`import cacheMachine from '../utils/cache-machine.mjs';`
)
expect(results.files[0].chunks[0].changes[2].type).toBe('DeletedLine')
expect(results.files.length).toBe(7)
expect(infoMock).toHaveBeenCalledWith('🏃 starting the git-diff-action')
expect(infoMock).toHaveBeenCalledWith(
expect.stringContaining('__tests__/fixtures/with-binary-files.diff')
)
expect(infoMock).toHaveBeenCalledWith(
'🧮 total detected files changed (raw diff): 7'
)
expect(infoMock).toHaveBeenCalledWith(
'🧮 total detected files changed (json diff): 7'
)
})
// this test case is a bug test
// there is an issue with the 'parseGitDiff' library where if the --binary flag is used, it will break the parsing
// it will still return "some" results, but it will break the parsing and return an incomplete set of results
test('executes gitDiff with binary files and --binary flag and breaks (bug test)', async () => {
process.env.INPUT_GIT_OPTIONS = '--no-color --full-index --binary'
process.env.INPUT_GIT_DIFF_FILE =
'__tests__/fixtures/with-binary-files-and-binary-flag.diff'
const results = await gitDiff()
const firstFile = results.files[0]
expect(firstFile.path).toBe('custom-endpoints/nightbot.mjs')
const lastFile = results.files[results.files.length - 1]
expect(lastFile.path).toBe('kv-cache.js')
expect(results.files.length).toBe(4)
expect(infoMock).toHaveBeenCalledWith('🏃 starting the git-diff-action')
expect(infoMock).toHaveBeenCalledWith(
expect.stringContaining(
'__tests__/fixtures/with-binary-files-and-binary-flag.diff'
)
)
// note that the total files changed is 7, but the json diff only has 4 files
expect(infoMock).toHaveBeenCalledWith(
'🧮 total detected files changed (raw diff): 7'
)
expect(infoMock).toHaveBeenCalledWith(
'🧮 total detected files changed (json diff): 4'
)
})
test('executes gitDiff by using the git binary', async () => {
process.env.INPUT_GIT_DIFF_FILE = 'false'
process.env.INPUT_MAX_BUFFER_SIZE = ''
process.env.INPUT_RAW_DIFF_FILE_OUTPUT = 'diff.txt'
process.env.INPUT_JSON_DIFF_FILE_OUTPUT = 'diff.json'
const diff = fs.readFileSync('__tests__/fixtures/main.diff', 'utf8')
jest.spyOn(execAsync, 'execFileAsync').mockImplementation(() => {
return {stdout: diff, stderr: null}
})
jest.spyOn(fs, 'writeFileSync').mockImplementation(() => {
return true
})
const results = await gitDiff()
expect(results.files.length).toBe(5)
const firstFile = results.files[0]
expect(firstFile.path).toBe('custom-endpoints/nightbot.mjs')
const lastFile = results.files[results.files.length - 1]
expect(lastFile.path).toBe('utils/cache-machine.mjs')
expect(lastFile.type).toBe('DeletedFile')
expect(lastFile.chunks[0].changes[0].type).toBe('DeletedLine')
expect(lastFile.chunks[0].changes[0].content).toBe(`// cache url`)
expect(lastFile.chunks[0].changes[0].lineBefore).toBe(1)
expect(lastFile.chunks[0].changes[0]?.lineAfter).toBe(undefined)
expect(results.files[0].type).toBe('ChangedFile')
expect(results.files[0].chunks[0].changes[0].content).toBe(
`import { v4 as uuidv4 } from 'uuid';`
)
expect(results.files[0].chunks[0].changes[0].type).toBe('UnchangedLine')
expect(results.files[0].chunks[0].changes[2].content).toBe(
`import cacheMachine from '../utils/cache-machine.mjs';`
)
expect(results.files[0].chunks[0].changes[2].type).toBe('DeletedLine')
expect(infoMock).toHaveBeenCalledWith(
'max_buffer_size is not defined, using default of 1000000'
)
expect(infoMock).toHaveBeenCalledWith(
'🧮 total detected files changed (raw diff): 5'
)
expect(infoMock).toHaveBeenCalledWith(
'🧮 total detected files changed (json diff): 5'
)
expect(debugMock).toHaveBeenCalledWith(gitArgvDebug(buildGitArgs()))
})
test('fails due to stderr being returned from the git binary', async () => {
process.env.INPUT_GIT_DIFF_FILE = 'false'
jest.spyOn(execAsync, 'execFileAsync').mockImplementation(() => {
return {stdout: '', stderr: 'oh no something went wrong'}
})
expect(await gitDiff()).toBe(undefined)
expect(setFailedMock).toHaveBeenCalledWith(
'git diff error: oh no something went wrong'
)
})
test('leaves a warning when --binary is used', async () => {
process.env.INPUT_GIT_DIFF_FILE = 'false'
process.env.INPUT_RAW_DIFF_FILE_OUTPUT = 'diff.txt'
process.env.INPUT_JSON_DIFF_FILE_OUTPUT = 'diff.json'
process.env.INPUT_GIT_OPTIONS = '--no-color --full-index --binary'
const diff = fs.readFileSync('__tests__/fixtures/main.diff', 'utf8')
jest.spyOn(execAsync, 'execFileAsync').mockImplementation(() => {
return {stdout: diff, stderr: null}
})
jest.spyOn(fs, 'writeFileSync').mockImplementation(() => {
return true
})
const results = await gitDiff()
expect(results.files.length).toBe(5)
expect(warningMock).toHaveBeenCalledWith(
'--binary flag is set, this may cause unexpected issues with the diff'
)
})
test('fails when no custom git diff file is found', async () => {
process.env.INPUT_GIT_DIFF_FILE = 'bad-file.txt'
expect(await gitDiff()).toBe(undefined)
expect(setFailedMock).toHaveBeenCalledWith(
expect.stringContaining('error getting git diff:')
)
})
test('executes gitDiff with file_output_only set to true', async () => {
process.env.INPUT_FILE_OUTPUT_ONLY = 'true'
const setOutputMock = core.setOutput
const results = await gitDiff()
expect(results.files.length).toBe(5)
// Verify that setOutput is not called for raw-diff and json-diff when file_output_only is true
expect(setOutputMock).not.toHaveBeenCalledWith('raw-diff', expect.anything())
expect(setOutputMock).not.toHaveBeenCalledWith('json-diff', expect.anything())
})
test('executes gitDiff with file_output_only true and file outputs', async () => {
process.env.INPUT_FILE_OUTPUT_ONLY = 'true'
process.env.INPUT_RAW_DIFF_FILE_OUTPUT = 'test-raw.txt'
process.env.INPUT_JSON_DIFF_FILE_OUTPUT = 'test-json.json'
const setOutputMock = core.setOutput
jest.spyOn(fs, 'writeFileSync').mockImplementation(() => {
return true
})
const results = await gitDiff()
expect(results.files.length).toBe(5)
// Verify that setOutput is not called for raw-diff and json-diff when file_output_only is true
expect(setOutputMock).not.toHaveBeenCalledWith('raw-diff', expect.anything())
expect(setOutputMock).not.toHaveBeenCalledWith('json-diff', expect.anything())
// But should be called for file paths
expect(setOutputMock).toHaveBeenCalledWith('raw-diff-path', 'test-raw.txt')
expect(setOutputMock).toHaveBeenCalledWith('json-diff-path', 'test-json.json')
})
// Edge case tests for search_path input option
test('executes gitDiff with specific search_path directory', async () => {
process.env.INPUT_SEARCH_PATH = 'src/'
process.env.INPUT_GIT_DIFF_FILE = 'false'
const diff = fs.readFileSync('__tests__/fixtures/main.diff', 'utf8')
jest.spyOn(execAsync, 'execFileAsync').mockImplementation(() => {
return {stdout: diff, stderr: null}
})
const results = await gitDiff()
expect(results.files.length).toBe(5)
expect(debugMock).toHaveBeenCalledWith('search_path: src/')
expect(debugMock).toHaveBeenCalledWith(
gitArgvDebug(buildGitArgs(['--no-color', '--full-index'], 'HEAD^1', 'src/'))
)
})
test('executes gitDiff with glob pattern search_path', async () => {
process.env.INPUT_SEARCH_PATH = '**/*.js'
process.env.INPUT_GIT_DIFF_FILE = 'false'
const diff = fs.readFileSync('__tests__/fixtures/main.diff', 'utf8')
jest.spyOn(execAsync, 'execFileAsync').mockImplementation(() => {
return {stdout: diff, stderr: null}
})
const results = await gitDiff()
expect(results.files.length).toBe(5)
expect(debugMock).toHaveBeenCalledWith('search_path: **/*.js')
expect(debugMock).toHaveBeenCalledWith(
gitArgvDebug(
buildGitArgs(['--no-color', '--full-index'], 'HEAD^1', '**/*.js')
)
)
})
test('executes gitDiff with specific file search_path', async () => {
process.env.INPUT_SEARCH_PATH = 'README.md'
process.env.INPUT_GIT_DIFF_FILE = 'false'
const diff = fs.readFileSync('__tests__/fixtures/main.diff', 'utf8')
jest.spyOn(execAsync, 'execFileAsync').mockImplementation(() => {
return {stdout: diff, stderr: null}
})
const results = await gitDiff()
expect(results.files.length).toBe(5)
expect(debugMock).toHaveBeenCalledWith('search_path: README.md')
expect(debugMock).toHaveBeenCalledWith(
gitArgvDebug(
buildGitArgs(['--no-color', '--full-index'], 'HEAD^1', 'README.md')
)
)
})
// Edge case tests for max_buffer_size input option
test('executes gitDiff with custom max_buffer_size', async () => {
process.env.INPUT_MAX_BUFFER_SIZE = '5000000'
process.env.INPUT_GIT_DIFF_FILE = 'false'
const diff = fs.readFileSync('__tests__/fixtures/main.diff', 'utf8')
const execAsyncSpy = jest
.spyOn(execAsync, 'execFileAsync')
.mockImplementation(() => {
return {stdout: diff, stderr: null}
})
const results = await gitDiff()
expect(results.files.length).toBe(5)
expect(debugMock).toHaveBeenCalledWith('max_buffer_size: 5000000')
expect(execAsyncSpy).toHaveBeenCalledWith('git', buildGitArgs(), {
maxBuffer: 5000000
})
})
test('executes gitDiff with invalid max_buffer_size (NaN)', async () => {
process.env.INPUT_MAX_BUFFER_SIZE = 'invalid'
process.env.INPUT_GIT_DIFF_FILE = 'false'
const diff = fs.readFileSync('__tests__/fixtures/main.diff', 'utf8')
const execAsyncSpy = jest
.spyOn(execAsync, 'execFileAsync')
.mockImplementation(() => {
return {stdout: diff, stderr: null}
})
const results = await gitDiff()
expect(results.files.length).toBe(5)
expect(debugMock).toHaveBeenCalledWith('max_buffer_size: NaN')
expect(infoMock).toHaveBeenCalledWith(
'max_buffer_size is not defined, using default of 1000000'
)
expect(execAsyncSpy).toHaveBeenCalledWith('git', buildGitArgs(), {
maxBuffer: 1000000
})
})
test('executes gitDiff with zero max_buffer_size', async () => {
process.env.INPUT_MAX_BUFFER_SIZE = '0'
process.env.INPUT_GIT_DIFF_FILE = 'false'
const diff = fs.readFileSync('__tests__/fixtures/main.diff', 'utf8')
const execAsyncSpy = jest
.spyOn(execAsync, 'execFileAsync')
.mockImplementation(() => {
return {stdout: diff, stderr: null}
})
const results = await gitDiff()
expect(results.files.length).toBe(5)
expect(debugMock).toHaveBeenCalledWith('max_buffer_size: 0')
expect(execAsyncSpy).toHaveBeenCalledWith('git', buildGitArgs(), {
maxBuffer: 0
})
})
// Edge case tests for git_options input option
test('executes gitDiff with custom git_options', async () => {
process.env.INPUT_GIT_OPTIONS = '--no-color --name-only'
process.env.INPUT_GIT_DIFF_FILE = 'false'
const diff = fs.readFileSync('__tests__/fixtures/main.diff', 'utf8')
const execAsyncSpy = jest
.spyOn(execAsync, 'execFileAsync')
.mockImplementation(() => {
return {stdout: diff, stderr: null}
})
const results = await gitDiff()
expect(results.files.length).toBe(5)
expect(debugMock).toHaveBeenCalledWith('git_options: --no-color --name-only')
expect(execAsyncSpy).toHaveBeenCalledWith(
'git',
buildGitArgs(['--no-color', '--name-only']),
{maxBuffer: 1000000}
)
})
test('executes gitDiff with minimal git_options', async () => {
process.env.INPUT_GIT_OPTIONS = ''
process.env.INPUT_GIT_DIFF_FILE = 'false'
const diff = fs.readFileSync('__tests__/fixtures/main.diff', 'utf8')
const execAsyncSpy = jest
.spyOn(execAsync, 'execFileAsync')
.mockImplementation(() => {
return {stdout: diff, stderr: null}
})
const results = await gitDiff()
expect(results.files.length).toBe(5)
expect(debugMock).toHaveBeenCalledWith('git_options: ')
expect(execAsyncSpy).toHaveBeenCalledWith('git', buildGitArgs([]), {
maxBuffer: 1000000
})
})
test('normalizes git_options with leading and repeated whitespace', async () => {
process.env.INPUT_GIT_OPTIONS = ' --no-color --name-only '
process.env.INPUT_GIT_DIFF_FILE = 'false'
const diff = fs.readFileSync('__tests__/fixtures/main.diff', 'utf8')
const execAsyncSpy = jest
.spyOn(execAsync, 'execFileAsync')
.mockImplementation(() => {
return {stdout: diff, stderr: null}
})
const results = await gitDiff()
expect(results.files.length).toBe(5)
expect(execAsyncSpy).toHaveBeenCalledWith(
'git',
buildGitArgs(['--no-color', '--name-only']),
{maxBuffer: 1000000}
)
})
test('executes gitDiff with complex git_options', async () => {
process.env.INPUT_GIT_OPTIONS = '--no-color --full-index --find-renames'
process.env.INPUT_GIT_DIFF_FILE = 'false'
const diff = fs.readFileSync('__tests__/fixtures/main.diff', 'utf8')
const execAsyncSpy = jest
.spyOn(execAsync, 'execFileAsync')
.mockImplementation(() => {
return {stdout: diff, stderr: null}
})
const results = await gitDiff()
expect(results.files.length).toBe(5)
expect(debugMock).toHaveBeenCalledWith(
'git_options: --no-color --full-index --find-renames'
)
expect(execAsyncSpy).toHaveBeenCalledWith(
'git',
buildGitArgs(['--no-color', '--full-index', '--find-renames']),
{maxBuffer: 1000000}
)
})
test('passes potentially dangerous git_options as argv instead of a shell string', async () => {
process.env.INPUT_GIT_OPTIONS = '--no-color && touch /tmp/pwned'
process.env.INPUT_GIT_DIFF_FILE = 'false'
const diff = fs.readFileSync('__tests__/fixtures/main.diff', 'utf8')
const execAsyncSpy = jest
.spyOn(execAsync, 'execFileAsync')
.mockImplementation(() => {
return {stdout: diff, stderr: null}
})
const results = await gitDiff()
expect(results.files.length).toBe(5)
expect(execAsyncSpy).toHaveBeenCalledWith(
'git',
buildGitArgs(['--no-color', '&&', 'touch', '/tmp/pwned']),
{maxBuffer: 1000000}
)
})
test('supports quoted git_options without invoking a shell', async () => {
process.env.INPUT_GIT_OPTIONS = '--word-diff-regex=\"foo bar\" --no-color'
process.env.INPUT_GIT_DIFF_FILE = 'false'
const diff = fs.readFileSync('__tests__/fixtures/main.diff', 'utf8')
const execAsyncSpy = jest
.spyOn(execAsync, 'execFileAsync')
.mockImplementation(() => {
return {stdout: diff, stderr: null}
})
const results = await gitDiff()
expect(results.files.length).toBe(5)
expect(execAsyncSpy).toHaveBeenCalledWith(
'git',
buildGitArgs(['--word-diff-regex=foo bar', '--no-color']),
{maxBuffer: 1000000}
)
})
test('ignores empty quoted git_options tokens', async () => {
process.env.INPUT_GIT_OPTIONS = '"" --no-color'
process.env.INPUT_GIT_DIFF_FILE = 'false'
const diff = fs.readFileSync('__tests__/fixtures/main.diff', 'utf8')
const execAsyncSpy = jest
.spyOn(execAsync, 'execFileAsync')
.mockImplementation(() => {
return {stdout: diff, stderr: null}
})
const results = await gitDiff()
expect(results.files.length).toBe(5)
expect(execAsyncSpy).toHaveBeenCalledWith(
'git',
buildGitArgs(['--no-color']),
{maxBuffer: 1000000}
)
})
test('supports single-quoted git_options without invoking a shell', async () => {
process.env.INPUT_GIT_OPTIONS = "--word-diff-regex='foo bar' --no-color"
process.env.INPUT_GIT_DIFF_FILE = 'false'
const diff = fs.readFileSync('__tests__/fixtures/main.diff', 'utf8')
const execAsyncSpy = jest
.spyOn(execAsync, 'execFileAsync')
.mockImplementation(() => {
return {stdout: diff, stderr: null}
})
const results = await gitDiff()
expect(results.files.length).toBe(5)
expect(execAsyncSpy).toHaveBeenCalledWith(
'git',
buildGitArgs(['--word-diff-regex=foo bar', '--no-color']),
{maxBuffer: 1000000}
)
})
test('ignores git_options that only contain an empty quoted token', async () => {
process.env.INPUT_GIT_OPTIONS = '""'
process.env.INPUT_GIT_DIFF_FILE = 'false'
const diff = fs.readFileSync('__tests__/fixtures/main.diff', 'utf8')
const execAsyncSpy = jest
.spyOn(execAsync, 'execFileAsync')
.mockImplementation(() => {
return {stdout: diff, stderr: null}
})
const results = await gitDiff()
expect(results.files.length).toBe(5)
expect(execAsyncSpy).toHaveBeenCalledWith('git', buildGitArgs([]), {
maxBuffer: 1000000
})
})
test('supports escaped characters in git_options without invoking a shell', async () => {
process.env.INPUT_GIT_OPTIONS = '--word-diff-regex="foo\\ bar" --no-color'
process.env.INPUT_GIT_DIFF_FILE = 'false'
const diff = fs.readFileSync('__tests__/fixtures/main.diff', 'utf8')
const execAsyncSpy = jest
.spyOn(execAsync, 'execFileAsync')
.mockImplementation(() => {
return {stdout: diff, stderr: null}
})
const results = await gitDiff()
expect(results.files.length).toBe(5)
expect(execAsyncSpy).toHaveBeenCalledWith(
'git',
buildGitArgs(['--word-diff-regex=foo bar', '--no-color']),
{maxBuffer: 1000000}
)
})
test('fails when git_options has an unterminated quote', async () => {
process.env.INPUT_GIT_OPTIONS = '--no-color \"unterminated'
process.env.INPUT_GIT_DIFF_FILE = 'false'
expect(await gitDiff()).toBe(undefined)
expect(setFailedMock).toHaveBeenCalledWith(
'error getting git diff: Error: git_options contains an unterminated quoted value'
)
})
test('fails when git_options has an incomplete escape sequence', async () => {
process.env.INPUT_GIT_OPTIONS = '--no-color ' + '\\'
process.env.INPUT_GIT_DIFF_FILE = 'false'
expect(await gitDiff()).toBe(undefined)
expect(setFailedMock).toHaveBeenCalledWith(
'error getting git diff: Error: git_options ends with an incomplete escape sequence'
)
})
// Edge case tests for git_diff_file input option
test('executes gitDiff with empty diff file', async () => {
process.env.INPUT_GIT_DIFF_FILE = '__tests__/fixtures/empty.diff'
const results = await gitDiff()
expect(results.files.length).toBe(0)
expect(infoMock).toHaveBeenCalledWith(
expect.stringContaining('__tests__/fixtures/empty.diff')
)
expect(infoMock).toHaveBeenCalledWith(
'🧮 total detected files changed (raw diff): 0'
)
expect(infoMock).toHaveBeenCalledWith(
'🧮 total detected files changed (json diff): 0'
)
})
test('executes gitDiff with special characters in diff file', async () => {
process.env.INPUT_GIT_DIFF_FILE = '__tests__/fixtures/special-chars.diff'
const results = await gitDiff()
expect(results.files.length).toBe(1)
expect(results.files[0].path).toBe('test-file-with-special-chars.txt')
expect(results.files[0].type).toBe('AddedFile')
expect(infoMock).toHaveBeenCalledWith(
expect.stringContaining('__tests__/fixtures/special-chars.diff')
)
expect(infoMock).toHaveBeenCalledWith(
'🧮 total detected files changed (raw diff): 1'
)
expect(infoMock).toHaveBeenCalledWith(
'🧮 total detected files changed (json diff): 1'
)
})
test('executes gitDiff with renames and mode changes', async () => {
process.env.INPUT_GIT_DIFF_FILE = '__tests__/fixtures/rename-and-mode.diff'
const results = await gitDiff()
// The parser may not handle all git diff types equally, so we check what we actually get
expect(results.files.length).toBeGreaterThanOrEqual(1)
expect(infoMock).toHaveBeenCalledWith(
expect.stringContaining('__tests__/fixtures/rename-and-mode.diff')
)
expect(infoMock).toHaveBeenCalledWith(
'🧮 total detected files changed (raw diff): 2'
)
// JSON diff may parse differently than raw diff for complex changes
expect(infoMock).toHaveBeenCalledWith(
expect.stringMatching(/🧮 total detected files changed \(json diff\): [12]/)
)
})
test('handles file read error when git_diff_file does not exist', async () => {
process.env.INPUT_GIT_DIFF_FILE = '__tests__/fixtures/nonexistent.diff'
expect(await gitDiff()).toBe(undefined)
expect(setFailedMock).toHaveBeenCalledWith(
expect.stringContaining('error getting git diff:')
)
})
test('rejects git_diff_file paths outside the workspace', async () => {
process.env.INPUT_GIT_DIFF_FILE = process.execPath
expect(await gitDiff()).toBe(undefined)
expect(setFailedMock).toHaveBeenCalledWith(
'error getting git diff: Error: git_diff_file must resolve to a file inside the GitHub workspace'
)
})
test('resolves git_diff_file relative to the current directory when GITHUB_WORKSPACE is unset', async () => {
delete process.env.GITHUB_WORKSPACE
process.env.INPUT_GIT_DIFF_FILE = '__tests__/fixtures/main.diff'
const results = await gitDiff()
expect(results.files.length).toBe(5)
expect(infoMock).toHaveBeenCalledWith(
expect.stringContaining('__tests__/fixtures/main.diff')
)
})
// Edge case tests for base_branch input option
test('executes gitDiff with custom base_branch', async () => {
process.env.INPUT_BASE_BRANCH = 'main'
process.env.INPUT_GIT_DIFF_FILE = 'false'
const diff = fs.readFileSync('__tests__/fixtures/main.diff', 'utf8')
jest.spyOn(execAsync, 'execFileAsync').mockImplementation(() => {
return {stdout: diff, stderr: null}
})
const results = await gitDiff()
expect(results.files.length).toBe(5)
expect(debugMock).toHaveBeenCalledWith('base_branch: main')
expect(debugMock).toHaveBeenCalledWith(
gitArgvDebug(buildGitArgs(['--no-color', '--full-index'], 'main'))
)
})
test('executes gitDiff with SHA base_branch', async () => {
process.env.INPUT_BASE_BRANCH = 'a1b2c3d'
process.env.INPUT_GIT_DIFF_FILE = 'false'
const diff = fs.readFileSync('__tests__/fixtures/main.diff', 'utf8')
jest.spyOn(execAsync, 'execFileAsync').mockImplementation(() => {
return {stdout: diff, stderr: null}
})
const results = await gitDiff()
expect(results.files.length).toBe(5)
expect(debugMock).toHaveBeenCalledWith('base_branch: a1b2c3d')
expect(debugMock).toHaveBeenCalledWith(
gitArgvDebug(buildGitArgs(['--no-color', '--full-index'], 'a1b2c3d'))
)
})
// Error handling and edge case tests
test('handles file system errors when writing output files', async () => {
process.env.INPUT_RAW_DIFF_FILE_OUTPUT = '/invalid/path/raw.txt'
process.env.INPUT_JSON_DIFF_FILE_OUTPUT = '/invalid/path/json.json'
jest.spyOn(fs, 'writeFileSync').mockImplementation(() => {
throw new Error('ENOENT: no such file or directory')
})
expect(await gitDiff()).toBe(undefined)
expect(setFailedMock).toHaveBeenCalledWith(
expect.stringContaining('error getting git diff:')
)
})
test('handles git command timeout/error', async () => {
process.env.INPUT_GIT_DIFF_FILE = 'false'
process.env.INPUT_MAX_BUFFER_SIZE = '100' // Very small buffer to potentially cause issues
jest.spyOn(execAsync, 'execFileAsync').mockImplementation(() => {
throw new Error('maxBuffer exceeded')
})
expect(await gitDiff()).toBe(undefined)
expect(setFailedMock).toHaveBeenCalledWith(
'error getting git diff: Error: maxBuffer exceeded'
)
})
test('executes gitDiff with very large max_buffer_size', async () => {
process.env.INPUT_MAX_BUFFER_SIZE = '999999999'
process.env.INPUT_GIT_DIFF_FILE = 'false'
process.env.INPUT_RAW_DIFF_FILE_OUTPUT = ''
process.env.INPUT_JSON_DIFF_FILE_OUTPUT = ''
const diff = fs.readFileSync('__tests__/fixtures/main.diff', 'utf8')
const execAsyncSpy = jest
.spyOn(execAsync, 'execFileAsync')
.mockImplementation(() => {
return {stdout: diff, stderr: null}
})
const results = await gitDiff()
expect(results).toBeDefined()
expect(results.files.length).toBe(5)
expect(debugMock).toHaveBeenCalledWith('max_buffer_size: 999999999')
expect(execAsyncSpy).toHaveBeenCalledWith('git', buildGitArgs(), {
maxBuffer: 999999999
})
})
// Real-world scenario tests
test('executes gitDiff with large diff file', async () => {
process.env.INPUT_GIT_DIFF_FILE = '__tests__/fixtures/large.diff'
const results = await gitDiff()
expect(results).toBeDefined()
expect(results.files.length).toBeGreaterThan(5) // Should have more files than the base test
expect(infoMock).toHaveBeenCalledWith(
expect.stringContaining('__tests__/fixtures/large.diff')
)
expect(infoMock).toHaveBeenCalledWith(
expect.stringMatching(/🧮 total detected files changed \(raw diff\): \d+/)
)
expect(infoMock).toHaveBeenCalledWith(
expect.stringMatching(/🧮 total detected files changed \(json diff\): \d+/)
)
})
test('executes gitDiff with all file outputs and options', async () => {
process.env.INPUT_GIT_DIFF_FILE = 'false'
process.env.INPUT_RAW_DIFF_FILE_OUTPUT = 'test-output.raw'
process.env.INPUT_JSON_DIFF_FILE_OUTPUT = 'test-output.json'
process.env.INPUT_FILE_OUTPUT_ONLY = 'false'
process.env.INPUT_SEARCH_PATH = 'src/**/*.js'
process.env.INPUT_GIT_OPTIONS = '--no-color --stat'
process.env.INPUT_MAX_BUFFER_SIZE = '2000000'
process.env.INPUT_BASE_BRANCH = 'origin/main'
const diff = fs.readFileSync('__tests__/fixtures/main.diff', 'utf8')
const execAsyncSpy = jest
.spyOn(execAsync, 'execFileAsync')
.mockImplementation(() => {
return {stdout: diff, stderr: null}
})
const writeFileSyncSpy = jest
.spyOn(fs, 'writeFileSync')
.mockImplementation(() => {
return true
})
const setOutputSpy = core.setOutput
const results = await gitDiff()
expect(results.files.length).toBe(5)
// Verify all inputs were processed correctly
expect(debugMock).toHaveBeenCalledWith('base_branch: origin/main')
expect(debugMock).toHaveBeenCalledWith('search_path: src/**/*.js')
expect(debugMock).toHaveBeenCalledWith('max_buffer_size: 2000000')
expect(debugMock).toHaveBeenCalledWith('git_options: --no-color --stat')
// Verify git command was built correctly
expect(execAsyncSpy).toHaveBeenCalledWith(
'git',
buildGitArgs(['--no-color', '--stat'], 'origin/main', 'src/**/*.js'),
{maxBuffer: 2000000}
)
// Verify files were written
expect(writeFileSyncSpy).toHaveBeenCalledWith('test-output.raw', diff)
expect(writeFileSyncSpy).toHaveBeenCalledWith(
'test-output.json',
expect.any(String)
)
// Verify outputs were set (since file_output_only is false)
expect(setOutputSpy).toHaveBeenCalledWith('raw-diff', diff)
expect(setOutputSpy).toHaveBeenCalledWith('json-diff', expect.any(String))
expect(setOutputSpy).toHaveBeenCalledWith('raw-diff-path', 'test-output.raw')
expect(setOutputSpy).toHaveBeenCalledWith(
'json-diff-path',
'test-output.json'
)
})
test('handles malformed diff content gracefully', async () => {
process.env.INPUT_GIT_DIFF_FILE = '__tests__/fixtures/empty.diff'
const results = await gitDiff()
expect(results.files.length).toBe(0)
expect(results.type).toBe('GitDiff')
// Should complete successfully even with empty diff
expect(infoMock).toHaveBeenCalledWith(
'✅ git-diff-action completed successfully'
)
})
// Test to verify the constant values are used correctly
test('uses correct default max buffer size constant', async () => {
process.env.INPUT_MAX_BUFFER_SIZE = ''
process.env.INPUT_GIT_DIFF_FILE = 'false'
const diff = fs.readFileSync('__tests__/fixtures/main.diff', 'utf8')
const execAsyncSpy = jest
.spyOn(execAsync, 'execFileAsync')
.mockImplementation(() => {
return {stdout: diff, stderr: null}
})
const results = await gitDiff()
expect(results.files.length).toBe(5)
expect(infoMock).toHaveBeenCalledWith(
'max_buffer_size is not defined, using default of 1000000'
)
expect(execAsyncSpy).toHaveBeenCalledWith('git', buildGitArgs(), {
maxBuffer: 1000000
})
})