diff --git a/AUTHORS b/AUTHORS index 9f6ee048d..e11400c1f 100644 --- a/AUTHORS +++ b/AUTHORS @@ -267,5 +267,6 @@ Wouter van Ackooy Xixi Zhao Xuan Luong Xuecong Liao +Yoav Caspi Zac Hatfield-Dodds Zoltán Máté diff --git a/changelog/5906.bugfix.rst b/changelog/5906.bugfix.rst new file mode 100644 index 000000000..f5ded6cd2 --- /dev/null +++ b/changelog/5906.bugfix.rst @@ -0,0 +1 @@ +Fix crash with ``KeyboardInterrupt`` during ``--setup-show``. diff --git a/doc/en/example/parametrize.rst b/doc/en/example/parametrize.rst index 0b94e425c..1220cfb4d 100644 --- a/doc/en/example/parametrize.rst +++ b/doc/en/example/parametrize.rst @@ -678,4 +678,4 @@ Or, if desired, you can ``pip install contextlib2`` and use: .. code-block:: python - from contextlib2 import ExitStack as does_not_raise + from contextlib2 import nullcontext as does_not_raise diff --git a/src/_pytest/cacheprovider.py b/src/_pytest/cacheprovider.py index 496931e0f..7a5deaa39 100755 --- a/src/_pytest/cacheprovider.py +++ b/src/_pytest/cacheprovider.py @@ -135,7 +135,7 @@ class Cache: readme_path.write_text(README_CONTENT) gitignore_path = self._cachedir.joinpath(".gitignore") - msg = "# Created by pytest automatically.\n*" + msg = "# Created by pytest automatically.\n*\n" gitignore_path.write_text(msg, encoding="UTF-8") cachedir_tag_path = self._cachedir.joinpath("CACHEDIR.TAG") diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index cd23281fa..ea709a26a 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -195,7 +195,6 @@ def get_plugin_manager(): def _prepareconfig(args=None, plugins=None): - warning = None if args is None: args = sys.argv[1:] elif isinstance(args, py.path.local): @@ -213,10 +212,6 @@ def _prepareconfig(args=None, plugins=None): pluginmanager.consider_pluginarg(plugin) else: pluginmanager.register(plugin) - if warning: - from _pytest.warnings import _issue_warning_captured - - _issue_warning_captured(warning, hook=config.hook, stacklevel=4) return pluginmanager.hook.pytest_cmdline_parse( pluginmanager=pluginmanager, args=args ) diff --git a/src/_pytest/setuponly.py b/src/_pytest/setuponly.py index 70d6ed12f..639349748 100644 --- a/src/_pytest/setuponly.py +++ b/src/_pytest/setuponly.py @@ -1,5 +1,3 @@ -import sys - import pytest @@ -51,7 +49,6 @@ def _show_fixture_action(fixturedef, msg): capman = config.pluginmanager.getplugin("capturemanager") if capman: capman.suspend_global_capture() - out, err = capman.read_global_capture() tw = config.get_terminal_writer() tw.line() @@ -74,8 +71,6 @@ def _show_fixture_action(fixturedef, msg): if capman: capman.resume_global_capture() - sys.stdout.write(out) - sys.stderr.write(err) @pytest.hookimpl(tryfirst=True) diff --git a/testing/test_cacheprovider.py b/testing/test_cacheprovider.py index e2fd5a4ca..fc788e304 100644 --- a/testing/test_cacheprovider.py +++ b/testing/test_cacheprovider.py @@ -1029,7 +1029,7 @@ def test_gitignore(testdir): config = testdir.parseconfig() cache = Cache.for_config(config) cache.set("foo", "bar") - msg = "# Created by pytest automatically.\n*" + msg = "# Created by pytest automatically.\n*\n" gitignore_path = cache._cachedir.joinpath(".gitignore") assert gitignore_path.read_text(encoding="UTF-8") == msg diff --git a/testing/test_setuponly.py b/testing/test_setuponly.py index 6343991ae..7549874db 100644 --- a/testing/test_setuponly.py +++ b/testing/test_setuponly.py @@ -1,4 +1,5 @@ import pytest +from _pytest.main import ExitCode @pytest.fixture(params=["--setup-only", "--setup-plan", "--setup-show"], scope="module") @@ -267,3 +268,27 @@ def test_show_fixtures_and_execute_test(testdir): result.stdout.fnmatch_lines( ["*SETUP F arg*", "*test_arg (fixtures used: arg)F*", "*TEARDOWN F arg*"] ) + + +def test_setup_show_with_KeyboardInterrupt_in_test(testdir): + p = testdir.makepyfile( + """ + import pytest + @pytest.fixture + def arg(): + pass + def test_arg(arg): + raise KeyboardInterrupt() + """ + ) + result = testdir.runpytest("--setup-show", p, no_reraise_ctrlc=True) + result.stdout.fnmatch_lines( + [ + "*SETUP F arg*", + "*test_arg (fixtures used: arg)*", + "*TEARDOWN F arg*", + "*! KeyboardInterrupt !*", + "*= no tests ran in *", + ] + ) + assert result.ret == ExitCode.INTERRUPTED