Pretty-print structures in assert_deepequal

By default, ipa-run-tests will now pretty-print structures
compared in the assert_deepequal function. This behaviour
can be turned off by the --no-pretty-print option.

https://fedorahosted.org/freeipa/ticket/6212

Reviewed-By: Martin Basti <mbasti@redhat.com>
Reviewed-By: Petr Spacek <pspacek@redhat.com>
This commit is contained in:
Stanislav Laznicka
2016-09-22 08:12:45 +02:00
committed by Martin Babinsky
parent 5ac1f36713
commit d982710bec
3 changed files with 37 additions and 7 deletions

View File

@@ -12,6 +12,7 @@ addopts = --doctest-modules
-p ipatests.pytest_plugins.declarative
-p ipatests.pytest_plugins.integration
-p ipatests.pytest_plugins.beakerlib
-p ipatests.pytest_plugins.additional_config
# Ignore files for doc tests.
# TODO: ideally, these should all use __name__=='__main__' guards
--ignore=setup.py

View File

@@ -0,0 +1,8 @@
#
# Copyright (C) 2016 FreeIPA Contributors see COPYING for license
#
def pytest_addoption(parser):
parser.addoption("--no-pretty-print", action="store_false",
dest="pretty_print", help="Don't pretty-print structures")

View File

@@ -28,7 +28,9 @@ import tempfile
import shutil
import re
import uuid
import pytest
from contextlib import contextmanager
from pprint import pformat
import six
import ldap
@@ -273,18 +275,29 @@ LEN = """assert_deepequal: list length mismatch.
%s
len(expected) = %r
len(got) = %r
expected = %r
got = %r
expected = %s
got = %s
path = %r"""
KEYS = """assert_deepequal: dict keys mismatch.
%s
missing keys = %r
extra keys = %r
expected = %r
got = %r
expected = %s
got = %s
path = %r"""
EXPECTED_LEN = len(' expected = ')
GOT_LEN = len(' got = ')
def struct_to_string(struct, indent=1):
"""
Function to pretty-format a structure and optionally indent its lines
so they match the visual indention of the first line
"""
return pformat(struct).replace('\n', '\n' + ' ' * indent)
def assert_deepequal(expected, got, doc='', stack=tuple()):
"""
@@ -315,6 +328,13 @@ def assert_deepequal(expected, got, doc='', stack=tuple()):
Note that lists and tuples are considered equivalent, and the order of
their elements does not matter.
"""
if pytest.config.getoption("pretty_print"): # pylint: disable=no-member
expected_str = struct_to_string(expected, EXPECTED_LEN)
got_str = struct_to_string(got, GOT_LEN)
else:
expected_str = repr(expected)
got_str = repr(got)
if isinstance(expected, tuple):
expected = list(expected)
if isinstance(got, tuple):
@@ -329,7 +349,8 @@ def assert_deepequal(expected, got, doc='', stack=tuple()):
if isinstance(expected, (list, tuple)):
if len(expected) != len(got):
raise AssertionError(
LEN % (doc, len(expected), len(got), expected, got, stack)
LEN % (doc, len(expected), len(got), expected_str, got_str,
stack)
)
# Sort list elements, unless they are dictionaries
if expected and isinstance(expected[0], dict):
@@ -352,8 +373,8 @@ def assert_deepequal(expected, got, doc='', stack=tuple()):
extra = set(got).difference(expected)
if missing or extra:
raise AssertionError(KEYS % (
doc, sorted(missing), sorted(extra), expected, got, stack
)
doc, sorted(missing), sorted(extra), expected_str, got_str,
stack)
)
for key in sorted(expected):
e_sub = expected[key]