From 1dfa303b1e4656e3e87171e0ffc7db3dc36627a0 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sun, 14 Oct 2018 21:20:34 +0200 Subject: [PATCH] fix #4135 - handle symlinks in tmp path cleanup --- changelog/4135.bugfix.rst | 1 + src/_pytest/pathlib.py | 2 ++ testing/test_tmpdir.py | 10 +++++++++- 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 changelog/4135.bugfix.rst diff --git a/changelog/4135.bugfix.rst b/changelog/4135.bugfix.rst new file mode 100644 index 0000000000..d4f8a851d5 --- /dev/null +++ b/changelog/4135.bugfix.rst @@ -0,0 +1 @@ +pathlib based tmpdir cleanup now correctly handles symlinks in the folder. diff --git a/src/_pytest/pathlib.py b/src/_pytest/pathlib.py index 439f4d9ba6..cda5e9947f 100644 --- a/src/_pytest/pathlib.py +++ b/src/_pytest/pathlib.py @@ -173,6 +173,8 @@ def delete_a_numbered_dir(path): def ensure_deletable(path, consider_lock_dead_if_created_before): """checks if a lock exists and breaks it if its considered dead""" + if path.is_symlink(): + return False lock = get_lock_path(path) if not lock.exists(): return True diff --git a/testing/test_tmpdir.py b/testing/test_tmpdir.py index 9244e309dd..18b931c3f8 100644 --- a/testing/test_tmpdir.py +++ b/testing/test_tmpdir.py @@ -232,7 +232,7 @@ def test_lock_register_cleanup_removal(self, tmp_path): assert not lock.exists() - def test_cleanup_keep(self, tmp_path): + def _do_cleanup(self, tmp_path): self.test_make(tmp_path) from _pytest.pathlib import cleanup_numbered_dir @@ -242,6 +242,9 @@ def test_cleanup_keep(self, tmp_path): keep=2, consider_lock_dead_if_created_before=0, ) + + def test_cleanup_keep(self, tmp_path): + self._do_cleanup(tmp_path) a, b = tmp_path.iterdir() print(a, b) @@ -275,3 +278,8 @@ def test_rmtree(self, tmp_path): rmtree(adir, force=True) assert not adir.exists() + + def test_cleanup_symlink(self, tmp_path): + the_symlink = tmp_path / (self.PREFIX + "current") + the_symlink.symlink_to(tmp_path / (self.PREFIX + "5")) + self._do_cleanup(tmp_path)