1111import mock
1212import pytest
1313
14+ import pre_commit .constants as C
1415from pre_commit .commands .install_uninstall import install
1516from pre_commit .commands .run import _get_skips
1617from pre_commit .commands .run import _has_unmerged_paths
2425from testing .auto_namedtuple import auto_namedtuple
2526from testing .fixtures import add_config_to_repo
2627from testing .fixtures import make_consuming_repo
28+ from testing .fixtures import modify_config
2729
2830
2931@pytest .yield_fixture
@@ -313,9 +315,8 @@ def test_multiple_hooks_same_id(
313315):
314316 with cwd (repo_with_passing_hook ):
315317 # Add bash hook on there again
316- with io .open ('.pre-commit-config.yaml' , 'a+' ) as config_file :
317- config_file .write (' - id: bash_hook\n ' )
318- cmd_output ('git' , 'add' , '.pre-commit-config.yaml' )
318+ with modify_config () as config :
319+ config [0 ]['hooks' ].append ({'id' : 'bash_hook' })
319320 stage_a_file ()
320321
321322 ret , output = _do_run (repo_with_passing_hook , _get_opts ())
@@ -343,12 +344,8 @@ def test_stdout_write_bug_py26(
343344 repo_with_failing_hook , mock_out_store_directory , tempdir_factory ,
344345):
345346 with cwd (repo_with_failing_hook ):
346- # Add bash hook on there again
347- with io .open (
348- '.pre-commit-config.yaml' , 'a+' , encoding = 'UTF-8' ,
349- ) as config_file :
350- config_file .write (' args: ["☃"]\n ' )
351- cmd_output ('git' , 'add' , '.pre-commit-config.yaml' )
347+ with modify_config () as config :
348+ config [0 ]['hooks' ][0 ]['args' ] = ['☃' ]
352349 stage_a_file ()
353350
354351 install (Runner (repo_with_failing_hook ))
@@ -382,8 +379,8 @@ def test_lots_of_files(mock_out_store_directory, tempdir_factory):
382379 git_path = make_consuming_repo (tempdir_factory , 'python_hooks_repo' )
383380 with cwd (git_path ):
384381 # Override files so we run against them
385- with io . open ( '.pre-commit-config.yaml' , 'a+' ) as config_file :
386- config_file . write ( ' files: "" \n ' )
382+ with modify_config ( ) as config :
383+ config [ 0 ][ 'hooks' ][ 0 ][ ' files' ] = ''
387384
388385 # Write a crap ton of files
389386 for i in range (400 ):
@@ -463,9 +460,7 @@ def test_local_hook_for_stages(
463460 )
464461
465462
466- def test_local_hook_passes (
467- repo_with_passing_hook , mock_out_store_directory ,
468- ):
463+ def test_local_hook_passes (repo_with_passing_hook , mock_out_store_directory ):
469464 config = OrderedDict ((
470465 ('repo' , 'local' ),
471466 ('hooks' , (OrderedDict ((
@@ -497,9 +492,7 @@ def test_local_hook_passes(
497492 )
498493
499494
500- def test_local_hook_fails (
501- repo_with_passing_hook , mock_out_store_directory ,
502- ):
495+ def test_local_hook_fails (repo_with_passing_hook , mock_out_store_directory ):
503496 config = OrderedDict ((
504497 ('repo' , 'local' ),
505498 ('hooks' , [OrderedDict ((
@@ -525,60 +518,47 @@ def test_local_hook_fails(
525518 )
526519
527520
521+ @pytest .yield_fixture
522+ def modified_config_repo (repo_with_passing_hook ):
523+ with modify_config (repo_with_passing_hook , commit = False ) as config :
524+ # Some minor modification
525+ config [0 ]['hooks' ][0 ]['files' ] = ''
526+ yield repo_with_passing_hook
527+
528+
528529def test_allow_unstaged_config_option (
529- repo_with_passing_hook , mock_out_store_directory ,
530+ modified_config_repo , mock_out_store_directory ,
530531):
531- with cwd (repo_with_passing_hook ):
532- with io .open ('.pre-commit-config.yaml' , 'a+' ) as config_file :
533- # writing a newline should be relatively harmless to get a change
534- config_file .write ('\n ' )
535-
536532 args = _get_opts (allow_unstaged_config = True )
537- ret , printed = _do_run (repo_with_passing_hook , args )
538- assert b'You have an unstaged config file' in printed
539- assert b'have specified the --allow-unstaged-config option.' in printed
533+ ret , printed = _do_run (modified_config_repo , args )
534+ expected = (
535+ b'You have an unstaged config file and have specified the '
536+ b'--allow-unstaged-config option.'
537+ )
538+ assert expected in printed
540539 assert ret == 0
541540
542541
543- def modify_config (path ):
544- with cwd (path ):
545- with io .open ('.pre-commit-config.yaml' , 'a+' ) as config_file :
546- # writing a newline should be relatively harmless to get a change
547- config_file .write ('\n ' )
548-
549-
550542def test_no_allow_unstaged_config_option (
551- repo_with_passing_hook , mock_out_store_directory ,
543+ modified_config_repo , mock_out_store_directory ,
552544):
553- modify_config (repo_with_passing_hook )
554545 args = _get_opts (allow_unstaged_config = False )
555- ret , printed = _do_run (repo_with_passing_hook , args )
546+ ret , printed = _do_run (modified_config_repo , args )
556547 assert b'Your .pre-commit-config.yaml is unstaged.' in printed
557548 assert ret == 1
558549
559550
560- def test_no_stash_suppresses_allow_unstaged_config_option (
561- repo_with_passing_hook , mock_out_store_directory ,
562- ):
563- modify_config (repo_with_passing_hook )
564- args = _get_opts (allow_unstaged_config = False , no_stash = True )
565- ret , printed = _do_run (repo_with_passing_hook , args )
566- assert b'Your .pre-commit-config.yaml is unstaged.' not in printed
567-
568-
569- def test_all_files_suppresses_allow_unstaged_config_option (
570- repo_with_passing_hook , mock_out_store_directory ,
571- ):
572- modify_config (repo_with_passing_hook )
573- args = _get_opts (all_files = True )
574- ret , printed = _do_run (repo_with_passing_hook , args )
575- assert b'Your .pre-commit-config.yaml is unstaged.' not in printed
576-
577-
578- def test_files_suppresses_allow_unstaged_config_option (
579- repo_with_passing_hook , mock_out_store_directory ,
551+ @pytest .mark .parametrize (
552+ 'opts' ,
553+ (
554+ {'allow_unstaged_config' : False , 'no_stash' : True },
555+ {'all_files' : True },
556+ {'files' : [C .CONFIG_FILE ]},
557+ ),
558+ )
559+ def test_unstaged_message_suppressed (
560+ modified_config_repo , mock_out_store_directory , opts ,
580561):
581- modify_config (repo_with_passing_hook )
582- args = _get_opts (files = ['.pre-commit-config.yaml' ])
583- ret , printed = _do_run (repo_with_passing_hook , args )
562+ args = _get_opts (** opts )
563+ ret , printed = _do_run (modified_config_repo , args )
584564 assert b'Your .pre-commit-config.yaml is unstaged.' not in printed
0 commit comments