Skip to content

Commit

Permalink
support wrongful stderr polution
Browse files Browse the repository at this point in the history
- suppress 'Unknown interpreted text role' output from some tests
- collect & validate cli error messages
- fixes issue #56
  • Loading branch information
freddrake committed Dec 26, 2018
1 parent b8d52a5 commit 897bb09
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 38 deletions.
20 changes: 20 additions & 0 deletions ZConfig/tests/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ def input_file(fname):
return os.path.abspath(os.path.join(INPUT_DIR, fname))


@contextlib.contextmanager
def _replaced_stream(name, buf=None):
if buf is None:
buf = StringIO()
old_stream = getattr(sys, name)
setattr(sys, name, buf)
try:
yield
finally:
setattr(sys, name, old_stream)


def stderr_replaced(buf=None):
return _replaced_stream('stderr', buf)


def stdout_replaced(buf=None):
return _replaced_stream('stdout', buf)


def with_stdin_from_input_file(fname):
input_fname = input_file(fname)

Expand Down
43 changes: 19 additions & 24 deletions ZConfig/tests/test_schema2html.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
##############################################################################
from __future__ import absolute_import

import contextlib
import sys
import textwrap
import unittest

Expand All @@ -40,28 +38,17 @@
from ZConfig.sphinx import SchemaToRstDirective
from ZConfig.sphinx import RstSchemaFormatter

from .support import input_file
from .support import with_stdin_from_input_file
from ZConfig.tests import support


docutils.parsers.rst.directives.register_directive("zconfig",
SchemaToRstDirective)


@contextlib.contextmanager
def stdout_replaced(buf):
old_stdout = sys.stdout
sys.stdout = buf
try:
yield
finally:
sys.stdout = old_stdout


def run_transform(*args):
if '--out' not in args and '-o' not in args:
buf = StringIO()
with stdout_replaced(buf):
with support.stdout_replaced(buf):
schema2html.main(args)
return buf
return schema2html.main(args) # pragma: no cover
Expand All @@ -70,7 +57,11 @@ def run_transform(*args):
if schema2html.RstSchemaPrinter:
def run_transform_rst(*args):
args += ('--format', 'xml')
return run_transform(*args)
# Capture stderr to suppress junk like
# description:3: (ERROR/3) Unknown interpreted text role "class".
# description:3: (ERROR/3) Unknown interpreted text role "func".
with support.stderr_replaced():
return run_transform(*args)
else:
def run_transform_rst(*args):
pass
Expand All @@ -79,11 +70,11 @@ def run_transform_rst(*args):
class TestSchema2HTML(unittest.TestCase):

def test_schema_only(self):
res = run_transform(input_file('simple.xml'))
res = run_transform(support.input_file('simple.xml'))
self.assertIn('</html>', res.getvalue())
run_transform_rst(input_file('simple.xml'))
run_transform_rst(support.input_file('simple.xml'))

@with_stdin_from_input_file('simple.xml')
@support.with_stdin_from_input_file('simple.xml')
def test_schema_only_redirect(self):
res = run_transform("-")
self.assertIn('</html>', res.getvalue())
Expand All @@ -96,20 +87,20 @@ def test_cover_all_schemas(self):
'base.xml',
'library.xml',
'simplesections.xml',):
res = run_transform(input_file(name))
res = run_transform(support.input_file(name))
self.assertIn('</html>', res.getvalue())
run_transform_rst(input_file(name))
run_transform_rst(support.input_file(name))

def test_html_section_example(self):
name = 'simplesections.xml'
res = run_transform(input_file(name))
res = run_transform(support.input_file(name))
out = res.getvalue()
self.assertIn('Section Example', out)
self.assertIn('Multisection Example', out)

def test_rst_section_example(self):
name = 'simplesections.xml'
res = run_transform_rst(input_file(name))
res = run_transform_rst(support.input_file(name))
out = res.getvalue()
self.assertIn('Section Example', out)
self.assertIn('Multisection Example', out)
Expand All @@ -131,7 +122,11 @@ def _parse(self, text):

parser = docutils.parsers.rst.Parser()
text = textwrap.dedent(text)
parser.parse(text, document)
with support.stderr_replaced():
# Capture stderr to suppress junk like
# description:3: (ERROR/3) Unknown interpreted text role "class".
# description:3: (ERROR/3) Unknown interpreted text role "func".
parser.parse(text, document)
return document

def test_parse_package(self):
Expand Down
38 changes: 24 additions & 14 deletions ZConfig/tests/test_validator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
##############################################################################
#
# Copyright (c) 2017 Zope Foundation and Contributors.
# Copyright (c) 2017, 2018 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
Expand All @@ -16,9 +16,8 @@
import unittest

from ZConfig import validator

from .support import input_file
from .support import with_stdin_from_input_file
from ZConfig._compat import NStringIO as StringIO
from ZConfig.tests import support


def run_validator(*args):
Expand All @@ -28,28 +27,39 @@ def run_validator(*args):
class TestValidator(unittest.TestCase):

def test_no_schema(self):
self.assertRaises(SystemExit,
run_validator)
sio = StringIO()
with support.stderr_replaced(sio):
with self.assertRaises(SystemExit) as cm:
run_validator()
self.assertEqual(cm.exception.code, 2)
err = sio.getvalue()
# Checked separately since these are included very differently
# with different versions of Python's argparse module.
self.assertIn('-s/--schema', err)
self.assertIn(' required', err)

def test_schema_only(self):
res = run_validator("--schema", input_file('simple.xml'))
res = run_validator("--schema", support.input_file('simple.xml'))
self.assertEqual(res, 0)

@with_stdin_from_input_file('simple.conf')
@support.with_stdin_from_input_file('simple.conf')
def test_schema_only_redirect(self):
res = run_validator("--schema", input_file('simple.xml'))
res = run_validator("--schema", support.input_file('simple.xml'))
self.assertEqual(res, 0)

def test_good_config(self):
res = run_validator("--schema", input_file('simple.xml'),
input_file('simple.conf'),
input_file('simple.conf'))
res = run_validator("--schema", support.input_file('simple.xml'),
support.input_file('simple.conf'),
support.input_file('simple.conf'))
self.assertEqual(res, 0)

def test_bad_config(self):
res = run_validator("--schema", input_file("simple.xml"),
input_file("outer.conf"))
sio = StringIO()
with support.stderr_replaced(sio):
res = run_validator("--schema", support.input_file("simple.xml"),
support.input_file("outer.conf"))
self.assertEqual(res, 1)
self.assertIn("'refouter' is not a known key name", sio.getvalue())


def test_suite():
Expand Down

0 comments on commit 897bb09

Please sign in to comment.