forked from oppia/oppia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre_push_hook_test.py
845 lines (755 loc) · 37.3 KB
/
pre_push_hook_test.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
# coding: utf-8
#
# Copyright 2019 The Oppia Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Unit tests for scripts/pre_push_hook.py."""
from __future__ import annotations
import os
import shutil
import subprocess
import sys
import tempfile
from core import python_utils
from core.tests import test_utils
from . import common
from . import install_backend_python_libs
from . import pre_push_hook
class PrePushHookTests(test_utils.GenericTestBase):
"""Test the methods for pre push hook script."""
def setUp(self):
super(PrePushHookTests, self).setUp()
process = subprocess.Popen(
['echo', 'test'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def mock_popen( # pylint: disable=unused-argument
unused_cmd_tokens, stdout=subprocess.PIPE,
stderr=subprocess.PIPE):
return process
def mock_get_remote_name():
return 'remote'
def mock_get_refs():
return ['ref1', 'ref2']
def mock_collect_files_being_pushed(unused_refs, unused_remote):
return {
'branch1': ([b'A:file1', b'M:file2'], [b'file1', b'file2']),
'branch2': ([], [])}
def mock_has_uncommitted_files():
return False
self.print_arr = []
def mock_print(msg):
self.print_arr.append(msg)
def mock_check_output(unused_cmd_tokens):
return 'Output'
self.linter_code = 0
def mock_start_linter(unused_files_to_lint):
return self.linter_code
self.mypy_check_code = 0
def mock_execute_mypy_checks():
return self.mypy_check_code
self.does_diff_include_js_or_ts_files = False
def mock_does_diff_include_js_or_ts_files(unused_diff_files):
return self.does_diff_include_js_or_ts_files
self.does_diff_include_ts_files = False
def mock_does_diff_include_ts_files(unused_diff_files):
return self.does_diff_include_ts_files
self.does_diff_include_ci_config_or_js_files = False
def mock_does_diff_include_ci_config_or_js_files(
unused_diff_files):
return self.does_diff_include_ci_config_or_js_files
def mock_check_backend_python_library_for_inconsistencies():
return
self.swap_check_backend_python_libs = self.swap(
pre_push_hook,
'check_for_backend_python_library_inconsistencies',
mock_check_backend_python_library_for_inconsistencies)
self.popen_swap = self.swap(subprocess, 'Popen', mock_popen)
self.get_remote_name_swap = self.swap(
pre_push_hook, 'get_remote_name', mock_get_remote_name)
self.get_refs_swap = self.swap(pre_push_hook, 'get_refs', mock_get_refs)
self.collect_files_swap = self.swap(
pre_push_hook, 'collect_files_being_pushed',
mock_collect_files_being_pushed)
self.uncommitted_files_swap = self.swap(
pre_push_hook, 'has_uncommitted_files', mock_has_uncommitted_files)
self.print_swap = self.swap(python_utils, 'PRINT', mock_print)
self.check_output_swap = self.swap(
subprocess, 'check_output', mock_check_output)
self.start_linter_swap = self.swap(
pre_push_hook, 'start_linter', mock_start_linter)
self.execute_mypy_checks_swap = self.swap(
pre_push_hook, 'execute_mypy_checks', mock_execute_mypy_checks)
self.js_or_ts_swap = self.swap(
pre_push_hook, 'does_diff_include_js_or_ts_files',
mock_does_diff_include_js_or_ts_files)
self.ts_swap = self.swap(
pre_push_hook, 'does_diff_include_ts_files',
mock_does_diff_include_ts_files)
self.ci_config_or_js_files_swap = self.swap(
pre_push_hook,
'does_diff_include_ci_config_or_js_files',
mock_does_diff_include_ci_config_or_js_files)
def test_start_subprocess_for_result(self):
with self.popen_swap:
self.assertEqual(
pre_push_hook.start_subprocess_for_result('cmd'),
(b'test\n', b''))
def test_get_remote_name_without_errors(self):
process_for_remote = subprocess.Popen(
[b'echo', b'origin\nupstream'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
process_for_upstream_url = subprocess.Popen(
[b'echo', b'url.oppia/oppia.git'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
process_for_origin_url = subprocess.Popen(
[b'echo', b'url.other/oppia.git'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
def mock_popen(cmd_tokens, stdout, stderr): # pylint: disable=unused-argument
if b'remote.origin.url' in cmd_tokens:
return process_for_origin_url
elif b'remote.upstream.url' in cmd_tokens:
return process_for_upstream_url
else:
return process_for_remote
popen_swap = self.swap(subprocess, 'Popen', mock_popen)
with popen_swap:
self.assertEqual(pre_push_hook.get_remote_name(), b'upstream')
def test_get_remote_name_with_error_in_obtaining_remote(self):
def mock_communicate():
return (b'test', b'Error')
process = subprocess.Popen(
[b'echo', b'test'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.communicate = mock_communicate
def mock_popen(unused_cmd_tokens, stdout, stderr): # pylint: disable=unused-argument
return process
popen_swap = self.swap(subprocess, 'Popen', mock_popen)
with popen_swap, self.assertRaisesRegexp(ValueError, 'Error'):
pre_push_hook.get_remote_name()
def test_get_remote_name_with_error_in_obtaining_remote_url(self):
def mock_communicate():
return ('test', 'Error')
process_for_remote = subprocess.Popen(
[b'echo', b'origin\nupstream'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
process_for_remote_url = subprocess.Popen(
[b'echo', b'test'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process_for_remote_url.communicate = mock_communicate
def mock_popen(cmd_tokens, stdout, stderr): # pylint: disable=unused-argument
if b'config' in cmd_tokens:
return process_for_remote_url
else:
return process_for_remote
popen_swap = self.swap(subprocess, 'Popen', mock_popen)
with popen_swap, self.assertRaisesRegexp(ValueError, 'Error'):
pre_push_hook.get_remote_name()
def test_get_remote_name_with_no_remote_set(self):
process_for_remote = subprocess.Popen(
[b'echo', b'origin\nupstream'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
process_for_upstream_url = subprocess.Popen(
[b'echo', b'url.other/oppia.git'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
process_for_origin_url = subprocess.Popen(
[b'echo', b'url.other/oppia.git'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
def mock_popen(cmd_tokens, stdout, stderr): # pylint: disable=unused-argument
if b'remote.origin.url' in cmd_tokens:
return process_for_origin_url
elif b'remote.upstream.url' in cmd_tokens:
return process_for_upstream_url
else:
return process_for_remote
popen_swap = self.swap(subprocess, 'Popen', mock_popen)
with popen_swap, self.assertRaisesRegexp(
Exception,
'Error: Please set upstream for the lint checks to run '
'efficiently. To do that follow these steps:\n'
'1. Run the command \'git remote -v\'\n'
'2a. If upstream is listed in the command output, then run the '
'command \'git remote set-url upstream '
'https://github.com/oppia/oppia.git\'\n'
'2b. If upstream is not listed in the command output, then run the '
'command \'git remote add upstream '
'https://github.com/oppia/oppia.git\'\n'):
pre_push_hook.get_remote_name()
def test_get_remote_name_with_multiple_remotes_set(self):
process_for_remote = subprocess.Popen(
[b'echo', b'origin\nupstream'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
process_for_upstream_url = subprocess.Popen(
[b'echo', b'url.oppia/oppia.git'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
process_for_origin_url = subprocess.Popen(
[b'echo', b'url.oppia/oppia.git'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
def mock_popen(cmd_tokens, stdout, stderr): # pylint: disable=unused-argument
if b'remote.origin.url' in cmd_tokens:
return process_for_origin_url
elif b'remote.upstream.url' in cmd_tokens:
return process_for_upstream_url
else:
return process_for_remote
popen_swap = self.swap(subprocess, 'Popen', mock_popen)
with popen_swap, self.print_swap:
self.assertIsNone(pre_push_hook.get_remote_name())
self.assertTrue(
'Warning: Please keep only one remote branch for oppia:develop '
'to run the lint checks efficiently.\n' in self.print_arr)
def test_git_diff_name_status_without_error(self):
def mock_start_subprocess_for_result(unused_cmd_tokens):
return (b'M\tfile1\nA\tfile2', None)
subprocess_swap = self.swap(
pre_push_hook, 'start_subprocess_for_result',
mock_start_subprocess_for_result)
with subprocess_swap:
self.assertEqual(
pre_push_hook.git_diff_name_status(
'left', 'right', diff_filter='filter'),
[
pre_push_hook.FileDiff(status=b'M', name=b'file1'),
pre_push_hook.FileDiff(status=b'A', name=b'file2')])
def test_git_diff_name_status_with_error(self):
def mock_start_subprocess_for_result(unused_cmd_tokens):
return ('M\tfile1\nA\tfile2', 'Error')
subprocess_swap = self.swap(
pre_push_hook, 'start_subprocess_for_result',
mock_start_subprocess_for_result)
with subprocess_swap, self.assertRaisesRegexp(ValueError, 'Error'):
pre_push_hook.git_diff_name_status(
'left', 'right', diff_filter='filter')
def test_compare_to_remote(self):
check_function_calls = {
'start_subprocess_for_result_is_called': False,
'git_diff_name_status_is_called': False,
'get_merge_base_is_called': False,
}
expected_check_function_calls = {
'start_subprocess_for_result_is_called': True,
'git_diff_name_status_is_called': True,
'get_merge_base_is_called': True,
}
def mock_start_subprocess_for_result(unused_cmd_tokens):
check_function_calls['start_subprocess_for_result_is_called'] = True
def mock_git_diff_name_status(unused_left, unused_right):
check_function_calls['git_diff_name_status_is_called'] = True
return 'Test'
def mock_get_merge_base(unused_left, unused_right):
check_function_calls['get_merge_base_is_called'] = True
return 'Merge Base'
subprocess_swap = self.swap(
pre_push_hook, 'start_subprocess_for_result',
mock_start_subprocess_for_result)
git_diff_swap = self.swap(
pre_push_hook, 'git_diff_name_status', mock_git_diff_name_status)
get_merge_base_swap = self.swap(
pre_push_hook, 'get_merge_base', mock_get_merge_base)
with subprocess_swap, git_diff_swap, get_merge_base_swap:
self.assertEqual(
pre_push_hook.compare_to_remote(b'remote', b'local branch'),
'Test')
self.assertEqual(check_function_calls, expected_check_function_calls)
def test_get_merge_base_reports_error(self):
def mock_start_subprocess_for_result(unused_cmd_tokens):
return None, 'Test'
subprocess_swap = self.swap(
pre_push_hook, 'start_subprocess_for_result',
mock_start_subprocess_for_result)
with subprocess_swap, self.assertRaisesRegexp(ValueError, 'Test'):
pre_push_hook.get_merge_base('A', 'B')
def test_get_merge_base_returns_merge_base(self):
check_function_calls = {
'start_subprocess_for_result_is_called': False,
}
expected_check_function_calls = {
'start_subprocess_for_result_is_called': True,
}
def mock_start_subprocess_for_result(unused_cmd_tokens):
check_function_calls['start_subprocess_for_result_is_called'] = True
return 'Test', None
subprocess_swap = self.swap(
pre_push_hook, 'start_subprocess_for_result',
mock_start_subprocess_for_result)
with subprocess_swap:
self.assertEqual(pre_push_hook.get_merge_base('A', 'B'), 'Test')
self.assertEqual(check_function_calls, expected_check_function_calls)
def test_extract_files_to_lint_with_empty_file_diffs(self):
self.assertEqual(pre_push_hook.extract_files_to_lint([]), [])
def test_extract_files_to_lint_with_non_empty_file_diffs(self):
self.assertEqual(
pre_push_hook.extract_files_to_lint([
pre_push_hook.FileDiff(status=b'M', name=b'file1'),
pre_push_hook.FileDiff(status=b'A', name=b'file2'),
pre_push_hook.FileDiff(status=b'W', name=b'file3')]),
[b'file1', b'file2'])
def test_get_parent_branch_name_for_diff_with_hotfix_branch(self):
def mock_get_branch():
return 'release-1.2.3-hotfix-1'
get_branch_swap = self.swap(
common, 'get_current_branch_name', mock_get_branch)
with get_branch_swap:
self.assertEqual(
pre_push_hook.get_parent_branch_name_for_diff(),
'release-1.2.3')
def test_get_parent_branch_name_for_diff_with_release_branch(self):
def mock_get_branch():
return 'release-1.2.3'
get_branch_swap = self.swap(
common, 'get_current_branch_name', mock_get_branch)
with get_branch_swap:
self.assertEqual(
pre_push_hook.get_parent_branch_name_for_diff(), b'develop')
def test_get_parent_branch_name_for_diff_with_non_release_branch(self):
def mock_get_branch():
return 'branch-1'
get_branch_swap = self.swap(
common, 'get_current_branch_name', mock_get_branch)
with get_branch_swap:
self.assertEqual(
pre_push_hook.get_parent_branch_name_for_diff(), b'develop')
def test_collect_files_being_pushed_with_empty_ref_list(self):
def mock_get_branch():
return 'branch-1'
get_branch_swap = self.swap(
common, 'get_current_branch_name', mock_get_branch)
with get_branch_swap:
self.assertEqual(
pre_push_hook.collect_files_being_pushed([], 'remote'), {})
def test_collect_files_being_pushed_with_non_empty_ref_list(self):
def mock_get_branch():
return 'branch-1'
def mock_compare_to_remote(
unused_remote, unused_local_branch, remote_branch=None): # pylint: disable=unused-argument
return ['A:file1', 'M:file2']
def mock_extract_files_to_lint(unused_file_diffs):
return ['file1', 'file2']
get_branch_swap = self.swap(
common, 'get_current_branch_name', mock_get_branch)
compare_to_remote_swap = self.swap(
pre_push_hook, 'compare_to_remote', mock_compare_to_remote)
extract_files_swap = self.swap(
pre_push_hook, 'extract_files_to_lint', mock_extract_files_to_lint)
with compare_to_remote_swap, extract_files_swap, get_branch_swap:
self.assertEqual(
pre_push_hook.collect_files_being_pushed([
pre_push_hook.GitRef(
local_ref='refs/heads/branch1', local_sha1='sha1',
remote_ref='remote/ref1', remote_sha1='rsha1'),
pre_push_hook.GitRef(
local_ref='refs/branch2', local_sha1='sha2',
remote_ref='remote/ref2', remote_sha1='rsha2')
], 'remote'),
{'branch1': (['A:file1', 'M:file2'], ['file1', 'file2'])})
def test_get_refs(self):
temp_stdin_file = tempfile.NamedTemporaryFile().name
with python_utils.open_file(temp_stdin_file, 'w') as f:
f.write('local_ref local_sha1 remote_ref remote_sha1')
with python_utils.open_file(temp_stdin_file, 'r') as f:
with self.swap(sys, 'stdin', f):
self.assertEqual(
pre_push_hook.get_refs(),
[
pre_push_hook.GitRef(
local_ref='local_ref', local_sha1='local_sha1',
remote_ref='remote_ref', remote_sha1='remote_sha1'
)])
def test_start_linter(self):
with self.popen_swap:
self.assertEqual(pre_push_hook.start_linter(['files']), 0)
def test_execute_mypy_checks(self):
with self.popen_swap:
self.assertEqual(pre_push_hook.execute_mypy_checks(), 0)
def test_run_script_and_get_returncode(self):
with self.popen_swap:
self.assertEqual(
pre_push_hook.run_script_and_get_returncode('script'), 0)
def test_has_uncommitted_files(self):
def mock_check_output(unused_cmd_tokens):
return 'file1'
check_output_swap = self.swap(
subprocess, 'check_output', mock_check_output)
with check_output_swap:
self.assertTrue(pre_push_hook.has_uncommitted_files())
def test_install_hook_with_existing_symlink(self):
def mock_islink(unused_file):
return True
def mock_exists(unused_file):
return True
def mock_start_subprocess_for_result(unused_cmd_tokens):
return ('Output', None)
islink_swap = self.swap(os.path, 'islink', mock_islink)
exists_swap = self.swap(os.path, 'exists', mock_exists)
subprocess_swap = self.swap(
pre_push_hook, 'start_subprocess_for_result',
mock_start_subprocess_for_result)
with islink_swap, exists_swap, subprocess_swap, self.print_swap:
pre_push_hook.install_hook()
self.assertTrue('Symlink already exists' in self.print_arr)
self.assertTrue(
'pre-push hook file is now executable!' in self.print_arr)
def test_install_hook_with_error_in_making_pre_push_executable(self):
def mock_islink(unused_file):
return True
def mock_exists(unused_file):
return True
def mock_start_subprocess_for_result(unused_cmd_tokens):
return ('Output', 'Error')
islink_swap = self.swap(os.path, 'islink', mock_islink)
exists_swap = self.swap(os.path, 'exists', mock_exists)
subprocess_swap = self.swap(
pre_push_hook, 'start_subprocess_for_result',
mock_start_subprocess_for_result)
with islink_swap, exists_swap, subprocess_swap, self.print_swap:
with self.assertRaisesRegexp(ValueError, 'Error'):
pre_push_hook.install_hook()
self.assertTrue('Symlink already exists' in self.print_arr)
self.assertFalse(
'pre-push hook file is now executable!' in self.print_arr)
def test_install_hook_with_creation_of_symlink(self):
check_function_calls = {
'symlink_is_called': False
}
def mock_islink(unused_file):
return False
def mock_exists(unused_file):
return False
def mock_start_subprocess_for_result(unused_cmd_tokens):
return ('Output', None)
def mock_symlink(unused_path, unused_file):
check_function_calls['symlink_is_called'] = True
islink_swap = self.swap(os.path, 'islink', mock_islink)
exists_swap = self.swap(os.path, 'exists', mock_exists)
subprocess_swap = self.swap(
pre_push_hook, 'start_subprocess_for_result',
mock_start_subprocess_for_result)
symlink_swap = self.swap(os, 'symlink', mock_symlink)
with islink_swap, exists_swap, subprocess_swap, symlink_swap, (
self.print_swap):
pre_push_hook.install_hook()
self.assertTrue(check_function_calls['symlink_is_called'])
self.assertTrue(
'Created symlink in .git/hooks directory' in self.print_arr)
self.assertTrue(
'pre-push hook file is now executable!' in self.print_arr)
def test_install_hook_with_error_in_creation_of_symlink(self):
check_function_calls = {
'symlink_is_called': False,
'copy_is_called': False
}
expected_check_function_calls = {
'symlink_is_called': True,
'copy_is_called': True
}
def mock_islink(unused_file):
return False
def mock_exists(unused_file):
return False
def mock_start_subprocess_for_result(unused_cmd_tokens):
return ('Output', None)
def mock_symlink(unused_path, unused_file):
check_function_calls['symlink_is_called'] = True
raise OSError
def mock_copy(unused_type, unused_file):
check_function_calls['copy_is_called'] = True
islink_swap = self.swap(os.path, 'islink', mock_islink)
exists_swap = self.swap(os.path, 'exists', mock_exists)
subprocess_swap = self.swap(
pre_push_hook, 'start_subprocess_for_result',
mock_start_subprocess_for_result)
symlink_swap = self.swap(os, 'symlink', mock_symlink)
copy_swap = self.swap(shutil, 'copy', mock_copy)
with islink_swap, exists_swap, subprocess_swap, symlink_swap, copy_swap:
with self.print_swap:
pre_push_hook.install_hook()
self.assertEqual(check_function_calls, expected_check_function_calls)
self.assertTrue('Copied file to .git/hooks directory' in self.print_arr)
self.assertTrue(
'pre-push hook file is now executable!' in self.print_arr)
def test_install_hook_with_broken_symlink(self):
check_function_calls = {
'unlink_is_called': False,
'symlink_is_called': False
}
def mock_islink(unused_file):
return True
def mock_exists(unused_file):
return False
def mock_start_subprocess_for_result(unused_cmd_tokens):
return ('Output', None)
def mock_unlink(unused_file):
check_function_calls['unlink_is_called'] = True
def mock_symlink(unused_path, unused_file):
check_function_calls['symlink_is_called'] = True
islink_swap = self.swap(os.path, 'islink', mock_islink)
exists_swap = self.swap(os.path, 'exists', mock_exists)
subprocess_swap = self.swap(
pre_push_hook, 'start_subprocess_for_result',
mock_start_subprocess_for_result)
unlink_swap = self.swap(os, 'unlink', mock_unlink)
symlink_swap = self.swap(os, 'symlink', mock_symlink)
with islink_swap, exists_swap, subprocess_swap, self.print_swap:
with unlink_swap, symlink_swap:
pre_push_hook.install_hook()
self.assertTrue(check_function_calls['unlink_is_called'])
self.assertTrue(check_function_calls['symlink_is_called'])
self.assertTrue('Removing broken symlink' in self.print_arr)
self.assertTrue(
'pre-push hook file is now executable!' in self.print_arr)
def test_does_diff_include_js_or_ts_files_with_js_file(self):
self.assertTrue(
pre_push_hook.does_diff_include_js_or_ts_files(
[b'file1.js', b'file2.py']))
def test_does_diff_include_js_or_ts_files_with_no_file(self):
self.assertFalse(
pre_push_hook.does_diff_include_js_or_ts_files(
[b'file1.html', b'file2.py']))
def test_does_diff_include_ts_files(self):
self.assertTrue(
pre_push_hook.does_diff_include_ts_files(
[b'file1.ts', b'file2.ts', b'file3.js']))
def test_does_diff_include_ts_files_fail(self):
self.assertFalse(
pre_push_hook.does_diff_include_ts_files(
[b'file1.html', b'file2.yml', b'file3.js']))
def test_does_diff_include_ci_config_or_js_files(self):
self.assertTrue(
pre_push_hook.does_diff_include_ci_config_or_js_files(
[b'file1.js', b'protractor.conf.js', b'e2e_dummy.yml']))
def test_does_diff_include_ci_config_or_js_files_fail(self):
self.assertFalse(
pre_push_hook.does_diff_include_ci_config_or_js_files(
[b'file1.ts', b'file2.ts', b'file3.html']))
def test_repo_in_dirty_state(self):
def mock_has_uncommitted_files():
return True
uncommitted_files_swap = self.swap(
pre_push_hook, 'has_uncommitted_files', mock_has_uncommitted_files)
with self.get_remote_name_swap, self.get_refs_swap, self.print_swap:
with self.collect_files_swap, uncommitted_files_swap:
with self.assertRaisesRegexp(SystemExit, '1'):
with self.swap_check_backend_python_libs:
pre_push_hook.main(args=[])
self.assertTrue(
'Your repo is in a dirty state which prevents the linting from'
' working.\nStash your changes or commit them.\n' in self.print_arr)
def test_error_while_branch_change(self):
def mock_check_output(cmd_tokens):
if 'symbolic-ref' in cmd_tokens:
return 'old-branch'
raise subprocess.CalledProcessError(1, 'cmd', output='Output')
check_output_swap = self.swap(
subprocess, 'check_output', mock_check_output)
with self.get_remote_name_swap, self.get_refs_swap, self.print_swap:
with self.collect_files_swap, self.uncommitted_files_swap:
with check_output_swap, self.assertRaisesRegexp(
SystemExit, '1'
):
with self.swap_check_backend_python_libs:
pre_push_hook.main(args=[])
self.assertIn(
'\nCould not change branch to branch1. This is most probably '
'because you are in a dirty state. Change manually to the branch '
'that is being linted or stash your changes.',
self.print_arr
)
def test_lint_failure(self):
self.linter_code = 1
with self.get_remote_name_swap, self.get_refs_swap, self.print_swap:
with self.collect_files_swap, self.uncommitted_files_swap:
with self.check_output_swap, self.start_linter_swap:
with self.execute_mypy_checks_swap:
with self.assertRaisesRegexp(SystemExit, '1'):
with self.swap_check_backend_python_libs:
pre_push_hook.main(args=[])
self.assertTrue(
'Push failed, please correct the linting issues above.'
in self.print_arr)
def test_mypy_check_failure(self):
self.mypy_check_code = 1
with self.get_remote_name_swap, self.get_refs_swap, self.print_swap:
with self.collect_files_swap, self.uncommitted_files_swap:
with self.check_output_swap, self.start_linter_swap:
with self.execute_mypy_checks_swap:
with self.assertRaisesRegexp(SystemExit, '1'):
with self.swap_check_backend_python_libs:
pre_push_hook.main(args=[])
self.assertIn(
'Push failed, please correct the mypy type annotation issues '
'above.', self.print_arr)
def test_typescript_check_failiure(self):
self.does_diff_include_ts_files = True
def mock_run_script_and_get_returncode(unused_script):
return 1
run_script_and_get_returncode_swap = self.swap(
pre_push_hook, 'run_script_and_get_returncode',
mock_run_script_and_get_returncode)
with self.get_remote_name_swap, self.get_refs_swap, self.print_swap:
with self.collect_files_swap, self.uncommitted_files_swap:
with self.check_output_swap, self.start_linter_swap:
with self.ts_swap, run_script_and_get_returncode_swap:
with self.execute_mypy_checks_swap:
with self.assertRaisesRegexp(SystemExit, '1'):
with self.swap_check_backend_python_libs:
pre_push_hook.main(args=[])
self.assertTrue(
'Push aborted due to failing typescript checks.' in self.print_arr)
def test_strict_typescript_check_failiure(self):
self.does_diff_include_ts_files = True
def mock_run_script_and_get_returncode(script):
if script == pre_push_hook.STRICT_TYPESCRIPT_CHECKS_CMDS:
return 1
return 0
run_script_and_get_returncode_swap = self.swap(
pre_push_hook, 'run_script_and_get_returncode',
mock_run_script_and_get_returncode)
with self.get_remote_name_swap, self.get_refs_swap, self.print_swap:
with self.collect_files_swap, self.uncommitted_files_swap:
with self.check_output_swap, self.start_linter_swap:
with self.ts_swap, run_script_and_get_returncode_swap:
with self.execute_mypy_checks_swap:
with self.assertRaisesRegexp(SystemExit, '1'):
with self.swap_check_backend_python_libs:
pre_push_hook.main(args=[])
self.assertTrue(
'Push aborted due to failing typescript checks in '
'strict mode.' in self.print_arr)
def test_frontend_test_failure(self):
self.does_diff_include_js_or_ts_files = True
def mock_run_script_and_get_returncode(unused_script):
return 1
run_script_and_get_returncode_swap = self.swap(
pre_push_hook, 'run_script_and_get_returncode',
mock_run_script_and_get_returncode)
with self.get_remote_name_swap, self.get_refs_swap, self.print_swap:
with self.collect_files_swap, self.uncommitted_files_swap:
with self.check_output_swap, self.start_linter_swap:
with self.js_or_ts_swap, run_script_and_get_returncode_swap:
with self.execute_mypy_checks_swap:
with self.assertRaisesRegexp(SystemExit, '1'):
with self.swap_check_backend_python_libs:
pre_push_hook.main(args=[])
self.assertTrue(
'Push aborted due to failing frontend tests.' in self.print_arr)
def test_invalid_ci_e2e_test_suites_failure(self):
self.does_diff_include_ci_config_or_js_files = True
def mock_run_script_and_get_returncode(unused_script):
return 1
run_script_and_get_returncode_swap = self.swap(
pre_push_hook, 'run_script_and_get_returncode',
mock_run_script_and_get_returncode)
with self.get_remote_name_swap, self.get_refs_swap, self.print_swap:
with self.collect_files_swap, self.uncommitted_files_swap:
with self.check_output_swap, self.start_linter_swap:
with run_script_and_get_returncode_swap:
with self.ci_config_or_js_files_swap:
with self.execute_mypy_checks_swap:
with self.assertRaisesRegexp(SystemExit, '1'):
with self.swap_check_backend_python_libs:
pre_push_hook.main(args=[])
self.assertTrue(
'Push aborted due to failing e2e test configuration check.'
in self.print_arr)
def test_main_with_install_arg(self):
check_function_calls = {
'install_hook_is_called': False
}
def mock_install_hook():
check_function_calls['install_hook_is_called'] = True
with self.swap(
pre_push_hook, 'install_hook', mock_install_hook), (
self.swap_check_backend_python_libs):
pre_push_hook.main(args=['--install'])
def test_main_without_install_arg_and_errors(self):
with self.get_remote_name_swap, self.get_refs_swap, self.print_swap:
with self.collect_files_swap, self.uncommitted_files_swap:
with self.check_output_swap, self.start_linter_swap:
with self.js_or_ts_swap:
with self.execute_mypy_checks_swap:
with self.swap_check_backend_python_libs:
pre_push_hook.main(args=[])
def test_main_exits_when_mismatches_exist_in_backend_python_libs(self):
"""Test that main exits with correct error message when mismatches are
found between the installed python libraries in
`third_party/python_libs` and the compiled 'requirements.txt' file.
"""
def mock_get_mismatches():
return {
'library': ('version', 'version')
}
def mock_exit_error(error_code):
self.assertEqual(error_code, 1)
swap_get_mismatches = self.swap(
install_backend_python_libs, 'get_mismatches',
mock_get_mismatches)
swap_sys_exit = self.swap(sys, 'exit', mock_exit_error)
with self.print_swap, swap_sys_exit, swap_get_mismatches:
pre_push_hook.check_for_backend_python_library_inconsistencies()
self.assertEqual(
self.print_arr,
[
'Your currently installed python libraries do not match the\n'
'libraries listed in your "requirements.txt" file. Here is a\n'
'full list of library/version discrepancies:\n',
'Library |Requirements Version '
'|Currently Installed Version',
'library |version '
'|version ',
'\n',
'Please fix these discrepancies by editing the '
'`requirements.in`\nfile or running '
'`scripts.install_third_party` to regenerate\nthe '
'`third_party/python_libs` directory.\n\n'
])
def test_main_exits_when_missing_backend_python_lib(self):
"""Test that main exits with correct error message when a python
library required in `requirements.txt` is missing in
`third_party/python_libs`.
"""
def mock_get_mismatches():
return {
'library': ('version', None)
}
def mock_exit_error(error_code):
self.assertEqual(error_code, 1)
swap_get_mismatches = self.swap(
install_backend_python_libs, 'get_mismatches',
mock_get_mismatches)
swap_sys_exit = self.swap(sys, 'exit', mock_exit_error)
with self.print_swap, swap_sys_exit, swap_get_mismatches:
pre_push_hook.check_for_backend_python_library_inconsistencies()
self.assertEqual(
self.print_arr,
[
'Your currently installed python libraries do not match the\n'
'libraries listed in your "requirements.txt" file. Here is a\n'
'full list of library/version discrepancies:\n',
'Library |Requirements Version '
'|Currently Installed Version',
'library |version '
'|None ',
'\n',
'Please fix these discrepancies by editing the '
'`requirements.in`\nfile or running '
'`scripts.install_third_party` to regenerate\nthe '
'`third_party/python_libs` directory.\n\n'
])
def test_main_with_no_inconsistencies_in_backend_python_libs(self):
def mock_get_mismatches():
return {}
swap_get_mismatches = self.swap(
install_backend_python_libs,
'get_mismatches',
mock_get_mismatches)
with swap_get_mismatches, self.print_swap:
pre_push_hook.check_for_backend_python_library_inconsistencies()
self.assertEqual(
self.print_arr,
['Python dependencies consistency check succeeded.'])