|
1 | 1 | import os |
| 2 | +import pathlib |
2 | 3 | from pathlib import Path |
3 | 4 | from typing import List |
4 | 5 |
|
|
25 | 26 | TESTS, |
26 | 27 | ids=TEST_NAMES, |
27 | 28 | ) |
28 | | -def test_formatting(test_file: str, capsys: pytest.CaptureFixture[str]) -> None: |
29 | | - """Test that we correctly format all files in the format directory""" |
30 | | - pydocstringformatter.run_docstring_formatter([test_file]) |
| 29 | +def test_formatting( |
| 30 | + test_file: str, capsys: pytest.CaptureFixture[str], tmp_path: pathlib.Path |
| 31 | +) -> None: |
| 32 | + """Test that we correctly format all files in the format directory. |
| 33 | +
|
| 34 | + We create and write to a temporary file so that the original test file |
| 35 | + isn't overwritten and the 'py.out' file can represent an actual |
| 36 | + python file instead of a diff. |
| 37 | + """ |
| 38 | + # Setup |
| 39 | + temp_file_name = str(tmp_path / "test_file.py") |
| 40 | + with open(test_file + ".out", encoding="utf-8") as expected_output: |
| 41 | + expected_lines = expected_output.readlines() |
| 42 | + |
| 43 | + # Get original lines from test file and write to temporary file |
| 44 | + with open(test_file, encoding="utf-8") as original_file: |
| 45 | + original_lines = original_file.readlines() |
| 46 | + with open(temp_file_name, "w", encoding="utf-8") as temp_file: |
| 47 | + temp_file.writelines(original_lines) |
| 48 | + |
| 49 | + pydocstringformatter.run_docstring_formatter([temp_file_name, "--write"]) |
| 50 | + |
31 | 51 | output = capsys.readouterr() |
32 | 52 | assert not output.err |
33 | | - with open(test_file + ".out", encoding="utf-8") as expected_output: |
34 | | - assert output.out == "".join(expected_output.readlines()) |
| 53 | + with open(temp_file_name, encoding="utf-8") as temp_file: |
| 54 | + assert temp_file.readlines() == expected_lines |
0 commit comments