Improve output for missing config keys (#7572)

Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
This commit is contained in:
Florian Bruhin 2020-09-19 17:57:29 +02:00 committed by GitHub
parent 9bfd14a443
commit 89305e7b09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 30 deletions

View File

@ -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.

View File

@ -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)

View File

@ -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: