diff options
author | Jon Turney <jon.turney@dronecode.org.uk> | 2017-11-16 03:47:30 +0300 |
---|---|---|
committer | Jon Turney <jon.turney@dronecode.org.uk> | 2017-11-17 14:39:32 +0300 |
commit | b666c2dbe4157293bef99a4abdcb2d92699e7bd8 (patch) | |
tree | d2fb648ab76f6abe77b472d4f5a3d272453aeffd | |
parent | 6f19fdfaafa959b26c21b708f0d47741668c682f (diff) |
Fix representation of OrderedDict in test data with python 3.6
pprint was fixed by [1] from printing an OrderedDict as a dict, with the
lines in order, to printing the repr of the OrderedDict.
Monkey-patch pprint on python 3.6 to get the python 3.4 behaviour. Probably
better to do that the other way around, but that means changing the test
data...
[1] https://bugs.python.org/issue23775
-rwxr-xr-x | test/test_calm.py | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/test/test_calm.py b/test/test_calm.py index 0f30e85..4d32370 100755 --- a/test/test_calm.py +++ b/test/test_calm.py @@ -25,6 +25,8 @@ # tests # +import collections +import contextlib import filecmp import logging import os @@ -87,6 +89,35 @@ def capture_dirtree(basedir): # +# a context to monkey-patch pprint so OrderedDict appears as with python <3.5 +# (a dict, with lines ordered, rather than OrderedDict repr) +# + +def patched_pprint_ordered_dict(self, object, stream, indent, allowance, context, level): + write = stream.write + write('{') + if self._indent_per_level > 1: + write((self._indent_per_level - 1) * ' ') + length = len(object) + if length: + items = list(object.items()) + self._format_dict_items(items, stream, indent, allowance + 1, + context, level) + write('}') + +@contextlib.contextmanager +def pprint_patch(): + if isinstance(getattr(pprint.PrettyPrinter, '_dispatch', None), dict): + orig = pprint.PrettyPrinter._dispatch[collections.OrderedDict.__repr__] + pprint.PrettyPrinter._dispatch[collections.OrderedDict.__repr__] = patched_pprint_ordered_dict + try: + yield + finally: + pprint.PrettyPrinter._dispatch[collections.OrderedDict.__repr__] = orig + else: + yield + +# # # @@ -101,7 +132,8 @@ class CalmTest(unittest.TestCase): with self.subTest(package=os.path.basename(dirpath)): logging.info('Reading %s' % os.path.join(dirpath, 'setup.hint')) results = hint.hint_file_parse(os.path.join(dirpath, 'setup.hint'), hint.setup) - compare_with_expected_file(self, os.path.join('testdata/hints', relpath), results) + with pprint_patch(): + compare_with_expected_file(self, os.path.join('testdata/hints', relpath), results) # # something like "find -name results -exec sh -c 'cd `dirname {}` ; cp results @@ -282,7 +314,8 @@ class CalmTest(unittest.TestCase): self.assertCountEqual(scan_result.to_vault, {'x86/release/testpackage': ['x86/release/testpackage/testpackage-0.1-1.tar.bz2']}) self.assertCountEqual(scan_result.remove_always, [f for (f, t) in ready_fns]) self.assertEqual(scan_result.remove_success, ['testdata/homes/Blooey McFooey/x86/release/testpackage/-testpackage-0.1-1-src.tar.bz2', 'testdata/homes/Blooey McFooey/x86/release/testpackage/-testpackage-0.1-1.tar.bz2']) - compare_with_expected_file(self, 'testdata/uploads', scan_result.packages, 'pkglist') + with pprint_patch(): + compare_with_expected_file(self, 'testdata/uploads', scan_result.packages, 'pkglist') def test_package_set(self): self.maxDiff = None |