Output a warning to stderr when an invalid key is read from an INI config file
This commit is contained in:
parent
56bf819c2f
commit
eef4f87e7b
|
@ -1020,6 +1020,7 @@ class Config:
|
|||
)
|
||||
|
||||
self._checkversion()
|
||||
self._validatekeys()
|
||||
self._consider_importhook(args)
|
||||
self.pluginmanager.consider_preparse(args, exclude_only=False)
|
||||
if not os.environ.get("PYTEST_DISABLE_PLUGIN_AUTOLOAD"):
|
||||
|
@ -1072,6 +1073,14 @@ class Config:
|
|||
)
|
||||
)
|
||||
|
||||
def _validatekeys(self):
|
||||
for key in self._get_unknown_ini_keys():
|
||||
sys.stderr.write("WARNING: unknown config ini key: {}\n".format(key))
|
||||
|
||||
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(
|
||||
|
|
|
@ -147,6 +147,52 @@ class TestParseIni:
|
|||
result = testdir.inline_run("--confcutdir=.")
|
||||
assert result.ret == 0
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"ini_file_text, invalid_keys, stderr_output",
|
||||
[
|
||||
(
|
||||
"""
|
||||
[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",
|
||||
],
|
||||
),
|
||||
(
|
||||
"""
|
||||
[pytest]
|
||||
unknown_ini = value1
|
||||
minversion = 5.0.0
|
||||
""",
|
||||
["unknown_ini"],
|
||||
["WARNING: unknown config ini key: unknown_ini"],
|
||||
),
|
||||
(
|
||||
"""
|
||||
[pytest]
|
||||
minversion = 5.0.0
|
||||
""",
|
||||
[],
|
||||
[],
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_invalid_ini_keys_generate_warings(
|
||||
self, testdir, ini_file_text, invalid_keys, stderr_output
|
||||
):
|
||||
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)
|
||||
|
||||
|
||||
class TestConfigCmdlineParsing:
|
||||
def test_parsing_again_fails(self, testdir):
|
||||
|
|
Loading…
Reference in New Issue