From 765e98c04e33f88859e414cc4210fa0c600458b3 Mon Sep 17 00:00:00 2001 From: Lanre Shittu <136805224+Shizoqua@users.noreply.github.com> Date: Sat, 21 Nov 2026 08:32:27 +0000 Subject: [PATCH] loopguard: count hatch test as a test run Hatch 1.10 and later run a project's tests with `hatch test`: pytest in Hatch's own test environment (`src/hatch/cli/test/__init__.py`). The loop guard read `hatch run pytest` as a test run but not `hatch test`, so a failing `hatch test` after the last edit left the turn counted as ended green. `hatch test` is now a verification, with its options and any path. With `-s`/`--show` it only shows the environments and runs nothing, so it is still not a test run, also when the flag is combined with others (`-ps`); after `--` the flags are pytest's, so `hatch test -- -s` counts. The alternative is added at the end of `VERIFY_RE`, apart from the lines other changes edit. --- tests/test_loopguard_hatch_test.py | 29 +++++++++++++++++++++++++++++ tokeneconomizer/loopguard/turn.py | 7 ++++++- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 tests/test_loopguard_hatch_test.py diff --git a/tests/test_loopguard_hatch_test.py b/tests/test_loopguard_hatch_test.py new file mode 100644 index 00000000..5045ba8b --- /dev/null +++ b/tests/test_loopguard_hatch_test.py @@ -0,0 +1,29 @@ +"""`hatch test` is a test run. + +Hatch 1.10 and later run the project's tests with `hatch test`, pytest in Hatch's own test +environment (`src/hatch/cli/test/__init__.py`). The loop guard read `hatch run pytest` as a test +run but not `hatch test`. Its `-s`/`--show` only shows the environments and runs nothing; click +lets short flags combine (`-ps`), and what follows `--` goes to pytest (`-- -s` is pytest's).""" +import pytest + +from tokeneconomizer import loopguard as guard +from tokeneconomizer.loopguard import turn + + +@pytest.mark.parametrize("cmd", ["hatch test", "hatch test tests/test_a.py", "hatch test -c", + "hatch test --cover -p", "hatch test -py 3.12", + "hatch test -i py=3.11 -x feature=slow", "hatch test --all", + "hatch test -- -s -k slow", "hatch test --retries 2 -r"]) +def test_hatch_test_counts_as_a_verification(cmd): + assert turn.status_of_tests(cmd) == "own" + g = guard.TurnGuard(turn_key="t") + g.fold("Edit", {"file_path": "/r/a.py", "old_string": "x", "new_string": "y"}, "ok") + g.fold("Bash", {"command": cmd}, "FAILED tests/test_a.py::test_x - AssertionError", 1) + assert g.verifications == 1 and not g.resolved_proxy() + + +@pytest.mark.parametrize("cmd", ["hatch test --show", "hatch test -s", "hatch test -ps", + "hatch test --all --show", "hatch test --help", + "hatch tests", "hatch build", "hatch env show"]) +def test_a_hatch_command_that_runs_no_tests_is_not_one(cmd): + assert turn.status_of_tests(cmd) is None diff --git a/tokeneconomizer/loopguard/turn.py b/tokeneconomizer/loopguard/turn.py index 74752985..a4c1ff10 100644 --- a/tokeneconomizer/loopguard/turn.py +++ b/tokeneconomizer/loopguard/turn.py @@ -73,7 +73,12 @@ r"ruby(?-i:\s+(?:-[Ir](?:\S+|\s+[^\s\-]\S*)|-C\s*\S+|-W[0-2]?(?::\S+)?|" r"--[\w\-]+(?:=\S+)?|-[abdf-zABDF-Z]+))*\s+" r"(?:[\w.\-~]*/)*(?:test_[\w.\-]*|[\w.\-]*_(?:test|spec))\.rb|" - r"deno\s+test|node\s+--test|bazel\s+test|(?:\./)?bin/test)(?![\w.\-/])") + r"deno\s+test|node\s+--test|bazel\s+test|(?:\./)?bin/test|" + # `hatch test` (Hatch 1.10+: pytest in its test environment), but not with `-s`/`--show`, + # which only shows the environments; short flags combine (`-ps`), and after `--` they are + # pytest's (`hatch test -- -s` runs the tests) + r"hatch\s+test(?!(?:\s+(?!--(?:\s|$))\S+)*?\s+(?:--show|-[rpca]*s[rpca]*)(?:\s|$)))" + r"(?![\w.\-/])") # A command line's exit status is its LAST command's, so it may not be the test run's: `npm test # 2>&1 | tail -n 8` exits with `tail`'s status, `pytest | grep FAILED` with `grep`'s, `python -m