summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2021-10-24 19:54:40 -0400
committerNed Batchelder <ned@nedbatchelder.com>2021-10-24 21:03:12 -0400
commit7d2459bb41a5c6c78f367baa6824d50ac9be05ee (patch)
treece0e3da10b854be772e28f9e941c8a24fa4a4230
parent7f202c43e24570476a91b0ab3b1b02aba2b05d3d (diff)
downloadpython-coveragepy-git-7d2459bb41a5c6c78f367baa6824d50ac9be05ee.tar.gz
test: finishing covering goldtest.py with test_goldtest.py
-rw-r--r--tests/gold/testing/getty/gettysburg.txt (renamed from tests/gold/testing/gettysburg.txt)0
-rw-r--r--tests/gold/testing/xml/output.xml6
-rw-r--r--tests/goldtest.py12
-rw-r--r--tests/test_goldtest.py84
4 files changed, 78 insertions, 24 deletions
diff --git a/tests/gold/testing/gettysburg.txt b/tests/gold/testing/getty/gettysburg.txt
index e759ba87..e759ba87 100644
--- a/tests/gold/testing/gettysburg.txt
+++ b/tests/gold/testing/getty/gettysburg.txt
diff --git a/tests/gold/testing/xml/output.xml b/tests/gold/testing/xml/output.xml
new file mode 100644
index 00000000..287251b2
--- /dev/null
+++ b/tests/gold/testing/xml/output.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" ?>
+<the_root a="1" b="2" c="3">
+ <also z="9" y="8" x="7">
+ Hello
+ </also>
+</the_root>
diff --git a/tests/goldtest.py b/tests/goldtest.py
index b2754219..bb88b1e4 100644
--- a/tests/goldtest.py
+++ b/tests/goldtest.py
@@ -118,7 +118,9 @@ def contains_rx(filename, *rxlist):
with open(filename) as fobj:
lines = fobj.readlines()
for rx in rxlist:
- assert any(re.search(rx, line) for line in lines), f"Missing rx in {filename}: {rx!r}"
+ assert any(re.search(rx, line) for line in lines), (
+ f"Missing regex in {filename}: r{rx!r}"
+ )
def contains_any(filename, *strlist):
@@ -135,9 +137,7 @@ def contains_any(filename, *strlist):
if s in text:
return
- assert False, ( # pragma: only failure
- f"Missing content in {filename}: {strlist[0]!r} [1 of {len(strlist)}]"
- )
+ assert False, f"Missing content in {filename}: {strlist[0]!r} [1 of {len(strlist)}]"
def doesnt_contain(filename, *strlist):
@@ -180,6 +180,6 @@ def scrub(strdata, scrubs):
`scrubs` is a list of (find, replace) pairs of regexes that are used on
`strdata`. A string is returned.
"""
- for rgx_find, rgx_replace in scrubs:
- strdata = re.sub(rgx_find, rgx_replace, strdata)
+ for rx_find, rx_replace in scrubs:
+ strdata = re.sub(rx_find, rx_replace, strdata)
return strdata
diff --git a/tests/test_goldtest.py b/tests/test_goldtest.py
index 4c2ffa99..5c0d6984 100644
--- a/tests/test_goldtest.py
+++ b/tests/test_goldtest.py
@@ -10,7 +10,8 @@ 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
+from tests.goldtest import contains, contains_any, contains_rx, doesnt_contain
+from tests.helpers import re_line, remove_tree
GOOD_GETTY = """\
Four score and seven years ago our fathers brought forth upon this continent, a
@@ -23,19 +24,26 @@ 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
+333:4444:55555, Gettysburg, Pennsylvania
"""
SCRUBS = [
# Numbers don't matter when comparing.
(r'\d+', 'D'),
+ (r'[/:]', '-'),
]
-ACTUAL_DIR = os.path.join(TESTS_DIR, "actual/testing")
+def path_regex(path):
+ """Convert a file path into a regex that will match that path on any OS."""
+ return re.sub(r"[/\\]", r"[/\\\\]", path.replace(".", "[.]"))
+
+ACTUAL_DIR = os.path.join(TESTS_DIR, "actual/testing/getty")
ACTUAL_GETTY_FILE = os.path.join(ACTUAL_DIR, "gettysburg.txt")
+GOLD_GETTY_FILE = os.path.join(TESTS_DIR, "gold/testing/getty/gettysburg.txt")
+GOLD_GETTY_FILE_RX = path_regex(GOLD_GETTY_FILE)
-GOLD_PATH_RE = re.escape(os_sep("/tests/gold/testing/gettysburg.txt"))
-OUT_PATH_RE = re.escape(os_sep("out/gettysburg.txt"))
+GOLD_PATH_RX = path_regex("/tests/gold/testing/getty/gettysburg.txt")
+OUT_PATH_RX = path_regex("out/gettysburg.txt")
class CompareTest(CoverageTest):
"""Tests of goldtest.py:compare()"""
@@ -46,25 +54,24 @@ class CompareTest(CoverageTest):
def test_good(self):
self.make_file("out/gettysburg.txt", GOOD_GETTY)
- compare(gold_path("testing"), "out", scrubs=SCRUBS)
+ compare(gold_path("testing/getty"), "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}"
+ msg = rf"Files differ: .*{GOLD_PATH_RX} != {OUT_PATH_RX}"
with pytest.raises(AssertionError, match=msg):
- compare(gold_path("testing"), "out", scrubs=SCRUBS)
+ compare(gold_path("testing/getty"), "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
+ assert re_line(stdout, rf"^:::: diff '.*{GOLD_PATH_RX}' and '{OUT_PATH_RX}'")
+ assert re_line(stdout, rf"^:::: end diff '.*{GOLD_PATH_RX}' and '{OUT_PATH_RX}'")
+ assert " D-D-D, Gettysburg, Pennsylvania" in stdout
# The actual file was saved.
with open(ACTUAL_GETTY_FILE) as f:
@@ -76,9 +83,9 @@ class CompareTest(CoverageTest):
self.make_file("out/gettysburg.txt", GOOD_GETTY)
# compare() raises an assertion.
- msg = rf"Files differ: .*{GOLD_PATH_RE} != {OUT_PATH_RE}"
+ msg = rf"Files differ: .*{GOLD_PATH_RX} != {OUT_PATH_RX}"
with pytest.raises(AssertionError, match=msg):
- compare(gold_path("testing"), "out")
+ compare(gold_path("testing/getty"), "out")
stdout = self.stdout()
assert "- 11/19/1863, Gettysburg, Pennsylvania" in stdout
@@ -89,13 +96,54 @@ class CompareTest(CoverageTest):
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)
+ compare(gold_path("testing/getty"), "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"))
+ compare(gold_path("testing/getty"), "out", scrubs=SCRUBS)
+ self.assert_exists(os.path.join(TESTS_DIR, "actual/testing/getty/another.more"))
# But only the files matching the file_pattern are considered.
- compare(gold_path("testing"), "out", file_pattern="*.txt", scrubs=SCRUBS)
+ compare(gold_path("testing/getty"), "out", file_pattern="*.txt", scrubs=SCRUBS)
+
+ def test_xml(self):
+ self.make_file("out/output.xml", """\
+ <?xml version="1.0" ?>
+ <the_root c="3" b="2" a="1">
+ <also z="9" x="7" y="8">
+ Hello
+ </also>
+ </the_root>
+ """)
+ compare(gold_path("testing/xml"), "out")
+
+
+class ContainsTest(CoverageTest):
+ """Tests of the various "contains" functions in goldtest.py"""
+
+ run_in_temp_dir = False
+
+ def test_contains(self):
+ contains(GOLD_GETTY_FILE, "Four", "fathers", "dedicated")
+ msg = rf"Missing content in {GOLD_GETTY_FILE_RX}: 'xyzzy'"
+ with pytest.raises(AssertionError, match=msg):
+ contains(GOLD_GETTY_FILE, "Four", "fathers", "xyzzy", "dedicated")
+
+ def test_contains_rx(self):
+ contains_rx(GOLD_GETTY_FILE, r"Fo.r", r"f[abc]thers", "dedi[cdef]ated")
+ msg = rf"Missing regex in {GOLD_GETTY_FILE_RX}: r'm\[opq\]thers'"
+ with pytest.raises(AssertionError, match=msg):
+ contains_rx(GOLD_GETTY_FILE, r"Fo.r", r"m[opq]thers")
+
+ def test_contains_any(self):
+ contains_any(GOLD_GETTY_FILE, "Five", "Four", "Three")
+ msg = rf"Missing content in {GOLD_GETTY_FILE_RX}: 'One' \[1 of 3\]"
+ with pytest.raises(AssertionError, match=msg):
+ contains_any(GOLD_GETTY_FILE, "One", "Two", "Three")
+
+ def test_doesnt_contain(self):
+ doesnt_contain(GOLD_GETTY_FILE, "One", "Two", "Three")
+ msg = rf"Forbidden content in {GOLD_GETTY_FILE_RX}: 'Four'"
+ with pytest.raises(AssertionError, match=msg):
+ doesnt_contain(GOLD_GETTY_FILE, "Three", "Four", "Five")