77
88import pre_commit .constants as C
99from pre_commit import git
10+ from pre_commit .commands .install_uninstall import _hook_types
1011from pre_commit .commands .install_uninstall import CURRENT_HASH
1112from pre_commit .commands .install_uninstall import install
1213from pre_commit .commands .install_uninstall import install_hooks
2728from testing .util import git_commit
2829
2930
31+ def test_hook_types_explicitly_listed ():
32+ assert _hook_types (os .devnull , ['pre-push' ]) == ['pre-push' ]
33+
34+
35+ def test_hook_types_default_value_when_not_specified ():
36+ assert _hook_types (os .devnull , None ) == ['pre-commit' ]
37+
38+
39+ def test_hook_types_configured (tmpdir ):
40+ cfg = tmpdir .join ('t.cfg' )
41+ cfg .write ('default_install_hook_types: [pre-push]\n repos: []\n ' )
42+
43+ assert _hook_types (str (cfg ), None ) == ['pre-push' ]
44+
45+
46+ def test_hook_types_configured_nonsense (tmpdir ):
47+ cfg = tmpdir .join ('t.cfg' )
48+ cfg .write ('default_install_hook_types: []\n repos: []\n ' )
49+
50+ # hopefully the user doesn't do this, but the code allows it!
51+ assert _hook_types (str (cfg ), None ) == []
52+
53+
54+ def test_hook_types_configuration_has_error (tmpdir ):
55+ cfg = tmpdir .join ('t.cfg' )
56+ cfg .write ('[' )
57+
58+ assert _hook_types (str (cfg ), None ) == ['pre-commit' ]
59+
60+
3061def test_is_not_script ():
3162 assert is_our_script ('setup.py' ) is False
3263
@@ -61,7 +92,7 @@ def test_install_multiple_hooks_at_once(in_git_dir, store):
6192 install (C .CONFIG_FILE , store , hook_types = ['pre-commit' , 'pre-push' ])
6293 assert in_git_dir .join ('.git/hooks/pre-commit' ).exists ()
6394 assert in_git_dir .join ('.git/hooks/pre-push' ).exists ()
64- uninstall (hook_types = ['pre-commit' , 'pre-push' ])
95+ uninstall (C . CONFIG_FILE , hook_types = ['pre-commit' , 'pre-push' ])
6596 assert not in_git_dir .join ('.git/hooks/pre-commit' ).exists ()
6697 assert not in_git_dir .join ('.git/hooks/pre-push' ).exists ()
6798
@@ -79,14 +110,14 @@ def test_install_hooks_dead_symlink(in_git_dir, store):
79110
80111
81112def test_uninstall_does_not_blow_up_when_not_there (in_git_dir ):
82- assert uninstall (hook_types = ['pre-commit' ]) == 0
113+ assert uninstall (C . CONFIG_FILE , hook_types = ['pre-commit' ]) == 0
83114
84115
85116def test_uninstall (in_git_dir , store ):
86117 assert not in_git_dir .join ('.git/hooks/pre-commit' ).exists ()
87118 install (C .CONFIG_FILE , store , hook_types = ['pre-commit' ])
88119 assert in_git_dir .join ('.git/hooks/pre-commit' ).exists ()
89- uninstall (hook_types = ['pre-commit' ])
120+ uninstall (C . CONFIG_FILE , hook_types = ['pre-commit' ])
90121 assert not in_git_dir .join ('.git/hooks/pre-commit' ).exists ()
91122
92123
@@ -416,7 +447,7 @@ def test_uninstall_restores_legacy_hooks(tempdir_factory, store):
416447
417448 # Now install and uninstall pre-commit
418449 assert install (C .CONFIG_FILE , store , hook_types = ['pre-commit' ]) == 0
419- assert uninstall (hook_types = ['pre-commit' ]) == 0
450+ assert uninstall (C . CONFIG_FILE , hook_types = ['pre-commit' ]) == 0
420451
421452 # Make sure we installed the "old" hook correctly
422453 ret , output = _get_commit_output (tempdir_factory , touch_file = 'baz' )
@@ -451,7 +482,7 @@ def test_uninstall_doesnt_remove_not_our_hooks(in_git_dir):
451482 pre_commit .write ('#!/usr/bin/env bash\n echo 1\n ' )
452483 make_executable (pre_commit .strpath )
453484
454- assert uninstall (hook_types = ['pre-commit' ]) == 0
485+ assert uninstall (C . CONFIG_FILE , hook_types = ['pre-commit' ]) == 0
455486
456487 assert pre_commit .exists ()
457488
@@ -1007,3 +1038,16 @@ def test_install_temporarily_allow_mising_config(tempdir_factory, store):
10071038 'Skipping `pre-commit`.'
10081039 )
10091040 assert expected in output
1041+
1042+
1043+ def test_install_uninstall_default_hook_types (in_git_dir , store ):
1044+ cfg_src = 'default_install_hook_types: [pre-commit, pre-push]\n repos: []\n '
1045+ in_git_dir .join (C .CONFIG_FILE ).write (cfg_src )
1046+
1047+ assert not install (C .CONFIG_FILE , store , hook_types = None )
1048+ assert os .access (in_git_dir .join ('.git/hooks/pre-commit' ).strpath , os .X_OK )
1049+ assert os .access (in_git_dir .join ('.git/hooks/pre-push' ).strpath , os .X_OK )
1050+
1051+ assert not uninstall (C .CONFIG_FILE , hook_types = None )
1052+ assert not in_git_dir .join ('.git/hooks/pre-commit' ).exists ()
1053+ assert not in_git_dir .join ('.git/hooks/pre-push' ).exists ()
0 commit comments