Improve test failure diff

Some url lines were missing a url prefix while others had a double url
prefix. diff was reporting more url lines as changed than actually had.
Improve formatting by removing newlines from control lines and adding
headings.

Before:

E   AssertionError: http://localhost:46031/tests/checker/data/sitemap.xml
E   ---
E
E   +++
E
E   @@ -1,4 +1,8 @@
E
E   -url http://localhost:46031/tests/checker/data/sitemap.xml
E   +http://www.example.com/
E   +cache key http://www.example.com/
E   +real url http://www.example.com/
E   +valid
E   +url url http://localhost:46031/tests/checker/data/sitemap.xml
E    cache key http://localhost:46031/tests/checker/data/sitemap.xml
E    real url http://localhost:46031/tests/checker/data/sitemap.xml
E    valid

After:

E   AssertionError: http://localhost:44021/tests/checker/data/sitemap.xml
E   --- expected
E   +++ result
E   @@ -2,3 +2,7 @@
E    cache key http://localhost:44021/tests/checker/data/sitemap.xml
E    real url http://localhost:44021/tests/checker/data/sitemap.xml
E    valid
E   +url http://www.example.com/
E   +cache key http://www.example.com/
E   +real url http://www.example.com/
E   +valid
This commit is contained in:
Chris Mayo 2019-10-29 20:03:08 +00:00
parent c294a4e6c1
commit 2f16152dc8

View file

@ -71,8 +71,16 @@ class TestLogger (linkcheck.logger._Logger):
def normalize(self, result_log):
# XXX we assume that each log entry has a URL key, maybe we should add an assert into log_url() to that effect?
sep = '\nurl '
return sep.join(sorted('\n'.join(result_log).split(sep))).splitlines()
# Ensure that log entries are sorted by URL key:
# - join the result_log items together
# - split into entries (starting with a URL key)
# - sort the entries and join together
# - split the entries back into a list
return '\n'.join(
sorted(['url %s' % x.strip() for x in
re.split(r'^url .*?', '\n'.join(result_log),
flags=re.DOTALL | re.MULTILINE)
if x])).splitlines()
def start_output (self):
"""
@ -130,7 +138,9 @@ class TestLogger (linkcheck.logger._Logger):
"""
self.expected = self.normalize(self.expected)
self.result = self.normalize(self.result)
for line in difflib.unified_diff(self.expected, self.result):
for line in difflib.unified_diff(self.expected, self.result,
fromfile="expected", tofile="result",
lineterm=""):
if not isinstance(line, str_text):
# The ---, +++ and @@ lines from diff format are ascii encoded.
# Make them unicode.