|
7 | 7 | import pytest
|
8 | 8 |
|
9 | 9 | import pydocstringformatter
|
| 10 | +from pydocstringformatter._testutils import UPDATE_OUTPUT_OPTION |
10 | 11 |
|
11 | 12 | HERE = Path(__file__)
|
12 | 13 | TEST_DATA = HERE.parent / "data" / "format"
|
|
28 | 29 | ids=TEST_NAMES,
|
29 | 30 | )
|
30 | 31 | def test_formatting(
|
31 |
| - test_file: str, capsys: pytest.CaptureFixture[str], tmp_path: pathlib.Path |
| 32 | + test_file: str, |
| 33 | + capsys: pytest.CaptureFixture[str], |
| 34 | + tmp_path: pathlib.Path, |
| 35 | + request: pytest.FixtureRequest, |
32 | 36 | ) -> None:
|
33 | 37 | """Test that we correctly format all files in the format directory.
|
34 | 38 |
|
@@ -76,7 +80,23 @@ def test_formatting(
|
76 | 80 | [temp_file_name, "--write"] + additional_args
|
77 | 81 | )
|
78 | 82 |
|
79 |
| - output = capsys.readouterr() |
80 |
| - assert output.err == error_message.format(testfile=os.path.abspath(temp_file_name)) |
| 83 | + error_output = capsys.readouterr() |
| 84 | + assert error_output.err == error_message.format( |
| 85 | + testfile=os.path.abspath(temp_file_name) |
| 86 | + ) |
81 | 87 | with open(temp_file_name, "rb") as f:
|
82 |
| - assert f.read() == expected_output |
| 88 | + output = f.read() |
| 89 | + try: |
| 90 | + assert output.decode("utf-8") == expected_output.decode("utf-8") |
| 91 | + except AssertionError as e: # pragma: no cover |
| 92 | + if request.config.getoption(UPDATE_OUTPUT_OPTION): |
| 93 | + with open(test_name, "wb") as fw: |
| 94 | + fw.write(output) |
| 95 | + pytest.fail( |
| 96 | + "Updated expected output. Please check the changes and commit them." |
| 97 | + ) |
| 98 | + |
| 99 | + raise AssertionError( |
| 100 | + f"Output of '{Path(test_file).stem}' does not match expected output. " |
| 101 | + f"Run with '{UPDATE_OUTPUT_OPTION}' to update the expected output." |
| 102 | + ) from e |
0 commit comments