Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve output for missing config keys #7572

Merged
merged 3 commits into from
Sep 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion changelog/7572.improvement.rst
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,7 @@ def _validate_plugins(self) -> None:

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)

Expand Down
66 changes: 38 additions & 28 deletions testing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,61 +181,66 @@ def test_confcutdir(self, testdir):
@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 =*=",
"*PytestConfigWarning:*Unknown config option: another_unknown_ini",
"*PytestConfigWarning:*Unknown config option: unknown_ini",
],
"Unknown config option: another_unknown_ini",
id="2-unknowns",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also made use of pytest.param(..., id=) here so the have a recognizable node id.

),
(
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",
),
],
)
Expand All @@ -247,19 +252,24 @@ def test_invalid_config_options(
"""
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)

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad while reviewing this: should have realized an internal error was not appropriate here in the first place, given that we are handling the problem. 😓

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:
Expand Down