throw if addoption called after preparse with no default

This commit is contained in:
Kevin Santana 2021-12-07 21:44:10 -05:00
parent 4d7a962ca0
commit 95e0e2ba8c
4 changed files with 23 additions and 0 deletions

View File

@ -185,6 +185,7 @@ Katerina Koukiou
Keri Volans
Kevin Cox
Kevin J. Foley
Kevin Santana
Kodi B. Arfer
Kostis Anagnostopoulos
Kristoffer Nordström

View File

@ -0,0 +1,2 @@
`Parser.addoption` now throws a value error if an attempt is made at adding an option
after the pre_parsing process without including a default.

View File

@ -362,6 +362,11 @@ class OptionGroup:
results in help showing ``--two-words`` only, but ``--twowords`` gets
accepted **and** the automatic destination is in ``args.twowords``.
"""
if getattr(self.parser, "after_preparse", False) and "default" not in attrs:
raise ValueError(
"Cannot add options without default after initial conftest discovery"
)
conflict = set(optnames).intersection(
name for opt in self.options for name in opt.names()
)

View File

@ -78,6 +78,21 @@ class TestPytestPluginInteractions:
)
assert config.option.test123
def test_do_option_postinit_nodefault(self, pytester: Pytester) -> None:
config = pytester.parseconfigure()
assert not hasattr(config.option, "deadbeef")
p = pytester.makepyfile(
"""
def pytest_addoption(parser):
parser.addoption('--deadbeef', action="store_true")
"""
)
with pytest.raises(ValueError):
config.pluginmanager._importconftest(
p, importmode="prepend", rootpath=pytester.path
)
assert not hasattr(config.option, "deadbeef")
def test_configure(self, pytester: Pytester) -> None:
config = pytester.parseconfig()
values = []