2010-11-18 05:12:16 +08:00
|
|
|
import py, pytest
|
2011-11-08 05:02:07 +08:00
|
|
|
import os
|
2010-10-10 19:48:49 +08:00
|
|
|
|
2010-11-22 00:43:18 +08:00
|
|
|
from _pytest.tmpdir import pytest_funcarg__tmpdir, TempdirHandler
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
|
|
def test_funcarg(testdir):
|
2012-07-20 20:16:28 +08:00
|
|
|
testdir.makepyfile("""
|
2010-10-17 06:24:59 +08:00
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
metafunc.addcall(id='a')
|
|
|
|
metafunc.addcall(id='b')
|
|
|
|
def test_func(tmpdir): pass
|
2012-07-20 20:16:28 +08:00
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
calls = reprec.getcalls("pytest_runtest_setup")
|
|
|
|
item = calls[0].item
|
|
|
|
# pytest_unconfigure has deleted the TempdirHandler already
|
|
|
|
config = item.config
|
|
|
|
config._tmpdirhandler = TempdirHandler(config)
|
2012-06-25 23:35:33 +08:00
|
|
|
p = pytest_funcarg__tmpdir(item)
|
2009-09-06 22:59:39 +08:00
|
|
|
assert p.check()
|
2010-04-27 22:10:25 +08:00
|
|
|
bn = p.basename.strip("0123456789")
|
2010-10-21 22:10:37 +08:00
|
|
|
assert bn.endswith("test_func_a_")
|
|
|
|
item.name = "qwe/\\abc"
|
2012-06-25 23:35:33 +08:00
|
|
|
p = pytest_funcarg__tmpdir(item)
|
2010-10-21 22:10:37 +08:00
|
|
|
assert p.check()
|
|
|
|
bn = p.basename.strip("0123456789")
|
|
|
|
assert bn == "qwe__abc"
|
2010-10-10 19:48:49 +08:00
|
|
|
|
|
|
|
def test_ensuretemp(recwarn):
|
|
|
|
#py.test.deprecated_call(py.test.ensuretemp, 'hello')
|
|
|
|
d1 = py.test.ensuretemp('hello')
|
|
|
|
d2 = py.test.ensuretemp('hello')
|
|
|
|
assert d1 == d2
|
|
|
|
assert d1.check(dir=1)
|
|
|
|
|
2010-11-22 00:43:18 +08:00
|
|
|
class TestTempdirHandler:
|
|
|
|
def test_mktemp(self, testdir):
|
|
|
|
config = testdir.Config()
|
|
|
|
config.option.basetemp = testdir.mkdir("hello")
|
|
|
|
t = TempdirHandler(config)
|
|
|
|
tmp = t.mktemp("world")
|
|
|
|
assert tmp.relto(t.getbasetemp()) == "world0"
|
|
|
|
tmp = t.mktemp("this")
|
|
|
|
assert tmp.relto(t.getbasetemp()).startswith("this")
|
|
|
|
tmp2 = t.mktemp("this")
|
|
|
|
assert tmp2.relto(t.getbasetemp()).startswith("this")
|
|
|
|
assert tmp2 != tmp
|
|
|
|
|
|
|
|
class TestConfigTmpdir:
|
|
|
|
def test_getbasetemp_custom_removes_old(self, testdir):
|
|
|
|
p = testdir.tmpdir.join("xyz")
|
|
|
|
config = testdir.parseconfigure("--basetemp=xyz")
|
|
|
|
b = config._tmpdirhandler.getbasetemp()
|
|
|
|
assert b == p
|
|
|
|
h = b.ensure("hello")
|
|
|
|
config._tmpdirhandler.getbasetemp()
|
|
|
|
assert h.check()
|
|
|
|
config = testdir.parseconfigure("--basetemp=xyz")
|
|
|
|
b2 = config._tmpdirhandler.getbasetemp()
|
|
|
|
assert b2.check()
|
|
|
|
assert not h.check()
|
|
|
|
|
|
|
|
def test_basetemp(testdir):
|
|
|
|
mytemp = testdir.tmpdir.mkdir("mytemp")
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
def test_1():
|
|
|
|
pytest.ensuretemp("hello")
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p, '--basetemp=%s' % mytemp)
|
|
|
|
assert result.ret == 0
|
|
|
|
assert mytemp.join('hello').check()
|
2011-11-07 03:34:02 +08:00
|
|
|
|
2011-11-08 06:00:12 +08:00
|
|
|
@pytest.mark.skipif("not hasattr(py.path.local, 'mksymlinkto')")
|
2011-11-07 03:34:02 +08:00
|
|
|
def test_tmpdir_keeps_symlinks(testdir):
|
|
|
|
realtemp = testdir.tmpdir.mkdir("myrealtemp")
|
|
|
|
linktemp = testdir.tmpdir.join("symlinktemp")
|
2011-11-08 06:00:12 +08:00
|
|
|
linktemp.mksymlinkto(realtemp)
|
2011-11-07 03:34:02 +08:00
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def test_1(tmpdir):
|
|
|
|
import os
|
|
|
|
assert os.path.realpath(str(tmpdir)) != str(tmpdir)
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest("-s", p, '--basetemp=%s/bt' % linktemp)
|
|
|
|
assert not result.ret
|