File tree Expand file tree Collapse file tree 3 files changed +39
-2
lines changed Expand file tree Collapse file tree 3 files changed +39
-2
lines changed Original file line number Diff line number Diff line change 1+ Validate arguments from the ``PYTEST_ADDOPTS `` environment variable and the ``addopts `` ini option separately.
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments