Skip to content

The agent that grows with you

test(pm): pin read-only file removal on a real Windows runner

teknium1d00d57b70f04
diff --git a/pm/install.py b/pm/install.py
index f1b8869da03..3b45415c22d 100644
--- a/pm/install.py
+++ b/pm/install.py
@@ -233,10 +233,8 @@ def _remove_entry(store: Store, entry_name: str, *, attempts: int = 5) -> None:
if entry.is_symlink() or not entry.is_dir():
entry.unlink(missing_ok=True)
else:
- # PortableGit ships etc/hosts read-only, and Windows refuses to unlink
- # a read-only file, so a bare rmtree raised WinError 5 and aborted the
- # whole update ("install failed: [WinError 5] Access is denied"). The
- # retry loop cannot help: the read-only bit is not a transient hold.
+ # Windows refuses to unlink read-only files (PortableGit ships etc/hosts
+ # read-only); no retry clears that bit, so the tree could never be removed.
rmtree_force(entry)
return
except FileNotFoundError:
diff --git a/tests/pm/test_held_entry_windows_live.py b/tests/pm/test_held_entry_windows_live.py
index f58576f324a..38b6746f611 100644
--- a/tests/pm/test_held_entry_windows_live.py
+++ b/tests/pm/test_held_entry_windows_live.py
@@ -1,10 +1,8 @@
-"""Live Windows hold on a replaced PM entry (#124807).
+"""Live Windows obstacles to removing a replaced PM entry.
-A running Hermes process keeps the replaced interpreter's DLLs mapped, so
-deleting the old entry fails with WinError 5 long after the new one is
-published. This maps a real DLL from inside the live entry, reinstalls over
-it, and requires the install to succeed and the tree to be reclaimed once
-the image is unmapped. No modelled hold: the failure comes from the OS.
+A running Hermes process keeps the replaced interpreter's DLLs mapped (#124807),
+and archives such as PortableGit ship read-only files; both make deleting the
+old entry fail with WinError 5. The OS produces each obstacle here, no model.
"""
from __future__ import annotations
@@ -50,3 +48,23 @@ def test_mapped_dll_in_replaced_entry_does_not_fail_reinstall(pm_env): # noqa:
kernel32.FreeLibrary(handle)
cmd_gc(None)
assert not list(runtime.glob(".reclaim-*"))
+
+
+def test_read_only_file_in_replaced_entry_is_removed_on_reinstall(pm_env): # noqa: F811
+ import os
+ import stat
+
+ from pm.install import ensure
+
+ _, runtime, *_ = pm_env
+ ensure("faketool", base_env={})
+ entry = runtime / Facts(runtime / "facts.json").get("faketool")["entry"]
+ hosts = entry / "etc" / "hosts"
+ hosts.parent.mkdir()
+ hosts.write_bytes(b"127.0.0.1 localhost")
+ os.chmod(hosts, stat.S_IREAD)
+ (entry / "bin/faketool").unlink()
+ ensure("faketool", base_env={})
+ assert not (runtime / f".previous-{entry.name}").exists()
+ assert not list(runtime.glob(".reclaim-*")), "a read-only file must not strand the replaced tree"