add --negate flag to pygrep

This commit is contained in:
Marco Gorelli 2020-10-17 14:21:12 +01:00
parent 6ba50f3aa7
commit a0658c06bf
2 changed files with 99 additions and 4 deletions

View file

@ -8,6 +8,9 @@ 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")
with tmpdir.as_cwd():
yield
@ -30,6 +33,58 @@ def test_main(cap_out, pattern, expected_retcode, expected_out):
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'))