From 657c53b0f571fedcfa4cb347de7573a6dcfd1963 Mon Sep 17 00:00:00 2001 From: Robert Lehmann Date: Fri, 20 Mar 2015 22:26:32 +0100 Subject: [PATCH] Fixes #1785: Reduce output for test generators. --- tests/path.py | 21 +++++++++++++++++++-- tests/test_intl.py | 19 +++++++++++-------- tests/util.py | 2 +- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/tests/path.py b/tests/path.py index 2f0cdc062..0c4823005 100755 --- a/tests/path.py +++ b/tests/path.py @@ -12,7 +12,7 @@ import sys import shutil from codecs import open -from six import PY2, text_type +from six import PY2, text_type, binary_type FILESYSTEMENCODING = sys.getfilesystemencoding() or sys.getdefaultencoding() @@ -142,9 +142,11 @@ class path(text_type): """ f = open(self, mode='U', **kwargs) try: - return f.read() + text = f.read() finally: f.close() + contents = repr_as(text, '<%s contents>' % self.basename()) + return contents def bytes(self): """ @@ -205,3 +207,18 @@ class path(text_type): def __repr__(self): return '%s(%s)' % (self.__class__.__name__, text_type.__repr__(self)) + + +# Lives here only to avoid circular references; use it from util.py! +class _repr_text(text_type): + def __repr__(self): + return self._repr +class _repr_bin(binary_type): + def __repr__(self): + return self._repr + +def repr_as(string, repr_): + wrapper = _repr_text if isinstance(string, text_type) else _repr_bin + proxy = wrapper(string) + proxy._repr = repr_ + return proxy diff --git a/tests/test_intl.py b/tests/test_intl.py index 5c6be546a..719829174 100644 --- a/tests/test_intl.py +++ b/tests/test_intl.py @@ -21,7 +21,7 @@ from six import string_types from util import tempdir, rootdir, path, gen_with_app, SkipTest, \ assert_re_search, assert_not_re_search, assert_in, assert_not_in, \ - assert_startswith + assert_startswith, repr_as root = tempdir / 'test-intl' @@ -112,7 +112,7 @@ def test_text_builder(app, status, warning): # --- warnings in translation - warnings = warning.getvalue().replace(os.sep, '/') + warnings = getwarning(warning) warning_expr = u'.*/warnings.txt:4: ' \ u'WARNING: Inline literal start-string without end-string.\n' yield assert_re_search, warning_expr, warnings @@ -149,7 +149,7 @@ def test_text_builder(app, status, warning): u"\n[100] THIS IS A NUMBERED FOOTNOTE.\n") yield assert_equal, result, expect - warnings = warning.getvalue().replace(os.sep, '/') + warnings = getwarning(warning) warning_fmt = u'.*/refs_inconsistency.txt:\\d+: ' \ u'WARNING: inconsistent %s in translated message\n' expected_warning_expr = ( @@ -170,7 +170,7 @@ def test_text_builder(app, status, warning): u"\n""" yield assert_count(expected_expr, result, 1) + +def getwarning(warnings): + return repr_as(warnings.getvalue().replace(os.sep, '/'), '') diff --git a/tests/util.py b/tests/util.py index f7d8411c7..ebd981584 100644 --- a/tests/util.py +++ b/tests/util.py @@ -23,7 +23,7 @@ from sphinx.theming import Theme from sphinx.ext.autodoc import AutoDirective from sphinx.pycode import ModuleAnalyzer -from path import path +from path import path, repr_as try: # Python >=3.3