1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
"""Tests of the helpers in goldtest.py"""
import os.path
import re
import pytest
from tests.coveragetest import CoverageTest, TESTS_DIR
from tests.goldtest import compare, gold_path
from tests.helpers import os_sep, re_line, remove_tree
GOOD_GETTY = """\
Four score and seven years ago our fathers brought forth upon this continent, a
new nation, conceived in Liberty, and dedicated to the proposition that all men
are created equal.
11/19/9999, Gettysburg, Pennsylvania
"""
BAD_GETTY = """\
Five score and seven years ago our fathers brought forth upon this continent, a
new nation, conceived in Liberty, and dedicated to the proposition that all men
are created equal.
333/4444/55555, Gettysburg, Pennsylvania
"""
SCRUBS = [
# Numbers don't matter when comparing.
(r'\d+', 'D'),
]
ACTUAL_DIR = os.path.join(TESTS_DIR, "actual/testing")
ACTUAL_GETTY_FILE = os.path.join(ACTUAL_DIR, "gettysburg.txt")
GOLD_PATH_RE = re.escape(os_sep("/tests/gold/testing/gettysburg.txt"))
OUT_PATH_RE = re.escape(os_sep("out/gettysburg.txt"))
class CompareTest(CoverageTest):
"""Tests of goldtest.py:compare()"""
def setup_test(self):
super().setup_test()
self.addCleanup(remove_tree, ACTUAL_DIR)
def test_good(self):
self.make_file("out/gettysburg.txt", GOOD_GETTY)
compare(gold_path("testing"), "out", scrubs=SCRUBS)
self.assert_doesnt_exist(ACTUAL_GETTY_FILE)
def test_bad(self):
self.make_file("out/gettysburg.txt", BAD_GETTY)
# compare() raises an assertion.
msg = rf"Files differ: .*{GOLD_PATH_RE} != {OUT_PATH_RE}"
with pytest.raises(AssertionError, match=msg):
compare(gold_path("testing"), "out", scrubs=SCRUBS)
# Stdout has a description of the diff. The diff shows the scrubbed content.
stdout = self.stdout()
print(stdout)
assert "- Four score" in stdout
assert "+ Five score" in stdout
assert re_line(stdout, rf"^:::: diff '.*{GOLD_PATH_RE}' and '{OUT_PATH_RE}'")
assert re_line(stdout, rf"^:::: end diff '.*{GOLD_PATH_RE}' and '{OUT_PATH_RE}'")
assert " D/D/D, Gettysburg, Pennsylvania" in stdout
# The actual file was saved.
with open(ACTUAL_GETTY_FILE) as f:
saved = f.read()
assert saved == BAD_GETTY
def test_good_needs_scrubs(self):
# Comparing the "good" result without scrubbing the variable parts will fail.
self.make_file("out/gettysburg.txt", GOOD_GETTY)
# compare() raises an assertion.
msg = rf"Files differ: .*{GOLD_PATH_RE} != {OUT_PATH_RE}"
with pytest.raises(AssertionError, match=msg):
compare(gold_path("testing"), "out")
stdout = self.stdout()
assert "- 11/19/1863, Gettysburg, Pennsylvania" in stdout
assert "+ 11/19/9999, Gettysburg, Pennsylvania" in stdout
def test_actual_extra(self):
self.make_file("out/gettysburg.txt", GOOD_GETTY)
self.make_file("out/another.more", "hi")
# Extra files in the output are ok with actual_extra=True.
compare(gold_path("testing"), "out", scrubs=SCRUBS, actual_extra=True)
# But not without it:
msg = r"Files in out only: \['another.more'\]"
with pytest.raises(AssertionError, match=msg):
compare(gold_path("testing"), "out", scrubs=SCRUBS)
self.assert_exists(os.path.join(TESTS_DIR, "actual/testing/another.more"))
# But only the files matching the file_pattern are considered.
compare(gold_path("testing"), "out", file_pattern="*.txt", scrubs=SCRUBS)
|