From: Bob Halley Date: Tue, 16 Jun 2020 00:14:30 +0000 (-0700) Subject: compare files in a way that avoids newlines issues on windows and gives better error... X-Git-Tag: v2.0.0rc1~102 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b29af56b8c7738278ca26fc209c5a78764c6d614;p=thirdparty%2Fdnspython.git compare files in a way that avoids newlines issues on windows and gives better error results --- diff --git a/tests/test_zone.py b/tests/test_zone.py index 4f6b58c3..96a9d1b2 100644 --- a/tests/test_zone.py +++ b/tests/test_zone.py @@ -18,7 +18,6 @@ from io import BytesIO, StringIO import difflib -import filecmp import os import sys import unittest @@ -169,11 +168,17 @@ def make_xfr(zone): add_rdataset(msg, soa_name, soa) return [msg] -def print_differences(a_name, b_name): +def compare_files(test_name, a_name, b_name): with open(a_name, 'r') as a: with open(b_name, 'r') as b: - differ = difflib.Differ() - sys.stdout.writelines(differ.compare(a.readlines(), b.readlines())) + differences = list(difflib.unified_diff(a.readlines(), + b.readlines())) + if len(differences) == 0: + return True + else: + print(f'{test_name} differences:') + sys.stdout.writelines(differences) + return False class ZoneTestCase(unittest.TestCase): @@ -182,11 +187,9 @@ class ZoneTestCase(unittest.TestCase): ok = False try: z.to_file(here('example1.out'), nl=b'\x0a') - ok = filecmp.cmp(here('example1.out'), - here('example1.good')) - if not ok: - print_differences(here('example1.out'), - here('example1.good')) + ok = compare_files('testFromFile1', + here('example1.out'), + here('example1.good')) finally: if not _keep_output: os.unlink(here('example1.out')) @@ -197,8 +200,9 @@ class ZoneTestCase(unittest.TestCase): ok = False try: z.to_file(here('example2.out'), relativize=False, nl=b'\x0a') - ok = filecmp.cmp(here('example2.out'), - here('example2.good')) + ok = compare_files('testFromFile2', + here('example2.out'), + here('example2.good')) finally: if not _keep_output: os.unlink(here('example2.out')) @@ -226,8 +230,9 @@ class ZoneTestCase(unittest.TestCase): f = open(here('example3-textual.out'), 'w') z.to_file(f) f.close() - ok = filecmp.cmp(here('example3-textual.out'), - here('example3.good')) + ok = compare_files('testToFileTextual', + here('example3-textual.out'), + here('example3.good')) finally: if not _keep_output: os.unlink(here('example3-textual.out')) @@ -239,8 +244,9 @@ class ZoneTestCase(unittest.TestCase): f = open(here('example3-binary.out'), 'wb') z.to_file(f) f.close() - ok = filecmp.cmp(here('example3-binary.out'), - here('example3.good')) + ok = compare_files('testToFileBinary', + here('example3-binary.out'), + here('example3.good')) finally: if not _keep_output: os.unlink(here('example3-binary.out')) @@ -250,8 +256,9 @@ class ZoneTestCase(unittest.TestCase): z = dns.zone.from_file(here('example'), 'example') try: z.to_file(here('example3-filename.out')) - ok = filecmp.cmp(here('example3-filename.out'), - here('example3.good')) + ok = compare_files('testToFileFilename', + here('example3-filename.out'), + here('example3.good')) finally: if not _keep_output: os.unlink(here('example3-filename.out')) @@ -265,8 +272,9 @@ class ZoneTestCase(unittest.TestCase): f = open(here('example3.out'), 'w') f.write(text_zone) f.close() - ok = filecmp.cmp(here('example3.out'), - here('example3.good')) + ok = compare_files('testToText', + here('example3.out'), + here('example3.good')) finally: if not _keep_output: os.unlink(here('example3.out'))