Skip to content

Output a warning to stderr when an invalid key is read from an INI config file #7286

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

Merged
merged 6 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ def _preparse(self, args: List[str], addopts: bool = True) -> None:
)

self._checkversion()
self._validatekeys()
self._validatekeys(args)
self._consider_importhook(args)
self.pluginmanager.consider_preparse(args, exclude_only=False)
if not os.environ.get("PYTEST_DISABLE_PLUGIN_AUTOLOAD"):
Expand Down Expand Up @@ -1073,9 +1073,12 @@ def _checkversion(self):
)
)

def _validatekeys(self):
def _validatekeys(self, args: Sequence[str]):
for key in self._get_unknown_ini_keys():
sys.stderr.write("WARNING: unknown config ini key: {}\n".format(key))
message = "Unknown config ini key: {}\n".format(key)
if "--strict-config" in args:
fail(message, pytrace=False)
sys.stderr.write("WARNING: {}".format(message))

def _get_unknown_ini_keys(self) -> List[str]:
parser_inicfg = self._parser._inidict
Expand Down
5 changes: 5 additions & 0 deletions src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ def pytest_addoption(parser):
default=0,
help="exit after first num failures or errors.",
)
group._addoption(
"--strict-config",
action="store_true",
help="invalid ini keys for the `pytest` section of the configuration file raise errors.",
)
group._addoption(
"--strict-markers",
"--strict",
Expand Down
20 changes: 14 additions & 6 deletions testing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def test_confcutdir(self, testdir):
assert result.ret == 0

@pytest.mark.parametrize(
"ini_file_text, invalid_keys, stderr_output",
"ini_file_text, invalid_keys, stderr_output, exception_text",
[
(
"""
Expand All @@ -158,9 +158,10 @@ def test_confcutdir(self, testdir):
""",
["unknown_ini", "another_unknown_ini"],
[
"WARNING: unknown config ini key: unknown_ini",
"WARNING: unknown config ini key: another_unknown_ini",
"WARNING: Unknown config ini key: unknown_ini",
"WARNING: Unknown config ini key: another_unknown_ini",
],
"Unknown config ini key: unknown_ini",
),
(
"""
Expand All @@ -169,7 +170,8 @@ def test_confcutdir(self, testdir):
minversion = 5.0.0
""",
["unknown_ini"],
["WARNING: unknown config ini key: unknown_ini"],
["WARNING: Unknown config ini key: unknown_ini"],
"Unknown config ini key: unknown_ini",
),
(
"""
Expand All @@ -180,6 +182,7 @@ def test_confcutdir(self, testdir):
""",
[],
[],
"",
),
(
"""
Expand All @@ -188,11 +191,12 @@ def test_confcutdir(self, testdir):
""",
[],
[],
"",
),
],
)
def test_invalid_ini_keys_generate_warings(
self, testdir, ini_file_text, invalid_keys, stderr_output
def test_invalid_ini_keys(
self, testdir, ini_file_text, invalid_keys, stderr_output, exception_text
):
testdir.tmpdir.join("pytest.ini").write(textwrap.dedent(ini_file_text))
config = testdir.parseconfig()
Expand All @@ -203,6 +207,10 @@ def test_invalid_ini_keys_generate_warings(
result = testdir.runpytest()
result.stderr.fnmatch_lines(stderr_output)

if stderr_output:
with pytest.raises(pytest.fail.Exception, match=exception_text):
testdir.runpytest("--strict-config")


class TestConfigCmdlineParsing:
def test_parsing_again_fails(self, testdir):
Expand Down