@@ -192,86 +192,51 @@ def get_write_mock_output(write_mock):
192192 return '' .join (call [0 ][0 ] for call in write_mock .call_args_list )
193193
194194
195- def test_run_all_hooks_passing (repo_with_passing_hook ):
196- stage_a_file ()
197- runner = Runner (repo_with_passing_hook )
195+ def _test_run (repo , options , expected_outputs , expected_ret , stage ):
196+ if stage :
197+ stage_a_file ()
198+ runner = Runner (repo )
198199 args = auto_namedtuple (
199- all_files = False , color = False , verbose = False , hook = None ,
200- )
201- write_mock = mock .Mock ()
202- ret = commands .run (runner , args , write = write_mock )
203- assert ret == 0
204- printed = get_write_mock_output (write_mock )
205- assert 'Bash hook' in printed
206- assert 'Passed' in printed
207-
208-
209- def test_verbose_prints_output (repo_with_passing_hook ):
210- stage_a_file ()
211- runner = Runner (repo_with_passing_hook )
212- args = auto_namedtuple (
213- all_files = False , color = False , verbose = True , hook = None ,
200+ ** dict (
201+ dict (all_files = False , color = False , verbose = False , hook = None ),
202+ ** options
203+ )
214204 )
215205 write_mock = mock .Mock ()
216206 ret = commands .run (runner , args , write = write_mock )
217- assert ret == 0
207+ assert ret == expected_ret
218208 printed = get_write_mock_output (write_mock )
219- assert 'foo.py\n Hello World\n ' in printed
209+ for expected_output_part in expected_outputs :
210+ assert expected_output_part in printed
220211
221212
222213def test_run_all_hooks_failing (repo_with_failing_hook ):
223- stage_a_file ()
224- runner = Runner (repo_with_failing_hook )
225- args = auto_namedtuple (
226- all_files = False , color = False , verbose = False , hook = None ,
227- )
228- write_mock = mock .Mock ()
229- ret = commands .run (runner , args , write = write_mock )
230- assert ret == 1
231- printed = get_write_mock_output (write_mock )
232- assert 'Failing hook' in printed
233- assert 'Failed' in printed
234- assert 'Fail\n foo.py\n ' in printed
235-
236-
237- def test_run_a_specific_hook (repo_with_passing_hook ):
238- stage_a_file ()
239- runner = Runner (repo_with_passing_hook )
240- args = auto_namedtuple (
241- all_files = False , color = False , verbose = False , hook = 'bash_hook' ,
242- )
243- write_mock = mock .Mock ()
244- ret = commands .run (runner , args , write = write_mock )
245- assert ret == 0
246- printed = get_write_mock_output (write_mock )
247- assert 'Bash hook' in printed
248- assert 'Passed' in printed
249-
250-
251- def test_run_a_non_existing_hook (repo_with_passing_hook ):
252- stage_a_file ()
253- runner = Runner (repo_with_passing_hook )
254- args = auto_namedtuple (
255- all_files = False , color = False , verbose = False , hook = 'nope' ,
214+ _test_run (
215+ repo_with_failing_hook ,
216+ {},
217+ ('Failing hook' , 'Failed' , 'Fail\n foo.py\n ' ),
218+ 1 ,
219+ True ,
256220 )
257- write_mock = mock .Mock ()
258- ret = commands .run (runner , args , write = write_mock )
259- assert ret == 1
260- printed = get_write_mock_output (write_mock )
261- assert 'No hook with id `nope`' in printed
262221
263222
264- def test_run_all_files (repo_with_passing_hook ):
265- stage_a_file ()
266- runner = Runner (repo_with_passing_hook )
267- args = auto_namedtuple (
268- all_files = True , color = False , verbose = True , hook = None ,
223+ @pytest .mark .parametrize (
224+ ('options' , 'outputs' , 'expected_ret' , 'stage' ),
225+ (
226+ ({}, ('Bash hook' , 'Passed' ), 0 , True ),
227+ ({'verbose' : True }, ('foo.py\n Hello World' ,), 0 , True ),
228+ ({'hook' : 'bash_hook' }, ('Bash hook' , 'Passed' ), 0 , True ),
229+ ({'hook' : 'nope' }, ('No hook with id `nope`' ,), 1 , True ),
230+ # All the files in the repo.
231+ # This seems kind of weird but it is beacuse py.test reuses fixtures
232+ (
233+ {'all_files' : True , 'verbose' : True },
234+ ('hooks.yaml' , 'bin/hook.sh' , 'foo.py' , 'dummy' ),
235+ 0 ,
236+ True ,
237+ ),
238+ ({}, ('Bash hook' , '(no files to check)' , 'Skipped' ), 0 , False ),
269239 )
270- write_mock = mock .Mock ()
271- ret = commands .run (runner , args , write = write_mock )
272- assert ret == 0
273- printed = get_write_mock_output (write_mock )
274- # These are all the files checked into the repo.
275- # This seems kind of weird but it is because py.test reuses fixtures
276- for filename in 'hooks.yaml bin/hook.sh foo.py dummy' .split ():
277- assert filename in printed
240+ )
241+ def test_run (repo_with_passing_hook , options , outputs , expected_ret , stage ):
242+ _test_run (repo_with_passing_hook , options , outputs , expected_ret , stage )
0 commit comments