summaryrefslogtreecommitdiffstats
path: root/tests/languages/pygrep_test.py
blob: c6271c807cc209dedbd3522d395fbd249ff78ed3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from __future__ import annotations

import pytest

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


@pytest.fixture
def some_files(tmpdir):
    tmpdir.join('f1').write_binary(b'foo\nbar\n')
    tmpdir.join('f2').write_binary(b'[INFO] hi\n')
    tmpdir.join('f3').write_binary(b"with'quotes\n")
    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


@pytest.mark.usefixtures('some_files')
@pytest.mark.parametrize(
    ('pattern', 'expected_retcode', 'expected_out'),
    (
        ('baz', 0, ''),
        ('foo', 1, 'f1:1:foo\n'),
        ('bar', 1, 'f1:2:bar\n'),
        (r'(?i)\[info\]', 1, 'f2:1:[INFO] hi\n'),
        ("h'q", 1, "f3:1:with'quotes\n"),
    ),
)
def test_main(cap_out, pattern, expected_retcode, expected_out):
    ret = pygrep.main((pattern, 'f1', 'f2', 'f3'))
    out = cap_out.get()
    assert ret == expected_retcode
    assert out == expected_out


@pytest.mark.usefixtures('some_files')
def test_negate_by_line_no_match(cap_out):
    ret = pygrep.main(('pattern\nbar', 'f4', 'f5', 'f6', '--negate'))
    out = cap_out.get()
    assert ret == 1
    assert out == 'f4\nf5\nf6\n'


@pytest.mark.usefixtures('some_files')
def test_negate_by_line_two_match(cap_out):
    ret = pygrep.main(('foo', 'f4', 'f5', 'f6', '--negate'))
    out = cap_out.get()
    assert ret == 1
    assert out == 'f5\n'


@pytest.mark.usefixtures('some_files')
def test_negate_by_line_all_match(cap_out):
    ret = pygrep.main(('pattern', 'f4', 'f5', 'f6', '--negate'))
    out = cap_out.get()
    assert ret == 0
    assert out == ''


@pytest.mark.usefixtures('some_files')
def test_negate_by_file_no_match(cap_out):
    ret = pygrep.main(('baz', 'f4', 'f5', 'f6', '--negate', '--multiline'))
    out = cap_out.get()
    assert ret == 1
    assert out == 'f4\nf5\nf6\n'


@pytest.mark.usefixtures('some_files')
def test_negate_by_file_one_match(cap_out):
    ret = pygrep.main(
        ('foo\npattern', 'f4', 'f5', 'f6', '--negate', '--multiline'),
    )
    out = cap_out.get()
    assert ret == 1
    assert out == 'f5\nf6\n'


@pytest.mark.usefixtures('some_files')
def test_negate_by_file_all_match(cap_out):
    ret = pygrep.main(
        ('pattern\nbar', 'f4', 'f5', 'f6', '--negate', '--multiline'),
    )
    out = cap_out.get()
    assert ret == 0
    assert out == ''


@pytest.mark.usefixtures('some_files')
def test_ignore_case(cap_out):
    ret = pygrep.main(('--ignore-case', 'info', 'f1', 'f2', 'f3'))
    out = cap_out.get()
    assert ret == 1
    assert out == 'f2:1:[INFO] hi\n'


@pytest.mark.usefixtures('some_files')
def test_multiline(cap_out):
    ret = pygrep.main(('--multiline', r'foo\nbar', 'f1', 'f2', 'f3'))
    out = cap_out.get()
    assert ret == 1
    assert out == 'f1:1:foo\nbar\n'


@pytest.mark.usefixtures('some_files')
def test_multiline_line_number(cap_out):
    ret = pygrep.main(('--multiline', r'ar', 'f1', 'f2', 'f3'))
    out = cap_out.get()
    assert ret == 1
    assert out == 'f1:2:bar\n'


@pytest.mark.usefixtures('some_files')
def test_multiline_dotall_flag_is_enabled(cap_out):
    ret = pygrep.main(('--multiline', r'o.*bar', 'f1', 'f2', 'f3'))
    out = cap_out.get()
    assert ret == 1
    assert out == 'f1:1:foo\nbar\n'


@pytest.mark.usefixtures('some_files')
def test_multiline_multiline_flag_is_enabled(cap_out):
    ret = pygrep.main(('--multiline', r'foo$.*bar', 'f1', 'f2', 'f3'))
    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'')