Skip to content

Commit

Permalink
Merge pull request pre-commit#2767 from pre-commit/test-more-directly
Browse files Browse the repository at this point in the history
test things more directly to improve coverage
  • Loading branch information
asottile authored Feb 20, 2023
2 parents c3402e6 + c3613b9 commit 4f6ba18
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
23 changes: 23 additions & 0 deletions tests/languages/python_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pre_commit.prefix import Prefix
from pre_commit.util import make_executable
from pre_commit.util import win_exe
from testing.language_helpers import run_language


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

assert python.health_check(prefix, C.DEFAULT) is None


def test_language_versioned_python_hook(tmp_path):
setup_py = '''\
from setuptools import setup
setup(
name='example',
py_modules=['mod'],
entry_points={'console_scripts': ['myexe=mod:main']},
)
'''
tmp_path.joinpath('setup.py').write_text(setup_py)
tmp_path.joinpath('mod.py').write_text('def main(): print("ohai")')

# we patch this to force virtualenv executing with `-p` since we can't
# reliably have multiple pythons available in CI
with mock.patch.object(
python,
'_sys_executable_matches',
return_value=False,
):
assert run_language(tmp_path, python, 'myexe') == (0, b'ohai\n')
14 changes: 14 additions & 0 deletions tests/languages/script_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from __future__ import annotations

from pre_commit.languages import script
from pre_commit.util import make_executable
from testing.language_helpers import run_language


def test_script_language(tmp_path):
exe = tmp_path.joinpath('main')
exe.write_text('#!/usr/bin/env bash\necho hello hello world\n')
make_executable(exe)

expected = (0, b'hello hello world\n')
assert run_language(tmp_path, script, 'main') == expected
9 changes: 9 additions & 0 deletions tests/languages/system_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from __future__ import annotations

from pre_commit.languages import system
from testing.language_helpers import run_language


def test_system_language(tmp_path):
expected = (0, b'hello hello world\n')
assert run_language(tmp_path, system, 'echo hello hello world') == expected
16 changes: 0 additions & 16 deletions tests/repository_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,22 +143,6 @@ def test_python_venv_deprecation(store, caplog):
)


def test_language_versioned_python_hook(tempdir_factory, store):
# we patch this force virtualenv executing with `-p` since we can't
# reliably have multiple pythons available in CI
with mock.patch.object(
python,
'_sys_executable_matches',
return_value=False,
):
_test_hook_repo(
tempdir_factory, store, 'python3_hooks_repo',
'python3-hook',
[os.devnull],
f'3\n[{os.devnull!r}]\nHello World\n'.encode(),
)


def test_system_hook_with_spaces(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'system_hook_with_spaces_repo',
Expand Down

0 comments on commit 4f6ba18

Please sign in to comment.