Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion scripts/ci/prune_merged_worktrees.py
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,21 @@ def classify_worktree_lock(reason: Optional[str]) -> tuple[Optional[str], Option
return ("optout", None)


def _on_windows() -> bool:
"""True sous Windows.

Une indirection de module plutot qu'un test `os.name` ecrit au point
d'appel : patcher `os.name` est une mutation *globale au process*. Tant
qu'elle est active sur un hote POSIX, tout `Path(...)` leve
`NotImplementedError: cannot instantiate 'WindowsPath'` -- y compris celui
que pytest appelle pour formater le rapport d'un test en echec. Le worker
xdist meurt alors sur un rapport qu'il ne sait pas ecrire, et l'echec reel
n'est jamais montre (#23353, ou ce patch a coute le diagnostic entier).
Les tests patchent cette fonction, pas la plateforme.
"""
return os.name == "nt"


def pid_is_alive(pid: int) -> bool:
"""Le pid est-il vivant ? Fail-safe : indeterminable = vivant.

Expand All @@ -1122,7 +1137,7 @@ def pid_is_alive(pid: int) -> bool:
verrouille un cycle de trop qu'un deverrouillage sur une mesure
impossible.
"""
if os.name == "nt":
if _on_windows():
try:
out = subprocess.run(
["tasklist", "/FI", f"PID eq {pid}", "/FO", "CSV", "/NH"],
Expand Down
14 changes: 7 additions & 7 deletions scripts/tests/test_prune_merged_worktrees.py
Original file line number Diff line number Diff line change
Expand Up @@ -3606,15 +3606,15 @@ def test_list_worktrees_parses_locked_reason(self, monkeypatch):
# --- pid_is_alive : fail-safe ---

def test_pid_is_alive_windows_found(self, monkeypatch):
monkeypatch.setattr(pmw.os, "name", "nt", raising=True)
monkeypatch.setattr(pmw, "_on_windows", lambda: True)
monkeypatch.setattr(
pmw.subprocess, "run",
lambda *a, **k: _fake_proc(0, '"python.exe","31337"'),
)
assert pmw.pid_is_alive(31337) is True

def test_pid_is_alive_windows_not_found(self, monkeypatch):
monkeypatch.setattr(pmw.os, "name", "nt", raising=True)
monkeypatch.setattr(pmw, "_on_windows", lambda: True)
monkeypatch.setattr(
pmw.subprocess, "run",
lambda *a, **k: _fake_proc(0, "INFO: No tasks are running"),
Expand All @@ -3626,7 +3626,7 @@ def test_pid_is_alive_windows_pid_in_other_column_is_not_alive(
):
# Le pid cherche n'apparait que dans une AUTRE colonne de la ligne
# d'un autre processus : un test de sous-chaine le lirait vivant.
monkeypatch.setattr(pmw.os, "name", "nt", raising=True)
monkeypatch.setattr(pmw, "_on_windows", lambda: True)
monkeypatch.setattr(
pmw.subprocess, "run",
lambda *a, **k: _fake_proc(0, '"python.exe","4242","Console","1","31337 K"'),
Expand All @@ -3636,7 +3636,7 @@ def test_pid_is_alive_windows_pid_in_other_column_is_not_alive(
def test_pid_is_alive_windows_failure_is_alive(self, monkeypatch):
# Fail-safe : tasklist injoignable = pid vivant -- on ne
# deverrouille jamais sur une mesure impossible.
monkeypatch.setattr(pmw.os, "name", "nt", raising=True)
monkeypatch.setattr(pmw, "_on_windows", lambda: True)

def _boom(*a, **k):
raise OSError("tasklist absent")
Expand All @@ -3646,7 +3646,7 @@ def _boom(*a, **k):

@pytest.mark.parametrize("stdout", ["", "ERROR: The remote procedure call failed."])
def test_pid_is_alive_windows_nonzero_exit_is_alive(self, monkeypatch, stdout):
monkeypatch.setattr(pmw.os, "name", "nt", raising=True)
monkeypatch.setattr(pmw, "_on_windows", lambda: True)
monkeypatch.setattr(
pmw.subprocess, "run",
lambda *a, **k: _fake_proc(1, stdout),
Expand All @@ -3660,7 +3660,7 @@ def test_pid_is_alive_windows_nonzero_exit_is_alive(self, monkeypatch, stdout):
assert status.dead_lock_unlocked is False

def test_pid_is_alive_posix_lookup_error(self, monkeypatch):
monkeypatch.setattr(pmw.os, "name", "posix", raising=True)
monkeypatch.setattr(pmw, "_on_windows", lambda: False)

def _lookup(pid, sig):
raise ProcessLookupError()
Expand All @@ -3669,7 +3669,7 @@ def _lookup(pid, sig):
assert pmw.pid_is_alive(31337) is False

def test_pid_is_alive_posix_permission_is_alive(self, monkeypatch):
monkeypatch.setattr(pmw.os, "name", "posix", raising=True)
monkeypatch.setattr(pmw, "_on_windows", lambda: False)

def _denied(pid, sig):
raise PermissionError()
Expand Down
Loading