@@ -59,6 +59,7 @@ def _get_opts(
5959 origin = '' ,
6060 source = '' ,
6161 allow_unstaged_config = False ,
62+ hook_stage = 'commit'
6263):
6364 # These are mutually exclusive
6465 assert not (all_files and files )
@@ -68,6 +69,7 @@ def _get_opts(
6869 color = color ,
6970 verbose = verbose ,
7071 hook = hook ,
72+ hook_stage = hook_stage ,
7173 no_stash = no_stash ,
7274 origin = origin ,
7375 source = source ,
@@ -89,6 +91,7 @@ def _test_run(repo, options, expected_outputs, expected_ret, stage):
8991 stage_a_file ()
9092 args = _get_opts (** options )
9193 ret , printed = _do_run (repo , args )
94+
9295 assert ret == expected_ret , (ret , expected_ret , printed )
9396 for expected_output_part in expected_outputs :
9497 assert expected_output_part in printed
@@ -371,6 +374,66 @@ def test_lots_of_files(mock_out_store_directory, tempdir_factory):
371374 )
372375
373376
377+ @pytest .mark .parametrize (
378+ ('hook_stage' , 'stage_for_first_hook' , 'stage_for_second_hook' ,
379+ 'expected_output' ),
380+ (
381+ ('push' , ['commit' ], ['commit' ], [b'' , b'' ]),
382+ ('push' , ['commit' , 'push' ], ['commit' , 'push' ],
383+ [b'hook 1' , b'hook 2' ]),
384+ ('push' , [], [], [b'hook 1' , b'hook 2' ]),
385+ ('push' , [], ['commit' ], [b'hook 1' , b'' ]),
386+ ('push' , ['push' ], ['commit' ], [b'hook 1' , b'' ]),
387+ ('push' , ['commit' ], ['push' ], [b'' , b'hook 2' ]),
388+ ('commit' , ['commit' , 'push' ], ['commit' , 'push' ],
389+ [b'hook 1' , b'hook 2' ]),
390+ ('commit' , ['commit' ], ['commit' ], [b'hook 1' , b'hook 2' ]),
391+ ('commit' , [], [], [b'hook 1' , b'hook 2' ]),
392+ ('commit' , [], ['commit' ], [b'' , b'hook 2' ]),
393+ ('commit' , ['push' ], ['commit' ], [b'' , b'hook 2' ]),
394+ ('commit' , ['commit' ], ['push' ], [b'hook 1' , b'' ]),
395+ )
396+ )
397+ def test_local_hook_for_stages (
398+ repo_with_passing_hook , mock_out_store_directory ,
399+ stage_for_first_hook ,
400+ stage_for_second_hook ,
401+ hook_stage ,
402+ expected_output
403+ ):
404+ config = OrderedDict ((
405+ ('repo' , 'local' ),
406+ ('hooks' , (OrderedDict ((
407+ ('id' , 'pylint' ),
408+ ('name' , 'hook 1' ),
409+ ('entry' , 'python -m pylint.__main__' ),
410+ ('language' , 'system' ),
411+ ('files' , r'\.py$' ),
412+ ('stages' , stage_for_first_hook )
413+ )), OrderedDict ((
414+ ('id' , 'do_not_commit' ),
415+ ('name' , 'hook 2' ),
416+ ('entry' , 'DO NOT COMMIT' ),
417+ ('language' , 'pcre' ),
418+ ('files' , '^(.*)$' ),
419+ ('stages' , stage_for_second_hook )
420+ ))))
421+ ))
422+ add_config_to_repo (repo_with_passing_hook , config )
423+
424+ with io .open ('dummy.py' , 'w' ) as staged_file :
425+ staged_file .write ('"""TODO: something"""\n ' )
426+ cmd_output ('git' , 'add' , 'dummy.py' )
427+
428+ _test_run (
429+ repo_with_passing_hook ,
430+ {'hook_stage' : hook_stage },
431+ expected_outputs = expected_output ,
432+ expected_ret = 0 ,
433+ stage = False
434+ )
435+
436+
374437def test_local_hook_passes (
375438 repo_with_passing_hook , mock_out_store_directory ,
376439):
0 commit comments