Improve output for missing config keys (#7572)
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
This commit is contained in:
parent
9bfd14a443
commit
89305e7b09
|
@ -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.
|
||||||
|
|
|
@ -1252,7 +1252,7 @@ class Config:
|
||||||
|
|
||||||
def _warn_or_fail_if_strict(self, message: str) -> None:
|
def _warn_or_fail_if_strict(self, message: str) -> None:
|
||||||
if self.known_args_namespace.strict_config:
|
if self.known_args_namespace.strict_config:
|
||||||
fail(message, pytrace=False)
|
raise UsageError(message)
|
||||||
|
|
||||||
self.issue_config_time_warning(PytestConfigWarning(message), stacklevel=3)
|
self.issue_config_time_warning(PytestConfigWarning(message), stacklevel=3)
|
||||||
|
|
||||||
|
|
|
@ -181,12 +181,12 @@ class TestParseIni:
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"ini_file_text, invalid_keys, warning_output, exception_text",
|
"ini_file_text, invalid_keys, warning_output, exception_text",
|
||||||
[
|
[
|
||||||
(
|
pytest.param(
|
||||||
"""
|
"""
|
||||||
[pytest]
|
[pytest]
|
||||||
unknown_ini = value1
|
unknown_ini = value1
|
||||||
another_unknown_ini = value2
|
another_unknown_ini = value2
|
||||||
""",
|
""",
|
||||||
["unknown_ini", "another_unknown_ini"],
|
["unknown_ini", "another_unknown_ini"],
|
||||||
[
|
[
|
||||||
"=*= warnings summary =*=",
|
"=*= warnings summary =*=",
|
||||||
|
@ -194,48 +194,53 @@ class TestParseIni:
|
||||||
"*PytestConfigWarning:*Unknown config option: unknown_ini",
|
"*PytestConfigWarning:*Unknown config option: unknown_ini",
|
||||||
],
|
],
|
||||||
"Unknown config option: another_unknown_ini",
|
"Unknown config option: another_unknown_ini",
|
||||||
|
id="2-unknowns",
|
||||||
),
|
),
|
||||||
(
|
pytest.param(
|
||||||
"""
|
"""
|
||||||
[pytest]
|
[pytest]
|
||||||
unknown_ini = value1
|
unknown_ini = value1
|
||||||
minversion = 5.0.0
|
minversion = 5.0.0
|
||||||
""",
|
""",
|
||||||
["unknown_ini"],
|
["unknown_ini"],
|
||||||
[
|
[
|
||||||
"=*= warnings summary =*=",
|
"=*= warnings summary =*=",
|
||||||
"*PytestConfigWarning:*Unknown config option: unknown_ini",
|
"*PytestConfigWarning:*Unknown config option: unknown_ini",
|
||||||
],
|
],
|
||||||
"Unknown config option: unknown_ini",
|
"Unknown config option: unknown_ini",
|
||||||
|
id="1-unknown",
|
||||||
),
|
),
|
||||||
(
|
pytest.param(
|
||||||
"""
|
"""
|
||||||
[some_other_header]
|
[some_other_header]
|
||||||
unknown_ini = value1
|
unknown_ini = value1
|
||||||
[pytest]
|
[pytest]
|
||||||
minversion = 5.0.0
|
minversion = 5.0.0
|
||||||
""",
|
""",
|
||||||
[],
|
[],
|
||||||
[],
|
[],
|
||||||
"",
|
"",
|
||||||
|
id="unknown-in-other-header",
|
||||||
),
|
),
|
||||||
(
|
pytest.param(
|
||||||
"""
|
"""
|
||||||
[pytest]
|
[pytest]
|
||||||
minversion = 5.0.0
|
minversion = 5.0.0
|
||||||
""",
|
""",
|
||||||
[],
|
[],
|
||||||
[],
|
[],
|
||||||
"",
|
"",
|
||||||
|
id="no-unknowns",
|
||||||
),
|
),
|
||||||
(
|
pytest.param(
|
||||||
"""
|
"""
|
||||||
[pytest]
|
[pytest]
|
||||||
conftest_ini_key = 1
|
conftest_ini_key = 1
|
||||||
""",
|
""",
|
||||||
[],
|
[],
|
||||||
[],
|
[],
|
||||||
"",
|
"",
|
||||||
|
id="1-known",
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
@ -247,9 +252,10 @@ class TestParseIni:
|
||||||
"""
|
"""
|
||||||
def pytest_addoption(parser):
|
def pytest_addoption(parser):
|
||||||
parser.addini("conftest_ini_key", "")
|
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()
|
config = testdir.parseconfig()
|
||||||
assert sorted(config._get_unknown_ini_keys()) == sorted(invalid_keys)
|
assert sorted(config._get_unknown_ini_keys()) == sorted(invalid_keys)
|
||||||
|
@ -257,9 +263,13 @@ class TestParseIni:
|
||||||
result = testdir.runpytest()
|
result = testdir.runpytest()
|
||||||
result.stdout.fnmatch_lines(warning_output)
|
result.stdout.fnmatch_lines(warning_output)
|
||||||
|
|
||||||
|
result = testdir.runpytest("--strict-config")
|
||||||
if exception_text:
|
if exception_text:
|
||||||
result = testdir.runpytest("--strict-config")
|
result.stderr.fnmatch_lines("ERROR: " + exception_text)
|
||||||
result.stdout.fnmatch_lines("INTERNALERROR>*" + 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")
|
@pytest.mark.filterwarnings("default")
|
||||||
def test_silence_unknown_key_warning(self, testdir: Testdir) -> None:
|
def test_silence_unknown_key_warning(self, testdir: Testdir) -> None:
|
||||||
|
|
Loading…
Reference in New Issue