From 0be9c399abbbc477c78d934d34165783824d7243 Mon Sep 17 00:00:00 2001 From: Anita Hammer <166057949+anitahammer@users.noreply.github.com> Date: Sat, 6 Apr 2024 21:58:40 +0100 Subject: [PATCH 01/10] Update runner.py --- src/_pytest/runner.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_pytest/runner.py b/src/_pytest/runner.py index 3f706b927c8..f02e4dcd9a2 100644 --- a/src/_pytest/runner.py +++ b/src/_pytest/runner.py @@ -388,7 +388,7 @@ def collect() -> List[Union[Item, Collector]]: return list(collector.collect()) - call = CallInfo.from_call(collect, "collect") + call = CallInfo.from_call(collect, "collect", reraise=(BaseException, SystemExit)) longrepr: Union[None, Tuple[str, int, str], str, TerminalRepr] = None if not call.excinfo: outcome: Literal["passed", "skipped", "failed"] = "passed" From 4a1f432d44ba308728d4081a9fc856261fb7c4e1 Mon Sep 17 00:00:00 2001 From: Anita Hammer <166057949+anitahammer@users.noreply.github.com> Date: Sat, 6 Apr 2024 22:21:46 +0100 Subject: [PATCH 02/10] Update runner.py --- src/_pytest/runner.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_pytest/runner.py b/src/_pytest/runner.py index f02e4dcd9a2..d6014c6599a 100644 --- a/src/_pytest/runner.py +++ b/src/_pytest/runner.py @@ -388,7 +388,7 @@ def collect() -> List[Union[Item, Collector]]: return list(collector.collect()) - call = CallInfo.from_call(collect, "collect", reraise=(BaseException, SystemExit)) + call = CallInfo.from_call(collect, "collect", reraise=(KeyboardInterrupt, SystemExit)) longrepr: Union[None, Tuple[str, int, str], str, TerminalRepr] = None if not call.excinfo: outcome: Literal["passed", "skipped", "failed"] = "passed" From 95fa59f78256a8e0144e0af80beded85bb0b14bd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 6 Apr 2024 21:22:03 +0000 Subject: [PATCH 03/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/_pytest/runner.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/_pytest/runner.py b/src/_pytest/runner.py index d6014c6599a..defa2a8e5a9 100644 --- a/src/_pytest/runner.py +++ b/src/_pytest/runner.py @@ -388,7 +388,9 @@ def collect() -> List[Union[Item, Collector]]: return list(collector.collect()) - call = CallInfo.from_call(collect, "collect", reraise=(KeyboardInterrupt, SystemExit)) + call = CallInfo.from_call( + collect, "collect", reraise=(KeyboardInterrupt, SystemExit) + ) longrepr: Union[None, Tuple[str, int, str], str, TerminalRepr] = None if not call.excinfo: outcome: Literal["passed", "skipped", "failed"] = "passed" From c0ffee6086620733750b0c6bbeeb7c2b63edfc63 Mon Sep 17 00:00:00 2001 From: Anita Hammer <166057949+anitahammer@users.noreply.github.com> Date: Mon, 8 Apr 2024 01:24:54 +0100 Subject: [PATCH 04/10] Add test Weather: overcast clouds --- testing/test_collection.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/testing/test_collection.py b/testing/test_collection.py index 1491ec85990..7c87e869537 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -7,6 +7,7 @@ import tempfile import textwrap from typing import List +from typing import Type from _pytest.assertion.util import running_on_ci from _pytest.config import ExitCode @@ -1857,3 +1858,23 @@ def test_do_not_collect_symlink_siblings( # Ensure we collect it only once if we pass the symlinked directory. result = pytester.runpytest(symlink_path, "-sv") result.assert_outcomes(passed=1) + + +@pytest.mark.parametrize("exception_class", (KeyboardInterrupt, SystemExit)) +def test_respect_system_exceptions( + pytester: Pytester, exception_class: Type[BaseException] +): + ensure_file(pytester.path / "test_bar.py").write_text( + "raise SystemError()", encoding="UTF-8" + ) + ensure_file(pytester.path / "test_baz.py").write_text( + f"raise {exception_class.__name__}()", encoding="UTF-8" + ) + ensure_file(pytester.path / "test_foo.py").write_text( + "raise NotImplementedError()", encoding="UTF-8" + ) + + result = pytester.runpytest_subprocess() + result.stdout.fnmatch_lines(["*SystemError*"]) + result.stderr.fnmatch_lines([f"*{exception_class.__name__}*"]) + result.stdout.no_fnmatch_line("*NotImplementedError*") From c0ffee73c66c972095f1febf5fb126a50822f8e7 Mon Sep 17 00:00:00 2001 From: Anita Hammer <166057949+anitahammer@users.noreply.github.com> Date: Mon, 8 Apr 2024 03:00:31 +0100 Subject: [PATCH 05/10] Fix test Weather: overcast clouds --- testing/test_collection.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/testing/test_collection.py b/testing/test_collection.py index 7c87e869537..f9c3cfafb89 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -1864,17 +1864,19 @@ def test_do_not_collect_symlink_siblings( def test_respect_system_exceptions( pytester: Pytester, exception_class: Type[BaseException] ): + head = "Before exception" + tail = "After exception" ensure_file(pytester.path / "test_bar.py").write_text( - "raise SystemError()", encoding="UTF-8" + f"print('{head}')", encoding="UTF-8" ) ensure_file(pytester.path / "test_baz.py").write_text( f"raise {exception_class.__name__}()", encoding="UTF-8" ) ensure_file(pytester.path / "test_foo.py").write_text( - "raise NotImplementedError()", encoding="UTF-8" + f"print('{tail}')", encoding="UTF-8" ) - result = pytester.runpytest_subprocess() - result.stdout.fnmatch_lines(["*SystemError*"]) - result.stderr.fnmatch_lines([f"*{exception_class.__name__}*"]) - result.stdout.no_fnmatch_line("*NotImplementedError*") + result = pytester.runpytest_subprocess("-s") + result.stdout.fnmatch_lines([f"*{head}*"]) + result.stdout.fnmatch_lines([f"*{exception_class.__name__}*"]) + result.stdout.no_fnmatch_line(f"*{tail}*") From c0ffee0f1edbaf40777c4e56f28a76e1873bb0eb Mon Sep 17 00:00:00 2001 From: Anita Hammer <166057949+anitahammer@users.noreply.github.com> Date: Mon, 8 Apr 2024 08:52:57 +0100 Subject: [PATCH 06/10] Changelog Weather: overcast clouds --- AUTHORS | 1 + changelog/12191.bugfix.rst | 1 + 2 files changed, 2 insertions(+) create mode 100644 changelog/12191.bugfix.rst diff --git a/AUTHORS b/AUTHORS index 53f7a8c2a16..7aaee15d144 100644 --- a/AUTHORS +++ b/AUTHORS @@ -36,6 +36,7 @@ Andrey Paramonov Andrzej Klajnert Andrzej Ostrowski Andy Freeland +Anita Hammer Anthon van der Neut Anthony Shaw Anthony Sottile diff --git a/changelog/12191.bugfix.rst b/changelog/12191.bugfix.rst new file mode 100644 index 00000000000..a94c9ef2d53 --- /dev/null +++ b/changelog/12191.bugfix.rst @@ -0,0 +1 @@ +Finally fixed the issue where keyboard interrupts and system exits were not being respected during the tests collection phase in pytest! No more frustration of tests not ending correctly — happy testing! 🐞🔨🎉 From c0ffee2be14b81cdfbf8880bd6718f05b58fc671 Mon Sep 17 00:00:00 2001 From: Anita Hammer <166057949+anitahammer@users.noreply.github.com> Date: Mon, 8 Apr 2024 09:07:02 +0100 Subject: [PATCH 07/10] Better Weather: overcast clouds --- testing/test_collection.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/testing/test_collection.py b/testing/test_collection.py index f9c3cfafb89..818ea0df31c 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -1866,13 +1866,13 @@ def test_respect_system_exceptions( ): head = "Before exception" tail = "After exception" - ensure_file(pytester.path / "test_bar.py").write_text( + ensure_file(pytester.path / "test_eggs.py").write_text( f"print('{head}')", encoding="UTF-8" ) - ensure_file(pytester.path / "test_baz.py").write_text( + ensure_file(pytester.path / "test_ham.py").write_text( f"raise {exception_class.__name__}()", encoding="UTF-8" ) - ensure_file(pytester.path / "test_foo.py").write_text( + ensure_file(pytester.path / "test_spam.py").write_text( f"print('{tail}')", encoding="UTF-8" ) From ef9bbb40345cf24a910126fdc06295da3b8da84b Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 2 May 2024 08:38:27 -0300 Subject: [PATCH 08/10] Update changelog/12191.bugfix.rst --- changelog/12191.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/12191.bugfix.rst b/changelog/12191.bugfix.rst index a94c9ef2d53..3fa98354d62 100644 --- a/changelog/12191.bugfix.rst +++ b/changelog/12191.bugfix.rst @@ -1 +1 @@ -Finally fixed the issue where keyboard interrupts and system exits were not being respected during the tests collection phase in pytest! No more frustration of tests not ending correctly — happy testing! 🐞🔨🎉 +Keyboard interrupts and system exits are now probably handled during the test collection. From 386d0d367ab427e246860ff729377cb9aab6fff2 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 2 May 2024 08:40:25 -0300 Subject: [PATCH 09/10] Check message explicitly --- testing/test_collection.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/testing/test_collection.py b/testing/test_collection.py index 818ea0df31c..3f7a3d5351b 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -1860,9 +1860,17 @@ def test_do_not_collect_symlink_siblings( result.assert_outcomes(passed=1) -@pytest.mark.parametrize("exception_class", (KeyboardInterrupt, SystemExit)) +@pytest.mark.parametrize( + "exception_class, msg", + [ + (KeyboardInterrupt, "*!!! KeyboardInterrupt !!!*"), + (SystemExit, "INTERNALERROR> SystemExit"), + ], +) def test_respect_system_exceptions( - pytester: Pytester, exception_class: Type[BaseException] + pytester: Pytester, + exception_class: Type[BaseException], + msg: str, ): head = "Before exception" tail = "After exception" @@ -1878,5 +1886,5 @@ def test_respect_system_exceptions( result = pytester.runpytest_subprocess("-s") result.stdout.fnmatch_lines([f"*{head}*"]) - result.stdout.fnmatch_lines([f"*{exception_class.__name__}*"]) + result.stdout.fnmatch_lines([msg]) result.stdout.no_fnmatch_line(f"*{tail}*") From 5551738a319e74fca47448ec30c709591a80c5ea Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 2 May 2024 08:44:59 -0300 Subject: [PATCH 10/10] Update 12191.bugfix.rst --- changelog/12191.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/12191.bugfix.rst b/changelog/12191.bugfix.rst index 3fa98354d62..5102d469814 100644 --- a/changelog/12191.bugfix.rst +++ b/changelog/12191.bugfix.rst @@ -1 +1 @@ -Keyboard interrupts and system exits are now probably handled during the test collection. +Keyboard interrupts and system exits are now properly handled during the test collection.