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
|
2010-11-13 18:10:45 +08:00
|
|
|
from _pytest.python import FuncargRequest
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
|
|
def test_funcarg(testdir):
|
2010-10-17 06:24:59 +08:00
|
|
|
item = testdir.getitem("""
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
metafunc.addcall(id='a')
|
|
|
|
metafunc.addcall(id='b')
|
|
|
|
def test_func(tmpdir): pass
|
|
|
|
""", 'test_func[a]')
|
2009-09-06 22:59:39 +08:00
|
|
|
p = pytest_funcarg__tmpdir(FuncargRequest(item))
|
|
|
|
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"
|
|
|
|
p = pytest_funcarg__tmpdir(FuncargRequest(item))
|
|
|
|
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
|
|
|
|
|
|
|
@pytest.mark.skipif("not hasattr(os, 'symlink')")
|
|
|
|
def test_tmpdir_keeps_symlinks(testdir):
|
|
|
|
realtemp = testdir.tmpdir.mkdir("myrealtemp")
|
|
|
|
linktemp = testdir.tmpdir.join("symlinktemp")
|
2011-11-08 05:02:07 +08:00
|
|
|
os.symlink(str(realtemp), str(linktemp))
|
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
|