Skip to content
Merged
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
38 changes: 38 additions & 0 deletions tests/test_live_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,44 @@ def test_a_non_test_command_mentioning_a_runner_is_not_a_verification(cmd):
assert g.verifications == 1 and not g.resolved_proxy()


def test_a_heredoc_writing_a_script_runs_no_tests():
assert turn.status_of_tests("cat <<EOF > ci.sh\nnpm test\nEOF") is None


@pytest.mark.parametrize("cmd", ["gh pr create --body-file - <<'EOF'\npytest -q\nEOF",
"gh issue comment 7 --body-file - <<EOF\nnpm test\nEOF"])
def test_a_heredoc_line_naming_a_runner_is_not_a_verification(cmd):
"""A heredoc's lines are the input of the command that reads them, as in
`exit_status_covers`: a body written with one runs no tests, so the failing run before it
is still the turn's last verification."""
assert turn.status_of_tests(cmd) is None
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 (failures=1)", 1)
g.fold("Bash", {"command": cmd}, "ok", 0)
assert g.verifications == 1 and not g.resolved_proxy()


@pytest.mark.parametrize("cmd", ["pytest -q <<EOF\ny\nEOF",
"cat <<EOF > ci.sh\nnpm test\nEOF\npytest -q"])
def test_a_runner_beside_a_heredoc_is_still_judged_by_its_status(cmd):
"""A runner that reads a heredoc, or runs after one, is the last command: its status is the
tests' own."""
assert turn.status_of_tests(cmd) == "own"


@pytest.mark.parametrize("cmd, want", [
("bash -lc 'pytest -q <<EOF\ny\nEOF'", "own"),
("bash -lc \"pytest -q && cat <<'EOF'\nsummary\nEOF\"", "own"),
("bash -lc \"gh pr create --body-file - <<'EOF'\npytest -q\nEOF\"", None),
])
def test_a_heredoc_that_ends_a_codex_script_is_read_inside_the_script(cmd, want):
"""Codex records `bash -lc '<script>'`. A heredoc that ends the script closes on `EOF'` there,
not on a line of its own, so its body is left out after the unwrap: the run before it still
counts, and a runner named in its body still does not."""
assert turn.status_of_tests(cmd) == want


TAP_FAILING = "# tests 9\n# suites 0\n# pass 8\n# fail 1\n# cancelled 0"


Expand Down
8 changes: 5 additions & 3 deletions tokeneconomizer/loopguard/turn.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,17 @@

def _pieces(cmd):
"""`cmd` as its commands and the separators between them (`[cmd, sep, cmd, ...]`), with
quoted text masked, and whether it sets `pipefail`. A command recorded as `bash -lc
'<script>'` (Codex) is split by its script."""
quoted text masked and heredoc bodies left out, and whether it sets `pipefail`. A command
recorded as `bash -lc '<script>'` (Codex) is split by its script: its heredocs are read
there, since one that ends the script closes on `EOF'`, not on a line of its own."""
try:
parts = shlex.split(cmd)
except ValueError:
parts = []
if (len(parts) >= 3 and os.path.basename(parts[0]) in _SHELLS
and parts[1].startswith("-") and "c" in parts[1]):
cmd = parts[2]
cmd = _split_heredocs(cmd)[0] # a heredoc's lines are input, not commands
pieces = _SEP_RE.split(re.sub(r"[(){}]", " ", _QUOTED_RE.sub("Q", cmd)))
while len(pieces) > 1 and not pieces[-1].strip(): # a trailing `;` or newline
del pieces[-2:]
Expand Down Expand Up @@ -123,7 +125,7 @@ def exit_status_covers(cmd):
lines are the input of the command that reads them, not commands of their own."""
if not isinstance(cmd, str) or not cmd.strip():
return False
pieces, pipefail = _pieces(_split_heredocs(cmd)[0])
pieces, pipefail = _pieces(cmd)
for i in range(0, len(pieces) - 1, 2): # every command but the last
sep = pieces[i + 1]
if sep == "&&" or (pipefail and sep in ("|", "|&")):
Expand Down
Loading