Add in --strict-config flag to force warnings to errors

This commit is contained in:
Gleb Nikonorov 2020-05-31 02:45:40 -04:00
parent 8f2c2a5dd9
commit db203afba3
3 changed files with 25 additions and 9 deletions

View File

@ -1020,7 +1020,7 @@ class Config:
) )
self._checkversion() self._checkversion()
self._validatekeys() self._validatekeys(args)
self._consider_importhook(args) self._consider_importhook(args)
self.pluginmanager.consider_preparse(args, exclude_only=False) self.pluginmanager.consider_preparse(args, exclude_only=False)
if not os.environ.get("PYTEST_DISABLE_PLUGIN_AUTOLOAD"): 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(): 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]: def _get_unknown_ini_keys(self) -> List[str]:
parser_inicfg = self._parser._inidict parser_inicfg = self._parser._inidict

View File

@ -70,6 +70,11 @@ def pytest_addoption(parser):
default=0, default=0,
help="exit after first num failures or errors.", 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( group._addoption(
"--strict-markers", "--strict-markers",
"--strict", "--strict",

View File

@ -148,7 +148,7 @@ class TestParseIni:
assert result.ret == 0 assert result.ret == 0
@pytest.mark.parametrize( @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"], ["unknown_ini", "another_unknown_ini"],
[ [
"WARNING: unknown config ini key: unknown_ini", "WARNING: Unknown config ini key: unknown_ini",
"WARNING: unknown config ini key: another_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 minversion = 5.0.0
""", """,
["unknown_ini"], ["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( def test_invalid_ini_keys(
self, testdir, ini_file_text, invalid_keys, stderr_output self, testdir, ini_file_text, invalid_keys, stderr_output, exception_text
): ):
testdir.tmpdir.join("pytest.ini").write(textwrap.dedent(ini_file_text)) testdir.tmpdir.join("pytest.ini").write(textwrap.dedent(ini_file_text))
config = testdir.parseconfig() config = testdir.parseconfig()
@ -203,6 +207,10 @@ class TestParseIni:
result = testdir.runpytest() result = testdir.runpytest()
result.stderr.fnmatch_lines(stderr_output) result.stderr.fnmatch_lines(stderr_output)
if stderr_output:
with pytest.raises(pytest.fail.Exception, match=exception_text):
testdir.runpytest("--strict-config")
class TestConfigCmdlineParsing: class TestConfigCmdlineParsing:
def test_parsing_again_fails(self, testdir): def test_parsing_again_fails(self, testdir):