Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4f6ba18

Browse files
authored
Merge pull request #2767 from pre-commit/test-more-directly
test things more directly to improve coverage
2 parents c3402e6 + c3613b9 commit 4f6ba18

4 files changed

Lines changed: 46 additions & 16 deletions

File tree

tests/languages/python_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from pre_commit.prefix import Prefix
1313
from pre_commit.util import make_executable
1414
from pre_commit.util import win_exe
15+
from testing.language_helpers import run_language
1516

1617

1718
def test_read_pyvenv_cfg(tmpdir):
@@ -210,3 +211,25 @@ def test_unhealthy_then_replaced(python_dir):
210211
os.replace(f'{py_exe}.tmp', py_exe)
211212

212213
assert python.health_check(prefix, C.DEFAULT) is None
214+
215+
216+
def test_language_versioned_python_hook(tmp_path):
217+
setup_py = '''\
218+
from setuptools import setup
219+
setup(
220+
name='example',
221+
py_modules=['mod'],
222+
entry_points={'console_scripts': ['myexe=mod:main']},
223+
)
224+
'''
225+
tmp_path.joinpath('setup.py').write_text(setup_py)
226+
tmp_path.joinpath('mod.py').write_text('def main(): print("ohai")')
227+
228+
# we patch this to force virtualenv executing with `-p` since we can't
229+
# reliably have multiple pythons available in CI
230+
with mock.patch.object(
231+
python,
232+
'_sys_executable_matches',
233+
return_value=False,
234+
):
235+
assert run_language(tmp_path, python, 'myexe') == (0, b'ohai\n')

tests/languages/script_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from __future__ import annotations
2+
3+
from pre_commit.languages import script
4+
from pre_commit.util import make_executable
5+
from testing.language_helpers import run_language
6+
7+
8+
def test_script_language(tmp_path):
9+
exe = tmp_path.joinpath('main')
10+
exe.write_text('#!/usr/bin/env bash\necho hello hello world\n')
11+
make_executable(exe)
12+
13+
expected = (0, b'hello hello world\n')
14+
assert run_language(tmp_path, script, 'main') == expected

tests/languages/system_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from __future__ import annotations
2+
3+
from pre_commit.languages import system
4+
from testing.language_helpers import run_language
5+
6+
7+
def test_system_language(tmp_path):
8+
expected = (0, b'hello hello world\n')
9+
assert run_language(tmp_path, system, 'echo hello hello world') == expected

tests/repository_test.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -143,22 +143,6 @@ def test_python_venv_deprecation(store, caplog):
143143
)
144144

145145

146-
def test_language_versioned_python_hook(tempdir_factory, store):
147-
# we patch this force virtualenv executing with `-p` since we can't
148-
# reliably have multiple pythons available in CI
149-
with mock.patch.object(
150-
python,
151-
'_sys_executable_matches',
152-
return_value=False,
153-
):
154-
_test_hook_repo(
155-
tempdir_factory, store, 'python3_hooks_repo',
156-
'python3-hook',
157-
[os.devnull],
158-
f'3\n[{os.devnull!r}]\nHello World\n'.encode(),
159-
)
160-
161-
162146
def test_system_hook_with_spaces(tempdir_factory, store):
163147
_test_hook_repo(
164148
tempdir_factory, store, 'system_hook_with_spaces_repo',

0 commit comments

Comments
 (0)