2121from pre_commit .runner import Runner
2222from pre_commit .util import cmd_output
2323from pre_commit .util import cwd
24+ from pre_commit .util import mkdirp
2425from pre_commit .util import resource_filename
2526from testing .fixtures import git_dir
2627from testing .fixtures import make_consuming_repo
@@ -83,7 +84,9 @@ def test_install_pre_commit(tempdir_factory):
8384def test_install_hooks_directory_not_present (tempdir_factory ):
8485 path = git_dir (tempdir_factory )
8586 # Simulate some git clients which don't make .git/hooks #234
86- shutil .rmtree (os .path .join (path , '.git' , 'hooks' ))
87+ hooks = os .path .join (path , '.git' , 'hooks' )
88+ if os .path .exists (hooks ): # pragma: no cover (latest git)
89+ shutil .rmtree (hooks )
8790 runner = Runner (path )
8891 install (runner )
8992 assert os .path .exists (runner .pre_commit_path )
@@ -94,8 +97,9 @@ def test_install_hooks_dead_symlink(
9497 tempdir_factory ,
9598): # pragma: no cover (non-windows)
9699 path = git_dir (tempdir_factory )
97- os .symlink ('/fake/baz' , os .path .join (path , '.git' , 'hooks' , 'pre-commit' ))
98100 runner = Runner (path )
101+ mkdirp (os .path .dirname (runner .pre_commit_path ))
102+ os .symlink ('/fake/baz' , os .path .join (path , '.git' , 'hooks' , 'pre-commit' ))
99103 install (runner )
100104 assert os .path .exists (runner .pre_commit_path )
101105
@@ -249,6 +253,7 @@ def test_install_existing_hooks_no_overwrite(tempdir_factory):
249253 runner = Runner (path )
250254
251255 # Write out an "old" hook
256+ mkdirp (os .path .dirname (runner .pre_commit_path ))
252257 with io .open (runner .pre_commit_path , 'w' ) as hook_file :
253258 hook_file .write ('#!/usr/bin/env bash\n echo "legacy hook"\n ' )
254259 make_executable (runner .pre_commit_path )
@@ -274,6 +279,7 @@ def test_install_existing_hook_no_overwrite_idempotent(tempdir_factory):
274279 runner = Runner (path )
275280
276281 # Write out an "old" hook
282+ mkdirp (os .path .dirname (runner .pre_commit_path ))
277283 with io .open (runner .pre_commit_path , 'w' ) as hook_file :
278284 hook_file .write ('#!/usr/bin/env bash\n echo "legacy hook"\n ' )
279285 make_executable (runner .pre_commit_path )
@@ -302,6 +308,7 @@ def test_failing_existing_hook_returns_1(tempdir_factory):
302308 runner = Runner (path )
303309
304310 # Write out a failing "old" hook
311+ mkdirp (os .path .dirname (runner .pre_commit_path ))
305312 with io .open (runner .pre_commit_path , 'w' ) as hook_file :
306313 hook_file .write ('#!/usr/bin/env bash\n echo "fail!"\n exit 1\n ' )
307314 make_executable (runner .pre_commit_path )
@@ -330,6 +337,7 @@ def test_install_overwrite(tempdir_factory):
330337 runner = Runner (path )
331338
332339 # Write out the "old" hook
340+ mkdirp (os .path .dirname (runner .pre_commit_path ))
333341 with io .open (runner .pre_commit_path , 'w' ) as hook_file :
334342 hook_file .write ('#!/usr/bin/env bash\n echo "legacy hook"\n ' )
335343 make_executable (runner .pre_commit_path )
@@ -347,6 +355,7 @@ def test_uninstall_restores_legacy_hooks(tempdir_factory):
347355 runner = Runner (path )
348356
349357 # Write out an "old" hook
358+ mkdirp (os .path .dirname (runner .pre_commit_path ))
350359 with io .open (runner .pre_commit_path , 'w' ) as hook_file :
351360 hook_file .write ('#!/usr/bin/env bash\n echo "legacy hook"\n ' )
352361 make_executable (runner .pre_commit_path )
@@ -374,6 +383,7 @@ def test_replace_old_commit_script(tempdir_factory):
374383 IDENTIFYING_HASH , PREVIOUS_IDENTIFYING_HASHES [- 1 ],
375384 )
376385
386+ mkdirp (os .path .dirname (runner .pre_commit_path ))
377387 with io .open (runner .pre_commit_path , 'w' ) as pre_commit_file :
378388 pre_commit_file .write (new_contents )
379389 make_executable (runner .pre_commit_path )
@@ -390,6 +400,7 @@ def test_uninstall_doesnt_remove_not_our_hooks(tempdir_factory):
390400 path = git_dir (tempdir_factory )
391401 with cwd (path ):
392402 runner = Runner (path )
403+ mkdirp (os .path .dirname (runner .pre_commit_path ))
393404 with io .open (runner .pre_commit_path , 'w' ) as pre_commit_file :
394405 pre_commit_file .write ('#!/usr/bin/env bash\n echo 1\n ' )
395406 make_executable (runner .pre_commit_path )
0 commit comments