Skip to content
Open
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
52 changes: 52 additions & 0 deletions tests/test_loopguard_windows_runs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""A test run spelled the Windows way is a verification.

On Windows a runner is named with backslashes, a drive letter and `.exe`
(`C:\\repo\\.venv\\Scripts\\python.exe -m pytest`, `.venv\\Scripts\\pytest.exe`), as Claude Code's
PowerShell tool and Codex record it. The micro-skills observer already reads these as test runs;
the loop guard did not, so a failing one after the last edit left the turn counted as ended
green. The same holds for the Windows `py` launcher, which runs the default Python (`py -m
pytest`) or the one a `-V:<tag>` or `-3.x` option chooses ("Using Python on Windows", CPython's
documentation)."""
import pytest

from tokeneconomizer import loopguard as guard
from tokeneconomizer.loopguard import turn
from tokeneconomizer.microskills import observe

RUNS = [r"C:\repo\.venv\Scripts\python.exe -m pytest tests -q",
r".venv\Scripts\python.exe -m pytest", ".venv/Scripts/python.exe -m pytest",
r".venv\Scripts\pytest.exe -q", "D:/work/.venv/Scripts/pytest.exe", "python.exe -m pytest",
r"C:\Python312\python.exe -m unittest discover", r".venv\Scripts\python.exe manage.py test",
r"cd C:\repo && .venv\Scripts\python.exe -m pytest"]


@pytest.mark.parametrize("cmd", RUNS)
def test_a_windows_test_run_counts_as_a_verification(cmd):
assert turn.status_of_tests(cmd) == "own"
g = guard.TurnGuard(turn_key="t")
g.fold("Edit", {"file_path": r"C:\repo\a.py", "old_string": "x", "new_string": "y"}, "ok")
g.fold("PowerShell", {"command": cmd}, "FAILED tests/test_a.py::test_x - AssertionError", 1)
assert g.verifications == 1 and not g.resolved_proxy()


PY_RUNS = ["py -m pytest", "py -3.12 -m pytest tests", "py -V:3.12 -m pytest -q",
"py -3 -m unittest discover", "py.exe -m pytest", r"C:\Windows\py.exe -m pytest"]


@pytest.mark.parametrize("cmd", PY_RUNS)
def test_a_test_run_through_the_py_launcher_counts_as_a_verification(cmd):
assert turn.status_of_tests(cmd) == "own"


@pytest.mark.parametrize("cmd", RUNS[:7])
def test_the_observer_and_the_loop_guard_agree(cmd):
assert "run_tests" in [o.intent for o in observe.classify_command(cmd)]
assert turn.status_of_tests(cmd) == "own"


@pytest.mark.parametrize("cmd", [r"python.exe repro.py", r".venv\Scripts\python.exe -m pip install x",
r"C:\tools\pytest-runner.exe", r"notepad.exe tests\test_a.py",
r".venv\Scripts\pytest.exe --collect-only", "py repro.py",
"py -m pip install pytest", "pyright src", "py -3.12 --version"])
def test_a_windows_command_that_runs_no_tests_is_not_one(cmd):
assert turn.status_of_tests(cmd) is None
23 changes: 22 additions & 1 deletion tokeneconomizer/loopguard/turn.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,27 @@ def exit_status_covers(cmd):
return True


# A command spelled the Windows way (`C:\repo\.venv\Scripts\python.exe -m pytest`,
# `.venv\Scripts\pytest.exe`) is read with forward slashes, no drive letter and no `.exe`, as the
# micro-skills observer reads it.
_DRIVE_RE = re.compile(r"(?<![\w/])[A-Za-z]:/")
_EXE_RE = re.compile(r"\.exe(?=\s|$)", re.I)


# The Windows `py` launcher runs the default Python, or the one a `-V:<tag>` or `-3.x` option
# chooses (`py -3.12 -m pytest`): it is read as `python`.
_PY_LAUNCHER_RE = re.compile(r"^(\s*)(?:\S*/)?py(?:\s+-(?:V:\S+|3\S*))?(?=\s)")


def _posix_spelling(stage):
return _EXE_RE.sub("", _DRIVE_RE.sub("/", stage.replace("\\", "/")))


def _as_run(stage):
"""The command a stage runs, past its prefix and in its POSIX spelling."""
return _PY_LAUNCHER_RE.sub(r"\1python", _PREFIX_RE.sub("", _posix_spelling(stage)))


def status_of_tests(cmd):
"""What `cmd`'s exit status says of the test runs in it: None when it runs none; "own" when
it is theirs (a run is last, or followed only by `&&`, or piped under `pipefail`); "hidden"
Expand All @@ -231,7 +252,7 @@ def status_of_tests(cmd):
found = None
for i in range(0, len(pieces), 2):
stage = pieces[i]
if not VERIFY_RE.match(_PREFIX_RE.sub("", stage)) or _NOT_A_RUN_RE.search(stage):
if not VERIFY_RE.match(_as_run(stage)) or _NOT_A_RUN_RE.search(stage):
continue
status = "own"
for sep, then in zip(pieces[i + 1::2], pieces[i + 2::2]):
Expand Down