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
29 changes: 29 additions & 0 deletions tests/test_loopguard_hatch_test.py
Original file line number Diff line number Diff line change
@@ -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
7 changes: 6 additions & 1 deletion tokeneconomizer/loopguard/turn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down