|
1 |
| -from unittest.mock import patch |
2 |
| - |
3 | 1 | import pytest
|
| 2 | +from pathlib import Path |
4 | 3 |
|
5 | 4 | from cpp_linter_hooks.clang_format import run_clang_format
|
6 | 5 |
|
7 | 6 |
|
8 |
| -@pytest.mark.skip(reason="don't know hwo to pass test.") |
9 | 7 | @pytest.mark.parametrize(
|
10 | 8 | ('args', 'expected_retval'), (
|
11 |
| - (['clang-format', '-i', '--style=Google', 'testing/main.c'], 0), |
12 |
| - (['clang-format', '-i', '--style=Google', '--version=13', 'testing/main.c'], 0), |
| 9 | + (['--style=Google'], (0, "")), |
| 10 | + (['--style=Google', '--version=16'], (0, "")), |
13 | 11 | ),
|
14 | 12 | )
|
15 |
| -@patch('cpp_linter_hooks.clang_format.subprocess.run') |
16 |
| -def test_run_clang_format_valid(mock_subprocess_run, args, expected_retval): |
17 |
| - mock_subprocess_run.return_value = expected_retval |
18 |
| - ret = run_clang_format(args) |
| 13 | +def test_run_clang_format_valid(args, expected_retval, tmp_path): |
| 14 | + # copy test file to tmp_path to prevent modifying repo data |
| 15 | + test_file = tmp_path / "main.c" |
| 16 | + test_file.write_bytes(Path("testing/main.c").read_bytes()) |
| 17 | + ret = run_clang_format(args + [str(test_file)]) |
19 | 18 | assert ret == expected_retval
|
| 19 | + assert test_file.read_text() == Path("testing/good.c").read_text() |
20 | 20 |
|
21 | 21 |
|
22 | 22 | @pytest.mark.parametrize(
|
23 | 23 | ('args', 'expected_retval'), (
|
24 |
| - (['clang-format', '-i', '--style=Google', 'abc/def.c'], 1), |
25 |
| - (['clang-format', '-i', '--style=Google', '--version=13', 'abc/def.c'], 1), |
| 24 | + (['--style=Google',], 1), |
| 25 | + (['--style=Google', '--version=16'], 1), |
26 | 26 | ),
|
27 | 27 | )
|
28 |
| -@patch('cpp_linter_hooks.clang_format.subprocess.run', side_effect=FileNotFoundError) |
29 |
| -def test_run_clang_format_invalid(mock_subprocess_run, args, expected_retval): |
30 |
| - mock_subprocess_run.return_value = expected_retval |
31 |
| - try: |
32 |
| - ret = run_clang_format(args) |
33 |
| - except FileNotFoundError: |
34 |
| - assert ret == expected_retval |
| 28 | +def test_run_clang_format_invalid(args, expected_retval, tmp_path): |
| 29 | + # non existent file |
| 30 | + test_file = tmp_path / "main.c" |
| 31 | + |
| 32 | + ret, _ = run_clang_format(args + [str(test_file)]) |
| 33 | + assert ret == expected_retval |
0 commit comments