[svn r62290] slightly sanitizing initialization of serialised config objects.

--HG--
branch : trunk
This commit is contained in:
hpk 2009-03-01 14:43:53 +01:00
parent 0b074ae555
commit 50acc51ac1
3 changed files with 39 additions and 19 deletions

View File

@ -28,8 +28,9 @@ class Config(object):
_initialized = False
_sessionclass = None
def __init__(self, pytestplugins=None):
def __init__(self, pytestplugins=None, topdir=None):
self.option = CmdOptions()
self.topdir = topdir
self._parser = parseopt.Parser(
usage="usage: %prog [options] [file_or_dir] [file_or_dir] [...]",
processopt=self._processopt,
@ -46,6 +47,11 @@ class Config(object):
if not hasattr(self.option, opt.dest):
setattr(self.option, opt.dest, opt.default)
def _preparse(self, args):
self._conftest.setinitial(args)
self.pytestplugins.consider_env()
self.pytestplugins.do_addoption(self._parser)
def parse(self, args):
""" parse cmdline arguments into this config object.
Note that this can only be called once per testing process.
@ -53,9 +59,7 @@ class Config(object):
assert not self._initialized, (
"can only parse cmdline args at most once per Config object")
self._initialized = True
self._conftest.setinitial(args)
self.pytestplugins.consider_env()
self.pytestplugins.do_addoption(self._parser)
self._preparse(args)
args = self._parser.parse_setoption(args, self.option)
if not args:
args.append(py.std.os.getcwd())
@ -74,11 +78,11 @@ class Config(object):
def _initafterpickle(self, topdir):
self.__init__(
#issue1
#pytestplugins=py.test._PytestPlugins(py._com.pyplugins)
#pytestplugins=py.test._PytestPlugins(py._com.pyplugins),
topdir=topdir,
)
self._initialized = True
self.topdir = py.path.local(topdir)
self._mergerepr(self._repr)
self._initialized = True
del self._repr
def _makerepr(self):
@ -98,7 +102,7 @@ class Config(object):
args, cmdlineopts = repr
self.args = [self.topdir.join(x) for x in args]
self.option = cmdlineopts
self._conftest.setinitial(self.args)
self._preparse(self.args)
def getcolitems(self):
return [self.getfsnode(arg) for arg in self.args]

View File

@ -4,6 +4,7 @@ pytes plugin for easing testing of pytest runs themselves.
import py
from py.__.test import event
from py.__.test.config import Config as pytestConfig
class PytesterPlugin:
def pytest_pyfuncarg_linecomp(self, pyfuncitem):
@ -49,8 +50,11 @@ class TmpTestdir:
self.tmpdir = tmpdir.mkdir(name)
self.plugins = []
self._syspathremove = []
from py.__.test.config import Config
self.Config = Config
def Config(self, pyplugins=None, topdir=None):
if topdir is None:
topdir = self.tmpdir.dirpath()
return pytestConfig(pyplugins, topdir=topdir)
def finalize(self):
for p in self._syspathremove:

View File

@ -285,10 +285,10 @@ class TestConfigPickling:
tmp.ensure("conftest.py").write("x=1 ; y=2")
hello = tmp.ensure("test_hello.py")
config = py.test.config._reparse([hello])
config2 = py.test.config._reparse([tmp.dirpath()])
config2 = testdir.Config()
config2._initialized = False # we have to do that from tests
config2._repr = config._makerepr()
config2._initafterpickle(topdir=tmp.dirpath())
config2._initafterpickle(tmp.dirpath())
for col1, col2 in zip(config.getcolitems(), config2.getcolitems()):
assert col1.fspath == col2.fspath
@ -305,12 +305,21 @@ class TestConfigPickling:
tmp.ensure("conftest.py").write("x=1")
config = py.test.config._reparse([tmp])
repr = config._makerepr()
config.option.verbose = 42
repr2 = config._makerepr()
config = py.test.config._reparse([tmp.dirpath()])
py.test.raises(KeyError, "config.getvalue('x')")
print "hello"
config = testdir.Config()
config._mergerepr(repr)
print config._conftest.getconftestmodules(None)
assert config.getvalue('x') == 1
config = testdir.Config()
config._preparse([])
py.test.raises(KeyError, "config.getvalue('x')")
config = testdir.Config()
config._mergerepr(repr2)
assert config.option.verbose == 42
@ -327,13 +336,16 @@ class TestConfigPickling:
config = py.test.config._reparse([tmp, "-G", "11"])
assert config.option.gdest == 11
repr = config._makerepr()
config = py.test.config._reparse([tmp.dirpath()])
config = testdir.Config()
py.test.raises(AttributeError, "config.option.gdest")
config._mergerepr(repr)
option = config.addoptions("testing group",
config.Option('-G', '--glong', action="store", default=42,
config2 = testdir.Config()
config2._mergerepr(repr)
option = config2.addoptions("testing group",
config2.Option('-G', '--glong', action="store", default=42,
type="int", dest="gdest", help="g value."))
assert config.option.gdest == 11
assert config2.option.gdest == 11
assert option.gdest == 11
def test_config_picklability(self, tmpdir):