From eef4f87e7b9958687b6032f8475535ac0beca10f Mon Sep 17 00:00:00 2001 From: Gleb Nikonorov Date: Sat, 30 May 2020 20:36:02 -0400 Subject: [PATCH] Output a warning to stderr when an invalid key is read from an INI config file --- src/_pytest/config/__init__.py | 9 +++++++ testing/test_config.py | 46 ++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index bb5034ab1..65e5271c2 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -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( diff --git a/testing/test_config.py b/testing/test_config.py index 17385dc17..9323e6716 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -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):