Skip to content

Commit 019f099

Browse files
committed
test: provide more guidance about working with gold files
And make it easier to add new gold tests.
1 parent 220e6a7 commit 019f099

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

tests/gold/README.rst

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,26 @@ these comparisons is in tests/goldtest.py.
99

1010
If gold tests are failing, you may need to update the gold files by copying the
1111
current output of the tests into the gold files. When a test fails, the actual
12-
output is in the tests/actual directory. Do not commit those files to git.
12+
output is in the tests/actual directory. Those files are ignored by git.
1313

14-
You can run just the failed tests again with::
14+
There's a Makefile in the html directory for working with gold files and their
15+
associated support files. If actual outputs have changed so that comparisons
16+
are failing, but the new output is correct, you can use "make update-gold" to
17+
copy the actual output as the new gold files.
18+
19+
If you have added a gold test, you'll need to manually copy the tests/actual
20+
files to tests/gold.
21+
22+
Once you've copied the actual results to the gold files, or to check your work
23+
again, you can run just the failed tests again with::
1524

1625
tox -e py39 -- -n 0 --lf
1726

1827
The saved HTML files in the html directories can't be viewed properly without
1928
the supporting CSS and Javascript files. But we don't want to save copies of
20-
those files in every subdirectory. There's a Makefile in the html directory
21-
for working with the saved copies of the support files.
29+
those files in every subdirectory. The make target "make complete" in
30+
tests/gold/html will copy the support file so you can open the HTML files to
31+
see how they look.
2232

2333
If the output files are correct, you can update the gold files with "make
2434
update-gold". If there are version-specific gold files (for example,

tests/gold/html/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ complete: ## Copy support files into directories so the HTML can be viewed prop
1717
clean: ## Remove the effects of this Makefile.
1818
@git clean -fq .
1919

20-
update-gold: ## Copy output files from latest tests to gold files.
20+
update-gold: ## Copy actual output files from latest tests to gold files.
2121
@for sub in ../../actual/html/*; do \
2222
rsync --verbose --existing --recursive $$sub/ $$(basename $$sub) ; \
2323
done ; \

tests/goldtest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ def compare(
4646
"""
4747
__tracebackhide__ = True # pytest, please don't show me this function.
4848
assert os_sep("/gold/") in expected_dir
49+
assert os.path.exists(actual_dir)
50+
os.makedirs(expected_dir, exist_ok=True)
4951

5052
dc = filecmp.dircmp(expected_dir, actual_dir)
5153
diff_files = _fnmatch_list(dc.diff_files, file_pattern)
@@ -56,9 +58,11 @@ def save_mismatch(f: str) -> None:
5658
"""Save a mismatched result to tests/actual."""
5759
save_path = expected_dir.replace(os_sep("/gold/"), os_sep("/actual/"))
5860
os.makedirs(save_path, exist_ok=True)
59-
with open(os.path.join(save_path, f), "w") as savef:
61+
save_file = os.path.join(save_path, f)
62+
with open(save_file, "w") as savef:
6063
with open(os.path.join(actual_dir, f)) as readf:
6164
savef.write(readf.read())
65+
print(os_sep(f"Saved actual output to '{save_file}': see tests/gold/README.rst"))
6266

6367
# filecmp only compares in binary mode, but we want text mode. So
6468
# look through the list of different files, and compare them

tests/test_goldtest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from tests.coveragetest import CoverageTest, TESTS_DIR
1414
from tests.goldtest import compare, gold_path
1515
from tests.goldtest import contains, contains_any, contains_rx, doesnt_contain
16-
from tests.helpers import re_line, remove_tree
16+
from tests.helpers import os_sep, re_line, remove_tree
1717

1818
GOOD_GETTY = """\
1919
Four score and seven years ago our fathers brought forth upon this continent, a
@@ -73,6 +73,10 @@ def test_bad(self) -> None:
7373
assert "+ Five score" in stdout
7474
assert re_line(rf"^:::: diff '.*{GOLD_PATH_RX}' and '{OUT_PATH_RX}'", stdout)
7575
assert re_line(rf"^:::: end diff '.*{GOLD_PATH_RX}' and '{OUT_PATH_RX}'", stdout)
76+
assert (
77+
os_sep(f"Saved actual output to '{ACTUAL_GETTY_FILE}': see tests/gold/README.rst")
78+
in os_sep(stdout)
79+
)
7680
assert " D/D/D, Gxxx, Pennsylvania" in stdout
7781

7882
# The actual file was saved.

0 commit comments

Comments
 (0)