Skip to content

Commit 17036ce

Browse files
authored
Add --update-expected-output (#492)
1 parent 5a56060 commit 17036ce

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

pydocstringformatter/_testutils/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
LOGGER = logging.getLogger(__name__)
1919

20+
UPDATE_OUTPUT_OPTION = "--update-expected-output"
21+
2022

2123
class FormatterAsserter(contextlib.AbstractContextManager): # type: ignore[type-arg]
2224
"""ContextManager used to assert that a Formatter does something on a docstring.
@@ -84,4 +86,10 @@ def __exit__(
8486
return None
8587

8688

87-
__all__ = ["FormatterAsserter", "MakeAFormatter", "MakeBFormatter", "AddBFormatter"]
89+
__all__ = [
90+
"FormatterAsserter",
91+
"MakeAFormatter",
92+
"MakeBFormatter",
93+
"AddBFormatter",
94+
"UPDATE_OUTPUT_OPTION",
95+
]

tests/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import pytest
44

5+
from pydocstringformatter._testutils import UPDATE_OUTPUT_OPTION
6+
57

68
@pytest.fixture
79
def test_file(tmp_path: Path) -> str:
@@ -10,3 +12,14 @@ def test_file(tmp_path: Path) -> str:
1012
with open(filename, "w", encoding="utf-8") as file:
1113
file.write('"""A multi-line\ndocstring."""')
1214
return str(filename)
15+
16+
17+
def pytest_addoption(parser: pytest.Parser) -> None:
18+
"""Add command line options to pytest."""
19+
parser.addoption(
20+
UPDATE_OUTPUT_OPTION,
21+
action="store_true",
22+
help="Update the expected output for tests. This will overwrite the output "
23+
"files to be as the tests are currently producing them.",
24+
default=False,
25+
)

tests/test_formatting.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pytest
88

99
import pydocstringformatter
10+
from pydocstringformatter._testutils import UPDATE_OUTPUT_OPTION
1011

1112
HERE = Path(__file__)
1213
TEST_DATA = HERE.parent / "data" / "format"
@@ -28,7 +29,10 @@
2829
ids=TEST_NAMES,
2930
)
3031
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,
3236
) -> None:
3337
"""Test that we correctly format all files in the format directory.
3438
@@ -76,7 +80,23 @@ def test_formatting(
7680
[temp_file_name, "--write"] + additional_args
7781
)
7882

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+
)
8187
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

Comments
 (0)