Skip to content

Commit

Permalink
test pygrep inline
Browse files Browse the repository at this point in the history
  • Loading branch information
m-rsha authored and asottile committed Feb 18, 2023
1 parent f5ec578 commit a2373d0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 46 deletions.
17 changes: 17 additions & 0 deletions tests/languages/pygrep_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

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


@pytest.fixture
Expand All @@ -13,6 +14,9 @@ def some_files(tmpdir):
tmpdir.join('f4').write_binary(b'foo\npattern\nbar\n')
tmpdir.join('f5').write_binary(b'[INFO] hi\npattern\nbar')
tmpdir.join('f6').write_binary(b"pattern\nbarwith'foo\n")
tmpdir.join('f7').write_binary(b"hello'hi\nworld\n")
tmpdir.join('f8').write_binary(b'foo\nbar\nbaz\n')
tmpdir.join('f9').write_binary(b'[WARN] hi\n')
with tmpdir.as_cwd():
yield

Expand Down Expand Up @@ -125,3 +129,16 @@ def test_multiline_multiline_flag_is_enabled(cap_out):
out = cap_out.get()
assert ret == 1
assert out == 'f1:1:foo\nbar\n'


def test_grep_hook_matching(some_files, tmp_path):
ret = run_language(
tmp_path, pygrep, 'ello', file_args=('f7', 'f8', 'f9'),
)
assert ret == (1, b"f7:1:hello'hi\n")


@pytest.mark.parametrize('regex', ('nope', "foo'bar", r'^\[INFO\]'))
def test_grep_hook_not_matching(regex, some_files, tmp_path):
ret = run_language(tmp_path, pygrep, regex, file_args=('f7', 'f8', 'f9'))
assert ret == (0, b'')
46 changes: 0 additions & 46 deletions tests/repository_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,52 +226,6 @@ def test_output_isatty(tempdir_factory, store):
)


def _make_grep_repo(entry, store, args=()):
config = {
'repo': 'local',
'hooks': [{
'id': 'grep-hook',
'name': 'grep-hook',
'language': 'pygrep',
'entry': entry,
'args': args,
'types': ['text'],
}],
}
return _get_hook(config, store, 'grep-hook')


@pytest.fixture
def greppable_files(tmpdir):
with tmpdir.as_cwd():
cmd_output_b('git', 'init', '.')
tmpdir.join('f1').write_binary(b"hello'hi\nworld\n")
tmpdir.join('f2').write_binary(b'foo\nbar\nbaz\n')
tmpdir.join('f3').write_binary(b'[WARN] hi\n')
yield tmpdir


def test_grep_hook_matching(greppable_files, store):
hook = _make_grep_repo('ello', store)
ret, out = _hook_run(hook, ('f1', 'f2', 'f3'), color=False)
assert ret == 1
assert _norm_out(out) == b"f1:1:hello'hi\n"


def test_grep_hook_case_insensitive(greppable_files, store):
hook = _make_grep_repo('ELLO', store, args=['-i'])
ret, out = _hook_run(hook, ('f1', 'f2', 'f3'), color=False)
assert ret == 1
assert _norm_out(out) == b"f1:1:hello'hi\n"


@pytest.mark.parametrize('regex', ('nope', "foo'bar", r'^\[INFO\]'))
def test_grep_hook_not_matching(regex, greppable_files, store):
hook = _make_grep_repo(regex, store)
ret, out = _hook_run(hook, ('f1', 'f2', 'f3'), color=False)
assert (ret, out) == (0, b'')


def _norm_pwd(path):
# Under windows bash's temp and windows temp is different.
# This normalizes to the bash /tmp
Expand Down

0 comments on commit a2373d0

Please sign in to comment.