From bbdebac87a37385e24b2856d9556b12905bc2a38 Mon Sep 17 00:00:00 2001 From: hpk Date: Tue, 17 Mar 2009 08:03:49 +0100 Subject: [PATCH] [svn r62982] report basic configuration errors more gracefully to the user --HG-- branch : trunk --- py/test/cmdline.py | 16 ++++++++++------ py/test/config.py | 7 ++++++- py/test/testing/acceptance_test.py | 14 +++++++++++++- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/py/test/cmdline.py b/py/test/cmdline.py index a48bce0bb..4bebbdc3e 100644 --- a/py/test/cmdline.py +++ b/py/test/cmdline.py @@ -9,12 +9,16 @@ def main(args=None): if args is None: args = py.std.sys.argv[1:] config = py.test.config - config.parse(args) - config.pytestplugins.do_configure(config) - session = config.initsession() - exitstatus = session.main() - config.pytestplugins.do_unconfigure(config) - raise SystemExit(exitstatus) + try: + config.parse(args) + config.pytestplugins.do_configure(config) + session = config.initsession() + exitstatus = session.main() + config.pytestplugins.do_unconfigure(config) + raise SystemExit(exitstatus) + except config.Error, e: + py.std.sys.stderr.write("config ERROR: %s\n" %(e.args[0],)) + raise SystemExit(3) def warn_about_missing_assertion(): try: diff --git a/py/test/config.py b/py/test/config.py index 709482591..4718a24b4 100644 --- a/py/test/config.py +++ b/py/test/config.py @@ -22,9 +22,13 @@ class CmdOptions(object): def __repr__(self): return "" %(self.__dict__,) +class Error(Exception): + """ Test Configuration Error. """ + class Config(object): """ central bus for dealing with configuration/initialization data. """ Option = py.compat.optparse.Option # deprecated + Error = Error _sessionclass = None def __init__(self, pytestplugins=None, topdir=None): @@ -124,7 +128,8 @@ class Config(object): def getfsnode(self, path): path = py.path.local(path) - assert path.check(), "%s: path does not exist" %(path,) + if not path.check(): + raise self.Error("file not found: %s" %(path,)) # we want our possibly custom collection tree to start at pkgroot pkgpath = path.pypkgpath() if pkgpath is None: diff --git a/py/test/testing/acceptance_test.py b/py/test/testing/acceptance_test.py index fbb7d61ff..7b5f9018d 100644 --- a/py/test/testing/acceptance_test.py +++ b/py/test/testing/acceptance_test.py @@ -5,6 +5,18 @@ pytestpath = pydir.join("bin", "py.test") EXPECTTIMEOUT=10.0 class TestPyTest: + def test_config_error(self, testdir): + testdir.makeconftest(""" + class ConftestPlugin: + def pytest_configure(self, config): + raise config.Error("hello") + """) + result = testdir.runpytest(testdir.tmpdir) + assert result.ret != 0 + assert result.stderr.fnmatch_lines([ + 'config ERROR: hello' + ]) + def test_assertion_magic(self, testdir): p = testdir.makepyfile(""" def test_this(): @@ -17,7 +29,7 @@ class TestPyTest: "E assert 0", ]) assert result.ret == 1 - + def test_collectonly_simple(self, testdir): p = testdir.makepyfile(""" def test_func1():