|
4 | 4 | from unittest import mock |
5 | 5 |
|
6 | 6 | import pre_commit.constants as C |
| 7 | +from pre_commit.commands import install_uninstall |
7 | 8 | from pre_commit.commands.install_uninstall import CURRENT_HASH |
8 | 9 | from pre_commit.commands.install_uninstall import install |
9 | 10 | from pre_commit.commands.install_uninstall import install_hooks |
@@ -39,25 +40,36 @@ def test_is_previous_pre_commit(tmpdir): |
39 | 40 | assert is_our_script(f.strpath) |
40 | 41 |
|
41 | 42 |
|
| 43 | +def patch_platform(platform): |
| 44 | + return mock.patch.object(sys, 'platform', platform) |
| 45 | + |
| 46 | + |
| 47 | +def patch_lookup_path(path): |
| 48 | + return mock.patch.object(install_uninstall, 'POSIX_SEARCH_PATH', path) |
| 49 | + |
| 50 | + |
| 51 | +def patch_sys_exe(exe): |
| 52 | + return mock.patch.object(install_uninstall, 'SYS_EXE', exe) |
| 53 | + |
| 54 | + |
42 | 55 | def test_shebang_windows(): |
43 | | - with mock.patch.object(sys, 'platform', 'win32'): |
44 | | - assert shebang() == '#!/usr/bin/env python' |
| 56 | + with patch_platform('win32'), patch_sys_exe('python.exe'): |
| 57 | + assert shebang() == '#!/usr/bin/env python.exe' |
45 | 58 |
|
46 | 59 |
|
47 | 60 | def test_shebang_posix_not_on_path(): |
48 | | - with mock.patch.object(sys, 'platform', 'posix'): |
49 | | - with mock.patch.object(os, 'defpath', ''): |
50 | | - assert shebang() == '#!/usr/bin/env python' |
| 61 | + with patch_platform('posix'), patch_lookup_path(()): |
| 62 | + with patch_sys_exe('python3.6'): |
| 63 | + assert shebang() == '#!/usr/bin/env python3.6' |
51 | 64 |
|
52 | 65 |
|
53 | 66 | def test_shebang_posix_on_path(tmpdir): |
54 | 67 | exe = tmpdir.join(f'python{sys.version_info[0]}').ensure() |
55 | 68 | make_executable(exe) |
56 | 69 |
|
57 | | - with mock.patch.object(sys, 'platform', 'posix'): |
58 | | - with mock.patch.object(os, 'defpath', tmpdir.strpath): |
59 | | - expected = f'#!/usr/bin/env python{sys.version_info[0]}' |
60 | | - assert shebang() == expected |
| 70 | + with patch_platform('posix'), patch_lookup_path((tmpdir.strpath,)): |
| 71 | + with patch_sys_exe('python'): |
| 72 | + assert shebang() == f'#!/usr/bin/env python{sys.version_info[0]}' |
61 | 73 |
|
62 | 74 |
|
63 | 75 | def test_install_pre_commit(in_git_dir, store): |
@@ -250,9 +262,18 @@ def _path_without_us(): |
250 | 262 | def test_environment_not_sourced(tempdir_factory, store): |
251 | 263 | path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') |
252 | 264 | with cwd(path): |
253 | | - # Patch the executable to simulate rming virtualenv |
254 | | - with mock.patch.object(sys, 'executable', '/does-not-exist'): |
255 | | - assert not install(C.CONFIG_FILE, store, hook_types=['pre-commit']) |
| 265 | + assert not install(C.CONFIG_FILE, store, hook_types=['pre-commit']) |
| 266 | + # simulate deleting the virtualenv by rewriting the exe |
| 267 | + hook = os.path.join(path, '.git/hooks/pre-commit') |
| 268 | + with open(hook) as f: |
| 269 | + src = f.read() |
| 270 | + src = re.sub( |
| 271 | + '\nINSTALL_PYTHON =.*\n', |
| 272 | + '\nINSTALL_PYTHON = "/dne"\n', |
| 273 | + src, |
| 274 | + ) |
| 275 | + with open(hook, 'w') as f: |
| 276 | + f.write(src) |
256 | 277 |
|
257 | 278 | # Use a specific homedir to ignore --user installs |
258 | 279 | homedir = tempdir_factory.get() |
|
0 commit comments