2019-07-10 20:52:23 +08:00
|
|
|
import os
|
|
|
|
import stat
|
2015-09-17 00:06:39 +08:00
|
|
|
import sys
|
2021-05-15 22:15:43 +08:00
|
|
|
import warnings
|
2020-10-03 23:08:14 +08:00
|
|
|
from pathlib import Path
|
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
|
2021-02-25 16:28:57 +08:00
|
|
|
from _pytest.monkeypatch import MonkeyPatch
|
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
|
|
|
|
from _pytest.pathlib import register_cleanup_lock_removal
|
|
|
|
from _pytest.pathlib import rm_rf
|
2020-11-09 12:23:31 +08:00
|
|
|
from _pytest.pytester import Pytester
|
2020-04-04 20:25:34 +08:00
|
|
|
from _pytest.tmpdir import get_user
|
|
|
|
from _pytest.tmpdir import TempPathFactory
|
2010-10-10 19:48:49 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
|
2021-03-14 03:22:54 +08:00
|
|
|
def test_tmp_path_fixture(pytester: Pytester) -> None:
|
|
|
|
p = pytester.copy_example("tmpdir/tmp_path_fixture.py")
|
2020-11-09 12:23:31 +08:00
|
|
|
results = pytester.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
|
|
|
|
|
2022-11-15 20:11:39 +08:00
|
|
|
def getini(self, name):
|
|
|
|
if name == "tmp_path_retention_count":
|
|
|
|
return 3
|
|
|
|
elif name == "tmp_path_retention_policy":
|
|
|
|
return "failed"
|
|
|
|
else:
|
|
|
|
assert False
|
|
|
|
|
2018-11-20 20:47:40 +08:00
|
|
|
@property
|
|
|
|
def option(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
2021-03-14 03:22:54 +08:00
|
|
|
class TestTmpPathHandler:
|
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))
|
2021-03-14 03:22:54 +08:00
|
|
|
t = TempPathFactory.from_config(config, _ispytest=True)
|
2010-11-22 00:43:18 +08:00
|
|
|
tmp = t.mktemp("world")
|
2021-03-14 03:22:54 +08:00
|
|
|
assert str(tmp.relative_to(t.getbasetemp())) == "world0"
|
2010-11-22 00:43:18 +08:00
|
|
|
tmp = t.mktemp("this")
|
2021-03-14 03:22:54 +08:00
|
|
|
assert str(tmp.relative_to(t.getbasetemp())).startswith("this")
|
2010-11-22 00:43:18 +08:00
|
|
|
tmp2 = t.mktemp("this")
|
2021-03-14 03:22:54 +08:00
|
|
|
assert str(tmp2.relative_to(t.getbasetemp())).startswith("this")
|
2010-11-22 00:43:18 +08:00
|
|
|
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"))
|
2020-09-28 03:20:31 +08:00
|
|
|
t = TempPathFactory.from_config(config, _ispytest=True)
|
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
|
|
|
|
2021-03-14 03:22:54 +08:00
|
|
|
class TestConfigTmpPath:
|
2020-11-09 12:23:31 +08:00
|
|
|
def test_getbasetemp_custom_removes_old(self, pytester: Pytester) -> None:
|
|
|
|
mytemp = pytester.path.joinpath("xyz")
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2021-03-14 03:22:54 +08:00
|
|
|
def test_1(tmp_path):
|
2015-07-16 07:03:58 +08:00
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
pytester.runpytest(p, "--basetemp=%s" % mytemp)
|
|
|
|
assert mytemp.exists()
|
|
|
|
mytemp.joinpath("hello").touch()
|
2015-07-16 07:03:58 +08:00
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
pytester.runpytest(p, "--basetemp=%s" % mytemp)
|
|
|
|
assert mytemp.exists()
|
|
|
|
assert not mytemp.joinpath("hello").exists()
|
2015-07-16 07:03:58 +08:00
|
|
|
|
2022-11-15 20:11:39 +08:00
|
|
|
def test_policy_failed_removes_only_passed_dir(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
|
|
|
"""
|
|
|
|
def test_1(tmp_path):
|
|
|
|
assert 0 == 0
|
|
|
|
def test_2(tmp_path):
|
|
|
|
assert 0 == 1
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
pytester.inline_run(p)
|
|
|
|
root = pytester._test_tmproot
|
|
|
|
|
|
|
|
for child in root.iterdir():
|
|
|
|
base_dir = list(
|
|
|
|
filter(lambda x: x.is_dir() and not x.is_symlink(), child.iterdir())
|
|
|
|
)
|
|
|
|
assert len(base_dir) == 1
|
|
|
|
test_dir = list(
|
|
|
|
filter(
|
|
|
|
lambda x: x.is_dir() and not x.is_symlink(), base_dir[0].iterdir()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
# Check only the failed one remains
|
|
|
|
assert len(test_dir) == 1
|
|
|
|
assert test_dir[0].name == "test_20"
|
|
|
|
|
|
|
|
def test_policy_failed_removes_basedir_when_all_passed(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
p = pytester.makepyfile(
|
|
|
|
"""
|
|
|
|
def test_1(tmp_path):
|
|
|
|
assert 0 == 0
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
pytester.inline_run(p)
|
|
|
|
root = pytester._test_tmproot
|
|
|
|
for child in root.iterdir():
|
|
|
|
# This symlink will be deleted by cleanup_numbered_dir **after**
|
|
|
|
# the test finishes because it's triggered by atexit.
|
|
|
|
# So it has to be ignored here.
|
|
|
|
base_dir = filter(lambda x: not x.is_symlink(), child.iterdir())
|
|
|
|
# Check the base dir itself is gone
|
|
|
|
assert len(list(base_dir)) == 0
|
|
|
|
|
2022-11-23 21:48:29 +08:00
|
|
|
# issue #10502
|
|
|
|
def test_policy_failed_removes_dir_when_skipped_from_fixture(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
p = pytester.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fixt(tmp_path):
|
|
|
|
pytest.skip()
|
|
|
|
|
|
|
|
def test_fixt(fixt):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
pytester.inline_run(p)
|
|
|
|
|
|
|
|
# Check if the whole directory is removed
|
|
|
|
root = pytester._test_tmproot
|
|
|
|
for child in root.iterdir():
|
|
|
|
base_dir = list(
|
|
|
|
filter(lambda x: x.is_dir() and not x.is_symlink(), child.iterdir())
|
|
|
|
)
|
|
|
|
assert len(base_dir) == 0
|
|
|
|
|
|
|
|
# issue #10502
|
|
|
|
def test_policy_all_keeps_dir_when_skipped_from_fixture(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
p = pytester.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fixt(tmp_path):
|
|
|
|
pytest.skip()
|
|
|
|
|
|
|
|
def test_fixt(fixt):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
pytester.makepyprojecttoml(
|
|
|
|
"""
|
|
|
|
[tool.pytest.ini_options]
|
|
|
|
tmp_path_retention_policy = "all"
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
pytester.inline_run(p)
|
|
|
|
|
|
|
|
# Check if the whole directory is kept
|
|
|
|
root = pytester._test_tmproot
|
|
|
|
for child in root.iterdir():
|
|
|
|
base_dir = list(
|
|
|
|
filter(lambda x: x.is_dir() and not x.is_symlink(), child.iterdir())
|
|
|
|
)
|
|
|
|
assert len(base_dir) == 1
|
|
|
|
test_dir = list(
|
|
|
|
filter(
|
|
|
|
lambda x: x.is_dir() and not x.is_symlink(), base_dir[0].iterdir()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
assert len(test_dir) == 1
|
|
|
|
|
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)
|
2020-11-09 12:23:31 +08:00
|
|
|
def test_mktemp(pytester: Pytester, basename: str, is_ok: bool) -> None:
|
|
|
|
mytemp = pytester.mkdir("mytemp")
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2021-03-14 03:22:54 +08:00
|
|
|
def test_abs_path(tmp_path_factory):
|
|
|
|
tmp_path_factory.mktemp('{}', numbered=False)
|
2019-12-07 05:09:42 +08:00
|
|
|
""".format(
|
|
|
|
basename
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2019-12-07 05:09:42 +08:00
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
result = pytester.runpytest(p, "--basetemp=%s" % mytemp)
|
2019-12-07 05:09:42 +08:00
|
|
|
if is_ok:
|
|
|
|
assert result.ret == 0
|
2020-11-09 12:23:31 +08:00
|
|
|
assert mytemp.joinpath(basename).exists()
|
2019-12-07 05:09:42 +08:00
|
|
|
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
|
|
|
|
2021-10-16 15:37:02 +08:00
|
|
|
def test_tmp_path_always_is_realpath(pytester: Pytester, monkeypatch) -> None:
|
|
|
|
# the reason why tmp_path should be a realpath is that
|
2012-10-11 19:05:16 +08:00
|
|
|
# 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"]
|
2020-11-09 12:23:31 +08:00
|
|
|
realtemp = pytester.mkdir("myrealtemp")
|
|
|
|
linktemp = pytester.path.joinpath("symlinktemp")
|
2018-10-15 05:21:04 +08:00
|
|
|
attempt_symlink_to(linktemp, str(realtemp))
|
2019-01-19 04:54:00 +08:00
|
|
|
monkeypatch.setenv("PYTEST_DEBUG_TEMPROOT", str(linktemp))
|
2020-11-09 12:23:31 +08:00
|
|
|
pytester.makepyfile(
|
2019-01-19 04:54:00 +08:00
|
|
|
"""
|
|
|
|
def test_1(tmp_path):
|
|
|
|
assert tmp_path.resolve() == tmp_path
|
|
|
|
"""
|
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
reprec = pytester.inline_run()
|
2019-01-19 04:54:00 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
|
|
|
|
2021-03-14 03:22:54 +08:00
|
|
|
def test_tmp_path_too_long_on_parametrization(pytester: Pytester) -> None:
|
2020-11-09 12:23:31 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2013-10-03 20:22:54 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize("arg", ["1"*1000])
|
2021-03-14 03:22:54 +08:00
|
|
|
def test_some(arg, tmp_path):
|
|
|
|
tmp_path.joinpath("hello").touch()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
reprec = pytester.inline_run()
|
2013-10-03 20:22:54 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
2015-07-29 07:35:13 +08:00
|
|
|
|
|
|
|
|
2021-03-14 03:22:54 +08:00
|
|
|
def test_tmp_path_factory(pytester: Pytester) -> None:
|
2020-11-09 12:23:31 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2015-07-29 07:35:13 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture(scope='session')
|
2021-03-14 03:22:54 +08:00
|
|
|
def session_dir(tmp_path_factory):
|
|
|
|
return tmp_path_factory.mktemp('data', numbered=False)
|
2015-07-29 07:35:13 +08:00
|
|
|
def test_some(session_dir):
|
2021-03-14 03:22:54 +08:00
|
|
|
assert session_dir.is_dir()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
reprec = pytester.inline_run()
|
2015-09-16 23:20:07 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
|
|
|
|
2021-03-14 03:22:54 +08:00
|
|
|
def test_tmp_path_fallback_tox_env(pytester: Pytester, monkeypatch) -> None:
|
|
|
|
"""Test that tmp_path works even if environment variables required by getpass
|
2015-09-16 23:20:07 +08:00
|
|
|
module are missing (#1010).
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.delenv("USER", raising=False)
|
|
|
|
monkeypatch.delenv("USERNAME", raising=False)
|
2020-11-09 12:23:31 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2021-03-14 03:22:54 +08:00
|
|
|
def test_some(tmp_path):
|
|
|
|
assert tmp_path.is_dir()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
reprec = pytester.inline_run()
|
2015-09-16 23:20:07 +08:00
|
|
|
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")
|
2021-03-14 03:22:54 +08:00
|
|
|
def test_tmp_path_fallback_uid_not_found(pytester: Pytester) -> None:
|
|
|
|
"""Test that tmp_path works even if the current process's user id does not
|
2015-09-19 09:06:13 +08:00
|
|
|
correspond to a valid user.
|
|
|
|
"""
|
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2021-03-14 03:22:54 +08:00
|
|
|
def test_some(tmp_path):
|
|
|
|
assert tmp_path.is_dir()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
reprec = pytester.inline_run()
|
2015-09-19 09:06:13 +08:00
|
|
|
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-10-06 09:13:05 +08:00
|
|
|
registry: 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()
|
|
|
|
|
2022-11-15 20:11:39 +08:00
|
|
|
def _do_cleanup(self, tmp_path: Path, keep: int = 2) -> 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,
|
2022-11-15 20:11:39 +08:00
|
|
|
keep=keep,
|
2018-09-29 04:06:43 +08:00
|
|
|
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
|
|
|
|
2022-11-15 20:11:39 +08:00
|
|
|
def test_cleanup_keep_0(self, tmp_path: Path):
|
|
|
|
self._do_cleanup(tmp_path, 0)
|
|
|
|
dir_num = len(list(tmp_path.iterdir()))
|
|
|
|
assert dir_num == 0
|
|
|
|
|
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
|
2021-05-15 22:15:43 +08:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter("ignore")
|
2021-05-17 16:50:59 +08:00
|
|
|
with pytest.warns(None) as warninfo: # type: ignore[call-overload]
|
2021-05-15 22:15:43 +08:00
|
|
|
exc_info4 = (None, PermissionError(), None)
|
|
|
|
on_rm_rf_error(os.open, str(fn), exc_info4, start_path=tmp_path)
|
|
|
|
assert fn.is_file()
|
|
|
|
assert not [x.message for x in warninfo]
|
2019-10-27 09:28:35 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
def test_basetemp_with_read_only_files(pytester: Pytester) -> None:
|
2019-07-10 20:52:23 +08:00
|
|
|
"""Integration test for #5524"""
|
2020-11-09 12:23:31 +08:00
|
|
|
pytester.makepyfile(
|
2019-07-10 20:52:23 +08:00
|
|
|
"""
|
|
|
|
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)
|
|
|
|
"""
|
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
result = pytester.runpytest("--basetemp=tmp")
|
2019-07-10 20:52:23 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
# running a second time and ensure we don't crash
|
2020-11-09 12:23:31 +08:00
|
|
|
result = pytester.runpytest("--basetemp=tmp")
|
2019-07-10 20:52:23 +08:00
|
|
|
assert result.ret == 0
|
2021-02-25 16:28:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_tmp_path_factory_handles_invalid_dir_characters(
|
|
|
|
tmp_path_factory: TempPathFactory, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
|
|
|
monkeypatch.setattr("getpass.getuser", lambda: "os/<:*?;>agnostic")
|
|
|
|
# _basetemp / _given_basetemp are cached / set in parallel runs, patch them
|
|
|
|
monkeypatch.setattr(tmp_path_factory, "_basetemp", None)
|
|
|
|
monkeypatch.setattr(tmp_path_factory, "_given_basetemp", None)
|
|
|
|
p = tmp_path_factory.getbasetemp()
|
|
|
|
assert "pytest-of-unknown" in str(p)
|
2021-03-06 23:01:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(not hasattr(os, "getuid"), reason="checks unix permissions")
|
|
|
|
def test_tmp_path_factory_create_directory_with_safe_permissions(
|
|
|
|
tmp_path: Path, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
|
|
|
"""Verify that pytest creates directories under /tmp with private permissions."""
|
|
|
|
# Use the test's tmp_path as the system temproot (/tmp).
|
|
|
|
monkeypatch.setenv("PYTEST_DEBUG_TEMPROOT", str(tmp_path))
|
2022-11-15 20:11:39 +08:00
|
|
|
tmp_factory = TempPathFactory(None, 3, "failed", lambda *args: None, _ispytest=True)
|
2021-03-06 23:01:29 +08:00
|
|
|
basetemp = tmp_factory.getbasetemp()
|
|
|
|
|
|
|
|
# No world-readable permissions.
|
|
|
|
assert (basetemp.stat().st_mode & 0o077) == 0
|
|
|
|
# Parent too (pytest-of-foo).
|
|
|
|
assert (basetemp.parent.stat().st_mode & 0o077) == 0
|
2021-03-07 05:17:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(not hasattr(os, "getuid"), reason="checks unix permissions")
|
|
|
|
def test_tmp_path_factory_fixes_up_world_readable_permissions(
|
|
|
|
tmp_path: Path, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
|
|
|
"""Verify that if a /tmp/pytest-of-foo directory already exists with
|
|
|
|
world-readable permissions, it is fixed.
|
|
|
|
|
|
|
|
pytest used to mkdir with such permissions, that's why we fix it up.
|
|
|
|
"""
|
|
|
|
# Use the test's tmp_path as the system temproot (/tmp).
|
|
|
|
monkeypatch.setenv("PYTEST_DEBUG_TEMPROOT", str(tmp_path))
|
2022-11-15 20:11:39 +08:00
|
|
|
tmp_factory = TempPathFactory(None, 3, "failed", lambda *args: None, _ispytest=True)
|
2021-03-07 05:17:46 +08:00
|
|
|
basetemp = tmp_factory.getbasetemp()
|
|
|
|
|
|
|
|
# Before - simulate bad perms.
|
|
|
|
os.chmod(basetemp.parent, 0o777)
|
|
|
|
assert (basetemp.parent.stat().st_mode & 0o077) != 0
|
|
|
|
|
2022-11-15 20:11:39 +08:00
|
|
|
tmp_factory = TempPathFactory(None, 3, "failed", lambda *args: None, _ispytest=True)
|
2021-03-07 05:17:46 +08:00
|
|
|
basetemp = tmp_factory.getbasetemp()
|
|
|
|
|
|
|
|
# After - fixed.
|
|
|
|
assert (basetemp.parent.stat().st_mode & 0o077) == 0
|