Add --ignore-regex for URI/email handling.

This is for issue #676, where typos are found in actually-okay URIs/emails. Because these are closer to names in context, this ignores them.

Mechanically, this erases the URI/email text before the word regex is applied.
This commit is contained in:
jonmeow
2020-07-08 09:19:27 -07:00
parent 540c85e8cc
commit 018f0c6465
2 changed files with 90 additions and 5 deletions

View File

@ -455,6 +455,70 @@ def test_context(tmpdir, capsys):
assert 'ERROR' in lines[0]
def test_uri(tmpdir, capsys):
"""Test ignore regex functionality for URIs."""
d = str(tmpdir)
# Ignoring text in path.
with open(op.join(d, 'uri.txt'), 'w') as f:
f.write('# Please see http://example.com/abandonned for info\n')
assert cs.main(f.name) == 0
# Same is a typo with ignores disabled.
assert cs.main(f.name, '--ignore-regex=^$') == 1
# Test a different protocol.
with open(op.join(d, 'uri.txt'), 'w') as f:
f.write('# Please see https://example.com/abandonned for info\n')
assert cs.main(f.name) == 0
# Ignoring text in path ending with /.
with open(op.join(d, 'uri.txt'), 'w') as f:
f.write('# Please see http://example.com/abandonned/ for info\n')
assert cs.main(f.name) == 0
# Ignoring text in domain.
with open(op.join(d, 'uri.txt'), 'w') as f:
f.write('# Please see http://abandonned.com/example for info\n')
assert cs.main(f.name) == 0
# Ignoring text in anchor.
with open(op.join(d, 'uri.txt'), 'w') as f:
f.write('# Please see http://example.com/ex#abandonned for info\n')
assert cs.main(f.name) == 0
# Typo because there's no protocol.
with open(op.join(d, 'uri.txt'), 'w') as f:
f.write('# Please see example.com/abandonned for info\n')
assert cs.main(f.name) == 1
# Typo because there aren't enough domain parts.
with open(op.join(d, 'uri.txt'), 'w') as f:
f.write('# Please see http://abandonned for info\n')
assert cs.main(f.name) == 1
def test_email(tmpdir, capsys):
"""Test ignore regex functionality for emails."""
d = str(tmpdir)
# Ignoring text in username.
with open(op.join(d, 'email.txt'), 'w') as f:
f.write('# Please contact abandonned@example.com for info\n')
assert cs.main(f.name) == 0
# Same is a typo with ignores disabled.
assert cs.main(f.name, '--ignore-regex=^$') == 1
# Ignoring text in domain.
with open(op.join(d, 'email.txt'), 'w') as f:
f.write('# Please contact example@abandonned.com for info\n')
assert cs.main(f.name) == 0
# Typo because there's no TLD for an email.
with open(op.join(d, 'email.txt'), 'w') as f:
f.write('# Please contact abandonned@example for info\n')
assert cs.main(f.name) == 1
@contextlib.contextmanager
def FakeStdin(text):
if sys.version[0] == '2':