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
53 changes: 53 additions & 0 deletions tests/test_loopguard_dry_run_and_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""A test runner asked only to list tests, show config, or compile/dry-run is no verification.

Commands like `cargo test --no-run`, `go test -list <pattern>`, `ctest --show-only`,
`jest --showConfig`, or `playwright test --dry-run` do not execute any tests.
If counted as a verification, such a run exiting 0 after a failing test would falsely mark
a failed turn as ended green in the envelope tracker.
"""
import pytest

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

NON_RUNS = [
"cargo test --no-run",
"cargo test --no-run tests/test_a.rs",
"go test -list TestFoo",
"go test -list=TestFoo ./...",
"ctest --show-only",
"ctest --show-only=json-v1",
"jest --showConfig",
"vitest --show-config",
"playwright test --dry-run",
"npx playwright test --dry-run",
]


@pytest.mark.parametrize("cmd", NON_RUNS)
def test_commands_that_only_list_or_dry_run_are_no_verification(cmd):
assert turn.status_of_tests(cmd) is None


@pytest.mark.parametrize("cmd", NON_RUNS)
def test_they_do_not_turn_a_failed_turn_green(cmd):
g = guard.TurnGuard(turn_key="t")
g.fold("Edit", {"file_path": "/r/src/lib.rs", "old_string": "x", "new_string": "y"}, "ok")
g.fold("Bash", {"command": "cargo test"}, "test result: FAILED. 1 failed", 101)
g.fold("Bash", {"command": cmd}, "finished in 0.05s", 0)
assert g.verifications == 1 and not g.resolved_proxy()


@pytest.mark.parametrize("cmd", [
"cargo test",
"cargo test -- --nocapture",
"go test ./...",
"ctest",
"ctest -V",
"jest",
"vitest",
"playwright test",
"pytest -c setup.cfg",
])
def test_runs_that_execute_tests_still_count(cmd):
assert turn.status_of_tests(cmd) == "own"
6 changes: 4 additions & 2 deletions tokeneconomizer/loopguard/turn.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@
_SHELLS = {"sh", "bash", "zsh", "dash", "ksh"}
_QUOTED_RE = re.compile(r"'[^']*'|\"(?:\\.|[^\"\\])*\"")
_SEP_RE = re.compile(r"(&&|\|\||\|&|(?<!>)\||;|\n)") # `|`, but not the `>|` redirect
# A runner asked for its help, version or test list, not to run the tests.
_NOT_A_RUN_RE = re.compile(r"\s(?:-h|--help|--version|--collect-only|--co|--list\S*)(?!\S)")
# A runner asked for its help, version, dry run, or test list/config, not to run the tests.
_NOT_A_RUN_RE = re.compile(
r"(?i)\s(?:-h|--help|--version|--collect-only|--co|-{1,2}list\S*|--no-run|--dry-run|"
r"--show-only\S*|--show-?config)(?!\S)")
# What may precede the command itself: `FOO=1 timeout 60 npm test`, `env -u VAR`, `nice -n 10`,
# `sudo -u bob`, `time -p`, and a launcher that runs it in the project's environment, `uv run
# pytest`, `npx jest`, `bundle exec rspec`. `command -v pytest` looks the runner up; only
Expand Down