-
-
Notifications
You must be signed in to change notification settings - Fork 175
/
test_publisher.py
1982 lines (1692 loc) · 105 KB
/
test_publisher.py
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
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import tempfile
import unittest
from collections.abc import Collection
from datetime import datetime, timezone
import github.CheckRun
import mock
from github import Github, GithubException
from publish import *
from publish.github_action import GithubAction
from publish.publisher import Publisher, Settings, PublishData
from publish.unittestresults import UnitTestCase, ParseError
errors = [ParseError('file', 'error', 1, 2)]
class TestPublisher(unittest.TestCase):
@staticmethod
def create_github_collection(collection: Collection) -> mock.Mock:
mocked = mock.MagicMock()
mocked.totalCount = len(collection)
mocked.__iter__ = mock.Mock(side_effect=collection.__iter__)
return mocked
@staticmethod
def create_github_pr(repo: str,
base_commit_sha: Optional[str] = 'base',
head_commit_sha: Optional[str] = 'head',
merge_commit_sha: Optional[str] = 'merge',
number: Optional[int] = None,
state: str = 'open'):
pr = mock.MagicMock()
pr.as_pull_request = mock.Mock(return_value=pr)
pr.base.repo.full_name = repo
pr.base.sha = base_commit_sha
pr.number = number
pr.state = state
pr.head.sha = head_commit_sha
pr.merge_commit_sha = merge_commit_sha
return pr
@staticmethod
def create_settings(comment_mode=comment_mode_create,
job_summary=True,
compare_earlier=True,
hide_comment_mode=hide_comments_mode_off,
report_individual_runs=False,
dedup_classes_by_file_name=False,
check_run_annotation=default_annotations,
event: Optional[dict] = {'before': 'before'},
event_name: str = 'event name',
json_file: Optional[str] = None,
pull_request_build: str = pull_request_build_mode_merge,
test_changes_limit: Optional[int] = 5):
return Settings(
token=None,
api_url='https://the-github-api-url',
graphql_url='https://the-github-graphql-url',
api_retries=1,
event=event,
event_file=None,
event_name=event_name,
repo='owner/repo',
commit='commit',
json_file=json_file,
fail_on_errors=True,
fail_on_failures=True,
junit_files_glob='*.xml',
time_factor=1.0,
check_name='Check Name',
comment_title='Comment Title',
comment_mode=comment_mode,
job_summary=job_summary,
compare_earlier=compare_earlier,
pull_request_build=pull_request_build,
test_changes_limit=test_changes_limit,
hide_comment_mode=hide_comment_mode,
report_individual_runs=report_individual_runs,
dedup_classes_by_file_name=dedup_classes_by_file_name,
ignore_runs=False,
check_run_annotation=check_run_annotation,
seconds_between_github_reads=1.5,
seconds_between_github_writes=2.5
)
stats = UnitTestRunResults(
files=1,
errors=[],
suites=2,
duration=3,
tests=22,
tests_succ=4,
tests_skip=5,
tests_fail=6,
tests_error=7,
runs=38,
runs_succ=8,
runs_skip=9,
runs_fail=10,
runs_error=11,
commit='commit'
)
def create_mocks(self,
repo_name: Optional[str] = None,
repo_login: Optional[str] = None,
commit: Optional[mock.Mock] = mock.MagicMock(),
digest: Optional[str] = None,
check_names: List[str] = None):
gh = mock.MagicMock(Github)
gh._Github__requester = mock.MagicMock()
gha = mock.MagicMock(GithubAction)
repo = mock.MagicMock()
# have repo.create_check_run return the arguments given to it
def create_check_run_hook(**kwargs) -> Mapping[str, Any]:
return mock.MagicMock(html_url='mock url', create_check_run_kwargs=kwargs)
repo.create_check_run = mock.Mock(side_effect=create_check_run_hook)
if commit:
runs = []
if digest and check_names:
for check_name in check_names:
run = mock.MagicMock()
run.name = check_name
check_run_output = mock.MagicMock(summary='summary\n{}{}'.format(digest_header, digest))
run.output = check_run_output
runs.append(run)
check_runs = self.create_github_collection(runs)
commit.get_check_runs = mock.Mock(return_value=check_runs)
repo.get_commit = mock.Mock(return_value=commit)
repo.owner.login = repo_login
repo.name = repo_name
gh.get_repo = mock.Mock(return_value=repo)
return gh, gha, gh._Github__requester, repo, commit
cases = UnitTestCaseResults([
((None, 'class', 'test'), dict(
success=[
UnitTestCase(
result_file='result file', test_file='test file', line=0,
class_name='class', test_name='test',
result='success', message=None, content=None,
time=1.2
)
],
failure=[
UnitTestCase(
result_file='result file', test_file='test file', line=0,
class_name='class', test_name='test',
result='failure', message='message', content='content',
time=1.234
)
]
)),
((None, 'class', 'test2'), dict(
skipped=[
UnitTestCase(
result_file='result file', test_file='test file', line=0,
class_name='class', test_name='test2',
result='skipped', message='skipped', content=None,
time=None
)
],
error=[
UnitTestCase(
result_file='result file', test_file='test file', line=0,
class_name='class', test_name='test2',
result='error', message='error message', content='error content',
time=1.2345
)
]
)),
((None, 'class', 'test3'), dict(
skipped=[
UnitTestCase(
result_file='result file', test_file='test file', line=0,
class_name='class', test_name='test3',
result='skipped', message='skipped', content=None,
time=None
)
]
))
])
@staticmethod
def get_stats(base: str) -> UnitTestRunResults:
return UnitTestRunResults(
files=1,
errors=[],
suites=2,
duration=3,
tests=21,
tests_succ=12,
tests_skip=4,
tests_fail=2,
tests_error=3,
runs=37,
runs_succ=25,
runs_skip=7,
runs_fail=4,
runs_error=1,
commit=base
)
# makes gzipped digest deterministic
with mock.patch('gzip.time.time', return_value=0):
base_digest = get_digest_from_stats(get_stats.__func__('base'))
past_digest = get_digest_from_stats(get_stats.__func__('past'))
@staticmethod
def call_mocked_publish(settings: Settings,
stats: UnitTestRunResults = stats,
cases: UnitTestCaseResults = cases,
prs: List[object] = [],
cr: object = None):
# UnitTestCaseResults is mutable, always copy it
cases = UnitTestCaseResults(cases)
# mock Publisher and call publish
publisher = mock.MagicMock(Publisher)
publisher._settings = settings
publisher.get_pulls = mock.Mock(return_value=prs)
publisher.publish_check = mock.Mock(return_value=(cr, None))
Publisher.publish(publisher, stats, cases, 'success')
# return calls to mocked instance, except call to _logger
mock_calls = [(call[0], call.args, call.kwargs)
for call in publisher.mock_calls
if not call[0].startswith('_logger.')]
return mock_calls
def test_get_test_list_annotations(self):
cases = UnitTestCaseResults([
((None, 'class', 'test abcd'), {'success': [None]}),
((None, 'class', 'test efgh'), {'skipped': [None]}),
((None, 'class', 'test ijkl'), {'skipped': [None]}),
])
settings = self.create_settings(check_run_annotation=[all_tests_list, skipped_tests_list])
gh = mock.MagicMock()
publisher = Publisher(settings, gh, None)
annotations = publisher.get_test_list_annotations(cases, max_chunk_size=42)
self.assertEqual([
Annotation(path='.github', start_line=0, end_line=0, start_column=None, end_column=None, annotation_level='notice', message='There are 2 skipped tests, see "Raw output" for the full list of skipped tests.', title='2 skipped tests found', raw_details='class ‑ test efgh\nclass ‑ test ijkl'),
Annotation(path='.github', start_line=0, end_line=0, start_column=None, end_column=None, annotation_level='notice', message='There are 3 tests, see "Raw output" for the list of tests 1 to 2.', title='3 tests found (test 1 to 2)', raw_details='class ‑ test abcd\nclass ‑ test efgh'),
Annotation(path='.github', start_line=0, end_line=0, start_column=None, end_column=None, annotation_level='notice', message='There are 3 tests, see "Raw output" for the list of tests 3 to 3.', title='3 tests found (test 3 to 3)', raw_details='class ‑ test ijkl')
], annotations)
def test_get_test_list_annotations_chunked_and_restricted_unicode(self):
cases = UnitTestCaseResults([
((None, 'class', 'test 𝒂'), {'success': [None]}),
((None, 'class', 'test 𝒃'), {'skipped': [None]}),
((None, 'class', 'test 𝒄'), {'skipped': [None]}),
])
settings = self.create_settings(check_run_annotation=[all_tests_list, skipped_tests_list])
gh = mock.MagicMock()
publisher = Publisher(settings, gh, None)
annotations = publisher.get_test_list_annotations(cases, max_chunk_size=42)
self.assertEqual([
Annotation(path='.github', start_line=0, end_line=0, start_column=None, end_column=None, annotation_level='notice', message='There are 2 skipped tests, see "Raw output" for the list of skipped tests 1 to 1.', title='2 skipped tests found (test 1 to 1)', raw_details='class ‑ test \\U0001d483'),
Annotation(path='.github', start_line=0, end_line=0, start_column=None, end_column=None, annotation_level='notice', message='There are 2 skipped tests, see "Raw output" for the list of skipped tests 2 to 2.', title='2 skipped tests found (test 2 to 2)', raw_details='class ‑ test \\U0001d484'),
Annotation(path='.github', start_line=0, end_line=0, start_column=None, end_column=None, annotation_level='notice', message='There are 3 tests, see "Raw output" for the list of tests 1 to 1.', title='3 tests found (test 1 to 1)', raw_details='class ‑ test \\U0001d482'),
Annotation(path='.github', start_line=0, end_line=0, start_column=None, end_column=None, annotation_level='notice', message='There are 3 tests, see "Raw output" for the list of tests 2 to 2.', title='3 tests found (test 2 to 2)', raw_details='class ‑ test \\U0001d483'),
Annotation(path='.github', start_line=0, end_line=0, start_column=None, end_column=None, annotation_level='notice', message='There are 3 tests, see "Raw output" for the list of tests 3 to 3.', title='3 tests found (test 3 to 3)', raw_details='class ‑ test \\U0001d484')
], annotations)
def test_publish_without_comment(self):
settings = self.create_settings(comment_mode=comment_mode_off, hide_comment_mode=hide_comments_mode_off)
mock_calls = self.call_mocked_publish(settings, prs=[object()])
self.assertEqual(2, len(mock_calls))
(method, args, kwargs) = mock_calls[0]
self.assertEqual('publish_check', method)
self.assertEqual((self.stats, self.cases, 'success'), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[1]
self.assertEqual('publish_job_summary', method)
self.assertEqual((settings.comment_title, self.stats, None, None), args)
self.assertEqual({}, kwargs)
def test_publish_without_job_summary_and_comment(self):
settings = self.create_settings(comment_mode=comment_mode_off, hide_comment_mode=hide_comments_mode_off, job_summary=False)
mock_calls = self.call_mocked_publish(settings, prs=[object()])
self.assertEqual(1, len(mock_calls))
(method, args, kwargs) = mock_calls[0]
self.assertEqual('publish_check', method)
self.assertEqual((self.stats, self.cases, 'success'), args)
self.assertEqual({}, kwargs)
def test_publish_without_comment_with_hiding(self):
settings = self.create_settings(comment_mode=comment_mode_off, hide_comment_mode=hide_comments_mode_all_but_latest)
mock_calls = self.call_mocked_publish(settings, prs=[object()])
self.assertEqual(2, len(mock_calls))
(method, args, kwargs) = mock_calls[0]
self.assertEqual('publish_check', method)
self.assertEqual((self.stats, self.cases, 'success'), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[1]
self.assertEqual('publish_job_summary', method)
self.assertEqual((settings.comment_title, self.stats, None, None), args)
self.assertEqual({}, kwargs)
def test_publish_with_comment_without_pr(self):
settings = self.create_settings(comment_mode=comment_mode_create, hide_comment_mode=hide_comments_mode_off)
mock_calls = self.call_mocked_publish(settings, prs=[])
self.assertEqual(3, len(mock_calls))
(method, args, kwargs) = mock_calls[0]
self.assertEqual('publish_check', method)
self.assertEqual((self.stats, self.cases, 'success'), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[1]
self.assertEqual('publish_job_summary', method)
self.assertEqual((settings.comment_title, self.stats, None, None), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[2]
self.assertEqual('get_pulls', method)
self.assertEqual((settings.commit, ), args)
self.assertEqual({}, kwargs)
def test_publish_with_comment_without_hiding(self):
pr = object()
cr = object()
settings = self.create_settings(comment_mode=comment_mode_create, hide_comment_mode=hide_comments_mode_off)
mock_calls = self.call_mocked_publish(settings, prs=[pr], cr=cr)
self.assertEqual(4, len(mock_calls))
(method, args, kwargs) = mock_calls[0]
self.assertEqual('publish_check', method)
self.assertEqual((self.stats, self.cases, 'success'), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[1]
self.assertEqual('publish_job_summary', method)
self.assertEqual((settings.comment_title, self.stats, cr, None), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[2]
self.assertEqual('get_pulls', method)
self.assertEqual((settings.commit, ), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[3]
self.assertEqual('publish_comment', method)
self.assertEqual((settings.comment_title, self.stats, pr, cr, self.cases), args)
self.assertEqual({}, kwargs)
def do_test_publish_with_comment_with_hide(self, hide_mode: str, hide_method: str):
pr = object()
cr = object()
settings = self.create_settings(comment_mode=comment_mode_create, hide_comment_mode=hide_mode)
mock_calls = self.call_mocked_publish(settings, prs=[pr], cr=cr)
self.assertEqual(5, len(mock_calls))
(method, args, kwargs) = mock_calls[0]
self.assertEqual('publish_check', method)
self.assertEqual((self.stats, self.cases, 'success'), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[1]
self.assertEqual('publish_job_summary', method)
self.assertEqual((settings.comment_title, self.stats, cr, None), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[2]
self.assertEqual('get_pulls', method)
self.assertEqual((settings.commit, ), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[3]
self.assertEqual('publish_comment', method)
self.assertEqual((settings.comment_title, self.stats, pr, cr, self.cases), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[4]
self.assertEqual(hide_method, method)
self.assertEqual((pr, ), args)
self.assertEqual({}, kwargs)
def test_publish_with_comment_hide_all_but_latest(self):
self.do_test_publish_with_comment_with_hide(
hide_comments_mode_all_but_latest,
'hide_all_but_latest_comments'
)
def test_publish_with_comment_hide_orphaned(self):
self.do_test_publish_with_comment_with_hide(
hide_comments_mode_orphaned,
'hide_orphaned_commit_comments'
)
def test_publish_without_compare(self):
pr = object()
cr = object()
settings = self.create_settings(comment_mode=comment_mode_create, hide_comment_mode=hide_comments_mode_all_but_latest, compare_earlier=False)
mock_calls = self.call_mocked_publish(settings, prs=[pr], cr=cr)
self.assertEqual(5, len(mock_calls))
(method, args, kwargs) = mock_calls[0]
self.assertEqual('publish_check', method)
self.assertEqual((self.stats, self.cases, 'success'), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[1]
self.assertEqual('publish_job_summary', method)
self.assertEqual((settings.comment_title, self.stats, cr, None), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[2]
self.assertEqual('get_pulls', method)
self.assertEqual((settings.commit, ), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[3]
self.assertEqual('publish_comment', method)
self.assertEqual((settings.comment_title, self.stats, pr, cr, self.cases), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[4]
self.assertEqual('hide_all_but_latest_comments', method)
self.assertEqual((pr, ), args)
self.assertEqual({}, kwargs)
def test_publish_comment_compare_earlier(self):
pr = mock.MagicMock()
cr = mock.MagicMock()
bcr = mock.MagicMock()
bs = mock.MagicMock()
stats = self.stats
cases = UnitTestCaseResults(self.cases)
settings = self.create_settings(comment_mode=comment_mode_create, compare_earlier=True)
publisher = mock.MagicMock(Publisher)
publisher._settings = settings
publisher.get_check_run = mock.Mock(return_value=bcr)
publisher.get_stats_from_check_run = mock.Mock(return_value=bs)
publisher.get_stats_delta = mock.Mock(return_value=bs)
publisher.get_base_commit_sha = mock.Mock(return_value="base commit")
publisher.get_test_lists_from_check_run = mock.Mock(return_value=(None, None))
with mock.patch('publish.publisher.get_long_summary_md', return_value='body'):
Publisher.publish_comment(publisher, 'title', stats, pr, cr, cases)
mock_calls = publisher.mock_calls
self.assertEqual(4, len(mock_calls))
(method, args, kwargs) = mock_calls[0]
self.assertEqual('get_base_commit_sha', method)
self.assertEqual((pr, ), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[1]
self.assertEqual('get_check_run', method)
self.assertEqual(('base commit', ), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[2]
self.assertEqual('get_stats_from_check_run', method)
self.assertEqual((bcr, ), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[3]
self.assertEqual('get_test_lists_from_check_run', method)
self.assertEqual((bcr, ), args)
self.assertEqual({}, kwargs)
mock_calls = pr.mock_calls
self.assertEqual(3, len(mock_calls))
(method, args, kwargs) = mock_calls[0]
self.assertEqual('create_issue_comment', method)
self.assertEqual(('## title\nbody', ), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[1]
self.assertEqual('number.__str__', method)
self.assertEqual((), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[2]
self.assertEqual('create_issue_comment().html_url.__str__', method)
self.assertEqual((), args)
self.assertEqual({}, kwargs)
def test_publish_comment_compare_earlier_with_restricted_unicode(self):
pr = mock.MagicMock()
cr = mock.MagicMock(html_url='html://url')
bcr = mock.MagicMock()
bs = UnitTestRunResults(1, [], 1, 1, 3, 1, 2, 0, 0, 3, 1, 2, 0, 0, 'commit')
stats = self.stats
# the new test cases with un-restricted unicode, as they come from test result files
cases = UnitTestCaseResults([
# removed test 𝒂
((None, 'class', 'test 𝒃'), {'success': [None]}), # unchanged test 𝒃
# removed skipped 𝒄
((None, 'class', 'skipped 𝒅'), {'skipped': [None]}), # unchanged skipped 𝒅
((None, 'class', 'skipped 𝒆'), {'skipped': [None]}), # added skipped 𝒆
((None, 'class', 'test 𝒇'), {'success': [None]}), # added test 𝒇
])
settings = self.create_settings(comment_mode=comment_mode_create, compare_earlier=True)
publisher = mock.MagicMock(Publisher)
publisher._settings = settings
publisher.get_check_run = mock.Mock(return_value=bcr)
publisher.get_stats_from_check_run = mock.Mock(return_value=bs)
publisher.get_stats_delta = mock.Mock(return_value=bs)
publisher.get_base_commit_sha = mock.Mock(return_value="base commit")
# the earlier test cases with restricted unicode as they come from the check runs API
publisher.get_test_lists_from_check_run = mock.Mock(return_value=(
# before, these existed: test 𝒂, test 𝒃, skipped 𝒄, skipped 𝒅
['class ‑ test \\U0001d482', 'class ‑ test \\U0001d483', 'class ‑ skipped \\U0001d484', 'class ‑ skipped \\U0001d485'],
['class ‑ skipped \\U0001d484', 'class ‑ skipped \\U0001d485']
))
Publisher.publish_comment(publisher, 'title', stats, pr, cr, cases)
mock_calls = publisher.mock_calls
self.assertEqual(4, len(mock_calls))
(method, args, kwargs) = mock_calls[0]
self.assertEqual('get_base_commit_sha', method)
self.assertEqual((pr, ), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[1]
self.assertEqual('get_check_run', method)
self.assertEqual(('base commit', ), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[2]
self.assertEqual('get_stats_from_check_run', method)
self.assertEqual((bcr, ), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[3]
self.assertEqual('get_test_lists_from_check_run', method)
self.assertEqual((bcr, ), args)
self.assertEqual({}, kwargs)
mock_calls = pr.mock_calls
self.assertEqual(3, len(mock_calls))
(method, args, kwargs) = mock_calls[0]
self.assertEqual('create_issue_comment', method)
self.assertEqual(('## title\n'
'\u205f\u20041 files\u2004 ±\u205f\u20040\u2002\u2003'
'2 suites\u2004 +1\u2002\u2003\u2002'
'3s [:stopwatch:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "duration of all tests") +2s\n'
'22 tests +19\u2002\u2003'
'4 [:heavy_check_mark:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "passed tests") +3\u2002\u2003'
'5 [:zzz:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "skipped / disabled tests") +3\u2002\u2003\u205f\u2004'
'6 [:x:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "failed tests") +\u205f\u20046\u2002\u2003\u205f\u2004'
'7 [:fire:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "test errors") +\u205f\u20047\u2002\n'
'38 runs\u2006 +35\u2002\u20038 [:heavy_check_mark:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "passed tests") +7\u2002\u2003'
'9 [:zzz:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "skipped / disabled tests") +7\u2002\u2003'
'10 [:x:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "failed tests") +10\u2002\u2003'
'11 [:fire:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "test errors") +11\u2002\n'
'\n'
'For more details on these failures and errors, see [this check](html://url).\n'
'\n'
'Results for commit commit.\u2003± Comparison against base commit commit.\n'
'\n'
'<details>\n'
' <summary>This pull request <b>removes</b> 2 and <b>adds</b> 2 tests. <i>Note that renamed tests count towards both.</i></summary>\n'
'\n'
'```\n'
'class ‑ skipped \\U0001d484\n'
'class ‑ test \\U0001d482\n'
'```\n'
'\n'
'```\n'
'class ‑ skipped \\U0001d486\n'
'class ‑ test \\U0001d487\n'
'```\n'
'</details>\n'
'\n'
'<details>\n'
' <summary>This pull request <b>removes</b> 1 skipped test and <b>adds</b> 1 skipped test. <i>Note that renamed tests count towards both.</i></summary>\n'
'\n'
'```\n'
'class ‑ skipped \\U0001d484\n'
'```\n'
'\n'
'```\n'
'class ‑ skipped \\U0001d486\n'
'```\n'
'</details>\n',), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[1]
self.assertEqual('number.__str__', method)
self.assertEqual((), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[2]
self.assertEqual('create_issue_comment().html_url.__str__', method)
self.assertEqual((), args)
self.assertEqual({}, kwargs)
def test_publish_comment_compare_with_itself(self):
pr = mock.MagicMock()
cr = mock.MagicMock()
stats = self.stats
cases = UnitTestCaseResults(self.cases)
settings = self.create_settings(comment_mode=comment_mode_create, compare_earlier=True)
publisher = mock.MagicMock(Publisher)
publisher._settings = settings
publisher.get_check_run = mock.Mock(return_value=None)
publisher.get_base_commit_sha = mock.Mock(return_value=stats.commit)
publisher.get_test_lists_from_check_run = mock.Mock(return_value=(None, None))
with mock.patch('publish.publisher.get_long_summary_md', return_value='body'):
Publisher.publish_comment(publisher, 'title', stats, pr, cr, cases)
mock_calls = publisher.mock_calls
self.assertEqual(1, len(mock_calls))
(method, args, kwargs) = mock_calls[0]
self.assertEqual('get_base_commit_sha', method)
self.assertEqual((pr, ), args)
self.assertEqual({}, kwargs)
mock_calls = pr.mock_calls
self.assertEqual(0, len(mock_calls))
def test_publish_comment_compare_with_None(self):
pr = mock.MagicMock()
cr = mock.MagicMock()
stats = self.stats
cases = UnitTestCaseResults(self.cases)
settings = self.create_settings(comment_mode=comment_mode_create, compare_earlier=True)
publisher = mock.MagicMock(Publisher)
publisher._settings = settings
publisher.get_check_run = mock.Mock(return_value=None)
publisher.get_base_commit_sha = mock.Mock(return_value=None)
publisher.get_test_lists_from_check_run = mock.Mock(return_value=(None, None))
with mock.patch('publish.publisher.get_long_summary_md', return_value='body'):
Publisher.publish_comment(publisher, 'title', stats, pr, cr, cases)
mock_calls = publisher.mock_calls
self.assertEqual(3, len(mock_calls))
(method, args, kwargs) = mock_calls[0]
self.assertEqual('get_base_commit_sha', method)
self.assertEqual((pr, ), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[1]
self.assertEqual('get_check_run', method)
self.assertEqual((None, ), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[2]
self.assertEqual('get_test_lists_from_check_run', method)
self.assertEqual((None, ), args)
self.assertEqual({}, kwargs)
mock_calls = pr.mock_calls
self.assertEqual(3, len(mock_calls))
(method, args, kwargs) = mock_calls[0]
self.assertEqual('create_issue_comment', method)
self.assertEqual(('## title\nbody', ), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[1]
self.assertEqual('number.__str__', method)
self.assertEqual((), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[2]
self.assertEqual('create_issue_comment().html_url.__str__', method)
self.assertEqual((), args)
self.assertEqual({}, kwargs)
def do_test_publish_comment_with_reuse_comment(self, one_exists: bool):
pr = mock.MagicMock()
cr = mock.MagicMock()
stats = self.stats
cases = UnitTestCaseResults(self.cases)
settings = self.create_settings(comment_mode=comment_mode_update, compare_earlier=False)
publisher = mock.MagicMock(Publisher)
publisher._settings = settings
publisher.get_test_lists_from_check_run = mock.Mock(return_value=(None, None))
publisher.reuse_comment = mock.Mock(return_value=one_exists)
with mock.patch('publish.publisher.get_long_summary_md', return_value='body'):
Publisher.publish_comment(publisher, 'title', stats, pr, cr, cases)
mock_calls = publisher.mock_calls
self.assertEqual(2, len(mock_calls))
(method, args, kwargs) = mock_calls[0]
self.assertEqual('get_test_lists_from_check_run', method)
self.assertEqual((None, ), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[1]
self.assertEqual('reuse_comment', method)
self.assertEqual((pr, '## title\nbody'), args)
self.assertEqual({}, kwargs)
mock_calls = pr.mock_calls
self.assertEqual(0 if one_exists else 3, len(mock_calls))
if not one_exists:
(method, args, kwargs) = mock_calls[0]
self.assertEqual('create_issue_comment', method)
self.assertEqual(('## title\nbody', ), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[1]
self.assertEqual('number.__str__', method)
self.assertEqual((), args)
self.assertEqual({}, kwargs)
(method, args, kwargs) = mock_calls[2]
self.assertEqual('create_issue_comment().html_url.__str__', method)
self.assertEqual((), args)
self.assertEqual({}, kwargs)
def test_publish_comment_with_reuse_comment_none_existing(self):
self.do_test_publish_comment_with_reuse_comment(one_exists=False)
def test_publish_comment_with_reuse_comment_one_existing(self):
self.do_test_publish_comment_with_reuse_comment(one_exists=True)
def do_test_reuse_comment(self,
pull_request_comments: List[Any],
action_comments: List[Mapping[str, int]],
body='body',
expected_body='body\n:recycle: This comment has been updated with latest results.'):
pr = mock.MagicMock()
comment = mock.MagicMock()
pr.get_issue_comment = mock.Mock(return_value=comment)
settings = self.create_settings(comment_mode=comment_mode_update, compare_earlier=False)
publisher = mock.MagicMock(Publisher)
publisher._settings = settings
publisher.get_pull_request_comments = mock.Mock(return_value=pull_request_comments)
publisher.get_action_comments = mock.Mock(return_value=action_comments)
Publisher.reuse_comment(publisher, pr, body)
mock_calls = publisher.mock_calls
self.assertEqual(2, len(mock_calls))
(method, args, kwargs) = mock_calls[0]
self.assertEqual('get_pull_request_comments', method)
self.assertEqual((pr, ), args)
self.assertEqual({'order_by_updated': True}, kwargs)
(method, args, kwargs) = mock_calls[1]
self.assertEqual('get_action_comments', method)
self.assertEqual((pull_request_comments, ), args)
self.assertEqual({}, kwargs)
if action_comments:
pr.get_issue_comment.assert_called_once_with(action_comments[-1].get('databaseId'))
comment.edit.assert_called_once_with(expected_body)
def test_reuse_comment_non_existing(self):
self.do_test_reuse_comment(pull_request_comments=[1, 2, 3], action_comments=[])
def test_reuse_comment_one_existing(self):
self.do_test_reuse_comment(pull_request_comments=[1, 2, 3], action_comments=[{'databaseId': 1}])
def test_reuse_comment_multiple_existing(self):
self.do_test_reuse_comment(pull_request_comments=[1, 2, 3], action_comments=[{'databaseId': 1}, {'databaseId': 2}, {'databaseId': 3}])
def test_reuse_comment_existing_not_updated(self):
# we do not expect the body to be extended by the recycle message
self.do_test_reuse_comment(pull_request_comments=[1, 2, 3], action_comments=[{'databaseId': 1}],
body='a new comment',
expected_body='a new comment\n:recycle: This comment has been updated with latest results.')
def test_reuse_comment_existing_updated(self):
# we do not expect the body to be extended by the recycle message
self.do_test_reuse_comment(pull_request_comments=[1, 2, 3], action_comments=[{'databaseId': 1}],
body='comment already updated\n:recycle: Has been updated',
expected_body='comment already updated\n:recycle: Has been updated')
def do_test_get_pulls(self,
settings: Settings,
search_issues: mock.Mock,
expected: List[mock.Mock]) -> mock.Mock:
gh, gha, req, repo, commit = self.create_mocks()
gh.search_issues = mock.Mock(return_value=search_issues)
publisher = Publisher(settings, gh, gha)
actual = publisher.get_pulls(settings.commit)
self.assertEqual(expected, actual)
gh.search_issues.assert_called_once_with('type:pr repo:"{}" {}'.format(settings.repo, settings.commit))
return gha
def test_get_pulls(self):
settings = self.create_settings()
pr = self.create_github_pr(settings.repo, head_commit_sha=settings.commit)
search_issues = self.create_github_collection([pr])
gha = self.do_test_get_pulls(settings, search_issues, [pr])
gha.warning.assert_not_called()
gha.error.assert_not_called()
def test_get_pulls_no_search_results(self):
settings = self.create_settings()
search_issues = self.create_github_collection([])
gha = self.do_test_get_pulls(settings, search_issues, [])
gha.warning.assert_not_called()
gha.error.assert_not_called()
def test_get_pulls_one_closed_matches(self):
settings = self.create_settings()
pr = self.create_github_pr(settings.repo, state='closed', head_commit_sha=settings.commit)
search_issues = self.create_github_collection([pr])
gha = self.do_test_get_pulls(settings, search_issues, [])
gha.warning.assert_not_called()
gha.error.assert_not_called()
def test_get_pulls_multiple_closed_matches(self):
settings = self.create_settings()
pr1 = self.create_github_pr(settings.repo, state='closed', head_commit_sha=settings.commit)
pr2 = self.create_github_pr(settings.repo, state='closed', head_commit_sha=settings.commit)
search_issues = self.create_github_collection([pr1, pr2])
gha = self.do_test_get_pulls(settings, search_issues, [])
gha.warning.assert_not_called()
gha.error.assert_not_called()
def test_get_pulls_one_closed_one_open_matches(self):
settings = self.create_settings()
pr1 = self.create_github_pr(settings.repo, state='closed', head_commit_sha=settings.commit)
pr2 = self.create_github_pr(settings.repo, state='open', head_commit_sha=settings.commit)
search_issues = self.create_github_collection([pr1, pr2])
gha = self.do_test_get_pulls(settings, search_issues, [pr2])
gha.warning.assert_not_called()
gha.error.assert_not_called()
def test_get_pulls_multiple_open_one_matches_head_commit(self):
settings = self.create_settings()
pr1 = self.create_github_pr(settings.repo, state='open', head_commit_sha=settings.commit, merge_commit_sha='merge one')
pr2 = self.create_github_pr(settings.repo, state='open', head_commit_sha='other head commit', merge_commit_sha='merge two')
search_issues = self.create_github_collection([pr1, pr2])
gha = self.do_test_get_pulls(settings, search_issues, [pr1])
gha.warning.assert_not_called()
gha.error.assert_not_called()
def test_get_pulls_multiple_open_one_matches_merge_commit(self):
settings = self.create_settings()
pr1 = self.create_github_pr(settings.repo, state='open', head_commit_sha='one head commit', merge_commit_sha=settings.commit)
pr2 = self.create_github_pr(settings.repo, state='open', head_commit_sha='two head commit', merge_commit_sha='other merge commit')
search_issues = self.create_github_collection([pr1, pr2])
gha = self.do_test_get_pulls(settings, search_issues, [pr1])
gha.warning.assert_not_called()
gha.error.assert_not_called()
def test_get_pulls_multiple_open_both_match_head_commit(self):
settings = self.create_settings()
pr1 = self.create_github_pr(settings.repo, state='open', head_commit_sha=settings.commit, merge_commit_sha='merge one')
pr2 = self.create_github_pr(settings.repo, state='open', head_commit_sha=settings.commit, merge_commit_sha='merge two')
search_issues = self.create_github_collection([pr1, pr2])
gha = self.do_test_get_pulls(settings, search_issues, [pr1, pr2])
gha.warning.assert_not_called()
gha.error.assert_not_called()
def test_get_pulls_multiple_open_both_match_merge_commit(self):
settings = self.create_settings()
pr1 = self.create_github_pr(settings.repo, state='open', head_commit_sha='one head commit', merge_commit_sha=settings.commit)
pr2 = self.create_github_pr(settings.repo, state='open', head_commit_sha='two head commit', merge_commit_sha=settings.commit)
search_issues = self.create_github_collection([pr1, pr2])
gha = self.do_test_get_pulls(settings, search_issues, [pr1, pr2])
gha.warning.assert_not_called()
gha.error.assert_not_called()
def test_get_pulls_forked_repo(self):
settings = self.create_settings()
fork = self.create_github_pr('other/fork', head_commit_sha=settings.commit)
search_issues = self.create_github_collection([fork])
self.do_test_get_pulls(settings, search_issues, [])
def test_get_pulls_forked_repos_and_own_repo(self):
settings = self.create_settings()
own = self.create_github_pr(settings.repo, head_commit_sha=settings.commit)
fork1 = self.create_github_pr('other/fork', head_commit_sha=settings.commit)
fork2 = self.create_github_pr('{}.fork'.format(settings.repo), head_commit_sha=settings.commit)
search_issues = self.create_github_collection([own, fork1, fork2])
self.do_test_get_pulls(settings, search_issues, [own])
def do_test_get_check_run_from_list(self, runs: List[github.CheckRun.CheckRun], expected: Optional[github.CheckRun.CheckRun]):
settings = self.create_settings()
gh, gha, req, repo, commit = self.create_mocks()
publisher = Publisher(settings, gh, gha)
actual = publisher.get_check_run_from_list(runs)
self.assertEqual(expected, actual)
def test_get_check_run_from_list_empty(self):
self.do_test_get_check_run_from_list([], None)
def test_get_check_run_from_list_many(self):
runs = [
self.mock_check_run(name='Other title', status='completed', started_at=datetime(2021, 3, 19, 12, 2, 4, tzinfo=timezone.utc), summary='summary\n[test-results]:data:application/gzip;base64,digest'),
self.mock_check_run(name='Check Name', status='other status', started_at=datetime(2021, 3, 19, 12, 2, 4, tzinfo=timezone.utc), summary='summary\n[test-results]:data:application/gzip;base64,digest'),
self.mock_check_run(name='Check Name', status='completed', started_at=datetime(2021, 3, 19, 12, 0, 0, tzinfo=timezone.utc), summary='summary\n[test-results]:data:application/gzip;base64,digest'),
self.mock_check_run(name='Check Name', status='completed', started_at=datetime(2021, 3, 19, 12, 2, 4, tzinfo=timezone.utc), summary='summary\n[test-results]:data:application/gzip;base64,digest'),
self.mock_check_run(name='Check Name', status='completed', started_at=datetime(2021, 3, 19, 12, 2, 4, tzinfo=timezone.utc), summary='no digest'),
self.mock_check_run(name='Check Name', status='completed', started_at=datetime(2021, 3, 19, 12, 2, 4, tzinfo=timezone.utc), summary=None)
]
expected = runs[3]
name = runs[0].name
self.do_test_get_check_run_from_list(runs, expected)
@staticmethod
def mock_check_run(name: str, status: str, started_at: datetime, summary: str) -> mock.Mock:
run = mock.MagicMock(status=status, started_at=started_at, output=mock.MagicMock(summary=summary))
run.name = name
return run
def do_test_get_stats_from_commit(self,
settings: Settings,
commit_sha: Optional[str],
commit: Optional[mock.Mock],
digest: Optional[str],
check_names: Optional[List[str]],
expected: Optional[Union[UnitTestRunResults, mock.Mock]]):
gh, gha, req, repo, commit = self.create_mocks(commit=commit, digest=digest, check_names=check_names)
publisher = Publisher(settings, gh, gha)
actual = publisher.get_stats_from_commit(commit_sha)
actual_dict = None
if actual is not None:
actual_dict = actual.to_dict()
del actual_dict['errors']
expected_dict = None
if expected is not None:
expected_dict = expected.to_dict()
del expected_dict['errors']
self.assertEqual(expected_dict, actual_dict)
if commit_sha is not None and commit_sha != '0000000000000000000000000000000000000000':
repo.get_commit.assert_called_once_with(commit_sha)
if commit is not None:
commit.get_check_runs.assert_called_once_with()
if expected is None and \
commit_sha is not None and \
commit_sha != '0000000000000000000000000000000000000000':
gha.error.assert_called_once_with('Could not find commit {}'.format(commit_sha))
def test_get_stats_from_commit(self):
settings = self.create_settings()
self.do_test_get_stats_from_commit(
settings, 'base commit', mock.Mock(), self.base_digest, [settings.check_name], self.get_stats('base')
)
def test_get_stats_from_commit_with_no_commit(self):
settings = self.create_settings()
self.do_test_get_stats_from_commit(settings, 'base commit', None, None, None, None)
def test_get_stats_from_commit_with_none_commit_sha(self):
settings = self.create_settings()
self.do_test_get_stats_from_commit(settings, None, mock.Mock(), self.base_digest, [settings.check_name], None)
def test_get_stats_from_commit_with_zeros_commit_sha(self):
settings = self.create_settings()