2008-08-16 23:26:59 +08:00
|
|
|
import py
|
2010-01-14 00:15:54 +08:00
|
|
|
from py._test.conftesthandle import Conftest
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-05-19 01:06:16 +08:00
|
|
|
def pytest_generate_tests(metafunc):
|
2009-05-19 00:59:45 +08:00
|
|
|
if "basedir" in metafunc.funcargnames:
|
|
|
|
metafunc.addcall(param="global")
|
|
|
|
metafunc.addcall(param="inpackage")
|
|
|
|
|
|
|
|
def pytest_funcarg__basedir(request):
|
|
|
|
def basedirmaker(request):
|
2009-12-30 05:26:03 +08:00
|
|
|
basedir = d = request.getfuncargvalue("tmpdir")
|
2008-08-16 23:26:59 +08:00
|
|
|
d.ensure("adir/conftest.py").write("a=1 ; Directory = 3")
|
|
|
|
d.ensure("adir/b/conftest.py").write("b=2 ; a = 1.5")
|
2009-05-19 00:59:45 +08:00
|
|
|
if request.param == "inpackage":
|
|
|
|
d.ensure("adir/__init__.py")
|
|
|
|
d.ensure("adir/b/__init__.py")
|
|
|
|
return d
|
2009-05-19 01:06:16 +08:00
|
|
|
return request.cached_setup(lambda: basedirmaker(request), extrakey=request.param)
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-12-29 00:49:46 +08:00
|
|
|
def ConftestWithSetinitial(path):
|
|
|
|
conftest = Conftest()
|
|
|
|
conftest.setinitial([path])
|
|
|
|
return conftest
|
|
|
|
|
2009-05-19 00:59:45 +08:00
|
|
|
class TestConftestValueAccessGlobal:
|
|
|
|
def test_basic_init(self, basedir):
|
2008-08-16 23:26:59 +08:00
|
|
|
conftest = Conftest()
|
2009-05-19 00:59:45 +08:00
|
|
|
conftest.setinitial([basedir.join("adir")])
|
2008-08-16 23:26:59 +08:00
|
|
|
assert conftest.rget("a") == 1
|
|
|
|
|
2009-05-19 00:59:45 +08:00
|
|
|
def test_onimport(self, basedir):
|
2009-02-27 18:18:27 +08:00
|
|
|
l = []
|
|
|
|
conftest = Conftest(onimport=l.append)
|
2009-05-19 00:59:45 +08:00
|
|
|
conftest.setinitial([basedir.join("adir")])
|
2010-02-07 05:37:04 +08:00
|
|
|
assert len(l) == 2
|
2009-02-27 18:18:27 +08:00
|
|
|
assert conftest.rget("a") == 1
|
2009-05-19 00:59:45 +08:00
|
|
|
assert conftest.rget("b", basedir.join("adir", "b")) == 2
|
2009-12-29 17:26:51 +08:00
|
|
|
assert len(l) == 2
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-05-19 00:59:45 +08:00
|
|
|
def test_immediate_initialiation_and_incremental_are_the_same(self, basedir):
|
2008-08-16 23:26:59 +08:00
|
|
|
conftest = Conftest()
|
|
|
|
snap0 = len(conftest._path2confmods)
|
2009-05-19 00:59:45 +08:00
|
|
|
conftest.getconftestmodules(basedir)
|
2008-08-16 23:26:59 +08:00
|
|
|
snap1 = len(conftest._path2confmods)
|
|
|
|
#assert len(conftest._path2confmods) == snap1 + 1
|
2009-05-19 00:59:45 +08:00
|
|
|
conftest.getconftestmodules(basedir.join('adir'))
|
2008-08-16 23:26:59 +08:00
|
|
|
assert len(conftest._path2confmods) == snap1 + 1
|
2009-05-19 00:59:45 +08:00
|
|
|
conftest.getconftestmodules(basedir.join('b'))
|
2008-08-16 23:26:59 +08:00
|
|
|
assert len(conftest._path2confmods) == snap1 + 2
|
|
|
|
|
2009-05-19 00:59:45 +08:00
|
|
|
def test_default_has_lower_prio(self, basedir):
|
2009-12-29 00:49:46 +08:00
|
|
|
conftest = ConftestWithSetinitial(basedir.join("adir"))
|
2008-08-16 23:26:59 +08:00
|
|
|
assert conftest.rget('Directory') == 3
|
|
|
|
#assert conftest.lget('Directory') == py.test.collect.Directory
|
|
|
|
|
2009-05-19 00:59:45 +08:00
|
|
|
def test_value_access_not_existing(self, basedir):
|
2009-12-29 00:49:46 +08:00
|
|
|
conftest = ConftestWithSetinitial(basedir)
|
2008-08-16 23:26:59 +08:00
|
|
|
py.test.raises(KeyError, "conftest.rget('a')")
|
|
|
|
#py.test.raises(KeyError, "conftest.lget('a')")
|
|
|
|
|
2009-05-19 00:59:45 +08:00
|
|
|
def test_value_access_by_path(self, basedir):
|
2009-12-29 00:49:46 +08:00
|
|
|
conftest = ConftestWithSetinitial(basedir)
|
2009-05-19 00:59:45 +08:00
|
|
|
assert conftest.rget("a", basedir.join('adir')) == 1
|
|
|
|
#assert conftest.lget("a", basedir.join('adir')) == 1
|
|
|
|
assert conftest.rget("a", basedir.join('adir', 'b')) == 1.5
|
|
|
|
#assert conftest.lget("a", basedir.join('adir', 'b')) == 1
|
|
|
|
#assert conftest.lget("b", basedir.join('adir', 'b')) == 2
|
2008-08-16 23:26:59 +08:00
|
|
|
#assert py.test.raises(KeyError,
|
2009-05-19 00:59:45 +08:00
|
|
|
# 'conftest.lget("b", basedir.join("a"))'
|
2008-08-16 23:26:59 +08:00
|
|
|
#)
|
|
|
|
|
2009-05-19 00:59:45 +08:00
|
|
|
def test_value_access_with_init_one_conftest(self, basedir):
|
2009-12-29 00:49:46 +08:00
|
|
|
conftest = ConftestWithSetinitial(basedir.join('adir'))
|
2008-08-16 23:26:59 +08:00
|
|
|
assert conftest.rget("a") == 1
|
|
|
|
#assert conftest.lget("a") == 1
|
|
|
|
|
2009-05-19 00:59:45 +08:00
|
|
|
def test_value_access_with_init_two_conftests(self, basedir):
|
2009-12-29 00:49:46 +08:00
|
|
|
conftest = ConftestWithSetinitial(basedir.join("adir", "b"))
|
2008-08-16 23:26:59 +08:00
|
|
|
conftest.rget("a") == 1.5
|
|
|
|
#conftest.lget("a") == 1
|
|
|
|
#conftest.lget("b") == 1
|
|
|
|
|
2009-05-19 00:59:45 +08:00
|
|
|
def test_value_access_with_confmod(self, basedir):
|
|
|
|
topdir = basedir.join("adir", "b")
|
2008-08-16 23:26:59 +08:00
|
|
|
topdir.ensure("xx", dir=True)
|
2009-12-29 00:49:46 +08:00
|
|
|
conftest = ConftestWithSetinitial(topdir)
|
2008-08-16 23:26:59 +08:00
|
|
|
mod, value = conftest.rget_with_confmod("a", topdir)
|
|
|
|
assert value == 1.5
|
|
|
|
path = py.path.local(mod.__file__)
|
2009-05-19 00:59:45 +08:00
|
|
|
assert path.dirpath() == basedir.join("adir", "b")
|
2008-08-16 23:26:59 +08:00
|
|
|
assert path.purebasename == "conftest"
|
2009-12-31 22:10:32 +08:00
|
|
|
|
|
|
|
def test_conftestcutdir(testdir):
|
|
|
|
conf = testdir.makeconftest("")
|
|
|
|
p = testdir.mkdir("x")
|
|
|
|
conftest = Conftest(confcutdir=p)
|
|
|
|
conftest.setinitial([testdir.tmpdir])
|
|
|
|
l = conftest.getconftestmodules(p)
|
|
|
|
assert len(l) == 0
|
|
|
|
l = conftest.getconftestmodules(conf.dirpath())
|
|
|
|
assert len(l) == 0
|
|
|
|
assert conf not in conftest._conftestpath2mod
|
|
|
|
# but we can still import a conftest directly
|
|
|
|
conftest.importconftest(conf)
|
|
|
|
l = conftest.getconftestmodules(conf.dirpath())
|
|
|
|
assert l[0].__file__.startswith(str(conf))
|
|
|
|
# and all sub paths get updated properly
|
|
|
|
l = conftest.getconftestmodules(p)
|
|
|
|
assert len(l) == 1
|
|
|
|
assert l[0].__file__.startswith(str(conf))
|
|
|
|
|
|
|
|
def test_conftestcutdir_inplace_considered(testdir):
|
|
|
|
conf = testdir.makeconftest("")
|
|
|
|
conftest = Conftest(confcutdir=conf.dirpath())
|
|
|
|
conftest.setinitial([conf.dirpath()])
|
|
|
|
l = conftest.getconftestmodules(conf.dirpath())
|
|
|
|
assert len(l) == 1
|
|
|
|
assert l[0].__file__.startswith(str(conf))
|
|
|
|
|
|
|
|
def test_setinitial_confcut(testdir):
|
|
|
|
conf = testdir.makeconftest("")
|
|
|
|
sub = testdir.mkdir("sub")
|
|
|
|
sub.chdir()
|
|
|
|
for opts in (["--confcutdir=%s" % sub, sub],
|
|
|
|
[sub, "--confcutdir=%s" % sub],
|
|
|
|
["--confcutdir=.", sub],
|
|
|
|
[sub, "--confcutdir", sub],
|
|
|
|
[str(sub), "--confcutdir", "."],
|
|
|
|
):
|
|
|
|
conftest = Conftest()
|
|
|
|
conftest.setinitial(opts)
|
|
|
|
assert conftest._confcutdir == sub
|
|
|
|
assert conftest.getconftestmodules(sub) == []
|
|
|
|
assert conftest.getconftestmodules(conf.dirpath()) == []
|
2010-02-04 19:26:53 +08:00
|
|
|
|
2010-02-07 05:37:04 +08:00
|
|
|
@py.test.mark.multi(name='test tests whatever .dotdir'.split())
|
|
|
|
def test_setinitial_conftest_subdirs(testdir, name):
|
2010-02-04 19:26:53 +08:00
|
|
|
sub = testdir.mkdir(name)
|
|
|
|
subconftest = sub.ensure("conftest.py")
|
|
|
|
conftest = Conftest()
|
|
|
|
conftest.setinitial([sub.dirpath()])
|
2010-02-07 05:37:04 +08:00
|
|
|
if name != ".dotdir":
|
|
|
|
assert subconftest in conftest._conftestpath2mod
|
|
|
|
assert len(conftest._conftestpath2mod) == 1
|
|
|
|
else:
|
|
|
|
assert subconftest not in conftest._conftestpath2mod
|
|
|
|
assert len(conftest._conftestpath2mod) == 0
|