Skip to content

Commit abb0dfc

Browse files
authored
Merge pull request #4523 from blueyed/addopts-separate
Ensure that PYTEST_ADDOPTS and addopts ini values are valid by themselves.
2 parents 818aa4d + f3babf1 commit abb0dfc

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

changelog/4265.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Validate arguments from the ``PYTEST_ADDOPTS`` environment variable and the ``addopts`` ini option separately.

src/_pytest/config/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,12 +784,21 @@ def _mark_plugins_for_rewrite(self, hook):
784784
for name in _iter_rewritable_modules(package_files):
785785
hook.mark_rewrite(name)
786786

787+
def _validate_args(self, args):
788+
"""Validate known args."""
789+
self._parser.parse_known_and_unknown_args(
790+
args, namespace=copy.copy(self.option)
791+
)
792+
return args
793+
787794
def _preparse(self, args, addopts=True):
788795
if addopts:
789-
args[:] = shlex.split(os.environ.get("PYTEST_ADDOPTS", "")) + args
796+
env_addopts = os.environ.get("PYTEST_ADDOPTS", "")
797+
if len(env_addopts):
798+
args[:] = self._validate_args(shlex.split(env_addopts)) + args
790799
self._initini(args)
791800
if addopts:
792-
args[:] = self.getini("addopts") + args
801+
args[:] = self._validate_args(self.getini("addopts")) + args
793802
self._checkversion()
794803
self._consider_importhook(args)
795804
self.pluginmanager.consider_preparse(args)

testing/test_config.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,33 @@ def test_addopts_before_initini(self, monkeypatch):
10821082
config._preparse([], addopts=True)
10831083
assert config._override_ini == ["cache_dir=%s" % cache_dir]
10841084

1085+
def test_addopts_from_env_not_concatenated(self, monkeypatch):
1086+
"""PYTEST_ADDOPTS should not take values from normal args (#4265)."""
1087+
from _pytest.config import get_config
1088+
1089+
monkeypatch.setenv("PYTEST_ADDOPTS", "-o")
1090+
config = get_config()
1091+
with pytest.raises(SystemExit) as excinfo:
1092+
config._preparse(["cache_dir=ignored"], addopts=True)
1093+
assert excinfo.value.args[0] == _pytest.main.EXIT_USAGEERROR
1094+
1095+
def test_addopts_from_ini_not_concatenated(self, testdir):
1096+
"""addopts from ini should not take values from normal args (#4265)."""
1097+
testdir.makeini(
1098+
"""
1099+
[pytest]
1100+
addopts=-o
1101+
"""
1102+
)
1103+
result = testdir.runpytest("cache_dir=ignored")
1104+
result.stderr.fnmatch_lines(
1105+
[
1106+
"%s: error: argument -o/--override-ini: expected one argument"
1107+
% (testdir.request.config._parser.optparser.prog,)
1108+
]
1109+
)
1110+
assert result.ret == _pytest.main.EXIT_USAGEERROR
1111+
10851112
def test_override_ini_does_not_contain_paths(self):
10861113
"""Check that -o no longer swallows all options after it (#3103)"""
10871114
from _pytest.config import get_config

0 commit comments

Comments
 (0)