From 89305e7b091a78f6288e0a4bba32f68d2f49bd07 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sat, 19 Sep 2020 17:57:29 +0200 Subject: [PATCH] Improve output for missing config keys (#7572) Co-authored-by: Bruno Oliveira --- changelog/7572.improvement.rst | 2 +- src/_pytest/config/__init__.py | 2 +- testing/test_config.py | 66 +++++++++++++++++++--------------- 3 files changed, 40 insertions(+), 30 deletions(-) diff --git a/changelog/7572.improvement.rst b/changelog/7572.improvement.rst index b42da81c1..e75d68341 100644 --- a/changelog/7572.improvement.rst +++ b/changelog/7572.improvement.rst @@ -1 +1 @@ -When a plugin listed in ``required_plugins`` is missing, a simple error message is now shown instead of a stacktrace. +When a plugin listed in ``required_plugins`` is missing or an unknown config key is used with ``--strict-config``, a simple error message is now shown instead of a stacktrace. diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 2a88d4637..0f25b76a6 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -1252,7 +1252,7 @@ class Config: def _warn_or_fail_if_strict(self, message: str) -> None: if self.known_args_namespace.strict_config: - fail(message, pytrace=False) + raise UsageError(message) self.issue_config_time_warning(PytestConfigWarning(message), stacklevel=3) diff --git a/testing/test_config.py b/testing/test_config.py index c1c660ab0..aa84a3cf5 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -181,12 +181,12 @@ class TestParseIni: @pytest.mark.parametrize( "ini_file_text, invalid_keys, warning_output, exception_text", [ - ( + pytest.param( """ - [pytest] - unknown_ini = value1 - another_unknown_ini = value2 - """, + [pytest] + unknown_ini = value1 + another_unknown_ini = value2 + """, ["unknown_ini", "another_unknown_ini"], [ "=*= warnings summary =*=", @@ -194,48 +194,53 @@ class TestParseIni: "*PytestConfigWarning:*Unknown config option: unknown_ini", ], "Unknown config option: another_unknown_ini", + id="2-unknowns", ), - ( + pytest.param( """ - [pytest] - unknown_ini = value1 - minversion = 5.0.0 - """, + [pytest] + unknown_ini = value1 + minversion = 5.0.0 + """, ["unknown_ini"], [ "=*= warnings summary =*=", "*PytestConfigWarning:*Unknown config option: unknown_ini", ], "Unknown config option: unknown_ini", + id="1-unknown", ), - ( + pytest.param( """ - [some_other_header] - unknown_ini = value1 - [pytest] - minversion = 5.0.0 - """, + [some_other_header] + unknown_ini = value1 + [pytest] + minversion = 5.0.0 + """, [], [], "", + id="unknown-in-other-header", ), - ( + pytest.param( """ - [pytest] - minversion = 5.0.0 - """, + [pytest] + minversion = 5.0.0 + """, [], [], "", + id="no-unknowns", ), - ( + pytest.param( """ - [pytest] - conftest_ini_key = 1 - """, + [pytest] + conftest_ini_key = 1 + """, [], [], "", + id="1-known", ), ], ) @@ -247,9 +252,10 @@ class TestParseIni: """ def pytest_addoption(parser): parser.addini("conftest_ini_key", "") - """ + """ ) - testdir.tmpdir.join("pytest.ini").write(textwrap.dedent(ini_file_text)) + testdir.makepyfile("def test(): pass") + testdir.makeini(ini_file_text) config = testdir.parseconfig() assert sorted(config._get_unknown_ini_keys()) == sorted(invalid_keys) @@ -257,9 +263,13 @@ class TestParseIni: result = testdir.runpytest() result.stdout.fnmatch_lines(warning_output) + result = testdir.runpytest("--strict-config") if exception_text: - result = testdir.runpytest("--strict-config") - result.stdout.fnmatch_lines("INTERNALERROR>*" + exception_text) + result.stderr.fnmatch_lines("ERROR: " + exception_text) + assert result.ret == pytest.ExitCode.USAGE_ERROR + else: + result.stderr.no_fnmatch_line(exception_text) + assert result.ret == pytest.ExitCode.OK @pytest.mark.filterwarnings("default") def test_silence_unknown_key_warning(self, testdir: Testdir) -> None: