Skip to content

Commit 58a16bc

Browse files
authored
Merge pull request #1363 from pre-commit/broken_symlinks_healthy
mark a python environment as unhealthy if python goes missing
2 parents 1c10340 + 7a49309 commit 58a16bc

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

pre_commit/languages/python.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,12 @@ def in_env(
158158
yield
159159

160160
def healthy(prefix: Prefix, language_version: str) -> bool:
161+
envdir = helpers.environment_dir(_dir, language_version)
162+
exe_name = 'python.exe' if sys.platform == 'win32' else 'python'
163+
py_exe = prefix.path(bin_dir(envdir), exe_name)
161164
with in_env(prefix, language_version):
162165
retcode, _, _ = cmd_output_b(
163-
'python', '-c',
164-
'import ctypes, datetime, io, os, ssl, weakref',
166+
py_exe, '-c', 'import ctypes, datetime, io, os, ssl, weakref',
165167
cwd='/',
166168
retcode=None,
167169
)

tests/languages/python_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,17 @@ def test_healthy_types_py_in_cwd(tmpdir):
5959
# even if a `types.py` file exists, should still be healthy
6060
tmpdir.join('types.py').ensure()
6161
assert python.healthy(prefix, C.DEFAULT) is True
62+
63+
64+
def test_healthy_python_goes_missing(tmpdir):
65+
with tmpdir.as_cwd():
66+
prefix = tmpdir.join('prefix').ensure_dir()
67+
prefix.join('setup.py').write('import setuptools; setuptools.setup()')
68+
prefix = Prefix(str(prefix))
69+
python.install_environment(prefix, C.DEFAULT, ())
70+
71+
exe_name = 'python' if sys.platform != 'win32' else 'python.exe'
72+
py_exe = prefix.path(python.bin_dir('py_env-default'), exe_name)
73+
os.remove(py_exe)
74+
75+
assert python.healthy(prefix, C.DEFAULT) is False

0 commit comments

Comments
 (0)