@@ -49,21 +49,21 @@ def test_install_pre_commit(tempdir_factory, store):
4949 path = git_dir (tempdir_factory )
5050 runner = Runner (path , C .CONFIG_FILE )
5151 assert not install (runner , store )
52- assert os .access (runner . pre_commit_path , os .X_OK )
52+ assert os .access (os . path . join ( path , '.git/hooks/pre-commit' ) , os .X_OK )
5353
5454 assert not install (runner , store , hook_type = 'pre-push' )
55- assert os .access (runner . pre_push_path , os .X_OK )
55+ assert os .access (os . path . join ( path , '.git/hooks/pre-push' ) , os .X_OK )
5656
5757
5858def test_install_hooks_directory_not_present (tempdir_factory , store ):
5959 path = git_dir (tempdir_factory )
6060 # Simulate some git clients which don't make .git/hooks #234
61- hooks = os .path .join (path , '.git' , ' hooks' )
61+ hooks = os .path .join (path , '.git/ hooks' )
6262 if os .path .exists (hooks ): # pragma: no cover (latest git)
6363 shutil .rmtree (hooks )
6464 runner = Runner (path , C .CONFIG_FILE )
6565 install (runner , store )
66- assert os .path .exists (runner . pre_commit_path )
66+ assert os .path .exists (os . path . join ( path , '.git/hooks/pre-commit' ) )
6767
6868
6969def test_install_refuses_core_hookspath (tempdir_factory , store ):
@@ -80,10 +80,10 @@ def test_install_hooks_dead_symlink(
8080): # pragma: no cover (non-windows)
8181 path = git_dir (tempdir_factory )
8282 runner = Runner (path , C .CONFIG_FILE )
83- mkdirp (os .path .dirname ( runner . pre_commit_path ))
84- os .symlink ('/fake/baz' , os .path .join (path , '.git' , ' hooks' , ' pre-commit' ))
83+ mkdirp (os .path .join ( path , '.git/hooks' ))
84+ os .symlink ('/fake/baz' , os .path .join (path , '.git/ hooks/ pre-commit' ))
8585 install (runner , store )
86- assert os .path .exists (runner . pre_commit_path )
86+ assert os .path .exists (os . path . join ( path , '.git/hooks/pre-commit' ) )
8787
8888
8989def test_uninstall_does_not_blow_up_when_not_there (tempdir_factory ):
@@ -96,11 +96,11 @@ def test_uninstall_does_not_blow_up_when_not_there(tempdir_factory):
9696def test_uninstall (tempdir_factory , store ):
9797 path = git_dir (tempdir_factory )
9898 runner = Runner (path , C .CONFIG_FILE )
99- assert not os .path .exists (runner . pre_commit_path )
99+ assert not os .path .exists (os . path . join ( path , '.git/hooks/pre-commit' ) )
100100 install (runner , store )
101- assert os .path .exists (runner . pre_commit_path )
101+ assert os .path .exists (os . path . join ( path , '.git/hooks/pre-commit' ) )
102102 uninstall (runner )
103- assert not os .path .exists (runner . pre_commit_path )
103+ assert not os .path .exists (os . path . join ( path , '.git/hooks/pre-commit' ) )
104104
105105
106106def _get_commit_output (tempdir_factory , touch_file = 'foo' , ** kwargs ):
@@ -282,16 +282,19 @@ def test_failing_hooks_returns_nonzero(tempdir_factory, store):
282282)
283283
284284
285+ def _write_legacy_hook (path ):
286+ mkdirp (os .path .join (path , '.git/hooks' ))
287+ with io .open (os .path .join (path , '.git/hooks/pre-commit' ), 'w' ) as f :
288+ f .write ('#!/usr/bin/env bash\n echo "legacy hook"\n ' )
289+ make_executable (f .name )
290+
291+
285292def test_install_existing_hooks_no_overwrite (tempdir_factory , store ):
286293 path = make_consuming_repo (tempdir_factory , 'script_hooks_repo' )
287294 with cwd (path ):
288295 runner = Runner (path , C .CONFIG_FILE )
289296
290- # Write out an "old" hook
291- mkdirp (os .path .dirname (runner .pre_commit_path ))
292- with io .open (runner .pre_commit_path , 'w' ) as hook_file :
293- hook_file .write ('#!/usr/bin/env bash\n echo "legacy hook"\n ' )
294- make_executable (runner .pre_commit_path )
297+ _write_legacy_hook (path )
295298
296299 # Make sure we installed the "old" hook correctly
297300 ret , output = _get_commit_output (tempdir_factory , touch_file = 'baz' )
@@ -313,11 +316,7 @@ def test_install_existing_hook_no_overwrite_idempotent(tempdir_factory, store):
313316 with cwd (path ):
314317 runner = Runner (path , C .CONFIG_FILE )
315318
316- # Write out an "old" hook
317- mkdirp (os .path .dirname (runner .pre_commit_path ))
318- with io .open (runner .pre_commit_path , 'w' ) as hook_file :
319- hook_file .write ('#!/usr/bin/env bash\n echo "legacy hook"\n ' )
320- make_executable (runner .pre_commit_path )
319+ _write_legacy_hook (path )
321320
322321 # Install twice
323322 assert install (runner , store ) == 0
@@ -343,10 +342,10 @@ def test_failing_existing_hook_returns_1(tempdir_factory, store):
343342 runner = Runner (path , C .CONFIG_FILE )
344343
345344 # Write out a failing "old" hook
346- mkdirp (os .path .dirname ( runner . pre_commit_path ))
347- with io .open (runner . pre_commit_path , 'w' ) as hook_file :
348- hook_file .write ('#!/usr/bin/env bash\n echo "fail!"\n exit 1\n ' )
349- make_executable (runner . pre_commit_path )
345+ mkdirp (os .path .join ( path , '.git/hooks' ))
346+ with io .open (os . path . join ( path , '.git/hooks/pre-commit' ), ' w' ) as f :
347+ f .write ('#!/usr/bin/env bash\n echo "fail!"\n exit 1\n ' )
348+ make_executable (f . name )
350349
351350 assert install (runner , store ) == 0
352351
@@ -372,12 +371,7 @@ def test_install_overwrite(tempdir_factory, store):
372371 with cwd (path ):
373372 runner = Runner (path , C .CONFIG_FILE )
374373
375- # Write out the "old" hook
376- mkdirp (os .path .dirname (runner .pre_commit_path ))
377- with io .open (runner .pre_commit_path , 'w' ) as hook_file :
378- hook_file .write ('#!/usr/bin/env bash\n echo "legacy hook"\n ' )
379- make_executable (runner .pre_commit_path )
380-
374+ _write_legacy_hook (path )
381375 assert install (runner , store , overwrite = True ) == 0
382376
383377 ret , output = _get_commit_output (tempdir_factory )
@@ -390,11 +384,7 @@ def test_uninstall_restores_legacy_hooks(tempdir_factory, store):
390384 with cwd (path ):
391385 runner = Runner (path , C .CONFIG_FILE )
392386
393- # Write out an "old" hook
394- mkdirp (os .path .dirname (runner .pre_commit_path ))
395- with io .open (runner .pre_commit_path , 'w' ) as hook_file :
396- hook_file .write ('#!/usr/bin/env bash\n echo "legacy hook"\n ' )
397- make_executable (runner .pre_commit_path )
387+ _write_legacy_hook (path )
398388
399389 # Now install and uninstall pre-commit
400390 assert install (runner , store ) == 0
@@ -412,17 +402,15 @@ def test_replace_old_commit_script(tempdir_factory, store):
412402 runner = Runner (path , C .CONFIG_FILE )
413403
414404 # Install a script that looks like our old script
415- pre_commit_contents = io .open (
416- resource_filename ('hook-tmpl' ),
417- ).read ()
405+ pre_commit_contents = io .open (resource_filename ('hook-tmpl' )).read ()
418406 new_contents = pre_commit_contents .replace (
419407 CURRENT_HASH , PRIOR_HASHES [- 1 ],
420408 )
421409
422- mkdirp (os .path .dirname ( runner . pre_commit_path ))
423- with io .open (runner . pre_commit_path , 'w' ) as pre_commit_file :
424- pre_commit_file .write (new_contents )
425- make_executable (runner . pre_commit_path )
410+ mkdirp (os .path .join ( path , '.git/hooks' ))
411+ with io .open (os . path . join ( path , '.git/hooks/pre-commit' ), ' w' ) as f :
412+ f .write (new_contents )
413+ make_executable (f . name )
426414
427415 # Install normally
428416 assert install (runner , store ) == 0
@@ -436,14 +424,14 @@ def test_uninstall_doesnt_remove_not_our_hooks(tempdir_factory):
436424 path = git_dir (tempdir_factory )
437425 with cwd (path ):
438426 runner = Runner (path , C .CONFIG_FILE )
439- mkdirp (os .path .dirname ( runner . pre_commit_path ))
440- with io .open (runner . pre_commit_path , 'w' ) as pre_commit_file :
441- pre_commit_file .write ('#!/usr/bin/env bash\n echo 1\n ' )
442- make_executable (runner . pre_commit_path )
427+ mkdirp (os .path .join ( path , '.git/hooks' ))
428+ with io .open (os . path . join ( path , '.git/hooks/pre-commit' ), ' w' ) as f :
429+ f .write ('#!/usr/bin/env bash\n echo 1\n ' )
430+ make_executable (f . name )
443431
444432 assert uninstall (runner ) == 0
445433
446- assert os .path .exists (runner . pre_commit_path )
434+ assert os .path .exists (os . path . join ( path , '.git/hooks/pre-commit' ) )
447435
448436
449437PRE_INSTALLED = re .compile (
@@ -583,17 +571,16 @@ def test_pre_push_legacy(tempdir_factory, store):
583571 with cwd (path ):
584572 runner = Runner (path , C .CONFIG_FILE )
585573
586- hook_path = runner .get_hook_path ('pre-push' )
587- mkdirp (os .path .dirname (hook_path ))
588- with io .open (hook_path , 'w' ) as hook_file :
589- hook_file .write (
574+ mkdirp (os .path .join (path , '.git/hooks' ))
575+ with io .open (os .path .join (path , '.git/hooks/pre-push' ), 'w' ) as f :
576+ f .write (
590577 '#!/usr/bin/env bash\n '
591578 'set -eu\n '
592579 'read lr ls rr rs\n '
593580 'test -n "$lr" -a -n "$ls" -a -n "$rr" -a -n "$rs"\n '
594581 'echo legacy\n ' ,
595582 )
596- make_executable (hook_path )
583+ make_executable (f . name )
597584
598585 install (runner , store , hook_type = 'pre-push' )
599586 assert _get_commit_output (tempdir_factory )[0 ] == 0
0 commit comments