|
| 1 | +import difflib |
| 2 | +import io |
1 | 3 | import sys
|
2 | 4 |
|
3 | 5 | import pytest
|
|
7 | 9 |
|
8 | 10 |
|
9 | 11 | 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 | + |
12 | 16 | with pytest.raises(SystemExit) as exc_info:
|
13 | 17 | main()
|
14 | 18 |
|
15 | 19 | assert exc_info.value.code == 0
|
16 | 20 |
|
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() |
22 | 38 |
|
23 | 39 | expected_message = 'Hint: Get it done right away!'
|
24 | 40 |
|
|
0 commit comments