2014-01-29 18:46:36 +08:00
|
|
|
import py
|
2014-01-29 17:20:13 +08:00
|
|
|
import pytest
|
2010-10-10 19:48:49 +08:00
|
|
|
|
2015-07-16 07:45:35 +08:00
|
|
|
from _pytest.tmpdir import tmpdir
|
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
|
|
|
""")
|
2015-07-16 07:45:35 +08:00
|
|
|
from _pytest.tmpdir import TempdirFactory
|
2012-07-20 20:16:28 +08:00
|
|
|
reprec = testdir.inline_run()
|
|
|
|
calls = reprec.getcalls("pytest_runtest_setup")
|
|
|
|
item = calls[0].item
|
|
|
|
config = item.config
|
2015-07-16 07:03:58 +08:00
|
|
|
tmpdirhandler = TempdirFactory(config)
|
2013-04-22 16:35:48 +08:00
|
|
|
item._initrequest()
|
2015-07-16 07:03:58 +08:00
|
|
|
p = tmpdir(item._request, tmpdirhandler)
|
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"
|
2015-07-16 07:03:58 +08:00
|
|
|
p = tmpdir(item._request, tmpdirhandler)
|
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):
|
2014-01-18 19:31:33 +08:00
|
|
|
#pytest.deprecated_call(pytest.ensuretemp, 'hello')
|
|
|
|
d1 = pytest.ensuretemp('hello')
|
|
|
|
d2 = pytest.ensuretemp('hello')
|
2010-10-10 19:48:49 +08:00
|
|
|
assert d1 == d2
|
|
|
|
assert d1.check(dir=1)
|
|
|
|
|
2010-11-22 00:43:18 +08:00
|
|
|
class TestTempdirHandler:
|
|
|
|
def test_mktemp(self, testdir):
|
2015-07-16 07:45:35 +08:00
|
|
|
from _pytest.tmpdir import TempdirFactory
|
2013-09-29 04:22:53 +08:00
|
|
|
config = testdir.parseconfig()
|
2010-11-22 00:43:18 +08:00
|
|
|
config.option.basetemp = testdir.mkdir("hello")
|
2015-07-16 07:03:58 +08:00
|
|
|
t = TempdirFactory(config)
|
2010-11-22 00:43:18 +08:00
|
|
|
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):
|
2015-07-16 07:03:58 +08:00
|
|
|
mytemp = testdir.tmpdir.join("xyz")
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def test_1(tmpdir):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
testdir.runpytest(p, '--basetemp=%s' % mytemp)
|
|
|
|
mytemp.check()
|
|
|
|
mytemp.ensure("hello")
|
|
|
|
|
|
|
|
testdir.runpytest(p, '--basetemp=%s' % mytemp)
|
|
|
|
mytemp.check()
|
|
|
|
assert not mytemp.join("hello").check()
|
|
|
|
|
2010-11-22 00:43:18 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2014-01-29 18:46:36 +08:00
|
|
|
@pytest.mark.skipif(not hasattr(py.path.local, 'mksymlinkto'),
|
|
|
|
reason="symlink not available on this platform")
|
2012-10-11 19:05:16 +08:00
|
|
|
def test_tmpdir_always_is_realpath(testdir):
|
|
|
|
# the reason why tmpdir should be a realpath is that
|
|
|
|
# when you cd to it and do "os.getcwd()" you will anyway
|
|
|
|
# get the realpath. Using the symlinked path can thus
|
|
|
|
# easily result in path-inequality
|
|
|
|
# XXX if that proves to be a problem, consider using
|
|
|
|
# os.environ["PWD"]
|
2011-11-07 03:34:02 +08:00
|
|
|
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
|
2012-10-11 19:05:16 +08:00
|
|
|
assert os.path.realpath(str(tmpdir)) == str(tmpdir)
|
2011-11-07 03:34:02 +08:00
|
|
|
""")
|
|
|
|
result = testdir.runpytest("-s", p, '--basetemp=%s/bt' % linktemp)
|
|
|
|
assert not result.ret
|
2012-10-11 19:05:16 +08:00
|
|
|
|
2015-07-29 07:35:13 +08:00
|
|
|
|
2013-10-03 20:22:54 +08:00
|
|
|
def test_tmpdir_too_long_on_parametrization(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize("arg", ["1"*1000])
|
|
|
|
def test_some(arg, tmpdir):
|
|
|
|
tmpdir.ensure("hello")
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
2015-07-29 07:35:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_tmpdir_factory(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def session_dir(tmpdir_factory):
|
|
|
|
return tmpdir_factory.mktemp('data', numbered=False)
|
|
|
|
def test_some(session_dir):
|
|
|
|
session_dir.isdir()
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
2015-09-16 23:20:07 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
|
|
|
|
|
|
|
def test_tmpdir_fallback_tox_env(testdir, monkeypatch):
|
|
|
|
"""Test that tmpdir works even if environment variables required by getpass
|
|
|
|
module are missing (#1010).
|
|
|
|
"""
|
|
|
|
monkeypatch.delenv('USER', raising=False)
|
|
|
|
monkeypatch.delenv('USERNAME', raising=False)
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
def test_some(tmpdir):
|
|
|
|
assert tmpdir.isdir()
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|