diff --git a/CHANGELOG b/CHANGELOG index 3757ead8d..e94a26593 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -10,6 +10,8 @@ Changes between 2.2.3 and 2.2.4 - fix issue 140: propperly get the real functions of bound classmethods for setup/teardown_class - fix issue #141: switch from the deceased paste.pocoo.org to bpaste.net +- fix issue #143: call unconfigure/sessionfinish always when + configure/sessionstart where called Changes between 2.2.2 and 2.2.3 ---------------------------------------- diff --git a/_pytest/core.py b/_pytest/core.py index 15ce9f4cc..e609f9465 100644 --- a/_pytest/core.py +++ b/_pytest/core.py @@ -465,13 +465,8 @@ def main(args=None, plugins=None): """ returned exit code integer, after an in-process testing run with the given command line arguments, preloading an optional list of passed in plugin objects. """ - try: - config = _prepareconfig(args, plugins) - exitstatus = config.hook.pytest_cmdline_main(config=config) - except UsageError: - e = sys.exc_info()[1] - sys.stderr.write("ERROR: %s\n" %(e.args[0],)) - exitstatus = 3 + config = _prepareconfig(args, plugins) + exitstatus = config.hook.pytest_cmdline_main(config=config) return exitstatus class UsageError(Exception): diff --git a/_pytest/main.py b/_pytest/main.py index 7ac673cf3..b234d0a8e 100644 --- a/_pytest/main.py +++ b/_pytest/main.py @@ -10,6 +10,7 @@ EXIT_OK = 0 EXIT_TESTSFAILED = 1 EXIT_INTERRUPTED = 2 EXIT_INTERNALERROR = 3 +EXIT_USAGEERROR = 4 name_re = py.std.re.compile("^[a-zA-Z_]\w*$") @@ -65,30 +66,34 @@ def wrap_session(config, doit): session.exitstatus = EXIT_OK initstate = 0 try: - config.pluginmanager.do_configure(config) - initstate = 1 - config.hook.pytest_sessionstart(session=session) - initstate = 2 - doit(config, session) - except pytest.UsageError: - raise - except KeyboardInterrupt: - excinfo = py.code.ExceptionInfo() - config.hook.pytest_keyboard_interrupt(excinfo=excinfo) - session.exitstatus = EXIT_INTERRUPTED - except: - excinfo = py.code.ExceptionInfo() - config.pluginmanager.notify_exception(excinfo, config.option) - session.exitstatus = EXIT_INTERNALERROR - if excinfo.errisinstance(SystemExit): - sys.stderr.write("mainloop: caught Spurious SystemExit!\n") - if initstate >= 2: - config.hook.pytest_sessionfinish(session=session, - exitstatus=session.exitstatus or (session._testsfailed and 1)) - if not session.exitstatus and session._testsfailed: - session.exitstatus = EXIT_TESTSFAILED - if initstate >= 1: - config.pluginmanager.do_unconfigure(config) + try: + config.pluginmanager.do_configure(config) + initstate = 1 + config.hook.pytest_sessionstart(session=session) + initstate = 2 + doit(config, session) + except pytest.UsageError: + msg = sys.exc_info()[1].args[0] + sys.stderr.write("ERROR: %s\n" %(msg,)) + session.exitstatus = EXIT_USAGEERROR + except KeyboardInterrupt: + excinfo = py.code.ExceptionInfo() + config.hook.pytest_keyboard_interrupt(excinfo=excinfo) + session.exitstatus = EXIT_INTERRUPTED + except: + excinfo = py.code.ExceptionInfo() + config.pluginmanager.notify_exception(excinfo, config.option) + session.exitstatus = EXIT_INTERNALERROR + if excinfo.errisinstance(SystemExit): + sys.stderr.write("mainloop: caught Spurious SystemExit!\n") + finally: + if initstate >= 2: + config.hook.pytest_sessionfinish(session=session, + exitstatus=session.exitstatus or (session._testsfailed and 1)) + if not session.exitstatus and session._testsfailed: + session.exitstatus = EXIT_TESTSFAILED + if initstate >= 1: + config.pluginmanager.do_unconfigure(config) return session.exitstatus def pytest_cmdline_main(config): diff --git a/doc/announce/release-2.2.4.txt b/doc/announce/release-2.2.4.txt index 79fe8130e..d54d6c805 100644 --- a/doc/announce/release-2.2.4.txt +++ b/doc/announce/release-2.2.4.txt @@ -32,3 +32,6 @@ Changes between 2.2.3 and 2.2.4 - fix issue 140: propperly get the real functions of bound classmethods for setup/teardown_class - fix issue #141: switch from the deceased paste.pocoo.org to bpaste.net +- fix issue #143: call unconfigure/sessionfinish always when + configure/sessionstart where called + diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 27d086f12..867b025fc 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -57,6 +57,22 @@ class TestGeneralUsage: assert result.ret != 0 result.stderr.fnmatch_lines(["ERROR: file not found*asd"]) + def test_file_not_found_unconfigure_issue143(self, testdir): + testdir.makeconftest(""" + def pytest_configure(): + print("---configure") + def pytest_unconfigure(): + print("---unconfigure") + """) + result = testdir.runpytest("-s", "asd") + assert result.ret == 4 # EXIT_USAGEERROR + result.stderr.fnmatch_lines(["ERROR: file not found*asd"]) + s = result.stdout.fnmatch_lines([ + "*---configure", + "*---unconfigure", + ]) + + def test_config_preparse_plugin_option(self, testdir): testdir.makepyfile(pytest_xyz=""" def pytest_addoption(parser): diff --git a/testing/test_terminal.py b/testing/test_terminal.py index deb9d92fe..1c006e62c 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -262,7 +262,7 @@ class TestCollectonly: not to have the items attribute """ result = testdir.runpytest("--collectonly", "uhm_missing_path") - assert result.ret == 3 + assert result.ret == 4 result.stderr.fnmatch_lines([ '*ERROR: file not found*', ])