| from __future__ import annotations | |
| import functools | |
| import os | |
| import shutil | |
| import sys | |
| from collections.abc import Iterator | |
| from pathlib import Path | |
| from unittest.mock import Mock | |
| import pytest | |
| import pip._internal.req.req_uninstall | |
| from pip._internal.exceptions import BlockedImportError | |
| from pip._internal.metadata import get_environment | |
| from pip._internal.req.req_uninstall import ( | |
| StashedUninstallPathSet, | |
| UninstallPathSet, | |
| UninstallPthEntries, | |
| compact, | |
| compress_for_output_listing, | |
| compress_for_rename, | |
| uninstallation_paths, | |
| ) | |
| from tests.lib.filesystem import create_file | |
| # Pretend all files are local, so UninstallPathSet accepts files in the tmpdir, | |
| # outside the virtualenv | |
| def mock_permitted(ups: UninstallPathSet, path: str) -> bool: | |
| return True | |
| def test_uninstallation_paths() -> None: | |
| class dist: | |
| def iter_declared_entries(self) -> Iterator[str] | None: | |
| return iter(["file.py", "file.pyc", "file.so", "nopyc.py"]) | |
| location = "" | |
| d = dist() | |
| paths = list(uninstallation_paths(d)) | |
| expected = [ | |
| "file.py", | |
| "file.pyc", | |
| "file.pyo", | |
| "file.so", | |
| "nopyc.py", | |
| "nopyc.pyc", | |
| "nopyc.pyo", | |
| ] | |
| assert paths == expected | |
| # Avoid an easy 'unique generator' bug | |
| paths2 = list(uninstallation_paths(d)) | |
| assert paths2 == paths | |
| def test_compressed_listing(tmpdir: Path) -> None: | |
| def in_tmpdir(paths: list[str]) -> list[str]: | |
| return [ | |
| str(os.path.join(tmpdir, path.replace("/", os.path.sep))) for path in paths | |
| ] | |
| sample = in_tmpdir( | |
| [ | |
| "lib/mypkg.dist-info/METADATA", | |
| "lib/mypkg.dist-info/PKG-INFO", | |
| "lib/mypkg/would_be_removed.txt", | |
| "lib/mypkg/would_be_skipped.skip.txt", | |
| "lib/mypkg/__init__.py", | |
| "lib/mypkg/my_awesome_code.py", | |
| "lib/mypkg/__pycache__/my_awesome_code-magic.pyc", | |
| "lib/mypkg/support/support_file.py", | |
| "lib/mypkg/support/more_support.py", | |
| "lib/mypkg/support/would_be_skipped.skip.py", | |
| "lib/mypkg/support/__pycache__/support_file-magic.pyc", | |
| "lib/random_other_place/file_without_a_dot_pyc", | |
| "bin/mybin", | |
| ] | |
| ) | |
| # Create the required files | |
| for fname in sample: | |
| create_file(fname, "random blub") | |
| # Remove the files to be skipped from the paths | |
| sample = [path for path in sample if ".skip." not in path] | |
| expected_remove = in_tmpdir( | |
| [ | |
| "bin/mybin", | |
| "lib/mypkg.dist-info/*", | |
| "lib/mypkg/*", | |
| "lib/random_other_place/file_without_a_dot_pyc", | |
| ] | |
| ) | |
| expected_skip = in_tmpdir( | |
| [ | |
| "lib/mypkg/would_be_skipped.skip.txt", | |
| "lib/mypkg/support/would_be_skipped.skip.py", | |
| ] | |
| ) | |
| expected_rename = in_tmpdir( | |
| [ | |
| "bin/", | |
| "lib/mypkg.dist-info/", | |
| "lib/mypkg/would_be_removed.txt", | |
| "lib/mypkg/__init__.py", | |
| "lib/mypkg/my_awesome_code.py", | |
| "lib/mypkg/__pycache__/", | |
| "lib/mypkg/support/support_file.py", | |
| "lib/mypkg/support/more_support.py", | |
| "lib/mypkg/support/__pycache__/", | |
| "lib/random_other_place/", | |
| ] | |
| ) | |
| will_remove, will_skip = compress_for_output_listing(sample) | |
| will_rename = compress_for_rename(sample) | |
| assert sorted(expected_skip) == sorted(compact(will_skip)) | |
| assert sorted(expected_remove) == sorted(compact(will_remove)) | |
| assert sorted(expected_rename) == sorted(compact(will_rename)) | |
| def test_compact_keeps_minimal_parent_paths(tmpdir: Path) -> None: | |
| parent = os.path.join(tmpdir, "pkg") | |
| nested = os.path.join(parent, "module.py") | |
| deeper = os.path.join(parent, "subpkg", "module.py") | |
| sibling = os.path.join(tmpdir, "pkg-extra", "module.py") | |
| assert compact([nested, deeper, sibling, parent]) == {parent, sibling} | |
| def test_compact_keeps_literal_trailing_star_paths(tmpdir: Path) -> None: | |
| # A trailing star is only special for display wildcards generated by pip. | |
| # A real POSIX filename ending with "*" must still be kept. | |
| plain = os.path.join(tmpdir, "pkg") | |
| star = os.path.join(tmpdir, "pkg*") | |
| assert compact([plain, star]) == {plain, star} | |
| class TestUninstallPathSet: | |
| def test_blocked_first_move_preserves_original( | |
| self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch | |
| ) -> None: | |
| """Discard an unused stash without removing the original installation.""" | |
| metadata = tmp_path / "sample-1.0.dist-info" | |
| metadata.mkdir() | |
| (metadata / "METADATA").write_text("Name: sample\nVersion: 1.0\n") | |
| dist = get_environment([str(tmp_path)]).get_distribution("sample") | |
| assert dist is not None | |
| path = tmp_path / "data.txt" | |
| path.write_text("original") | |
| pathset = UninstallPathSet(dist) | |
| monkeypatch.setattr(UninstallPathSet, "_permitted", mock_permitted) | |
| pathset.add(str(path)) | |
| monkeypatch.setattr(os, "rename", Mock(side_effect=BlockedImportError("cold"))) | |
| with pytest.raises(BlockedImportError): | |
| pathset.remove(auto_confirm=True) | |
| assert path.read_text() == "original" | |
| assert not pathset.can_rollback | |
| assert not pathset._moved_paths._save_dirs | |
| @pytest.mark.parametrize( | |
| "error", [OSError("cannot delete"), BlockedImportError("cold")] | |
| ) | |
| def test_failed_first_move_retains_backup( | |
| self, | |
| tmp_path: Path, | |
| monkeypatch: pytest.MonkeyPatch, | |
| caplog: pytest.LogCaptureFixture, | |
| error: Exception, | |
| ) -> None: | |
| caplog.set_level("ERROR") | |
| metadata = tmp_path / "sample-1.0.dist-info" | |
| metadata.mkdir() | |
| (metadata / "METADATA").write_text("Name: sample\nVersion: 1.0\n") | |
| dist = get_environment([str(tmp_path)]).get_distribution("sample") | |
| assert dist is not None | |
| source = tmp_path / "package" | |
| source.mkdir() | |
| pathset = UninstallPathSet(dist) | |
| monkeypatch.setattr(UninstallPathSet, "_permitted", mock_permitted) | |
| for name in ("first", "second"): | |
| path = source / f"{name}.txt" | |
| path.write_text(name) | |
| pathset.add(str(path)) | |
| def incomplete_removal(path: str) -> None: | |
| (Path(path) / "first.txt").unlink() | |
| raise error | |
| with monkeypatch.context() as patch: | |
| patch.setattr(os, "rename", Mock(side_effect=OSError("cross-device"))) | |
| patch.setattr(shutil, "rmtree", incomplete_removal) | |
| with pytest.raises(type(error)): | |
| pathset.remove(auto_confirm=True) | |
| ((original, saved),) = pathset._moved_paths.recovery_paths | |
| backup = Path(saved) | |
| assert Path(original) == source | |
| assert not pathset.can_rollback | |
| assert not (source / "first.txt").exists() | |
| assert (source / "second.txt").read_text() == "second" | |
| assert repr(original) in caplog.text | |
| assert repr(saved) in caplog.text | |
| assert len(caplog.records) == 1 | |
| (source / "first.txt").write_text("manually restored") | |
| pathset.rollback() | |
| pathset.commit() | |
| assert (source / "first.txt").read_text() == "manually restored" | |
| assert (backup / "first.txt").read_text() == "first" | |
| assert (backup / "second.txt").read_text() == "second" | |
| def test_manual_file_recovery_does_not_skip_pth_restoration( | |
| self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch | |
| ) -> None: | |
| metadata = tmp_path / "sample-1.0.dist-info" | |
| metadata.mkdir() | |
| (metadata / "METADATA").write_text("Name: sample\nVersion: 1.0\n") | |
| dist = get_environment([str(tmp_path)]).get_distribution("sample") | |
| assert dist is not None | |
| source = tmp_path / "file.txt" | |
| source.write_text("original") | |
| pth = tmp_path / "example.pth" | |
| pth.write_bytes(b"keep\nremove\n") | |
| pathset = UninstallPathSet(dist) | |
| monkeypatch.setattr(UninstallPathSet, "_permitted", mock_permitted) | |
| pathset.add(str(source)) | |
| pathset.add_pth(str(pth), "remove") | |
| pathset.remove(auto_confirm=True) | |
| assert pth.read_bytes() == b"keep\n" | |
| with monkeypatch.context() as patch: | |
| patch.setattr(os, "rename", Mock(side_effect=BlockedImportError("cold"))) | |
| with pytest.raises(BlockedImportError): | |
| pathset.rollback() | |
| ((original, backup),) = pathset._moved_paths.recovery_paths | |
| assert pth.read_bytes() == b"keep\n" | |
| pathset.rollback() | |
| pathset.commit() | |
| assert pth.read_bytes() == b"keep\nremove\n" | |
| assert not Path(original).exists() | |
| assert Path(backup).read_text() == "original" | |
| def test_add(self, tmpdir: Path, monkeypatch: pytest.MonkeyPatch) -> None: | |
| monkeypatch.setattr( | |
| pip._internal.req.req_uninstall.UninstallPathSet, | |
| "_permitted", | |
| mock_permitted, | |
| ) | |
| # Fix case for windows tests | |
| file_extant = os.path.normcase(os.path.join(tmpdir, "foo")) | |
| file_nonexistent = os.path.normcase(os.path.join(tmpdir, "nonexistent")) | |
| with open(file_extant, "w"): | |
| pass | |
| ups = UninstallPathSet(dist=Mock()) | |
| assert ups._paths == set() | |
| ups.add(file_extant) | |
| assert ups._paths == {file_extant} | |
| ups.add(file_nonexistent) | |
| assert ups._paths == {file_extant} | |
| def test_add_pth(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: | |
| monkeypatch.setattr( | |
| pip._internal.req.req_uninstall.UninstallPathSet, | |
| "_permitted", | |
| mock_permitted, | |
| ) | |
| # Fix case for windows tests | |
| tmpdir = os.path.normcase(tmp_path) | |
| on_windows = sys.platform == "win32" | |
| pth_file = os.path.join(tmpdir, "foo.pth") | |
| relative = "../../example" | |
| if on_windows: | |
| share = "\\\\example\\share\\" | |
| share_com = "\\\\example.com\\share\\" | |
| # Create a .pth file for testing | |
| with open(pth_file, "w") as f: | |
| f.writelines([tmpdir, "\n", relative, "\n"]) | |
| if on_windows: | |
| f.writelines([share, "\n", share_com, "\n"]) | |
| # Add paths to be removed | |
| pth = UninstallPthEntries(pth_file) | |
| pth.add(tmpdir) | |
| pth.add(relative) | |
| if on_windows: | |
| pth.add(share) | |
| pth.add(share_com) | |
| # Check that the paths were added to entries | |
| if on_windows: | |
| check = {tmpdir, relative, share, share_com} | |
| else: | |
| check = {tmpdir, relative} | |
| assert pth.entries == check | |
| @pytest.mark.skipif("sys.platform == 'win32'") | |
| def test_add_symlink(self, tmpdir: Path, monkeypatch: pytest.MonkeyPatch) -> None: | |
| monkeypatch.setattr( | |
| pip._internal.req.req_uninstall.UninstallPathSet, | |
| "_permitted", | |
| mock_permitted, | |
| ) | |
| f = os.path.join(tmpdir, "foo") | |
| with open(f, "w"): | |
| pass | |
| foo_link = os.path.join(tmpdir, "foo_link") | |
| os.symlink(f, foo_link) | |
| ups = UninstallPathSet(dist=Mock()) | |
| ups.add(foo_link) | |
| assert ups._paths == {foo_link} | |
| def test_compact_shorter_path(self, monkeypatch: pytest.MonkeyPatch) -> None: | |
| monkeypatch.setattr( | |
| pip._internal.req.req_uninstall.UninstallPathSet, | |
| "_permitted", | |
| mock_permitted, | |
| ) | |
| monkeypatch.setattr("os.path.exists", lambda p: True) | |
| # This deals with nt/posix path differences | |
| short_path = os.path.normcase( | |
| os.path.abspath(os.path.join(os.path.sep, "path")) | |
| ) | |
| ups = UninstallPathSet(dist=Mock()) | |
| ups.add(short_path) | |
| ups.add(os.path.join(short_path, "longer")) | |
| assert compact(ups._paths) == {short_path} | |
| @pytest.mark.skipif("sys.platform == 'win32'") | |
| def test_detect_symlink_dirs( | |
| self, monkeypatch: pytest.MonkeyPatch, tmpdir: Path | |
| ) -> None: | |
| monkeypatch.setattr( | |
| pip._internal.req.req_uninstall.UninstallPathSet, | |
| "_permitted", | |
| mock_permitted, | |
| ) | |
| # construct 2 paths: | |
| # tmpdir/dir/file | |
| # tmpdir/dirlink/file (where dirlink is a link to dir) | |
| d = tmpdir.joinpath("dir") | |
| d.mkdir() | |
| dlink = tmpdir.joinpath("dirlink") | |
| os.symlink(d, dlink) | |
| d.joinpath("file").touch() | |
| path1 = str(d.joinpath("file")) | |
| path2 = str(dlink.joinpath("file")) | |
| ups = UninstallPathSet(dist=Mock()) | |
| ups.add(path1) | |
| ups.add(path2) | |
| assert ups._paths == {path1} | |
| @pytest.mark.parametrize( | |
| "original, remaining", | |
| [ | |
| (b"keep\nremove\n", b"keep\n"), | |
| (b"keep\nremove", b"keep\n"), | |
| (b"keep\r\nremove\r\n", b"keep\r\n"), | |
| (b"keep\r\nremove", b"keep\r\n"), | |
| ], | |
| ) | |
| def test_pth_rollback_restores_original( | |
| tmp_path: Path, original: bytes, remaining: bytes | |
| ) -> None: | |
| path = tmp_path / "sample.pth" | |
| path.write_bytes(original) | |
| entries = UninstallPthEntries(str(path)) | |
| entries.add("remove") | |
| entries.remove() | |
| assert path.read_bytes() == remaining | |
| assert entries.rollback() | |
| assert path.read_bytes() == original | |
| @pytest.mark.parametrize( | |
| "error", [OSError("cannot restore"), BlockedImportError("cold import")] | |
| ) | |
| def test_pth_failed_rollback_reports_path( | |
| tmp_path: Path, | |
| monkeypatch: pytest.MonkeyPatch, | |
| caplog: pytest.LogCaptureFixture, | |
| error: Exception, | |
| ) -> None: | |
| caplog.set_level("ERROR") | |
| path = tmp_path / "sample.pth" | |
| original = b"keep\nremove\n" | |
| path.write_bytes(original) | |
| entries = UninstallPthEntries(str(path)) | |
| entries.add("remove") | |
| entries.remove() | |
| with monkeypatch.context() as patch: | |
| patch.setattr( | |
| pip._internal.req.req_uninstall, | |
| "open", | |
| Mock(side_effect=error), | |
| raising=False, | |
| ) | |
| with pytest.raises(type(error)) as caught: | |
| entries.rollback() | |
| assert caught.value is error | |
| assert path.read_bytes() == b"keep\n" | |
| assert len(caplog.records) == 1 | |
| assert repr(str(path)) in caplog.text | |
| caplog.clear() | |
| assert entries.rollback() | |
| assert path.read_bytes() == original | |
| assert not caplog.records | |
| class TestStashedUninstallPathSet: | |
| WALK_RESULT: list[tuple[str, list[str], list[str]]] = [ | |
| ("A", ["B", "C"], ["a.py"]), | |
| ("A/B", ["D"], ["b.py"]), | |
| ("A/B/D", [], ["c.py"]), | |
| ("A/C", [], ["d.py", "e.py"]), | |
| ("A/E", ["F"], ["f.py"]), | |
| ("A/E/F", [], []), | |
| ("A/G", ["H"], ["g.py"]), | |
| ("A/G/H", [], ["h.py"]), | |
| ] | |
| @classmethod | |
| def mock_walk(cls, root: str) -> Iterator[tuple[str, list[str], list[str]]]: | |
| for dirname, subdirs, files in cls.WALK_RESULT: | |
| dirname = os.path.sep.join(dirname.split("/")) | |
| if dirname.startswith(root): | |
| yield dirname[len(root) + 1 :], subdirs, files | |
| def test_compress_for_rename(self, monkeypatch: pytest.MonkeyPatch) -> None: | |
| paths = [ | |
| os.path.sep.join(p.split("/")) | |
| for p in [ | |
| "A/B/b.py", | |
| "A/B/D/c.py", | |
| "A/C/d.py", | |
| "A/E/f.py", | |
| "A/G/g.py", | |
| ] | |
| ] | |
| expected_paths = [ | |
| os.path.sep.join(p.split("/")) | |
| for p in [ | |
| "A/B/", # selected everything below A/B | |
| "A/C/d.py", # did not select everything below A/C | |
| "A/E/", # only empty folders remain under A/E | |
| "A/G/g.py", # non-empty folder remains under A/G | |
| ] | |
| ] | |
| monkeypatch.setattr("os.walk", self.mock_walk) | |
| actual_paths = compress_for_rename(paths) | |
| assert set(expected_paths) == set(actual_paths) | |
| @classmethod | |
| def make_stash( | |
| cls, tmpdir: Path, paths: list[str] | |
| ) -> tuple[StashedUninstallPathSet, list[tuple[str, str]]]: | |
| for dirname, subdirs, files in cls.WALK_RESULT: | |
| root = os.path.join(tmpdir, *dirname.split("/")) | |
| if not os.path.exists(root): | |
| os.mkdir(root) | |
| for d in subdirs: | |
| os.mkdir(os.path.join(root, d)) | |
| for f in files: | |
| with open(os.path.join(root, f), "wb"): | |
| pass | |
| pathset = StashedUninstallPathSet() | |
| paths = [os.path.join(tmpdir, *p.split("/")) for p in paths] | |
| stashed_paths = [(p, pathset.stash(p)) for p in paths] | |
| return pathset, stashed_paths | |
| def test_stash(self, tmpdir: Path) -> None: | |
| pathset, stashed_paths = self.make_stash( | |
| tmpdir, | |
| [ | |
| "A/B/", | |
| "A/C/d.py", | |
| "A/E/", | |
| "A/G/g.py", | |
| ], | |
| ) | |
| for old_path, new_path in stashed_paths: | |
| assert not os.path.exists(old_path) | |
| assert os.path.exists(new_path) | |
| assert stashed_paths == pathset._moves | |
| @pytest.mark.parametrize( | |
| "kind", | |
| [ | |
| "file", | |
| "directory", | |
| pytest.param( | |
| "symlink", marks=pytest.mark.skipif("sys.platform == 'win32'") | |
| ), | |
| ], | |
| ) | |
| def test_stash_across_filesystems( | |
| self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, kind: str | |
| ) -> None: | |
| source = tmp_path / "source" | |
| if kind == "directory": | |
| source.mkdir() | |
| contents = source / "contents.txt" | |
| elif kind == "symlink": | |
| contents = tmp_path / "target.txt" | |
| source.symlink_to(contents) | |
| else: | |
| contents = source | |
| contents.write_text("original") | |
| pathset = StashedUninstallPathSet() | |
| with monkeypatch.context() as patch: | |
| patch.setattr(os, "rename", Mock(side_effect=OSError("cross-device"))) | |
| stashed = Path(pathset.stash(str(source))) | |
| assert not source.exists() | |
| assert stashed.is_symlink() == (kind == "symlink") | |
| stashed_contents = stashed / "contents.txt" if kind == "directory" else stashed | |
| assert stashed_contents.read_text() == "original" | |
| with monkeypatch.context() as patch: | |
| patch.setattr(os, "rename", Mock(side_effect=OSError("cross-device"))) | |
| pathset.rollback() | |
| assert not pathset.can_rollback | |
| assert source.is_symlink() == (kind == "symlink") | |
| assert contents.read_text() == "original" | |
| assert not stashed.exists() | |
| def test_commit(self, tmpdir: Path) -> None: | |
| pathset, stashed_paths = self.make_stash( | |
| tmpdir, | |
| [ | |
| "A/B/", | |
| "A/C/d.py", | |
| "A/E/", | |
| "A/G/g.py", | |
| ], | |
| ) | |
| pathset.commit() | |
| for old_path, new_path in stashed_paths: | |
| assert not os.path.exists(old_path) | |
| assert not os.path.exists(new_path) | |
| def test_rollback(self, tmpdir: Path, monkeypatch: pytest.MonkeyPatch) -> None: | |
| pathset, stashed_paths = self.make_stash( | |
| tmpdir, | |
| [ | |
| "A/B/", | |
| "A/C/d.py", | |
| "A/E/", | |
| "A/G/g.py", | |
| ], | |
| ) | |
| original_rename = os.rename | |
| def rename(src: str, dst: str) -> None: | |
| # Windows cannot rename a directory over an existing directory. | |
| if os.path.lexists(dst): | |
| raise FileExistsError(dst) | |
| original_rename(src, dst) | |
| monkeypatch.setattr(os, "rename", rename) | |
| pathset.rollback() | |
| assert not pathset.can_rollback | |
| for old_path, new_path in stashed_paths: | |
| assert os.path.exists(old_path) | |
| assert not os.path.exists(new_path) | |
| @pytest.mark.parametrize("failure", ["os", "import", "mixed"]) | |
| def test_failed_rollback_keeps_pending_backups( | |
| self, | |
| tmp_path: Path, | |
| monkeypatch: pytest.MonkeyPatch, | |
| caplog: pytest.LogCaptureFixture, | |
| failure: str, | |
| ) -> None: | |
| caplog.set_level("ERROR") | |
| metadata = tmp_path / "sample-1.0.dist-info" | |
| metadata.mkdir() | |
| (metadata / "METADATA").write_text("Name: sample\nVersion: 1.0\n") | |
| dist = get_environment([str(tmp_path)]).get_distribution("sample") | |
| assert dist is not None | |
| uninstall = UninstallPathSet(dist) | |
| pathset = uninstall._moved_paths | |
| paths = [tmp_path / f"file{index}.txt" for index in range(3)] | |
| for path in paths: | |
| path.write_text(path.name) | |
| backups = [pathset.stash(str(path)) for path in paths] | |
| errors: dict[str, Exception] = {} | |
| if failure == "mixed": | |
| errors[str(paths[0])] = OSError("cannot restore") | |
| errors[str(paths[2])] = BlockedImportError("cold import") | |
| pending = [0, 2] | |
| elif failure == "import": | |
| errors[str(paths[1])] = BlockedImportError("cold import") | |
| pending = [1, 2] | |
| else: | |
| errors[str(paths[1])] = OSError("cannot restore") | |
| pending = [1] | |
| original_rename = os.rename | |
| original_copy = shutil.copy2 | |
| def rename(src: str, dst: str) -> None: | |
| if dst in errors: | |
| raise errors[dst] | |
| original_rename(src, dst) | |
| def copy(src: str, dst: str) -> str: | |
| if dst in errors: | |
| raise errors[dst] | |
| return original_copy(src, dst) | |
| with monkeypatch.context() as patch: | |
| patch.setattr(os, "rename", rename) | |
| patch.setattr( | |
| shutil, "move", functools.partial(shutil.move, copy_function=copy) | |
| ) | |
| if failure == "os": | |
| uninstall.rollback() | |
| else: | |
| with pytest.raises(BlockedImportError, match="cold import"): | |
| uninstall.rollback() | |
| assert set(pathset.recovery_paths) == { | |
| (str(paths[index]), backups[index]) for index in pending | |
| } | |
| for index, path in enumerate(paths): | |
| if index in pending: | |
| assert Path(backups[index]).read_text() == path.name | |
| assert repr(str(path)) in caplog.text | |
| assert repr(backups[index]) in caplog.text | |
| else: | |
| assert path.read_text() == path.name | |
| assert repr(str(path)) not in caplog.text | |
| path.write_text("changed after restoration") | |
| assert len(caplog.records) == len(pending) | |
| caplog.clear() | |
| uninstall.rollback() | |
| pathset.commit() | |
| assert not pathset.can_rollback | |
| assert set(pathset.recovery_paths) == { | |
| (str(path), backup) | |
| for path, backup in zip(paths, backups) | |
| if str(path) in errors | |
| } | |
| for index, path in enumerate(paths): | |
| if str(path) in errors: | |
| assert not path.exists() | |
| assert Path(backups[index]).read_text() == path.name | |
| assert repr(str(path)) in caplog.text | |
| assert repr(backups[index]) in caplog.text | |
| continue | |
| expected = path.name if index in pending else "changed after restoration" | |
| assert path.read_text() == expected | |
| assert not os.path.exists(backups[index]) | |
| @pytest.mark.parametrize("stage", ["copy", "source_cleanup", "replacement_cleanup"]) | |
| def test_interrupted_restore_is_not_retried( | |
| self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, stage: str | |
| ) -> None: | |
| source = tmp_path / "package" | |
| source.mkdir() | |
| (source / "first.txt").write_text("first") | |
| (source / "second.txt").write_text("second") | |
| pathset = StashedUninstallPathSet() | |
| backup = Path(pathset.stash(str(source))) | |
| error = BlockedImportError("cold import") | |
| original_copy = shutil.copy2 | |
| def incomplete_copy(src: str, dst: str) -> None: | |
| original_copy(src, dst) | |
| raise error | |
| def incomplete_removal(path: str) -> None: | |
| (Path(path) / "first.txt").unlink() | |
| raise error | |
| if stage == "replacement_cleanup": | |
| source.mkdir() | |
| (source / "first.txt").write_text("replacement") | |
| with monkeypatch.context() as patch: | |
| patch.setattr(os, "rename", Mock(side_effect=OSError("cross-device"))) | |
| if stage == "copy": | |
| patch.setattr( | |
| shutil, | |
| "move", | |
| functools.partial(shutil.move, copy_function=incomplete_copy), | |
| ) | |
| elif stage == "replacement_cleanup": | |
| patch.setattr( | |
| pip._internal.req.req_uninstall, "rmtree", incomplete_removal | |
| ) | |
| else: | |
| patch.setattr(shutil, "rmtree", incomplete_removal) | |
| with pytest.raises(BlockedImportError, match="cold import"): | |
| pathset.rollback() | |
| original_files = {path.name: path.read_text() for path in source.glob("*.txt")} | |
| backup_files = {path.name: path.read_text() for path in backup.glob("*.txt")} | |
| assert original_files | backup_files == { | |
| "first.txt": "first", | |
| "second.txt": "second", | |
| } | |
| if stage == "copy": | |
| assert len(original_files) == 1 | |
| elif stage == "source_cleanup": | |
| assert len(backup_files) == 1 | |
| assert not pathset.can_rollback | |
| assert pathset.recovery_paths == ((str(source), str(backup)),) | |
| (source / "first.txt").write_text("changed after interruption") | |
| pathset.rollback() | |
| pathset.commit() | |
| assert (source / "first.txt").read_text() == "changed after interruption" | |
| assert { | |
| path.name: path.read_text() for path in backup.glob("*.txt") | |
| } == backup_files | |
| def test_rollback_missing_backup_keeps_destination(self, tmp_path: Path) -> None: | |
| original = tmp_path / "file.txt" | |
| original.write_text("original") | |
| pathset = StashedUninstallPathSet() | |
| backup = Path(pathset.stash(str(original))) | |
| backup.unlink() | |
| original.parent.mkdir(parents=True, exist_ok=True) | |
| original.write_text("replacement") | |
| pathset.rollback() | |
| assert not pathset.can_rollback | |
| assert pathset.recovery_paths == ((str(original), str(backup)),) | |
| assert original.read_text() == "replacement" | |
| pathset.commit() | |
| @pytest.mark.parametrize( | |
| "stage", ["rename", "parent_cleanup", "copy", "source_cleanup"] | |
| ) | |
| @pytest.mark.parametrize("prior_move", [False, True]) | |
| def test_blocked_stash_preserves_data( | |
| self, | |
| tmp_path: Path, | |
| monkeypatch: pytest.MonkeyPatch, | |
| stage: str, | |
| prior_move: bool, | |
| ) -> None: | |
| """Leave interrupted transfers alone while restoring completed moves.""" | |
| previous = tmp_path / "previous.txt" | |
| source = tmp_path / "package" | |
| source.mkdir() | |
| (source / "first.txt").write_text("first") | |
| (source / "second.txt").write_text("second") | |
| pathset = StashedUninstallPathSet() | |
| if prior_move: | |
| previous.write_text("previous") | |
| pathset.stash(str(previous)) | |
| error = BlockedImportError("cold import") | |
| original_copy = shutil.copy2 | |
| def incomplete_copy(src: str, dst: str) -> None: | |
| original_copy(src, dst) | |
| raise error | |
| def incomplete_removal(path: str) -> None: | |
| (Path(path) / "first.txt").unlink() | |
| raise error | |
| with monkeypatch.context() as patch: | |
| if stage == "rename": | |
| patch.setattr(os, "rename", Mock(side_effect=error)) | |
| elif stage == "parent_cleanup": | |
| patch.setattr(os, "removedirs", Mock(side_effect=error)) | |
| else: | |
| patch.setattr(os, "rename", Mock(side_effect=OSError("cross-device"))) | |
| if stage == "copy": | |
| patch.setattr( | |
| shutil, | |
| "move", | |
| functools.partial(shutil.move, copy_function=incomplete_copy), | |
| ) | |
| else: | |
| patch.setattr(shutil, "rmtree", incomplete_removal) | |
| with pytest.raises(BlockedImportError) as caught: | |
| pathset.stash(str(source)) | |
| assert caught.value is error | |
| stash_directories = [ | |
| directory.path for directory in pathset._save_dirs.values() | |
| ] | |
| if pathset.can_rollback: | |
| pathset.rollback() | |
| else: | |
| pathset.commit() | |
| pathset.rollback() | |
| pathset.commit() | |
| if prior_move: | |
| assert previous.read_text() == "previous" | |
| assert not pathset.can_rollback | |
| original_files = {path.name: path.read_text() for path in source.glob("*.txt")} | |
| if stage == "copy": | |
| assert original_files == {"first.txt": "first", "second.txt": "second"} | |
| if stage in {"copy", "source_cleanup"}: | |
| assert len(pathset.recovery_paths) == 1 | |
| original, backup = pathset.recovery_paths[0] | |
| assert original == str(source) | |
| backup_files = { | |
| path.name: path.read_text() for path in Path(backup).glob("*.txt") | |
| } | |
| assert original_files | backup_files == { | |
| "first.txt": "first", | |
| "second.txt": "second", | |
| } | |
| if stage == "copy": | |
| assert len(backup_files) == 1 | |
| assert all(os.path.exists(path) for path in stash_directories) | |
| else: | |
| assert original_files == {"first.txt": "first", "second.txt": "second"} | |
| assert not pathset.recovery_paths | |
| assert all(not os.path.exists(path) for path in stash_directories) | |
| @pytest.mark.skipif("sys.platform == 'win32'") | |
| def test_commit_symlinks(self, tmpdir: Path) -> None: | |
| adir = tmpdir / "dir" | |
| adir.mkdir() | |
| dirlink = tmpdir / "dirlink" | |
| dirlink.symlink_to(adir) | |
| afile = tmpdir / "file" | |
| afile.write_text("...") | |
| filelink = tmpdir / "filelink" | |
| filelink.symlink_to(afile) | |
| pathset = StashedUninstallPathSet() | |
| stashed_paths = [] | |
| stashed_paths.append(pathset.stash(os.fspath(dirlink))) | |
| stashed_paths.append(pathset.stash(os.fspath(filelink))) | |
| for stashed_path in stashed_paths: | |
| assert os.path.lexists(stashed_path) | |
| assert not os.path.exists(dirlink) | |
| assert not os.path.exists(filelink) | |
| pathset.commit() | |
| # stash removed, links removed | |
| for stashed_path in stashed_paths: | |
| assert not os.path.lexists(stashed_path) | |
| assert not os.path.lexists(dirlink) | |
| assert not os.path.isdir(dirlink) | |
| assert not os.path.lexists(filelink) | |
| assert not os.path.isfile(filelink) | |
| # link targets untouched | |
| assert os.path.isdir(adir) | |
| assert os.path.isfile(afile) | |
| @pytest.mark.skipif("sys.platform == 'win32'") | |
| def test_rollback_symlinks(self, tmpdir: Path) -> None: | |
| adir = tmpdir / "dir" | |
| adir.mkdir() | |
| dirlink = tmpdir / "dirlink" | |
| dirlink.symlink_to(adir) | |
| afile = tmpdir / "file" | |
| afile.write_text("...") | |
| filelink = tmpdir / "filelink" | |
| filelink.symlink_to(afile) | |
| pathset = StashedUninstallPathSet() | |
| stashed_paths = [] | |
| stashed_paths.append(pathset.stash(os.fspath(dirlink))) | |
| stashed_paths.append(pathset.stash(os.fspath(filelink))) | |
| for stashed_path in stashed_paths: | |
| assert os.path.lexists(stashed_path) | |
| assert not os.path.lexists(dirlink) | |
| assert not os.path.lexists(filelink) | |
| pathset.rollback() | |
| # stash removed, links restored | |
| for stashed_path in stashed_paths: | |
| assert not os.path.lexists(stashed_path) | |
| assert os.path.lexists(dirlink) | |
| assert os.path.isdir(dirlink) | |
| assert os.path.lexists(filelink) | |
| assert os.path.isfile(filelink) | |
| # link targets untouched | |
| assert os.path.isdir(adir) | |
| assert os.path.isfile(afile) |