diff --git a/tests/test_live_core.py b/tests/test_live_core.py index 0c496d4..6fd2dd2 100644 --- a/tests/test_live_core.py +++ b/tests/test_live_core.py @@ -1490,6 +1490,30 @@ def test_env_and_nice_leave_what_they_run_to_decide(cmd, status): assert turn.status_of_tests(cmd) == status +@pytest.mark.parametrize("cmd", ["sudo pytest -q", "sudo -u bob python -m pytest tests", + "sudo -E npm test", "sudo -Eu bob go test ./...", + "sudo --user=bob cargo test", "sudo env -u CI pytest", + "time -p pytest", "time -o /tmp/t.txt npm test", "exec pytest", + "exec -a runner pytest", "command -p pytest", + "nice --adjustment 5 pytest"]) +def test_a_runner_behind_sudo_time_or_exec_counts_as_a_verification(cmd): + """`sudo`, `time -p` and `exec` run the command after them, as `env` and `nice` do. They + were not passed over, so the run was no verification and a failing one after the last edit + left the turn green.""" + 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 (failures=1)", 1) + assert g.verifications == 1 and not g.resolved_proxy() + + +@pytest.mark.parametrize("cmd,status", [("command -v pytest", None), ("sudo make", None), + ("sudo -u bob pytest --collect-only", None), + ("time -p echo pytest", None), ("sudo -l", None)]) +def test_sudo_time_and_command_leave_what_they_run_to_decide(cmd, status): + assert turn.status_of_tests(cmd) == status + + @pytest.mark.parametrize("cmd", ["python -W error -m pytest -q", "python3 -Werror -m pytest", "python -X dev -m pytest tests", "python3.12 -Xfaulthandler -m pytest", diff --git a/tokeneconomizer/loopguard/turn.py b/tokeneconomizer/loopguard/turn.py index 5495c89..95af9cb 100644 --- a/tokeneconomizer/loopguard/turn.py +++ b/tokeneconomizer/loopguard/turn.py @@ -68,10 +68,19 @@ # 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)") # What may precede the command itself: `FOO=1 timeout 60 npm test`, `env -u VAR`, `nice -n 10`, -# and a launcher that runs it in the project's environment, `uv run pytest`, `npx jest`, -# `bundle exec rspec`. -_PREFIX_RE = re.compile(r"^\s*(?:(?:\w+=\S*|time|nohup|command|" - r"nice(?:\s+(?:-n\s*|--adjustment=)?[-+]?\d+)?|" +# `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 +# `command -p` runs it. +_PREFIX_RE = re.compile(r"^\s*(?:(?:\w+=\S*|nohup|command(?:\s+-p)?|" + # time's options; `-f` and `-o` take a value + r"time(?:\s+(?:-[fo]\s*[^\s-]\S*|--(?:format|output)(?:=|\s+)\S+|" + r"-[pvaq]+|--(?:portability|verbose|append|quiet)))*|" + r"exec(?:\s+(?:-a\s*[^\s-]\S*|-[cl]+))*|" + # sudo's options; these letters and long forms take a value + r"sudo(?:\s+(?:-\w*[ugpCDRrtTU]\s*[^\s-]\S*|--(?:user|group|prompt|" + r"close-from|chdir|chroot|role|type|command-timeout|other-user)" + r"(?:=|\s+)\S+|-[-\w]*))*|" + r"nice(?:\s+(?:-n\s*|--adjustment(?:=|\s+))?[-+]?\d+)?|" # env's options; `-u`, `-C` and their long forms take a value r"env(?:\s+(?:(?:-[uC]|--unset|--chdir)(?:=|\s*)[^\s-]\S*|-[-\w]*))*|" r"timeout(?:\s+(?:-\S+|[A-Z]+|\d\S*))+|"