From db203afba32cc162ab9e8d1da079dde600bd21cc Mon Sep 17 00:00:00 2001 From: Gleb Nikonorov Date: Sun, 31 May 2020 02:45:40 -0400 Subject: [PATCH] Add in --strict-config flag to force warnings to errors --- src/_pytest/config/__init__.py | 9 ++++++--- src/_pytest/main.py | 5 +++++ testing/test_config.py | 20 ++++++++++++++------ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 65e5271c2..63f7d4cb4 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -1020,7 +1020,7 @@ class Config: ) 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"): @@ -1073,9 +1073,12 @@ class Config: ) ) - 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 diff --git a/src/_pytest/main.py b/src/_pytest/main.py index de7e16744..4eb47be2c 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -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", diff --git a/testing/test_config.py b/testing/test_config.py index e35019337..6a08e93f3 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -148,7 +148,7 @@ class TestParseIni: assert result.ret == 0 @pytest.mark.parametrize( - "ini_file_text, invalid_keys, stderr_output", + "ini_file_text, invalid_keys, stderr_output, exception_text", [ ( """ @@ -158,9 +158,10 @@ class TestParseIni: """, ["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", ), ( """ @@ -169,7 +170,8 @@ class TestParseIni: 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", ), ( """ @@ -180,6 +182,7 @@ class TestParseIni: """, [], [], + "", ), ( """ @@ -188,11 +191,12 @@ class TestParseIni: """, [], [], + "", ), ], ) - 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() @@ -203,6 +207,10 @@ class TestParseIni: 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):