Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Gabriel Reis
Gene Wood
George Kussumoto
Georgy Dyuldin
Gleb Nikonorov
Graham Horler
Greg Price
Gregory Lee
Expand Down
3 changes: 3 additions & 0 deletions changelog/6856.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
A warning is now shown when an unknown key is read from a config INI file.

The `--strict-config` flag has been added to treat these warnings as errors.
12 changes: 12 additions & 0 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,7 @@ def _preparse(self, args: List[str], addopts: bool = True) -> None:
self.known_args_namespace = ns = self._parser.parse_known_args(
args, namespace=copy.copy(self.option)
)
self._validatekeys()
if self.known_args_namespace.confcutdir is None and self.inifile:
confcutdir = py.path.local(self.inifile).dirname
self.known_args_namespace.confcutdir = confcutdir
Expand Down Expand Up @@ -1072,6 +1073,17 @@ def _checkversion(self):
)
)

def _validatekeys(self):
for key in self._get_unknown_ini_keys():
message = "Unknown config ini key: {}\n".format(key)
if self.known_args_namespace.strict_config:
fail(message, pytrace=False)
sys.stderr.write("WARNING: {}".format(message))

def _get_unknown_ini_keys(self) -> List[str]:
parser_inicfg = self._parser._inidict
return [name for name in self.inicfg if name not in parser_inicfg]

def parse(self, args: List[str], addopts: bool = True) -> None:
# parse given cmdline arguments into this config object.
assert not hasattr(
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
64 changes: 64 additions & 0 deletions testing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,70 @@ def test_confcutdir(self, testdir):
result = testdir.inline_run("--confcutdir=.")
assert result.ret == 0

@pytest.mark.parametrize(
"ini_file_text, invalid_keys, stderr_output, exception_text",
[
(
"""
[pytest]
unknown_ini = value1
another_unknown_ini = value2
""",
["unknown_ini", "another_unknown_ini"],
[
"WARNING: Unknown config ini key: unknown_ini",
"WARNING: Unknown config ini key: another_unknown_ini",
],
"Unknown config ini key: unknown_ini",
),
(
"""
[pytest]
unknown_ini = value1
minversion = 5.0.0
""",
["unknown_ini"],
["WARNING: Unknown config ini key: unknown_ini"],
"Unknown config ini key: unknown_ini",
),
(
"""
[some_other_header]
unknown_ini = value1
[pytest]
minversion = 5.0.0
""",
[],
[],
"",
),
(
"""
[pytest]
minversion = 5.0.0
""",
[],
[],
"",
),
],
)
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()
assert config._get_unknown_ini_keys() == invalid_keys, str(
config._get_unknown_ini_keys()
)

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