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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
|
import os.path
import shlex
import sys
import time
from typing import MutableMapping
from unittest import mock
import pytest
import pre_commit.constants as C
from pre_commit import color
from pre_commit.commands.install_uninstall import install
from pre_commit.commands.run import _compute_cols
from pre_commit.commands.run import _full_msg
from pre_commit.commands.run import _get_skips
from pre_commit.commands.run import _has_unmerged_paths
from pre_commit.commands.run import _start_msg
from pre_commit.commands.run import Classifier
from pre_commit.commands.run import filter_by_include_exclude
from pre_commit.commands.run import run
from pre_commit.util import cmd_output
from pre_commit.util import make_executable
from testing.auto_namedtuple import auto_namedtuple
from testing.fixtures import add_config_to_repo
from testing.fixtures import git_dir
from testing.fixtures import make_consuming_repo
from testing.fixtures import modify_config
from testing.fixtures import read_config
from testing.fixtures import sample_meta_config
from testing.fixtures import write_config
from testing.util import cmd_output_mocked_pre_commit_home
from testing.util import cwd
from testing.util import git_commit
from testing.util import run_opts
def test_start_msg():
ret = _start_msg(start='start', end_len=5, cols=15)
# 4 dots: 15 - 5 - 5 - 1
assert ret == 'start....'
def test_full_msg():
ret = _full_msg(
start='start',
end_msg='end',
end_color='',
use_color=False,
cols=15,
)
# 6 dots: 15 - 5 - 3 - 1
assert ret == 'start......end\n'
def test_full_msg_with_cjk():
ret = _full_msg(
start='啊あ아',
end_msg='end',
end_color='',
use_color=False,
cols=15,
)
# 5 dots: 15 - 6 - 3 - 1
assert ret == '啊あ아.....end\n'
def test_full_msg_with_color():
ret = _full_msg(
start='start',
end_msg='end',
end_color=color.RED,
use_color=True,
cols=15,
)
# 6 dots: 15 - 5 - 3 - 1
assert ret == f'start......{color.RED}end{color.NORMAL}\n'
def test_full_msg_with_postfix():
ret = _full_msg(
start='start',
postfix='post ',
end_msg='end',
end_color='',
use_color=False,
cols=20,
)
# 6 dots: 20 - 5 - 5 - 3 - 1
assert ret == 'start......post end\n'
def test_full_msg_postfix_not_colored():
ret = _full_msg(
start='start',
postfix='post ',
end_msg='end',
end_color=color.RED,
use_color=True,
cols=20,
)
# 6 dots: 20 - 5 - 5 - 3 - 1
assert ret == f'start......post {color.RED}end{color.NORMAL}\n'
@pytest.fixture
def repo_with_passing_hook(tempdir_factory):
git_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
with cwd(git_path):
yield git_path
@pytest.fixture
def repo_with_failing_hook(tempdir_factory):
git_path = make_consuming_repo(tempdir_factory, 'failing_hook_repo')
with cwd(git_path):
yield git_path
@pytest.fixture
def aliased_repo(tempdir_factory):
git_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
with cwd(git_path):
with modify_config() as config:
config['repos'][0]['hooks'].append(
{'id': 'bash_hook', 'alias': 'foo_bash'},
)
stage_a_file()
yield git_path
def stage_a_file(filename='foo.py'):
open(filename, 'a').close()
cmd_output('git', 'add', filename)
def _do_run(cap_out, store, repo, args, environ={}, config_file=C.CONFIG_FILE):
with cwd(repo): # replicates `main._adjust_args_and_chdir` behaviour
ret = run(config_file, store, args, environ=environ)
printed = cap_out.get_bytes()
return ret, printed
def _test_run(
cap_out, store, repo, opts, expected_outputs, expected_ret, stage,
config_file=C.CONFIG_FILE,
):
if stage:
stage_a_file()
args = run_opts(**opts)
ret, printed = _do_run(cap_out, store, repo, args, config_file=config_file)
assert ret == expected_ret, (ret, expected_ret, printed)
for expected_output_part in expected_outputs:
assert expected_output_part in printed
def test_run_all_hooks_failing(cap_out, store, repo_with_failing_hook):
_test_run(
cap_out,
store,
repo_with_failing_hook,
{},
(
b'Failing hook',
b'Failed',
b'hook id: failing_hook',
b'Fail\nfoo.py\n',
),
expected_ret=1,
stage=True,
)
def test_arbitrary_bytes_hook(cap_out, store, tempdir_factory):
git_path = make_consuming_repo(tempdir_factory, 'arbitrary_bytes_repo')
with cwd(git_path):
_test_run(
cap_out, store, git_path, {}, (b'\xe2\x98\x83\xb2\n',), 1, True,
)
def test_hook_that_modifies_but_returns_zero(cap_out, store, tempdir_factory):
git_path = make_consuming_repo(
tempdir_factory, 'modified_file_returns_zero_repo',
)
with cwd(git_path):
stage_a_file('bar.py')
_test_run(
cap_out,
store,
git_path,
{},
(
# The first should fail
b'Failed',
# With a modified file (default message + the hook's output)
b'- files were modified by this hook\n\n'
b'Modified: foo.py',
# The next hook should pass despite the first modifying
b'Passed',
# The next hook should fail
b'Failed',
# bar.py was modified, but provides no additional output
b'- files were modified by this hook\n',
),
1,
True,
)
def test_types_hook_repository(cap_out, store, tempdir_factory):
git_path = make_consuming_repo(tempdir_factory, 'types_repo')
with cwd(git_path):
stage_a_file('bar.py')
stage_a_file('bar.notpy')
ret, printed = _do_run(cap_out, store, git_path, run_opts())
assert ret == 1
assert b'bar.py' in printed
assert b'bar.notpy' not in printed
def test_types_or_hook_repository(cap_out, store, tempdir_factory):
git_path = make_consuming_repo(tempdir_factory, 'types_or_repo')
with cwd(git_path):
stage_a_file('bar.notpy')
stage_a_file('bar.pxd')
stage_a_file('bar.py')
ret, printed = _do_run(cap_out, store, git_path, run_opts())
assert ret == 1
assert b'bar.notpy' not in printed
assert b'bar.pxd' in printed
assert b'bar.py' in printed
def test_exclude_types_hook_repository(cap_out, store, tempdir_factory):
git_path = make_consuming_repo(tempdir_factory, 'exclude_types_repo')
with cwd(git_path):
with open('exe', 'w') as exe:
exe.write('#!/usr/bin/env python3\n')
make_executable('exe')
cmd_output('git', 'add', 'exe')
stage_a_file('bar.py')
ret, printed = _do_run(cap_out, store, git_path, run_opts())
assert ret == 1
assert b'bar.py' in printed
assert b'exe' not in printed
def test_global_exclude(cap_out, store, in_git_dir):
config = {
'exclude': r'^foo\.py$',
'repos': [{'repo': 'meta', 'hooks': [{'id': 'identity'}]}],
}
write_config('.', config)
open('foo.py', 'a').close()
open('bar.py', 'a').close()
cmd_output('git', 'add', '.')
opts = run_opts(verbose=True)
ret, printed = _do_run(cap_out, store, str(in_git_dir), opts)
assert ret == 0
# Does not contain foo.py since it was excluded
assert printed.startswith(f'identity{"." * 65}Passed\n'.encode())
assert printed.endswith(b'\n\n.pre-commit-config.yaml\nbar.py\n\n')
def test_global_files(cap_out, store, in_git_dir):
config = {
'files': r'^bar\.py$',
'repos': [{'repo': 'meta', 'hooks': [{'id': 'identity'}]}],
}
write_config('.', config)
open('foo.py', 'a').close()
open('bar.py', 'a').close()
cmd_output('git', 'add', '.')
opts = run_opts(verbose=True)
ret, printed = _do_run(cap_out, store, str(in_git_dir), opts)
assert ret == 0
# Does not contain foo.py since it was excluded
assert printed.startswith(f'identity{"." * 65}Passed\n'.encode())
assert printed.endswith(b'\n\nbar.py\n\n')
@pytest.mark.parametrize(
('t1', 't2', 'expected'),
(
(1.234, 2., b'\n- duration: 0.77s\n'),
(1., 1., b'\n- duration: 0s\n'),
),
)
def test_verbose_duration(cap_out, store, in_git_dir, t1, t2, expected):
write_config('.', {'repo': 'meta', 'hooks': [{'id': 'identity'}]})
cmd_output('git', 'add', '.')
opts = run_opts(verbose=True)
with mock.patch.object(time, 'time', side_effect=(t1, t2)):
ret, printed = _do_run(cap_out, store, str(in_git_dir), opts)
assert ret == 0
assert expected in printed
@pytest.mark.parametrize(
('args', 'expected_out'),
[
(
{
'show_diff_on_failure': True,
},
b'All changes made by hooks:',
),
(
{
'show_diff_on_failure': True,
'color': True,
},
b'All changes made by hooks:',
),
(
{
'show_diff_on_failure': True,
'all_files': True,
},
b'reproduce locally with: pre-commit run --all-files',
),
],
)
def test_show_diff_on_failure(
args,
expected_out,
capfd,
cap_out,
store,
tempdir_factory,
):
git_path = make_consuming_repo(
tempdir_factory, 'modified_file_returns_zero_repo',
)
with cwd(git_path):
stage_a_file('bar.py')
_test_run(
cap_out, store, git_path, args,
# we're only testing the output after running
expected_out, 1, True,
)
out, _ = capfd.readouterr()
assert 'diff --git' in out
@pytest.mark.parametrize(
('options', 'outputs', 'expected_ret', 'stage'),
(
({}, (b'Bash hook', b'Passed'), 0, True),
({'verbose': True}, (b'foo.py\nHello World',), 0, True),
({'hook': 'bash_hook'}, (b'Bash hook', b'Passed'), 0, True),
(
{'hook': 'nope'},
(b'No hook with id `nope` in stage `commit`',),
1,
True,
),
(
{'hook': 'nope', 'hook_stage': 'push'},
(b'No hook with id `nope` in stage `push`',),
1,
True,
),
(
{'all_files': True, 'verbose': True},
(b'foo.py',),
0,
True,
),
(
{'files': ('foo.py',), 'verbose': True},
(b'foo.py',),
0,
True,
),
({}, (b'Bash hook', b'(no files to check)', b'Skipped'), 0, False),
),
)
def test_run(
cap_out,
store,
repo_with_passing_hook,
options,
outputs,
expected_ret,
stage,
):
_test_run(
cap_out,
store,
repo_with_passing_hook,
options,
outputs,
expected_ret,
stage,
)
def test_run_output_logfile(cap_out, store, tempdir_factory):
expected_output = (
b'This is STDOUT output\n',
b'This is STDERR output\n',
)
git_path = make_consuming_repo(tempdir_factory, 'logfile_repo')
with cwd(git_path):
_test_run(
cap_out,
store,
git_path, {},
expected_output,
expected_ret=1,
stage=True,
)
logfile_path = os.path.join(git_path, 'test.log')
assert os.path.exists(logfile_path)
with open(logfile_path, 'rb') as logfile:
logfile_content = logfile.readlines()
for expected_output_part in expected_output:
assert expected_output_part in logfile_content
def test_always_run(cap_out, store, repo_with_passing_hook):
with modify_config() as config:
config['repos'][0]['hooks'][0]['always_run'] = True
_test_run(
cap_out,
store,
repo_with_passing_hook,
{},
(b'Bash hook', b'Passed'),
0,
stage=False,
)
def test_always_run_alt_config(cap_out, store, repo_with_passing_hook):
repo_root = '.'
config = read_config(repo_root)
config['repos'][0]['hooks'][0]['always_run'] = True
alt_config_file = 'alternate_config.yaml'
add_config_to_repo(repo_root, config, config_file=alt_config_file)
_test_run(
cap_out,
store,
repo_with_passing_hook,
{},
(b'Bash hook', b'Passed'),
0,
stage=False,
config_file=alt_config_file,
)
def test_hook_verbose_enabled(cap_out, store, repo_with_passing_hook):
with modify_config() as config:
config['repos'][0]['hooks'][0]['always_run'] = True
config['repos'][0]['hooks'][0]['verbose'] = True
_test_run(
cap_out,
store,
repo_with_passing_hook,
{},
(b'Hello World',),
0,
stage=False,
)
@pytest.mark.parametrize(
('from_ref', 'to_ref'), (('master', ''), ('', 'master')),
)
def test_from_ref_to_ref_error_msg_error(
cap_out, store, repo_with_passing_hook, from_ref, to_ref,
):
args = run_opts(from_ref=from_ref, to_ref=to_ref)
ret, printed = _do_run(cap_out, store, repo_with_passing_hook, args)
assert ret == 1
assert b'Specify both --from-ref and --to-ref.' in printed
def test_all_push_options_ok(cap_out, store, repo_with_passing_hook):
args = run_opts(
from_ref='master', to_ref='master',
remote_branch='master',
local_branch='master',
remote_name='origin', remote_url='https://example.com/repo',
)
ret, printed = _do_run(cap_out, store, repo_with_passing_hook, args)
assert ret == 0
assert b'Specify both --from-ref and --to-ref.' not in printed
def test_is_squash_merge(cap_out, store, repo_with_passing_hook):
args = run_opts(is_squash_merge='1')
environ: MutableMapping[str, str] = {}
ret, printed = _do_run(
cap_out, store, repo_with_passing_hook, args, environ,
)
assert environ['PRE_COMMIT_IS_SQUASH_MERGE'] == '1'
def test_checkout_type(cap_out, store, repo_with_passing_hook):
args = run_opts(from_ref='', to_ref='', checkout_type='1')
environ: MutableMapping[str, str] = {}
ret, printed = _do_run(
cap_out, store, repo_with_passing_hook, args, environ,
)
assert environ['PRE_COMMIT_CHECKOUT_TYPE'] == '1'
def test_has_unmerged_paths(in_merge_conflict):
assert _has_unmerged_paths() is True
cmd_output('git', 'add', '.')
assert _has_unmerged_paths() is False
def test_merge_conflict(cap_out, store, in_merge_conflict):
ret, printed = _do_run(cap_out, store, in_merge_conflict, run_opts())
assert ret == 1
assert b'Unmerged files. Resolve before committing.' in printed
def test_merge_conflict_modified(cap_out, store, in_merge_conflict):
# Touch another file so we have unstaged non-conflicting things
assert os.path.exists('placeholder')
with open('placeholder', 'w') as placeholder_file:
placeholder_file.write('bar\nbaz\n')
ret, printed = _do_run(cap_out, store, in_merge_conflict, run_opts())
assert ret == 1
assert b'Unmerged files. Resolve before committing.' in printed
def test_merge_conflict_resolved(cap_out, store, in_merge_conflict):
cmd_output('git', 'add', '.')
ret, printed = _do_run(cap_out, store, in_merge_conflict, run_opts())
for msg in (
b'Checking merge-conflict files only.', b'Bash hook', b'Passed',
):
assert msg in printed
@pytest.mark.parametrize(
('hooks', 'expected'),
(
([], 80),
([auto_namedtuple(id='a', name='a' * 51)], 81),
(
[
auto_namedtuple(id='a', name='a' * 51),
auto_namedtuple(id='b', name='b' * 52),
],
82,
),
),
)
def test_compute_cols(hooks, expected):
assert _compute_cols(hooks) == expected
@pytest.mark.parametrize(
('environ', 'expected_output'),
(
({}, set()),
({'SKIP': ''}, set()),
({'SKIP': ','}, set()),
({'SKIP': ',foo'}, {'foo'}),
({'SKIP': 'foo'}, {'foo'}),
({'SKIP': 'foo,bar'}, {'foo', 'bar'}),
({'SKIP': ' foo , bar'}, {'foo', 'bar'}),
),
)
def test_get_skips(environ, expected_output):
ret = _get_skips(environ)
assert ret == expected_output
def test_skip_hook(cap_out, store, repo_with_passing_hook):
ret, printed = _do_run(
cap_out, store, repo_with_passing_hook, run_opts(),
{'SKIP': 'bash_hook'},
)
for msg in (b'Bash hook', b'Skipped'):
assert msg in printed
def test_skip_aliased_hook(cap_out, store, aliased_repo):
ret, printed = _do_run(
cap_out, store, aliased_repo,
run_opts(hook='foo_bash'),
{'SKIP': 'foo_bash'},
)
assert ret == 0
# Only the aliased hook runs and is skipped
for msg in (b'Bash hook', b'Skipped'):
assert printed.count(msg) == 1
def test_skip_bypasses_installation(cap_out, store, repo_with_passing_hook):
config = {
'repo': 'local',
'hooks': [
{
'id': 'skipme',
'name': 'skipme',
'entry': 'skipme',
'language': 'python',
'additional_dependencies': ['/pre-commit-does-not-exist'],
},
],
}
add_config_to_repo(repo_with_passing_hook, config)
ret, printed = _do_run(
cap_out, store, repo_with_passing_hook,
run_opts(all_files=True),
{'SKIP': 'skipme'},
)
assert ret == 0
def test_hook_id_not_in_non_verbose_output(
cap_out, store, repo_with_passing_hook,
):
ret, printed = _do_run(
cap_out, store, repo_with_passing_hook, run_opts(verbose=False),
)
assert b'[bash_hook]' not in printed
def test_hook_id_in_verbose_output(cap_out, store, repo_with_passing_hook):
ret, printed = _do_run(
cap_out, store, repo_with_passing_hook, run_opts(verbose=True),
)
assert b'- hook id: bash_hook' in printed
def test_multiple_hooks_same_id(cap_out, store, repo_with_passing_hook):
with cwd(repo_with_passing_hook):
# Add bash hook on there again
with modify_config() as config:
config['repos'][0]['hooks'].append({'id': 'bash_hook'})
stage_a_file()
ret, output = _do_run(cap_out, store, repo_with_passing_hook, run_opts())
assert ret == 0
assert output.count(b'Bash hook') == 2
def test_aliased_hook_run(cap_out, store, aliased_repo):
ret, output = _do_run(
cap_out, store, aliased_repo,
run_opts(verbose=True, hook='bash_hook'),
)
assert ret == 0
# Both hooks will run since they share the same ID
assert output.count(b'Bash hook') == 2
ret, output = _do_run(
cap_out, store, aliased_repo,
run_opts(verbose=True, hook='foo_bash'),
)
assert ret == 0
# Only the aliased hook runs
assert output.count(b'Bash hook') == 1
def test_non_ascii_hook_id(repo_with_passing_hook, tempdir_factory):
with cwd(repo_with_passing_hook):
_, stdout, _ = cmd_output_mocked_pre_commit_home(
sys.executable, '-m', 'pre_commit.main', 'run', '☃',
retcode=None, tempdir_factory=tempdir_factory,
)
assert 'UnicodeDecodeError' not in stdout
# Doesn't actually happen, but a reasonable assertion
assert 'UnicodeEncodeError' not in stdout
def test_stdout_write_bug_py26(repo_with_failing_hook, store, tempdir_factory):
with cwd(repo_with_failing_hook):
with modify_config() as config:
config['repos'][0]['hooks'][0]['args'] = ['☃']
stage_a_file()
install(C.CONFIG_FILE, store, hook_types=['pre-commit'])
# Have to use subprocess because pytest monkeypatches sys.stdout
_, out = git_commit(
fn=cmd_output_mocked_pre_commit_home,
tempdir_factory=tempdir_factory,
retcode=None,
)
assert 'UnicodeEncodeError' not in out
# Doesn't actually happen, but a reasonable assertion
assert 'UnicodeDecodeError' not in out
def test_lots_of_files(store, tempdir_factory):
# windows xargs seems to have a bug, here's a regression test for
# our workaround
git_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
with cwd(git_path):
# Override files so we run against them
with modify_config() as config:
config['repos'][0]['hooks'][0]['files'] = ''
# Write a crap ton of files
for i in range(400):
open(f'{"a" * 100}{i}', 'w').close()
cmd_output('git', 'add', '.')
install(C.CONFIG_FILE, store, hook_types=['pre-commit'])
git_commit(
fn=cmd_output_mocked_pre_commit_home,
tempdir_factory=tempdir_factory,
)
def test_stages(cap_out, store, repo_with_passing_hook):
config = {
'repo': 'local',
'hooks': [
{
'id': f'do-not-commit-{i}',
'name': f'hook {i}',
'entry': 'DO NOT COMMIT',
'language': 'pygrep',
'stages': [stage],
}
for i, stage in enumerate(('commit', 'push', 'manual'), 1)
],
}
add_config_to_repo(repo_with_passing_hook, config)
stage_a_file()
def _run_for_stage(stage):
args = run_opts(hook_stage=stage)
ret, printed = _do_run(cap_out, store, repo_with_passing_hook, args)
assert not ret, (ret, printed)
# this test should only run one hook
assert printed.count(b'hook ') == 1
return printed
assert _run_for_stage('commit').startswith(b'hook 1...')
assert _run_for_stage('push').startswith(b'hook 2...')
assert _run_for_stage('manual').startswith(b'hook 3...')
def test_commit_msg_hook(cap_out, store, commit_msg_repo):
filename = '.git/COMMIT_EDITMSG'
with open(filename, 'w') as f:
f.write('This is the commit message')
_test_run(
cap_out,
store,
commit_msg_repo,
{'hook_stage': 'commit-msg', 'commit_msg_filename': filename},
expected_outputs=[b'Must have "Signed off by:"', b'Failed'],
expected_ret=1,
stage=False,
)
def test_post_checkout_hook(cap_out, store, tempdir_factory):
path = git_dir(tempdir_factory)
config = {
'repo': 'meta', 'hooks': [
{'id': 'identity', 'stages': ['post-checkout']},
],
}
add_config_to_repo(path, config)
with cwd(path):
_test_run(
cap_out,
store,
path,
{'hook_stage': 'post-checkout'},
expected_outputs=[b'identity...'],
expected_ret=0,
stage=False,
)
def test_prepare_commit_msg_hook(cap_out, store, prepare_commit_msg_repo):
filename = '.git/COMMIT_EDITMSG'
with open(filename, 'w') as f:
f.write('This is the commit message')
_test_run(
cap_out,
store,
prepare_commit_msg_repo,
{'hook_stage': 'prepare-commit-msg', 'commit_msg_filename': filename},
expected_outputs=[b'Add "Signed off by:"', b'Passed'],
expected_ret=0,
stage=False,
)
with open(filename) as f:
assert 'Signed off by: ' in f.read()
def test_local_hook_passes(cap_out, store, repo_with_passing_hook):
config = {
'repo': 'local',
'hooks': [
{
'id': 'identity-copy',
'name': 'identity-copy',
'entry': '{} -m pre_commit.meta_hooks.identity'.format(
shlex.quote(sys.executable),
),
'language': 'system',
'files': r'\.py$',
},
{
'id': 'do_not_commit',
'name': 'Block if "DO NOT COMMIT" is found',
'entry': 'DO NOT COMMIT',
'language': 'pygrep',
},
],
}
add_config_to_repo(repo_with_passing_hook, config)
with open('placeholder.py', 'w') as staged_file:
staged_file.write('"""TODO: something"""\n')
cmd_output('git', 'add', 'placeholder.py')
_test_run(
cap_out,
store,
repo_with_passing_hook,
opts={},
expected_outputs=[b''],
expected_ret=0,
stage=False,
)
def test_local_hook_fails(cap_out, store, repo_with_passing_hook):
config = {
'repo': 'local',
'hooks': [{
'id': 'no-todo',
'name': 'No TODO',
'entry': 'sh -c "! grep -iI todo $@" --',
'language': 'system',
}],
}
add_config_to_repo(repo_with_passing_hook, config)
with open('placeholder.py', 'w') as staged_file:
staged_file.write('"""TODO: something"""\n')
cmd_output('git', 'add', 'placeholder.py')
_test_run(
cap_out,
store,
repo_with_passing_hook,
opts={},
expected_outputs=[b''],
expected_ret=1,
stage=False,
)
def test_meta_hook_passes(cap_out, store, repo_with_passing_hook):
add_config_to_repo(repo_with_passing_hook, sample_meta_config())
_test_run(
cap_out,
store,
repo_with_passing_hook,
opts={},
expected_outputs=[b'Check for useless excludes'],
expected_ret=0,
stage=False,
)
@pytest.fixture
def modified_config_repo(repo_with_passing_hook):
with modify_config(repo_with_passing_hook, commit=False) as config:
# Some minor modification
config['repos'][0]['hooks'][0]['files'] = ''
yield repo_with_passing_hook
def test_error_with_unstaged_config(cap_out, store, modified_config_repo):
args = run_opts()
ret, printed = _do_run(cap_out, store, modified_config_repo, args)
assert b'Your pre-commit configuration is unstaged.' in printed
assert ret == 1
def test_commit_msg_missing_filename(cap_out, store, repo_with_passing_hook):
args = run_opts(hook_stage='commit-msg')
ret, printed = _do_run(cap_out, store, repo_with_passing_hook, args)
assert ret == 1
assert printed == (
b'[ERROR] `--commit-msg-filename` is required for '
b'`--hook-stage commit-msg`\n'
)
@pytest.mark.parametrize(
'opts', (run_opts(all_files=True), run_opts(files=[C.CONFIG_FILE])),
)
def test_no_unstaged_error_with_all_files_or_files(
cap_out, store, modified_config_repo, opts,
):
ret, printed = _do_run(cap_out, store, modified_config_repo, opts)
assert b'Your pre-commit configuration is unstaged.' not in printed
def test_files_running_subdir(repo_with_passing_hook, tempdir_factory):
with cwd(repo_with_passing_hook):
os.mkdir('subdir')
open('subdir/foo.py', 'w').close()
cmd_output('git', 'add', 'subdir/foo.py')
with cwd('subdir'):
# Use subprocess to demonstrate behaviour in main
_, stdout, _ = cmd_output_mocked_pre_commit_home(
sys.executable, '-m', 'pre_commit.main', 'run', '-v',
# Files relative to where we are (#339)
'--files', 'foo.py',
tempdir_factory=tempdir_factory,
)
assert 'subdir/foo.py' in stdout
@pytest.mark.parametrize(
('pass_filenames', 'hook_args', 'expected_out'),
(
(True, [], b'foo.py'),
(False, [], b''),
(True, ['some', 'args'], b'some args foo.py'),
(False, ['some', 'args'], b'some args'),
),
)
def test_pass_filenames(
cap_out, store, repo_with_passing_hook,
pass_filenames, hook_args, expected_out,
):
with modify_config() as config:
config['repos'][0]['hooks'][0]['pass_filenames'] = pass_filenames
config['repos'][0]['hooks'][0]['args'] = hook_args
stage_a_file()
ret, printed = _do_run(
cap_out, store, repo_with_passing_hook, run_opts(verbose=True),
)
assert expected_out + b'\nHello World' in printed
assert (b'foo.py' in printed) == pass_filenames
def test_fail_fast(cap_out, store, repo_with_failing_hook):
with modify_config() as config:
# More than one hook
config['fail_fast'] = True
config['repos'][0]['hooks'] *= 2
stage_a_file()
ret, printed = _do_run(cap_out, store, repo_with_failing_hook, run_opts())
# it should have only run one hook
assert printed.count(b'Failing hook') == 1
def test_classifier_removes_dne():
classifier = Classifier(('this_file_does_not_exist',))
assert classifier.filenames == []
def test_classifier_normalizes_filenames_on_windows_to_forward_slashes(tmpdir):
with tmpdir.as_cwd():
tmpdir.join('a/b/c').ensure()
with mock.patch.object(os, 'altsep', '/'):
with mock.patch.object(os, 'sep', '\\'):
classifier = Classifier.from_config((r'a\b\c',), '', '^$')
assert classifier.filenames == ['a/b/c']
def test_classifier_does_not_normalize_backslashes_non_windows(tmpdir):
with mock.patch.object(os.path, 'lexists', return_value=True):
with mock.patch.object(os, 'altsep', None):
with mock.patch.object(os, 'sep', '/'):
classifier = Classifier.from_config((r'a/b\c',), '', '^$')
assert classifier.filenames == [r'a/b\c']
def test_classifier_empty_types_or(tmpdir):
tmpdir.join('bar').ensure()
os.symlink(tmpdir.join('bar'), tmpdir.join('foo'))
with tmpdir.as_cwd():
classifier = Classifier(('foo', 'bar'))
for_symlink = classifier.by_types(
classifier.filenames,
types=['symlink'],
types_or=[],
exclude_types=[],
)
for_file = classifier.by_types(
classifier.filenames,
types=['file'],
types_or=[],
exclude_types=[],
)
assert for_symlink == ['foo']
assert for_file == ['bar']
@pytest.fixture
def some_filenames():
return (
'.pre-commit-hooks.yaml',
'pre_commit/git.py',
'pre_commit/main.py',
)
def test_include_exclude_base_case(some_filenames):
ret = filter_by_include_exclude(some_filenames, '', '^$')
assert ret == [
'.pre-commit-hooks.yaml',
'pre_commit/git.py',
'pre_commit/main.py',
]
def test_matches_broken_symlink(tmpdir):
with tmpdir.as_cwd():
os.symlink('does-not-exist', 'link')
ret = filter_by_include_exclude({'link'}, '', '^$')
assert ret == ['link']
def test_include_exclude_total_match(some_filenames):
ret = filter_by_include_exclude(some_filenames, r'^.*\.py$', '^$')
assert ret == ['pre_commit/git.py', 'pre_commit/main.py']
def test_include_exclude_does_search_instead_of_match(some_filenames):
ret = filter_by_include_exclude(some_filenames, r'\.yaml$', '^$')
assert ret == ['.pre-commit-hooks.yaml']
def test_include_exclude_exclude_removes_files(some_filenames):
ret = filter_by_include_exclude(some_filenames, '', r'\.py$')
assert ret == ['.pre-commit-hooks.yaml']
def test_args_hook_only(cap_out, store, repo_with_passing_hook):
config = {
'repo': 'local',
'hooks': [
{
'id': 'identity-copy',
'name': 'identity-copy',
'entry': '{} -m pre_commit.meta_hooks.identity'.format(
shlex.quote(sys.executable),
),
'language': 'system',
'files': r'\.py$',
'stages': ['commit'],
},
{
'id': 'do_not_commit',
'name': 'Block if "DO NOT COMMIT" is found',
'entry': 'DO NOT COMMIT',
'language': 'pygrep',
},
],
}
add_config_to_repo(repo_with_passing_hook, config)
stage_a_file()
ret, printed = _do_run(
cap_out,
store,
repo_with_passing_hook,
run_opts(hook='do_not_commit'),
)
assert b'identity-copy' not in printed
def test_skipped_without_any_setup_for_post_checkout(in_git_dir, store):
environ = {'_PRE_COMMIT_SKIP_POST_CHECKOUT': '1'}
opts = run_opts(hook_stage='post-checkout')
assert run(C.CONFIG_FILE, store, opts, environ=environ) == 0
def test_pre_commit_env_variable_set(cap_out, store, repo_with_passing_hook):
args = run_opts()
environ: MutableMapping[str, str] = {}
ret, printed = _do_run(
cap_out, store, repo_with_passing_hook, args, environ,
)
assert environ['PRE_COMMIT'] == '1'
|