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
36 changes: 36 additions & 0 deletions tests/test_loopguard_pytest_listing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""A pytest run asked only to show something runs no tests, so it is no verification.

pytest's own help says `--fixtures` (`--funcargs`), `--fixtures-per-test` and `--markers` show
what is available, `--setup-plan` shows what would run "but don't execute anything",
`--setup-only` sets up fixtures and does "not execute tests", and `--cache-show` shows the cache
and does not "perform collection or tests". Counted as a verification, such a run that exits 0
after a failing test run made the turn count as ended green."""
import pytest

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

LISTINGS = ["pytest --fixtures", "python -m pytest --funcargs", "pytest --fixtures-per-test tests",
"pytest --markers", "python3 -m pytest --setup-plan tests/test_a.py",
"pytest --setup-only", "pytest --cache-show", "pytest --cache-show=cache/lastfailed",
"uv run pytest --fixtures"]


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


@pytest.mark.parametrize("cmd", LISTINGS)
def test_it_does_not_turn_a_failed_turn_green(cmd):
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": "pytest -q"}, "FAILED tests/test_a.py::test_x - AssertionError", 1)
g.fold("Bash", {"command": cmd}, "cache -- _pytest/cacheprovider.py\n Return a cache", 0)
assert g.verifications == 1 and not g.resolved_proxy()


@pytest.mark.parametrize("cmd", ["pytest --setup-show", "pytest -q --fixtures-dir x",
"pytest tests/test_markers.py", "pytest -m slow"])
def test_runs_that_execute_tests_still_count(cmd):
assert turn.status_of_tests(cmd) == "own"
7 changes: 5 additions & 2 deletions tokeneconomizer/loopguard/turn.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@
_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 or test list, not to run the tests; and pytest asked
# only to show its fixtures, markers, setup plan or cache, or to set up fixtures, which runs none.
_NOT_A_RUN_RE = re.compile(r"\s(?:-h|--help|--version|--collect-only|--co|--list\S*|"
r"--fixtures(?:-per-test)?|--funcargs|--markers|--setup-plan|"
r"--setup-only|--cache-show(?:=\S*)?)(?!\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