URL: https://github.com/freeipa/freeipa/pull/120
Author: stlaz
 Title: #120: Pretty-print structures in assert_deepequal
Action: opened

PR body:
"""
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
"""

To pull the PR as Git branch:
git remote add ghfreeipa https://github.com/freeipa/freeipa
git fetch ghfreeipa pull/120/head:pr120
git checkout pr120
From 3f0e300a8572aa99d9258f57b40cf116abecdaff Mon Sep 17 00:00:00 2001
From: Stanislav Laznicka <slazn...@redhat.com>
Date: Thu, 22 Sep 2016 08:12:45 +0200
Subject: [PATCH] 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
---
 ipatests/pytest.ini                          |  1 +
 ipatests/pytest_plugins/additional_config.py |  8 +++++++
 ipatests/util.py                             | 35 ++++++++++++++++++++++------
 3 files changed, 37 insertions(+), 7 deletions(-)
 create mode 100644 ipatests/pytest_plugins/additional_config.py

diff --git a/ipatests/pytest.ini b/ipatests/pytest.ini
index 233cf43..5b89942 100644
--- a/ipatests/pytest.ini
+++ b/ipatests/pytest.ini
@@ -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
diff --git a/ipatests/pytest_plugins/additional_config.py b/ipatests/pytest_plugins/additional_config.py
new file mode 100644
index 0000000..06ae970
--- /dev/null
+++ b/ipatests/pytest_plugins/additional_config.py
@@ -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")
diff --git a/ipatests/util.py b/ipatests/util.py
index 8878993..c81249f 100644
--- a/ipatests/util.py
+++ b/ipatests/util.py
@@ -28,7 +28,9 @@
 import shutil
 import re
 import uuid
+import pytest
 from contextlib import contextmanager
+from pprint import pformat
 
 import six
 import ldap
@@ -273,18 +275,29 @@ def __ne__(self, other):
   %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]
-- 
Manage your subscription for the Freeipa-devel mailing list:
https://www.redhat.com/mailman/listinfo/freeipa-devel
Contribute to FreeIPA: http://www.freeipa.org/page/Contribute/Code

Reply via email to