Skip to content

Commit e950930

Browse files
committed
Implement pygrep language as a replacement for pcre
1 parent 3a7806e commit e950930

File tree

8 files changed

+186
-112
lines changed

8 files changed

+186
-112
lines changed

pre_commit/languages/all.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pre_commit.languages import golang
66
from pre_commit.languages import node
77
from pre_commit.languages import pcre
8+
from pre_commit.languages import pygrep
89
from pre_commit.languages import python
910
from pre_commit.languages import ruby
1011
from pre_commit.languages import script
@@ -54,6 +55,7 @@
5455
'golang': golang,
5556
'node': node,
5657
'pcre': pcre,
58+
'pygrep': pygrep,
5759
'python': python,
5860
'ruby': ruby,
5961
'script': script,

pre_commit/languages/pygrep.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from __future__ import absolute_import
2+
from __future__ import unicode_literals
3+
4+
import argparse
5+
import re
6+
import sys
7+
8+
from pre_commit import output
9+
from pre_commit.languages import helpers
10+
from pre_commit.xargs import xargs
11+
12+
13+
ENVIRONMENT_DIR = None
14+
get_default_version = helpers.basic_get_default_version
15+
healthy = helpers.basic_healthy
16+
install_environment = helpers.no_install
17+
18+
19+
def _process_filename_by_line(pattern, filename):
20+
retv = 0
21+
with open(filename, 'rb') as f:
22+
for line_no, line in enumerate(f, start=1):
23+
if pattern.search(line):
24+
retv = 1
25+
output.write('{}:{}:'.format(filename, line_no))
26+
output.write_line(line.rstrip(b'\r\n'))
27+
return retv
28+
29+
30+
def run_hook(repo_cmd_runner, hook, file_args):
31+
exe = (sys.executable, '-m', __name__)
32+
exe += tuple(hook['args']) + (hook['entry'],)
33+
return xargs(exe, file_args)
34+
35+
36+
def main(argv=None):
37+
parser = argparse.ArgumentParser(
38+
description=(
39+
'grep-like finder using python regexes. Unlike grep, this tool '
40+
'returns nonzero when it finds a match and zero otherwise. The '
41+
'idea here being that matches are "problems".'
42+
),
43+
)
44+
parser.add_argument('-i', '--ignore-case', action='store_true')
45+
parser.add_argument('pattern', help='python regex pattern.')
46+
parser.add_argument('filenames', nargs='*')
47+
args = parser.parse_args(argv)
48+
49+
flags = re.IGNORECASE if args.ignore_case else 0
50+
pattern = re.compile(args.pattern.encode(), flags)
51+
52+
retv = 0
53+
for filename in args.filenames:
54+
retv |= _process_filename_by_line(pattern, filename)
55+
return retv
56+
57+
58+
if __name__ == '__main__':
59+
exit(main())

pre_commit/repository.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ class LocalRepository(Repository):
202202
def _cmd_runner_from_deps(self, language_name, deps):
203203
"""local repositories have a cmd runner per hook"""
204204
language = languages[language_name]
205-
# pcre / script / system / docker_image do not have environments so
206-
# they work out of the current directory
205+
# pcre / pygrep / script / system / docker_image do not have
206+
# environments so they work out of the current directory
207207
if language.ENVIRONMENT_DIR is None:
208208
return PrefixedCommandRunner(git.get_root())
209209
else:

testing/fixtures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def config_with_local_hooks():
7373
('id', 'do_not_commit'),
7474
('name', 'Block if "DO NOT COMMIT" is found'),
7575
('entry', 'DO NOT COMMIT'),
76-
('language', 'pcre'),
76+
('language', 'pygrep'),
7777
('files', '^(.*)$'),
7878
))],
7979
),

testing/resources/pcre_hooks_repo/.pre-commit-hooks.yaml

Lines changed: 0 additions & 16 deletions
This file was deleted.

tests/languages/pygrep_test.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from __future__ import absolute_import
2+
from __future__ import unicode_literals
3+
4+
import pytest
5+
6+
from pre_commit.languages import pygrep
7+
8+
9+
@pytest.fixture
10+
def some_files(tmpdir):
11+
tmpdir.join('f1').write_binary(b'foo\nbar\n')
12+
tmpdir.join('f2').write_binary(b'[INFO] hi\n')
13+
tmpdir.join('f3').write_binary(b"with'quotes\n")
14+
with tmpdir.as_cwd():
15+
yield
16+
17+
18+
@pytest.mark.usefixtures('some_files')
19+
@pytest.mark.parametrize(
20+
('pattern', 'expected_retcode', 'expected_out'),
21+
(
22+
('baz', 0, ''),
23+
('foo', 1, 'f1:1:foo\n'),
24+
('bar', 1, 'f1:2:bar\n'),
25+
(r'(?i)\[info\]', 1, 'f2:1:[INFO] hi\n'),
26+
("h'q", 1, "f3:1:with'quotes\n"),
27+
),
28+
)
29+
def test_main(some_files, cap_out, pattern, expected_retcode, expected_out):
30+
ret = pygrep.main((pattern, 'f1', 'f2', 'f3'))
31+
out = cap_out.get()
32+
assert ret == expected_retcode
33+
assert out == expected_out
34+
35+
36+
def test_ignore_case(some_files, cap_out):
37+
ret = pygrep.main(('--ignore-case', 'info', 'f1', 'f2', 'f3'))
38+
out = cap_out.get()
39+
assert ret == 1
40+
assert out == 'f2:1:[INFO] hi\n'

tests/repository_test.py

Lines changed: 80 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import absolute_import
22
from __future__ import unicode_literals
33

4+
import collections
45
import io
56
import os.path
67
import re
@@ -36,6 +37,10 @@
3637
from testing.util import xfailif_windows_no_ruby
3738

3839

40+
def _norm_out(b):
41+
return b.replace(b'\r\n', b'\n')
42+
43+
3944
def _test_hook_repo(
4045
tempdir_factory,
4146
store,
@@ -54,7 +59,7 @@ def _test_hook_repo(
5459
]
5560
ret = repo.run_hook(hook_dict, args)
5661
assert ret[0] == expected_return_code
57-
assert ret[1].replace(b'\r\n', b'\n') == expected
62+
assert _norm_out(ret[1]) == expected
5863

5964

6065
@pytest.mark.integration
@@ -114,7 +119,7 @@ def run_on_version(version, expected_output):
114119
]
115120
ret = repo.run_hook(hook_dict, [])
116121
assert ret[0] == 0
117-
assert ret[1].replace(b'\r\n', b'\n') == expected_output
122+
assert _norm_out(ret[1]) == expected_output
118123

119124
run_on_version('python3.4', b'3.4\n[]\nHello World\n')
120125
run_on_version('python3.5', b'3.5\n[]\nHello World\n')
@@ -277,25 +282,6 @@ def test_missing_executable(tempdir_factory, store):
277282
)
278283

279284

280-
@pytest.mark.integration
281-
def test_missing_pcre_support(tempdir_factory, store):
282-
orig_find_executable = parse_shebang.find_executable
283-
284-
def no_grep(exe, **kwargs):
285-
if exe == pcre.GREP:
286-
return None
287-
else:
288-
return orig_find_executable(exe, **kwargs)
289-
290-
with mock.patch.object(parse_shebang, 'find_executable', no_grep):
291-
_test_hook_repo(
292-
tempdir_factory, store, 'pcre_hooks_repo',
293-
'regex-with-quotes', ['/dev/null'],
294-
'Executable `{}` not found'.format(pcre.GREP).encode('UTF-8'),
295-
expected_return_code=1,
296-
)
297-
298-
299285
@pytest.mark.integration
300286
def test_run_a_script_hook(tempdir_factory, store):
301287
_test_hook_repo(
@@ -330,85 +316,88 @@ def test_run_hook_with_curly_braced_arguments(tempdir_factory, store):
330316
)
331317

332318

333-
@xfailif_no_pcre_support
334-
@pytest.mark.integration
335-
def test_pcre_hook_no_match(tempdir_factory, store):
336-
path = git_dir(tempdir_factory)
337-
with cwd(path):
338-
with io.open('herp', 'w') as herp:
339-
herp.write('foo')
340-
341-
with io.open('derp', 'w') as derp:
342-
derp.write('bar')
343-
344-
_test_hook_repo(
345-
tempdir_factory, store, 'pcre_hooks_repo',
346-
'regex-with-quotes', ['herp', 'derp'], b'',
347-
)
348-
349-
_test_hook_repo(
350-
tempdir_factory, store, 'pcre_hooks_repo',
351-
'other-regex', ['herp', 'derp'], b'',
352-
)
353-
319+
def _make_grep_repo(language, entry, store, args=()):
320+
config = collections.OrderedDict((
321+
('repo', 'local'),
322+
(
323+
'hooks', [
324+
collections.OrderedDict((
325+
('id', 'grep-hook'),
326+
('name', 'grep-hook'),
327+
('language', language),
328+
('entry', entry),
329+
('args', args),
330+
('types', ['text']),
331+
)),
332+
],
333+
),
334+
))
335+
repo = Repository.create(config, store)
336+
(_, hook), = repo.hooks
337+
return repo, hook
354338

355-
@xfailif_no_pcre_support
356-
@pytest.mark.integration
357-
def test_pcre_hook_matching(tempdir_factory, store):
358-
path = git_dir(tempdir_factory)
359-
with cwd(path):
360-
with io.open('herp', 'w') as herp:
361-
herp.write("\nherpfoo'bard\n")
362339

363-
with io.open('derp', 'w') as derp:
364-
derp.write('[INFO] information yo\n')
340+
@pytest.fixture
341+
def greppable_files(tmpdir):
342+
with tmpdir.as_cwd():
343+
cmd_output('git', 'init', '.')
344+
tmpdir.join('f1').write_binary(b"hello'hi\nworld\n")
345+
tmpdir.join('f2').write_binary(b'foo\nbar\nbaz\n')
346+
tmpdir.join('f3').write_binary(b'[WARN] hi\n')
347+
yield tmpdir
365348

366-
_test_hook_repo(
367-
tempdir_factory, store, 'pcre_hooks_repo',
368-
'regex-with-quotes', ['herp', 'derp'], b"herp:2:herpfoo'bard\n",
369-
expected_return_code=1,
370-
)
371349

372-
_test_hook_repo(
373-
tempdir_factory, store, 'pcre_hooks_repo',
374-
'other-regex', ['herp', 'derp'], b'derp:1:[INFO] information yo\n',
375-
expected_return_code=1,
376-
)
350+
class TestPygrep(object):
351+
language = 'pygrep'
377352

353+
def test_grep_hook_matching(self, greppable_files, store):
354+
repo, hook = _make_grep_repo(self.language, 'ello', store)
355+
ret, out, _ = repo.run_hook(hook, ('f1', 'f2', 'f3'))
356+
assert ret == 1
357+
assert _norm_out(out) == b"f1:1:hello'hi\n"
378358

379-
@xfailif_no_pcre_support
380-
@pytest.mark.integration
381-
def test_pcre_hook_case_insensitive_option(tempdir_factory, store):
382-
path = git_dir(tempdir_factory)
383-
with cwd(path):
384-
with io.open('herp', 'w') as herp:
385-
herp.write('FoOoOoObar\n')
359+
def test_grep_hook_case_insensitive(self, greppable_files, store):
360+
repo, hook = _make_grep_repo(self.language, 'ELLO', store, args=['-i'])
361+
ret, out, _ = repo.run_hook(hook, ('f1', 'f2', 'f3'))
362+
assert ret == 1
363+
assert _norm_out(out) == b"f1:1:hello'hi\n"
386364

387-
_test_hook_repo(
388-
tempdir_factory, store, 'pcre_hooks_repo',
389-
'regex-with-grep-args', ['herp'], b'herp:1:FoOoOoObar\n',
390-
expected_return_code=1,
391-
)
365+
@pytest.mark.parametrize('regex', ('nope', "foo'bar", r'^\[INFO\]'))
366+
def test_grep_hook_not_matching(self, regex, greppable_files, store):
367+
repo, hook = _make_grep_repo(self.language, regex, store)
368+
ret, out, _ = repo.run_hook(hook, ('f1', 'f2', 'f3'))
369+
assert (ret, out) == (0, b'')
392370

393371

394372
@xfailif_no_pcre_support
395-
@pytest.mark.integration
396-
def test_pcre_many_files(tempdir_factory, store):
397-
# This is intended to simulate lots of passing files and one failing file
398-
# to make sure it still fails. This is not the case when naively using
399-
# a system hook with `grep -H -n '...'` and expected_return_code=1.
400-
path = git_dir(tempdir_factory)
401-
with cwd(path):
402-
with io.open('herp', 'w') as herp:
403-
herp.write('[INFO] info\n')
404-
405-
_test_hook_repo(
406-
tempdir_factory, store, 'pcre_hooks_repo',
407-
'other-regex',
408-
['/dev/null'] * 15000 + ['herp'],
409-
b'herp:1:[INFO] info\n',
410-
expected_return_code=1,
411-
)
373+
class TestPCRE(TestPygrep):
374+
"""organized as a class for xfailing pcre"""
375+
language = 'pcre'
376+
377+
def test_pcre_hook_many_files(self, greppable_files, store):
378+
# This is intended to simulate lots of passing files and one failing
379+
# file to make sure it still fails. This is not the case when naively
380+
# using a system hook with `grep -H -n '...'`
381+
repo, hook = _make_grep_repo('pcre', 'ello', store)
382+
ret, out, _ = repo.run_hook(hook, (os.devnull,) * 15000 + ('f1',))
383+
assert ret == 1
384+
assert _norm_out(out) == b"f1:1:hello'hi\n"
385+
386+
def test_missing_pcre_support(self, greppable_files, store):
387+
orig_find_executable = parse_shebang.find_executable
388+
389+
def no_grep(exe, **kwargs):
390+
if exe == pcre.GREP:
391+
return None
392+
else:
393+
return orig_find_executable(exe, **kwargs)
394+
395+
with mock.patch.object(parse_shebang, 'find_executable', no_grep):
396+
repo, hook = _make_grep_repo('pcre', 'ello', store)
397+
ret, out, _ = repo.run_hook(hook, ('f1', 'f2', 'f3'))
398+
assert ret == 1
399+
expected = 'Executable `{}` not found'.format(pcre.GREP).encode()
400+
assert out == expected
412401

413402

414403
def _norm_pwd(path):
@@ -703,7 +692,7 @@ def test_local_python_repo(store):
703692
(_, hook), = repo.hooks
704693
ret = repo.run_hook(hook, ('filename',))
705694
assert ret[0] == 0
706-
assert ret[1].replace(b'\r\n', b'\n') == b"['filename']\nHello World\n"
695+
assert _norm_out(ret[1]) == b"['filename']\nHello World\n"
707696

708697

709698
def test_hook_id_not_present(tempdir_factory, store, fake_log_handler):

0 commit comments

Comments
 (0)