X Tutup
Skip to content

Commit fe70c72

Browse files
authored
Merge pull request pre-commit#1576 from pre-commit/immediately_unhealthy_cache
fix cache of invalidated unhealthy environment version info
2 parents 2e0ee5f + b63b37a commit fe70c72

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

pre_commit/languages/python.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ def healthy(prefix: Prefix, language_version: str) -> bool:
191191

192192
return (
193193
'version_info' in cfg and
194-
_version_info(py_exe) == cfg['version_info'] and (
194+
# always use uncached lookup here in case we replaced an unhealthy env
195+
_version_info.__wrapped__(py_exe) == cfg['version_info'] and (
195196
'base-executable' not in cfg or
196197
_version_info(cfg['base-executable']) == cfg['version_info']
197198
)

tests/languages/python_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pre_commit.envcontext import envcontext
99
from pre_commit.languages import python
1010
from pre_commit.prefix import Prefix
11+
from pre_commit.util import make_executable
1112

1213

1314
def test_read_pyvenv_cfg(tmpdir):
@@ -141,3 +142,26 @@ def test_unhealthy_old_virtualenv(python_dir):
141142
os.remove(prefix.path('py_env-default/pyvenv.cfg'))
142143

143144
assert python.healthy(prefix, C.DEFAULT) is False
145+
146+
147+
def test_unhealthy_then_replaced(python_dir):
148+
prefix, tmpdir = python_dir
149+
150+
python.install_environment(prefix, C.DEFAULT, ())
151+
152+
# simulate an exe which returns an old version
153+
exe_name = 'python.exe' if sys.platform == 'win32' else 'python'
154+
py_exe = prefix.path(python.bin_dir('py_env-default'), exe_name)
155+
os.rename(py_exe, f'{py_exe}.tmp')
156+
157+
with open(py_exe, 'w') as f:
158+
f.write('#!/usr/bin/env bash\necho 1.2.3\n')
159+
make_executable(py_exe)
160+
161+
# should be unhealthy due to version mismatch
162+
assert python.healthy(prefix, C.DEFAULT) is False
163+
164+
# now put the exe back and it should be healthy again
165+
os.replace(f'{py_exe}.tmp', py_exe)
166+
167+
assert python.healthy(prefix, C.DEFAULT) is True

0 commit comments

Comments
 (0)
X Tutup