44import os .path
55import re
66
7+ from pre_commit import git
78from pre_commit .commands .try_repo import try_repo
89from pre_commit .util import cmd_output
910from testing .auto_namedtuple import auto_namedtuple
1011from testing .fixtures import git_dir
1112from testing .fixtures import make_repo
13+ from testing .fixtures import modify_manifest
1214from testing .util import cwd
15+ from testing .util import git_commit
1316from testing .util import run_opts
1417
1518
@@ -21,22 +24,26 @@ def _get_out(cap_out):
2124 out = cap_out .get ().replace ('\r \n ' , '\n ' )
2225 out = re .sub (r'\[INFO\].+\n' , '' , out )
2326 start , using_config , config , rest = out .split ('=' * 79 + '\n ' )
24- assert start == ''
2527 assert using_config == 'Using config:\n '
26- return config , rest
28+ return start , config , rest
29+
30+
31+ def _add_test_file ():
32+ open ('test-file' , 'a' ).close ()
33+ cmd_output ('git' , 'add' , '.' )
2734
2835
2936def _run_try_repo (tempdir_factory , ** kwargs ):
3037 repo = make_repo (tempdir_factory , 'modified_file_returns_zero_repo' )
3138 with cwd (git_dir (tempdir_factory )):
32- open ('test-file' , 'a' ).close ()
33- cmd_output ('git' , 'add' , '.' )
39+ _add_test_file ()
3440 assert not try_repo (try_repo_opts (repo , ** kwargs ))
3541
3642
3743def test_try_repo_repo_only (cap_out , tempdir_factory ):
3844 _run_try_repo (tempdir_factory , verbose = True )
39- config , rest = _get_out (cap_out )
45+ start , config , rest = _get_out (cap_out )
46+ assert start == ''
4047 assert re .match (
4148 '^repos:\n '
4249 '- repo: .+\n '
@@ -48,19 +55,20 @@ def test_try_repo_repo_only(cap_out, tempdir_factory):
4855 config ,
4956 )
5057 assert rest == (
51- '[bash_hook] Bash hook................................(no files to check)Skipped\n ' # noqa
52- '[bash_hook2] Bash hook...................................................Passed\n ' # noqa
58+ '[bash_hook] Bash hook................................(no files to check)Skipped\n ' # noqa: E501
59+ '[bash_hook2] Bash hook...................................................Passed\n ' # noqa: E501
5360 'hookid: bash_hook2\n '
5461 '\n '
5562 'test-file\n '
5663 '\n '
57- '[bash_hook3] Bash hook...............................(no files to check)Skipped\n ' # noqa
64+ '[bash_hook3] Bash hook...............................(no files to check)Skipped\n ' # noqa: E501
5865 )
5966
6067
6168def test_try_repo_with_specific_hook (cap_out , tempdir_factory ):
6269 _run_try_repo (tempdir_factory , hook = 'bash_hook' , verbose = True )
63- config , rest = _get_out (cap_out )
70+ start , config , rest = _get_out (cap_out )
71+ assert start == ''
6472 assert re .match (
6573 '^repos:\n '
6674 '- repo: .+\n '
@@ -69,14 +77,49 @@ def test_try_repo_with_specific_hook(cap_out, tempdir_factory):
6977 ' - id: bash_hook\n $' ,
7078 config ,
7179 )
72- assert rest == '[bash_hook] Bash hook................................(no files to check)Skipped\n ' # noqa
80+ assert rest == '[bash_hook] Bash hook................................(no files to check)Skipped\n ' # noqa: E501
7381
7482
7583def test_try_repo_relative_path (cap_out , tempdir_factory ):
7684 repo = make_repo (tempdir_factory , 'modified_file_returns_zero_repo' )
7785 with cwd (git_dir (tempdir_factory )):
78- open ('test-file' , 'a' ).close ()
79- cmd_output ('git' , 'add' , '.' )
86+ _add_test_file ()
8087 relative_repo = os .path .relpath (repo , '.' )
8188 # previously crashed on cloning a relative path
8289 assert not try_repo (try_repo_opts (relative_repo , hook = 'bash_hook' ))
90+
91+
92+ def test_try_repo_specific_revision (cap_out , tempdir_factory ):
93+ repo = make_repo (tempdir_factory , 'script_hooks_repo' )
94+ ref = git .head_rev (repo )
95+ git_commit (cwd = repo )
96+ with cwd (git_dir (tempdir_factory )):
97+ _add_test_file ()
98+ assert not try_repo (try_repo_opts (repo , ref = ref ))
99+
100+ _ , config , _ = _get_out (cap_out )
101+ assert ref in config
102+
103+
104+ def test_try_repo_uncommitted_changes (cap_out , tempdir_factory ):
105+ repo = make_repo (tempdir_factory , 'script_hooks_repo' )
106+ # make an uncommitted change
107+ with modify_manifest (repo , commit = False ) as manifest :
108+ manifest [0 ]['name' ] = 'modified name!'
109+
110+ with cwd (git_dir (tempdir_factory )):
111+ open ('test-fie' , 'a' ).close ()
112+ cmd_output ('git' , 'add' , '.' )
113+ assert not try_repo (try_repo_opts (repo ))
114+
115+ start , config , rest = _get_out (cap_out )
116+ assert start == '[WARNING] Creating temporary repo with uncommitted changes...\n ' # noqa: E501
117+ assert re .match (
118+ '^repos:\n '
119+ '- repo: .+shadow-repo\n '
120+ ' rev: .+\n '
121+ ' hooks:\n '
122+ ' - id: bash_hook\n $' ,
123+ config ,
124+ )
125+ assert rest == 'modified name!...........................................................Passed\n ' # noqa: E501
0 commit comments