Skip to content

Commit 5bc5688

Browse files
authored
Merge pull request pre-commit#2761 from m-rsha/test-pygrep
test pygrep inline
2 parents f5ec578 + a2373d0 commit 5bc5688

File tree

2 files changed

+17
-46
lines changed

2 files changed

+17
-46
lines changed

tests/languages/pygrep_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44

55
from pre_commit.languages import pygrep
6+
from testing.language_helpers import run_language
67

78

89
@pytest.fixture
@@ -13,6 +14,9 @@ def some_files(tmpdir):
1314
tmpdir.join('f4').write_binary(b'foo\npattern\nbar\n')
1415
tmpdir.join('f5').write_binary(b'[INFO] hi\npattern\nbar')
1516
tmpdir.join('f6').write_binary(b"pattern\nbarwith'foo\n")
17+
tmpdir.join('f7').write_binary(b"hello'hi\nworld\n")
18+
tmpdir.join('f8').write_binary(b'foo\nbar\nbaz\n')
19+
tmpdir.join('f9').write_binary(b'[WARN] hi\n')
1620
with tmpdir.as_cwd():
1721
yield
1822

@@ -125,3 +129,16 @@ def test_multiline_multiline_flag_is_enabled(cap_out):
125129
out = cap_out.get()
126130
assert ret == 1
127131
assert out == 'f1:1:foo\nbar\n'
132+
133+
134+
def test_grep_hook_matching(some_files, tmp_path):
135+
ret = run_language(
136+
tmp_path, pygrep, 'ello', file_args=('f7', 'f8', 'f9'),
137+
)
138+
assert ret == (1, b"f7:1:hello'hi\n")
139+
140+
141+
@pytest.mark.parametrize('regex', ('nope', "foo'bar", r'^\[INFO\]'))
142+
def test_grep_hook_not_matching(regex, some_files, tmp_path):
143+
ret = run_language(tmp_path, pygrep, regex, file_args=('f7', 'f8', 'f9'))
144+
assert ret == (0, b'')

tests/repository_test.py

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -226,52 +226,6 @@ def test_output_isatty(tempdir_factory, store):
226226
)
227227

228228

229-
def _make_grep_repo(entry, store, args=()):
230-
config = {
231-
'repo': 'local',
232-
'hooks': [{
233-
'id': 'grep-hook',
234-
'name': 'grep-hook',
235-
'language': 'pygrep',
236-
'entry': entry,
237-
'args': args,
238-
'types': ['text'],
239-
}],
240-
}
241-
return _get_hook(config, store, 'grep-hook')
242-
243-
244-
@pytest.fixture
245-
def greppable_files(tmpdir):
246-
with tmpdir.as_cwd():
247-
cmd_output_b('git', 'init', '.')
248-
tmpdir.join('f1').write_binary(b"hello'hi\nworld\n")
249-
tmpdir.join('f2').write_binary(b'foo\nbar\nbaz\n')
250-
tmpdir.join('f3').write_binary(b'[WARN] hi\n')
251-
yield tmpdir
252-
253-
254-
def test_grep_hook_matching(greppable_files, store):
255-
hook = _make_grep_repo('ello', store)
256-
ret, out = _hook_run(hook, ('f1', 'f2', 'f3'), color=False)
257-
assert ret == 1
258-
assert _norm_out(out) == b"f1:1:hello'hi\n"
259-
260-
261-
def test_grep_hook_case_insensitive(greppable_files, store):
262-
hook = _make_grep_repo('ELLO', store, args=['-i'])
263-
ret, out = _hook_run(hook, ('f1', 'f2', 'f3'), color=False)
264-
assert ret == 1
265-
assert _norm_out(out) == b"f1:1:hello'hi\n"
266-
267-
268-
@pytest.mark.parametrize('regex', ('nope', "foo'bar", r'^\[INFO\]'))
269-
def test_grep_hook_not_matching(regex, greppable_files, store):
270-
hook = _make_grep_repo(regex, store)
271-
ret, out = _hook_run(hook, ('f1', 'f2', 'f3'), color=False)
272-
assert (ret, out) == (0, b'')
273-
274-
275229
def _norm_pwd(path):
276230
# Under windows bash's temp and windows temp is different.
277231
# This normalizes to the bash /tmp

0 commit comments

Comments
 (0)