Skip to content

Commit 8faf1e8

Browse files
Merge pull request #7286 from gnikonorov/issue_6856
Output a warning to stderr when an invalid key is read from an INI config file
2 parents 5517f72 + a5d13d4 commit 8faf1e8

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Gabriel Reis
109109
Gene Wood
110110
George Kussumoto
111111
Georgy Dyuldin
112+
Gleb Nikonorov
112113
Graham Horler
113114
Greg Price
114115
Gregory Lee

changelog/6856.feature.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
A warning is now shown when an unknown key is read from a config INI file.
2+
3+
The `--strict-config` flag has been added to treat these warnings as errors.

src/_pytest/config/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,7 @@ def _preparse(self, args: List[str], addopts: bool = True) -> None:
10301030
self.known_args_namespace = ns = self._parser.parse_known_args(
10311031
args, namespace=copy.copy(self.option)
10321032
)
1033+
self._validatekeys()
10331034
if self.known_args_namespace.confcutdir is None and self.inifile:
10341035
confcutdir = py.path.local(self.inifile).dirname
10351036
self.known_args_namespace.confcutdir = confcutdir
@@ -1072,6 +1073,17 @@ def _checkversion(self):
10721073
)
10731074
)
10741075

1076+
def _validatekeys(self):
1077+
for key in self._get_unknown_ini_keys():
1078+
message = "Unknown config ini key: {}\n".format(key)
1079+
if self.known_args_namespace.strict_config:
1080+
fail(message, pytrace=False)
1081+
sys.stderr.write("WARNING: {}".format(message))
1082+
1083+
def _get_unknown_ini_keys(self) -> List[str]:
1084+
parser_inicfg = self._parser._inidict
1085+
return [name for name in self.inicfg if name not in parser_inicfg]
1086+
10751087
def parse(self, args: List[str], addopts: bool = True) -> None:
10761088
# parse given cmdline arguments into this config object.
10771089
assert not hasattr(

src/_pytest/main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ def pytest_addoption(parser):
7070
default=0,
7171
help="exit after first num failures or errors.",
7272
)
73+
group._addoption(
74+
"--strict-config",
75+
action="store_true",
76+
help="invalid ini keys for the `pytest` section of the configuration file raise errors.",
77+
)
7378
group._addoption(
7479
"--strict-markers",
7580
"--strict",

testing/test_config.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,70 @@ def test_confcutdir(self, testdir):
147147
result = testdir.inline_run("--confcutdir=.")
148148
assert result.ret == 0
149149

150+
@pytest.mark.parametrize(
151+
"ini_file_text, invalid_keys, stderr_output, exception_text",
152+
[
153+
(
154+
"""
155+
[pytest]
156+
unknown_ini = value1
157+
another_unknown_ini = value2
158+
""",
159+
["unknown_ini", "another_unknown_ini"],
160+
[
161+
"WARNING: Unknown config ini key: unknown_ini",
162+
"WARNING: Unknown config ini key: another_unknown_ini",
163+
],
164+
"Unknown config ini key: unknown_ini",
165+
),
166+
(
167+
"""
168+
[pytest]
169+
unknown_ini = value1
170+
minversion = 5.0.0
171+
""",
172+
["unknown_ini"],
173+
["WARNING: Unknown config ini key: unknown_ini"],
174+
"Unknown config ini key: unknown_ini",
175+
),
176+
(
177+
"""
178+
[some_other_header]
179+
unknown_ini = value1
180+
[pytest]
181+
minversion = 5.0.0
182+
""",
183+
[],
184+
[],
185+
"",
186+
),
187+
(
188+
"""
189+
[pytest]
190+
minversion = 5.0.0
191+
""",
192+
[],
193+
[],
194+
"",
195+
),
196+
],
197+
)
198+
def test_invalid_ini_keys(
199+
self, testdir, ini_file_text, invalid_keys, stderr_output, exception_text
200+
):
201+
testdir.tmpdir.join("pytest.ini").write(textwrap.dedent(ini_file_text))
202+
config = testdir.parseconfig()
203+
assert config._get_unknown_ini_keys() == invalid_keys, str(
204+
config._get_unknown_ini_keys()
205+
)
206+
207+
result = testdir.runpytest()
208+
result.stderr.fnmatch_lines(stderr_output)
209+
210+
if stderr_output:
211+
with pytest.raises(pytest.fail.Exception, match=exception_text):
212+
testdir.runpytest("--strict-config")
213+
150214

151215
class TestConfigCmdlineParsing:
152216
def test_parsing_again_fails(self, testdir):

0 commit comments

Comments
 (0)