From 7d2459bb41a5c6c78f367baa6824d50ac9be05ee Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sun, 24 Oct 2021 19:54:40 -0400 Subject: test: finishing covering goldtest.py with test_goldtest.py --- tests/gold/testing/getty/gettysburg.txt | 4 ++ tests/gold/testing/gettysburg.txt | 4 -- tests/gold/testing/xml/output.xml | 6 +++ tests/goldtest.py | 12 ++--- tests/test_goldtest.py | 84 ++++++++++++++++++++++++++------- 5 files changed, 82 insertions(+), 28 deletions(-) create mode 100644 tests/gold/testing/getty/gettysburg.txt delete mode 100644 tests/gold/testing/gettysburg.txt create mode 100644 tests/gold/testing/xml/output.xml diff --git a/tests/gold/testing/getty/gettysburg.txt b/tests/gold/testing/getty/gettysburg.txt new file mode 100644 index 00000000..e759ba87 --- /dev/null +++ b/tests/gold/testing/getty/gettysburg.txt @@ -0,0 +1,4 @@ +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/1863, Gettysburg, Pennsylvania diff --git a/tests/gold/testing/gettysburg.txt b/tests/gold/testing/gettysburg.txt deleted file mode 100644 index e759ba87..00000000 --- a/tests/gold/testing/gettysburg.txt +++ /dev/null @@ -1,4 +0,0 @@ -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/1863, Gettysburg, Pennsylvania 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 @@ + + + + Hello + + 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", """\ + + + + Hello + + + """) + 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") -- cgit v1.2.1