Skip to content

Commit 7cb7630

Browse files
committed
Escape filenames which looks like regexp
1 parent d5443b7 commit 7cb7630

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

Diff for: relint.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def main():
188188
paths = {
189189
path
190190
for file in args.files
191-
for path in glob.iglob(file, recursive=True)
191+
for path in glob.iglob(glob.escape(file), recursive=True)
192192
}
193193

194194
tests = list(load_config(args.config))

Diff for: setup.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ setup_requires =
2929
tests_require =
3030
pytest
3131
pytest-cov
32+
pytest-mock
3233
py_modules = relint
3334

3435
[options.entry_points]

Diff for: test_relint.py

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import difflib
2+
import io
13
import sys
24

35
import pytest
@@ -7,18 +9,32 @@
79

810

911
class TestMain:
10-
def test_main_execution(self):
11-
sys.argv.extend(['relint.py', 'test_relint.py'])
12+
@pytest.mark.parametrize('filename', ['test_relint.py', '[a-b].py', '[b-a].py'])
13+
def test_main_execution(self, filename, mocker):
14+
mocker.patch.object(sys, 'argv', ['relint.py', filename])
15+
1216
with pytest.raises(SystemExit) as exc_info:
1317
main()
1418

1519
assert exc_info.value.code == 0
1620

17-
def test_main_execution_with_diff(self, capsys):
18-
sys.argv.extend(['relint.py', 'test_relint.py', '--diff'])
19-
sys.stdin = open('test.diff')
20-
with pytest.raises(SystemExit) as exc_info:
21-
main()
21+
def test_main_execution_with_diff(self, capsys, mocker, tmpdir):
22+
old_content = ["# dummy test file\n", "pass"]
23+
new_content = ["# dummy test file\n", "# TODO do something\n", "pass"]
24+
tmpdir.join('dummy.py').write(''.join(new_content))
25+
26+
diff = io.StringIO()
27+
diff.write("diff --git a/dummy.py b/dummy.py\n")
28+
diff.writelines(difflib.unified_diff(old_content, new_content))
29+
diff.seek(0)
30+
31+
mocker.patch.object(sys, 'argv', ['relint.py', 'dummy.py', '--diff'])
32+
mocker.patch.object(sys, 'stdin', diff)
33+
34+
tmpdir.join('.relint.yml').write(open('.relint.yml').read())
35+
with tmpdir.as_cwd():
36+
with pytest.raises(SystemExit) as exc_info:
37+
main()
2238

2339
expected_message = 'Hint: Get it done right away!'
2440

0 commit comments

Comments
 (0)