Skip to content

Commit 0e55a87

Browse files
author
Buck Golemon
committed
all tests pass
1 parent 49d46a0 commit 0e55a87

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

_pytest/config.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,11 @@ def addoption(self, *opts, **attrs):
455455
"""
456456
self._anonymous.addoption(*opts, **attrs)
457457

458-
def parse(self, args):
458+
def parse(self, args, namespace=None):
459459
from _pytest._argcomplete import try_argcomplete
460460
self.optparser = self._getparser()
461461
try_argcomplete(self.optparser)
462-
return self.optparser.parse_args([str(x) for x in args])
462+
return self.optparser.parse_args([str(x) for x in args], namespace=namespace)
463463

464464
def _getparser(self):
465465
from _pytest._argcomplete import filescompleter
@@ -477,25 +477,25 @@ def _getparser(self):
477477
optparser.add_argument(FILE_OR_DIR, nargs='*').completer=filescompleter
478478
return optparser
479479

480-
def parse_setoption(self, args, option):
481-
parsedoption = self.parse(args)
480+
def parse_setoption(self, args, option, namespace=None):
481+
parsedoption = self.parse(args, namespace=namespace)
482482
for name, value in parsedoption.__dict__.items():
483483
setattr(option, name, value)
484484
return getattr(parsedoption, FILE_OR_DIR)
485485

486-
def parse_known_args(self, args):
486+
def parse_known_args(self, args, namespace=None):
487487
"""parses and returns a namespace object with known arguments at this
488488
point.
489489
"""
490-
return self.parse_known_and_unknown_args(args)[0]
490+
return self.parse_known_and_unknown_args(args, namespace=namespace)[0]
491491

492-
def parse_known_and_unknown_args(self, args):
492+
def parse_known_and_unknown_args(self, args, namespace=None):
493493
"""parses and returns a namespace object with known arguments, and
494494
the remaining arguments unknown at this point.
495495
"""
496496
optparser = self._getparser()
497497
args = [str(x) for x in args]
498-
return optparser.parse_known_args(args)
498+
return optparser.parse_known_args(args, namespace=namespace)
499499

500500
def addini(self, name, help, type=None, default=None):
501501
""" register an ini-file option.
@@ -779,10 +779,12 @@ def _ensure_removed_sysmodule(modname):
779779

780780
class CmdOptions(object):
781781
""" holds cmdline options as attributes."""
782-
def __init__(self, **kwargs):
783-
self.__dict__.update(kwargs)
782+
def __init__(self, values=()):
783+
self.__dict__.update(values)
784784
def __repr__(self):
785785
return "<CmdOptions %r>" %(self.__dict__,)
786+
def copy(self):
787+
return CmdOptions(self.__dict__)
786788

787789
class Notset:
788790
def __repr__(self):
@@ -879,8 +881,8 @@ def cwd_relative_nodeid(self, nodeid):
879881
def fromdictargs(cls, option_dict, args):
880882
""" constructor useable for subprocesses. """
881883
config = get_config()
882-
config.parse(args, addopts=False)
883884
config.option.__dict__.update(option_dict)
885+
config.parse(args, addopts=False)
884886
for x in config.option.plugins:
885887
config.pluginmanager.consider_pluginarg(x)
886888
return config
@@ -898,7 +900,7 @@ def pytest_load_initial_conftests(self, early_config):
898900
self.pluginmanager._set_initial_conftests(early_config.known_args_namespace)
899901

900902
def _initini(self, args):
901-
ns, unknown_args = self._parser.parse_known_and_unknown_args(args)
903+
ns, unknown_args = self._parser.parse_known_and_unknown_args(args, namespace=self.option.copy())
902904
r = determine_setup(ns.inifilename, ns.file_or_dir + unknown_args)
903905
self.rootdir, self.inifile, self.inicfg = r
904906
self._parser.extra_info['rootdir'] = self.rootdir
@@ -919,7 +921,7 @@ def _preparse(self, args, addopts=True):
919921
except ImportError as e:
920922
self.warn("I2", "could not load setuptools entry import: %s" % (e,))
921923
self.pluginmanager.consider_env()
922-
self.known_args_namespace = ns = self._parser.parse_known_args(args)
924+
self.known_args_namespace = ns = self._parser.parse_known_args(args, namespace=self.option.copy())
923925
if self.known_args_namespace.confcutdir is None and self.inifile:
924926
confcutdir = py.path.local(self.inifile).dirname
925927
self.known_args_namespace.confcutdir = confcutdir
@@ -957,7 +959,7 @@ def parse(self, args, addopts=True):
957959
self._preparse(args, addopts=addopts)
958960
# XXX deprecated hook:
959961
self.hook.pytest_cmdline_preparse(config=self, args=args)
960-
args = self._parser.parse_setoption(args, self.option)
962+
args = self._parser.parse_setoption(args, self.option, namespace=self.option)
961963
if not args:
962964
cwd = os.getcwd()
963965
if cwd == self.rootdir:

testing/test_config.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -269,42 +269,56 @@ class TestConfigFromdictargs:
269269
def test_basic_behavior(self):
270270
from _pytest.config import Config
271271
option_dict = {
272-
'verbose': 1e100,
272+
'verbose': 444,
273273
'foo': 'bar',
274+
'capture': 'no',
274275
}
275276
args = ['a', 'b']
276277

277278
config = Config.fromdictargs(option_dict, args)
278279
with pytest.raises(AssertionError):
279-
config.parse(['should to parse again'])
280-
assert config.option.verbose == 1e100
280+
config.parse(['should refuse to parse again'])
281+
assert config.option.verbose == 444
281282
assert config.option.foo == 'bar'
283+
assert config.option.capture == 'no'
282284
assert config.args == args
283285

284286
def test_origargs(self):
285287
"""Show that fromdictargs can handle args in their "orig" format"""
286288
from _pytest.config import Config
287289
option_dict = {}
288-
args = ['-vvvv', 'a', 'b']
290+
args = ['-vvvv', '-s', 'a', 'b']
289291

290292
config = Config.fromdictargs(option_dict, args)
291293
assert config.args == ['a', 'b']
292-
assert config._origargs == ['-vvvv', 'a', 'b']
294+
assert config._origargs == args
293295
assert config.option.verbose == 4
296+
assert config.option.capture == 'no'
297+
298+
def test_inifilename(self, tmpdir):
299+
tmpdir.join("foo/bar.ini").ensure().write(py.code.Source("""
300+
[pytest]
301+
name = value
302+
"""))
294303

295-
@pytest.mark.xfail(reason="fromdictargs currently broken #1060")
296-
def test_inifilename(self):
297304
from _pytest.config import Config
298-
inifile = '../../foo/bar.ini',
305+
inifile = '../../foo/bar.ini'
299306
option_dict = {
300307
'inifilename': inifile,
308+
'capture': 'no',
301309
}
302310

303-
config = Config.fromdictargs(option_dict, ())
311+
cwd = tmpdir.join('a/b')
312+
with cwd.ensure(dir=True).as_cwd():
313+
config = Config.fromdictargs(option_dict, ())
314+
315+
assert config.args == [str(cwd)]
304316
assert config.option.inifilename == inifile
317+
assert config.option.capture == 'no'
305318

306319
# this indicates this is the file used for getting configuration values
307320
assert config.inifile == inifile
321+
assert config.inicfg.get('name') == 'value'
308322

309323

310324
def test_options_on_small_file_do_not_blow_up(testdir):

0 commit comments

Comments
 (0)