2019-07-10 20:52:23 +08:00
|
|
|
import os
|
|
|
|
import stat
|
2015-09-17 00:06:39 +08:00
|
|
|
import sys
|
2020-05-01 19:40:17 +08:00
|
|
|
from typing import Callable
|
2020-07-14 15:55:17 +08:00
|
|
|
from typing import cast
|
2020-05-01 19:40:17 +08:00
|
|
|
from typing import List
|
2018-10-15 05:21:04 +08:00
|
|
|
|
2018-11-20 20:47:40 +08:00
|
|
|
import attr
|
2018-10-15 05:21:04 +08:00
|
|
|
|
2014-01-29 17:20:13 +08:00
|
|
|
import pytest
|
2018-10-24 13:45:53 +08:00
|
|
|
from _pytest import pathlib
|
2020-07-14 15:55:17 +08:00
|
|
|
from _pytest.config import Config
|
2020-04-04 20:25:34 +08:00
|
|
|
from _pytest.pathlib import cleanup_numbered_dir
|
|
|
|
from _pytest.pathlib import create_cleanup_lock
|
|
|
|
from _pytest.pathlib import make_numbered_dir
|
|
|
|
from _pytest.pathlib import maybe_delete_a_numbered_dir
|
|
|
|
from _pytest.pathlib import on_rm_rf_error
|
2018-10-15 05:21:04 +08:00
|
|
|
from _pytest.pathlib import Path
|
2020-04-04 20:25:34 +08:00
|
|
|
from _pytest.pathlib import register_cleanup_lock_removal
|
|
|
|
from _pytest.pathlib import rm_rf
|
|
|
|
from _pytest.tmpdir import get_user
|
|
|
|
from _pytest.tmpdir import TempdirFactory
|
|
|
|
from _pytest.tmpdir import TempPathFactory
|
2010-10-10 19:48:49 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
|
2018-07-22 22:10:44 +08:00
|
|
|
def test_tmpdir_fixture(testdir):
|
2018-07-27 07:10:40 +08:00
|
|
|
p = testdir.copy_example("tmpdir/tmpdir_fixture.py")
|
|
|
|
results = testdir.runpytest(p)
|
2019-03-23 18:36:18 +08:00
|
|
|
results.stdout.fnmatch_lines(["*1 passed*"])
|
2010-10-10 19:48:49 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2018-11-20 20:47:40 +08:00
|
|
|
@attr.s
|
2019-06-03 06:32:00 +08:00
|
|
|
class FakeConfig:
|
2018-11-20 20:47:40 +08:00
|
|
|
basetemp = attr.ib()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def trace(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def get(self, key):
|
|
|
|
return lambda *k: None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def option(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestTempdirHandler:
|
2018-11-20 20:47:40 +08:00
|
|
|
def test_mktemp(self, tmp_path):
|
2020-07-14 15:55:17 +08:00
|
|
|
config = cast(Config, FakeConfig(tmp_path))
|
2018-09-16 04:30:44 +08:00
|
|
|
t = TempdirFactory(TempPathFactory.from_config(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
|
|
|
|
|
2018-11-20 20:47:40 +08:00
|
|
|
def test_tmppath_relative_basetemp_absolute(self, tmp_path, monkeypatch):
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#4425"""
|
2018-11-20 20:47:40 +08:00
|
|
|
monkeypatch.chdir(tmp_path)
|
2020-07-14 15:55:17 +08:00
|
|
|
config = cast(Config, FakeConfig("hello"))
|
2018-11-20 20:47:40 +08:00
|
|
|
t = TempPathFactory.from_config(config)
|
2018-11-22 23:10:12 +08:00
|
|
|
assert t.getbasetemp().resolve() == (tmp_path / "hello").resolve()
|
2018-11-20 20:47:40 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestConfigTmpdir:
|
2010-11-22 00:43:18 +08:00
|
|
|
def test_getbasetemp_custom_removes_old(self, testdir):
|
2015-07-16 07:03:58 +08:00
|
|
|
mytemp = testdir.tmpdir.join("xyz")
|
2018-05-23 22:48:46 +08:00
|
|
|
p = testdir.makepyfile(
|
|
|
|
"""
|
2015-07-16 07:03:58 +08:00
|
|
|
def test_1(tmpdir):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.runpytest(p, "--basetemp=%s" % mytemp)
|
2015-07-16 07:03:58 +08:00
|
|
|
mytemp.check()
|
|
|
|
mytemp.ensure("hello")
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.runpytest(p, "--basetemp=%s" % mytemp)
|
2015-07-16 07:03:58 +08:00
|
|
|
mytemp.check()
|
|
|
|
assert not mytemp.join("hello").check()
|
|
|
|
|
2010-11-22 00:43:18 +08:00
|
|
|
|
2019-12-07 05:09:42 +08:00
|
|
|
testdata = [
|
|
|
|
("mypath", True),
|
|
|
|
("/mypath1", False),
|
|
|
|
("./mypath1", True),
|
|
|
|
("../mypath3", False),
|
|
|
|
("../../mypath4", False),
|
|
|
|
("mypath5/..", False),
|
|
|
|
("mypath6/../mypath6", True),
|
|
|
|
("mypath7/../mypath7/..", False),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("basename, is_ok", testdata)
|
|
|
|
def test_mktemp(testdir, basename, is_ok):
|
2010-11-22 00:43:18 +08:00
|
|
|
mytemp = testdir.tmpdir.mkdir("mytemp")
|
2018-05-23 22:48:46 +08:00
|
|
|
p = testdir.makepyfile(
|
|
|
|
"""
|
2019-12-07 05:09:42 +08:00
|
|
|
def test_abs_path(tmpdir_factory):
|
|
|
|
tmpdir_factory.mktemp('{}', numbered=False)
|
|
|
|
""".format(
|
|
|
|
basename
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2019-12-07 05:09:42 +08:00
|
|
|
|
2019-06-30 23:56:27 +08:00
|
|
|
result = testdir.runpytest(p, "--basetemp=%s" % mytemp)
|
2019-12-07 05:09:42 +08:00
|
|
|
if is_ok:
|
|
|
|
assert result.ret == 0
|
|
|
|
assert mytemp.join(basename).check()
|
|
|
|
else:
|
|
|
|
assert result.ret == 1
|
|
|
|
result.stdout.fnmatch_lines("*ValueError*")
|
2011-11-07 03:34:02 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
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")
|
2018-10-15 05:21:04 +08:00
|
|
|
attempt_symlink_to(linktemp, str(realtemp))
|
2018-05-23 22:48:46 +08:00
|
|
|
p = testdir.makepyfile(
|
|
|
|
"""
|
2011-11-07 03:34:02 +08:00
|
|
|
def test_1(tmpdir):
|
|
|
|
import os
|
2012-10-11 19:05:16 +08:00
|
|
|
assert os.path.realpath(str(tmpdir)) == str(tmpdir)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest("-s", p, "--basetemp=%s/bt" % linktemp)
|
2011-11-07 03:34:02 +08:00
|
|
|
assert not result.ret
|
2012-10-11 19:05:16 +08:00
|
|
|
|
2015-07-29 07:35:13 +08:00
|
|
|
|
2019-01-19 04:54:00 +08:00
|
|
|
def test_tmp_path_always_is_realpath(testdir, monkeypatch):
|
|
|
|
# for reasoning see: test_tmpdir_always_is_realpath test-case
|
|
|
|
realtemp = testdir.tmpdir.mkdir("myrealtemp")
|
|
|
|
linktemp = testdir.tmpdir.join("symlinktemp")
|
|
|
|
attempt_symlink_to(linktemp, str(realtemp))
|
|
|
|
monkeypatch.setenv("PYTEST_DEBUG_TEMPROOT", str(linktemp))
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
def test_1(tmp_path):
|
|
|
|
assert tmp_path.resolve() == tmp_path
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
|
|
|
|
2013-10-03 20:22:54 +08:00
|
|
|
def test_tmpdir_too_long_on_parametrization(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2013-10-03 20:22:54 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize("arg", ["1"*1000])
|
|
|
|
def test_some(arg, tmpdir):
|
|
|
|
tmpdir.ensure("hello")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2013-10-03 20:22:54 +08:00
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
2015-07-29 07:35:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_tmpdir_factory(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2015-07-29 07:35:13 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def session_dir(tmpdir_factory):
|
|
|
|
return tmpdir_factory.mktemp('data', numbered=False)
|
|
|
|
def test_some(session_dir):
|
2018-09-20 00:17:10 +08:00
|
|
|
assert session_dir.isdir()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-07-29 07:35:13 +08:00
|
|
|
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).
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.delenv("USER", raising=False)
|
|
|
|
monkeypatch.delenv("USERNAME", raising=False)
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2015-09-16 23:20:07 +08:00
|
|
|
def test_some(tmpdir):
|
|
|
|
assert tmpdir.isdir()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-09-16 23:20:07 +08:00
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
2015-09-16 23:47:50 +08:00
|
|
|
|
|
|
|
|
2015-09-30 03:00:12 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def break_getuser(monkeypatch):
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.setattr("os.getuid", lambda: -1)
|
2015-09-30 03:00:12 +08:00
|
|
|
# taken from python 2.7/3.4
|
2018-05-23 22:48:46 +08:00
|
|
|
for envvar in ("LOGNAME", "USER", "LNAME", "USERNAME"):
|
2015-09-30 03:00:12 +08:00
|
|
|
monkeypatch.delenv(envvar, raising=False)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("break_getuser")
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.skipif(sys.platform.startswith("win"), reason="no os.getuid on windows")
|
2015-09-30 03:00:12 +08:00
|
|
|
def test_tmpdir_fallback_uid_not_found(testdir):
|
2015-09-19 09:06:13 +08:00
|
|
|
"""Test that tmpdir works even if the current process's user id does not
|
|
|
|
correspond to a valid user.
|
|
|
|
"""
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2015-09-19 09:06:13 +08:00
|
|
|
def test_some(tmpdir):
|
|
|
|
assert tmpdir.isdir()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-09-19 09:06:13 +08:00
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
|
|
|
|
2015-09-30 03:00:12 +08:00
|
|
|
@pytest.mark.usefixtures("break_getuser")
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.skipif(sys.platform.startswith("win"), reason="no os.getuid on windows")
|
2015-09-30 03:00:12 +08:00
|
|
|
def test_get_user_uid_not_found():
|
2015-09-19 09:06:13 +08:00
|
|
|
"""Test that get_user() function works even if the current process's
|
|
|
|
user id does not correspond to a valid user (e.g. running pytest in a
|
|
|
|
Docker container with 'docker run -u'.
|
|
|
|
"""
|
|
|
|
assert get_user() is None
|
|
|
|
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.skipif(not sys.platform.startswith("win"), reason="win only")
|
2015-09-16 23:47:50 +08:00
|
|
|
def test_get_user(monkeypatch):
|
|
|
|
"""Test that get_user() function works even if environment variables
|
2015-09-17 00:06:39 +08:00
|
|
|
required by getpass module are missing from the environment on Windows
|
|
|
|
(#1010).
|
2015-09-16 23:47:50 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.delenv("USER", raising=False)
|
|
|
|
monkeypatch.delenv("USERNAME", raising=False)
|
2015-09-17 03:42:07 +08:00
|
|
|
assert get_user() is None
|
2018-09-20 22:10:33 +08:00
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestNumberedDir:
|
2018-09-20 22:10:33 +08:00
|
|
|
PREFIX = "fun-"
|
|
|
|
|
|
|
|
def test_make(self, tmp_path):
|
|
|
|
for i in range(10):
|
|
|
|
d = make_numbered_dir(root=tmp_path, prefix=self.PREFIX)
|
|
|
|
assert d.name.startswith(self.PREFIX)
|
|
|
|
assert d.name.endswith(str(i))
|
|
|
|
|
2018-10-18 02:43:27 +08:00
|
|
|
symlink = tmp_path.joinpath(self.PREFIX + "current")
|
2018-10-18 03:39:23 +08:00
|
|
|
if symlink.exists():
|
|
|
|
# unix
|
|
|
|
assert symlink.is_symlink()
|
|
|
|
assert symlink.resolve() == d.resolve()
|
2018-10-18 02:43:27 +08:00
|
|
|
|
2018-09-20 22:10:33 +08:00
|
|
|
def test_cleanup_lock_create(self, tmp_path):
|
|
|
|
d = tmp_path.joinpath("test")
|
|
|
|
d.mkdir()
|
|
|
|
lockfile = create_cleanup_lock(d)
|
2020-03-27 23:40:23 +08:00
|
|
|
with pytest.raises(OSError, match="cannot create lockfile in .*"):
|
2018-09-20 22:10:33 +08:00
|
|
|
create_cleanup_lock(d)
|
|
|
|
|
|
|
|
lockfile.unlink()
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_lock_register_cleanup_removal(self, tmp_path: Path) -> None:
|
2018-09-20 22:10:33 +08:00
|
|
|
lock = create_cleanup_lock(tmp_path)
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
registry = [] # type: List[Callable[..., None]]
|
2018-09-20 22:10:33 +08:00
|
|
|
register_cleanup_lock_removal(lock, register=registry.append)
|
|
|
|
|
2019-11-17 01:53:29 +08:00
|
|
|
(cleanup_func,) = registry
|
2018-09-20 22:10:33 +08:00
|
|
|
|
|
|
|
assert lock.is_file()
|
|
|
|
|
|
|
|
cleanup_func(original_pid="intentionally_different")
|
|
|
|
|
|
|
|
assert lock.is_file()
|
|
|
|
|
|
|
|
cleanup_func()
|
|
|
|
|
|
|
|
assert not lock.exists()
|
|
|
|
|
|
|
|
cleanup_func()
|
|
|
|
|
|
|
|
assert not lock.exists()
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def _do_cleanup(self, tmp_path: Path) -> None:
|
2018-09-20 22:10:33 +08:00
|
|
|
self.test_make(tmp_path)
|
|
|
|
cleanup_numbered_dir(
|
2018-09-29 04:06:43 +08:00
|
|
|
root=tmp_path,
|
|
|
|
prefix=self.PREFIX,
|
|
|
|
keep=2,
|
|
|
|
consider_lock_dead_if_created_before=0,
|
2018-09-20 22:10:33 +08:00
|
|
|
)
|
2018-10-15 03:20:34 +08:00
|
|
|
|
|
|
|
def test_cleanup_keep(self, tmp_path):
|
|
|
|
self._do_cleanup(tmp_path)
|
2018-10-18 03:16:44 +08:00
|
|
|
a, b = (x for x in tmp_path.iterdir() if not x.is_symlink())
|
2018-09-20 22:10:33 +08:00
|
|
|
print(a, b)
|
2018-09-27 02:41:33 +08:00
|
|
|
|
|
|
|
def test_cleanup_locked(self, tmp_path):
|
2020-04-04 20:25:34 +08:00
|
|
|
p = make_numbered_dir(root=tmp_path, prefix=self.PREFIX)
|
2018-09-27 02:41:33 +08:00
|
|
|
|
2020-04-04 20:25:34 +08:00
|
|
|
create_cleanup_lock(p)
|
2018-09-29 04:06:43 +08:00
|
|
|
|
2018-09-29 17:42:31 +08:00
|
|
|
assert not pathlib.ensure_deletable(
|
2018-09-29 04:06:43 +08:00
|
|
|
p, consider_lock_dead_if_created_before=p.stat().st_mtime - 1
|
|
|
|
)
|
2018-09-29 17:42:31 +08:00
|
|
|
assert pathlib.ensure_deletable(
|
2018-09-29 04:06:43 +08:00
|
|
|
p, consider_lock_dead_if_created_before=p.stat().st_mtime + 1
|
|
|
|
)
|
2018-10-01 22:39:24 +08:00
|
|
|
|
2019-07-19 02:07:45 +08:00
|
|
|
def test_cleanup_ignores_symlink(self, tmp_path):
|
|
|
|
the_symlink = tmp_path / (self.PREFIX + "current")
|
|
|
|
attempt_symlink_to(the_symlink, tmp_path / (self.PREFIX + "5"))
|
|
|
|
self._do_cleanup(tmp_path)
|
|
|
|
|
|
|
|
def test_removal_accepts_lock(self, tmp_path):
|
2020-04-04 20:25:34 +08:00
|
|
|
folder = make_numbered_dir(root=tmp_path, prefix=self.PREFIX)
|
|
|
|
create_cleanup_lock(folder)
|
|
|
|
maybe_delete_a_numbered_dir(folder)
|
2019-07-19 02:07:45 +08:00
|
|
|
assert folder.is_dir()
|
|
|
|
|
|
|
|
|
|
|
|
class TestRmRf:
|
|
|
|
def test_rm_rf(self, tmp_path):
|
2018-10-01 22:39:24 +08:00
|
|
|
adir = tmp_path / "adir"
|
|
|
|
adir.mkdir()
|
2019-07-10 20:52:23 +08:00
|
|
|
rm_rf(adir)
|
2018-10-01 22:39:24 +08:00
|
|
|
|
|
|
|
assert not adir.exists()
|
|
|
|
|
|
|
|
adir.mkdir()
|
|
|
|
afile = adir / "afile"
|
|
|
|
afile.write_bytes(b"aa")
|
|
|
|
|
2019-07-10 20:52:23 +08:00
|
|
|
rm_rf(adir)
|
2018-10-01 22:39:24 +08:00
|
|
|
assert not adir.exists()
|
2018-10-15 03:20:34 +08:00
|
|
|
|
2019-07-19 02:07:45 +08:00
|
|
|
def test_rm_rf_with_read_only_file(self, tmp_path):
|
2019-07-10 20:52:23 +08:00
|
|
|
"""Ensure rm_rf can remove directories with read-only files in them (#5524)"""
|
|
|
|
fn = tmp_path / "dir/foo.txt"
|
|
|
|
fn.parent.mkdir()
|
|
|
|
|
|
|
|
fn.touch()
|
|
|
|
|
2019-07-19 02:07:45 +08:00
|
|
|
self.chmod_r(fn)
|
2019-07-10 20:52:23 +08:00
|
|
|
|
|
|
|
rm_rf(fn.parent)
|
|
|
|
|
|
|
|
assert not fn.parent.is_dir()
|
|
|
|
|
2019-07-19 02:07:45 +08:00
|
|
|
def chmod_r(self, path):
|
|
|
|
mode = os.stat(str(path)).st_mode
|
|
|
|
os.chmod(str(path), mode & ~stat.S_IWRITE)
|
|
|
|
|
|
|
|
def test_rm_rf_with_read_only_directory(self, tmp_path):
|
2019-07-10 20:52:23 +08:00
|
|
|
"""Ensure rm_rf can remove read-only directories (#5524)"""
|
|
|
|
adir = tmp_path / "dir"
|
|
|
|
adir.mkdir()
|
|
|
|
|
|
|
|
(adir / "foo.txt").touch()
|
2019-07-19 02:07:45 +08:00
|
|
|
self.chmod_r(adir)
|
2019-07-10 20:52:23 +08:00
|
|
|
|
|
|
|
rm_rf(adir)
|
|
|
|
|
|
|
|
assert not adir.is_dir()
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_on_rm_rf_error(self, tmp_path: Path) -> None:
|
2019-07-19 02:07:45 +08:00
|
|
|
adir = tmp_path / "dir"
|
|
|
|
adir.mkdir()
|
|
|
|
|
|
|
|
fn = adir / "foo.txt"
|
|
|
|
fn.touch()
|
|
|
|
self.chmod_r(fn)
|
|
|
|
|
|
|
|
# unknown exception
|
|
|
|
with pytest.warns(pytest.PytestWarning):
|
2020-05-01 19:40:17 +08:00
|
|
|
exc_info1 = (None, RuntimeError(), None)
|
|
|
|
on_rm_rf_error(os.unlink, str(fn), exc_info1, start_path=tmp_path)
|
2019-07-19 02:07:45 +08:00
|
|
|
assert fn.is_file()
|
|
|
|
|
2019-10-23 07:19:52 +08:00
|
|
|
# we ignore FileNotFoundError
|
2020-05-01 19:40:17 +08:00
|
|
|
exc_info2 = (None, FileNotFoundError(), None)
|
|
|
|
assert not on_rm_rf_error(None, str(fn), exc_info2, start_path=tmp_path)
|
2019-10-23 07:19:52 +08:00
|
|
|
|
2019-07-19 02:07:45 +08:00
|
|
|
# unknown function
|
2019-10-28 19:34:40 +08:00
|
|
|
with pytest.warns(
|
|
|
|
pytest.PytestWarning,
|
|
|
|
match=r"^\(rm_rf\) unknown function None when removing .*foo.txt:\nNone: ",
|
|
|
|
):
|
2020-05-01 19:40:17 +08:00
|
|
|
exc_info3 = (None, PermissionError(), None)
|
|
|
|
on_rm_rf_error(None, str(fn), exc_info3, start_path=tmp_path)
|
2019-07-19 02:07:45 +08:00
|
|
|
assert fn.is_file()
|
|
|
|
|
2019-10-27 09:28:35 +08:00
|
|
|
# ignored function
|
|
|
|
with pytest.warns(None) as warninfo:
|
2020-05-01 19:40:17 +08:00
|
|
|
exc_info4 = (None, PermissionError(), None)
|
|
|
|
on_rm_rf_error(os.open, str(fn), exc_info4, start_path=tmp_path)
|
2019-10-27 09:28:35 +08:00
|
|
|
assert fn.is_file()
|
|
|
|
assert not [x.message for x in warninfo]
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
exc_info5 = (None, PermissionError(), None)
|
|
|
|
on_rm_rf_error(os.unlink, str(fn), exc_info5, start_path=tmp_path)
|
2019-07-19 02:07:45 +08:00
|
|
|
assert not fn.is_file()
|
2018-10-24 13:45:53 +08:00
|
|
|
|
2018-10-15 05:21:04 +08:00
|
|
|
|
|
|
|
def attempt_symlink_to(path, to_path):
|
|
|
|
"""Try to make a symlink from "path" to "to_path", skipping in case this platform
|
|
|
|
does not support it or we don't have sufficient privileges (common on Windows)."""
|
|
|
|
try:
|
|
|
|
Path(path).symlink_to(Path(to_path))
|
|
|
|
except OSError:
|
|
|
|
pytest.skip("could not create symbolic link")
|
2019-01-27 20:05:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_tmpdir_equals_tmp_path(tmpdir, tmp_path):
|
|
|
|
assert Path(tmpdir) == tmp_path
|
2019-07-10 20:52:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_basetemp_with_read_only_files(testdir):
|
|
|
|
"""Integration test for #5524"""
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
import stat
|
|
|
|
|
|
|
|
def test(tmp_path):
|
|
|
|
fn = tmp_path / 'foo.txt'
|
|
|
|
fn.write_text('hello')
|
|
|
|
mode = os.stat(str(fn)).st_mode
|
|
|
|
os.chmod(str(fn), mode & ~stat.S_IREAD)
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest("--basetemp=tmp")
|
|
|
|
assert result.ret == 0
|
|
|
|
# running a second time and ensure we don't crash
|
|
|
|
result = testdir.runpytest("--basetemp=tmp")
|
|
|
|
assert result.ret == 0
|