Repository navigation
fix(test): exit 2 when a binary operator has no right operand - #347
ferdingler wants to merge 9 commits into
Conversation
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Verdict: approve — no blockers, three should-fix. The forms from #321 and the other claimed ones now match bash 5.2.15 (exit 2 plus an error), and the suite is green at 651b4f2. The new "words left over" check also fires on some valid expressions that the existing parser doesn't fully consume, so they go from a quiet 0/1 to exit 2 with an error.
- 🟡
x=; [ ! $x = b ]andc='!'; [ "$c" = x ](also() now print[: too many argumentsand exit 2; bash andmainexit 1. A six-line three-argument rule fixes it (inline, tested). - 🟡
[ ! -v FOO ]and[ -o errexit -a -n x ]now exit 2, though the description says-v/-R/-N/-obehave as before (inline, a question). - 🟡 Four of the new guards survive deletion with all tests green (inline).
What I verified, and non-blocking notes
Head 651b4f2, base dbb6189, bash 5.2.15, debug builds on Linux aarch64 only.
Suites (head)
cargo test --workspace --all-targets: exit 0, 2734 passed, 0 failed, 0 ignored. The new integration test ran;cargo test --test differential -- --nocapturereports 109 passed and 17 known differences, 126 cases in total including both new ones.cargo fmt --checkclean;cargo clippy --workspace --all-targetshas no warnings in the changed code.- "Failed on main" holds: with only the test changes applied to
dbb6189, both new differential cases and the new integration test fail (rc=1where 2 is expected). - Both
.expectedfiles match bash byte for byte in the README's recording environment.
Three-way differential (bash / base / head), exit status plus whether stderr is empty, each vector run as [ … ] and as test …:
| Run | Cases | Fixed | Regressed | Pre-existing | Changed, still wrong |
|---|---|---|---|---|---|
| ≤4 args, 27 tokens, exhaustive | 1,103,762 | 999,958 | 8,701 | 47,310 | 7,809 |
| 5 args, 15 tokens, exhaustive | 1,518,750 | 1,377,985 | 11,476 | 69,531 | 4,992 |
| 6–8 args, 23 tokens, random | 300,000 | 295,489 | 561 | 1,975 | 373 |
| ≤4 args, 18 odd tokens | 222,302 | 196,610 | 1,472 | 13,752 | 1,036 |
| Total, de-duplicated | 3,122,592 | 2,852,942 | 21,846 | 130,348 | 13,974 |
"Regressed" means bash and base agree and head differs. Every one is head exiting 2 with a message where bash and base exit 0 or 1; nothing flipped between 0 and 1, and there was no panic or hang. 19,222 of the 21,846 are the -v/-R/-N/-o shapes, which says more about the token alphabet than about real scripts.
Also held: [[ … ]] is unchanged (src/exec.rs is identical and only uses the untouched is_*_op/eval_*). The new exit 2 behaves like bash inside if, while, &&, ||, !, set -e, subshells and with stderr redirected (63 probes).
Appendix — non-blocking (5)
c=a; test "$c" = "]"exits 1 in bash and on base, 2 here, and the message names[.a=x; b=']'; test "$a" -o "$b"goes from 0 to 2. The cause is the older trailing-]strip atsrc/builtins/test.rs:13-19applying totesttoo; a real fix needs the invoked name, which builtins aren't given (src/builtins/mod.rs:68). Unlikely input, not filed.x=; [ ! ! "$x" ]anda=-n; b=x; [ ! "$a" -a "$b" ]go from 1 to 2 (bash 1). Contrived.- Same family as #321 and still silent, on base too:
[ a = a -a ](bash 2, head 1) ande=; [ -n x -o $e ](bash 2, head 0). Possible follow-up, not filed. tests/differential/cases/test_binary_operator_without_right_operand.sh:2uses$HOME, which the README says to avoid. It can't change the result here (rc=2with any value, or unset); a literal would follow the README.- Unrelated and on base too:
cmd &> fileis read ascmd &then> file(ls /nonexistent &> /tmp/x; echo "rc=$?"printsrc=0and nothing lands in the file; bash printsrc=2). Not filed; I can open an issue if you want one.
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Verdict: changes requested — one 🔴, and it comes from the shortcut I suggested. The three earlier findings are fixed at 2dcdec9. I replied on each thread but could not resolve them (my account lacks permission for that), so they are yours to close.
- ✅
!/(comparisons match bash again, andc='!'; [ "$c" = '!' ]is now true like bash. - ✅
-v/-R/-N/-oare a quiet false as onmain, in longer expressions too. - ✅ Tests: six mutations of the guards each fail a differential case now.
- 🔴
x=; [ ! $x -lt 3 ]is now true (bash exits 2,mainexits 1), and #348 as written would not fix it. One-line fix inline, tested alone and merged with #348.
Correction to my first review: the &> gap I offered to file is already recorded as the redirect_both_ampersand known difference, so ignore that offer.
What I verified at 2dcdec9
Head 2dcdec9, previous head 651b4f2, base dbb6189, bash 5.2.15, debug builds on Linux aarch64 only.
Suites (head)
cargo test --workspace --all-targets --no-fail-fast: exit 0, 2734 passed, 0 failed, 0 ignored.cargo test --test differential -- --nocapture: 110 passed, 17 known differences, 127 cases including the newtest_valid_expressions_stay_quiet.cargo fmt --checkclean;cargo clippy --workspace --all-targetsexits 0 with no warnings insrc/builtins/test.rs.- Mutations of
src/builtins/test.rs, one at a time, run withcargo test --no-fail-fast --lib --test differential --test shell_integration: all six exit 101.
Three-way differential (bash / base / head), same script and alphabets as last round, each vector as [ … ] and test …. "Regressed" is bash and base agreeing while head differs. Counts are per run, not de-duplicated.
| Run | Cases | Regressed at 651b4f2 |
Regressed at 2dcdec9 |
|---|---|---|---|
| ≤4 args, 27 tokens, exhaustive | 1,103,762 | 8,701 | 827 |
| 5 args, 15 tokens, exhaustive | 1,518,750 | 11,476 | 946 |
| 6–8 args, 23 tokens, random | 300,000 | 561 | 30 |
| ≤4 args, 18 odd tokens | 222,302 | 1,472 | 312 |
- Fewer vectors count as fixed than last round (for example 824,196 instead of 999,958 in the first run). That is the exemption working as described: malformed expressions that contain
-v/-R/-N/-oare quiet again, as onmain. - In the
[ … ]spelling, the vectors where head exits 0 while bash and base both don't number 50 across the four runs, and all 50 are the three-word integer shortcut in the inline comment. - The rest of the
[ … ]regressions exit 2 with a message where bash and base exit 1: a doubled!,!before a three-word-a/-otest,(-led shapes such asc='('; [ ! "$c" = '(' ], and-tshapes. I rated these contrived last round and still do. - The
test … ]rows from last round's appendix are unchanged.
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Verdict: approve. The 🔴 from last round is fixed at 80e9467, and I found nothing new.
- ✅
x=; if [ ! $x -lt 3 ]takes theelsebranch again. All six integer operators with!or(on the left exit 2 with an error, like bash;[ 1 -lt 3 ]and the!/(string comparisons are unchanged. - ✅ The new line in
test_binary_operator_without_right_operand.shpins it: without the exclusion that case fails. - ✅ It composes with #348: a trial merge with its current head
581183f, both sides of the conflict kept, passes, ande=; [ "$e" -lt 3 ]exits 2 there.
All four of my threads are answered and still open, since I can't resolve them.
What I verified at 80e9467
Head 80e9467, previous head 2dcdec9, base dbb6189, bash 5.2.15, debug builds on Linux aarch64 only.
cargo test --workspace --all-targets --no-fail-fast: exit 0, 2734 passed, 0 failed, 0 ignored.cargo test --test differential -- --nocapture: 110 passed, 17 known differences, 127 cases.cargo fmt --checkclean;cargo clippy --workspace --all-targetsexits 0 with no warnings insrc/builtins/test.rs.- The three
test_*differential cases this PR adds match bash's stdout and stderr presence in the README's recording environment. - Mutation: with the integer-operator exclusion removed,
cargo test --no-fail-fast --lib --test differential --test shell_integrationexits 101 ontest_binary_operator_without_right_operand. - Trial merge of
80e9467with #348 at581183f, conflicts insrc/builtins/test.rsresolved by keeping both sides: lib 86, differential 1 andshell_integration2066 passed.
Three-way differential (bash / base / head), same four runs as before (3,144,814 cases, not de-duplicated):
- No vector regresses at
80e9467that didn't at2dcdec9. Per-run regression counts are 825, 946, 30 and 312 (827, 946, 30, 312 last round). - In the
[ … ]spelling no vector exits 0 while bash and base both don't; last round there were 50. - In the
testspelling 94 vectors still do. All are four words ending in a literal], such astest ! != x ], where the older trailing-]strip turns them into a three-word comparison. That is the item from my first review's appendix; I don't think a real script writes it. - The other remaining regressions are the contrived shapes listed last round (a doubled
!,!before a three-word-a/-otest,(-led and-tshapes). They exit 2 with a message where bash and base exit 1.
kevin-orellana
left a comment
There was a problem hiding this comment.
Comparing a string with a literal ] now reports a missing operand. The test command accepts ] as data. Only the [ form needs a closing bracket. Could we use the invoked command name to decide when to remove the final ]?
test x = ']' returns 1 with no diagnostic in Bash and on main, as expected for two unequal strings. This branch exits 2 with unary operator expected.
|
@kevin-orellana Good catch, thanks.
I also merged main into the branch (183c20f). |
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Verdict: approve. The test / [ split in 977d28b fixes the trailing-] problem kevin-orellana found, and I found nothing new.
- ✅ Under
test,]is an ordinary word again:test x = ']'exits 1 quietly, andtest ']' = ']'andtest ']'are now true like bash (both false onmain). - ✅
[without its closing]reportsmissing `]'and exits 2 like bash, including throughcommand [,builtin [, and a function wrapping[ "$@"./bin/testmatches too. - ✅ The new
test_closing_bracket_as_operandcase pins it: putting the old strip back undertestfails it. ⚠️ The red Rust (windows-latest) job is not from this PR.compound_pipe_into_function_redirected_from_clioverflows the stack, and #348 fails the same way after merging the samemain. Filed as #406.
What I verified at 977d28b
Head 977d28b (with main 4f838f9 merged in), compared against main 4f838f9, bash 5.2.15, debug builds on Linux aarch64 only. I couldn't run the Windows job.
cargo test --workspace --all-targets --no-fail-fast: exit 0, 2775 passed, 0 failed, 0 ignored. Differential: 132 passed, 17 known differences, 149 cases.cargo fmt --checkclean; clippy reports nothing insrc/builtins/test.rsorsrc/builtins/mod.rs.builtin_testandbuiltin_bracketare dispatched only fromsrc/builtins/mod.rs:68-69;/bin/testgoes through the same name.- 37 hand probes covering
test/[, indirect calls (command,builtin,eval, functions,/bin/test,xargs) and the earlier findings: every one matches bash exceptxargs test x = < /dev/null. GNU xargs runs the command once on empty input; this shell doesn't. That gap is the same onmainand isn't related to this PR. - Exhaustive differential, up to 4 args from 27 tokens (1,103,762 cases), against the new
main:- The 94
test … ]vectors that were wrongly true at80e9467(e.g.test ! != x ]) are fixed. - 137 vectors still exit 0 where bash exits 2. All are integer tests on non-integers, e.g.
test x -eq ]:]is now an operand, so they hit the #334 bug that #348 fixes. - 77 vectors are newly quiet-false where bash is true, e.g.
test x -o -v ]. They come from the deliberate-v/-R/-N/-oexemption now that]counts as a word. I think these are contrived.
- The 94
Description
When
[ortestwas given a binary operator with nothing after it, like[ x = ], it quietly returned false. The realistic way to hit this is an unquoted empty variable,[ "$HOME" = $e ]. Bash treats that as a usage error: it prints an error and exits 2. So a script checking for 2 or for stderr saw something different from bash. The panic in the original report was already fixed on main. This is the exit status and error that were still missing.Now
[andtestfollow bash's argument-count rules here. Two arguments that aren't a unary operator and its operand are an error, and so is the same pair after a leading!. With three arguments, an operator in the middle wins, so[ "$c" = x ]still works whencis!or(. Words left over in a longer expression, such as a trailing operator after-a, give bash's "too many arguments" error. Both errors exit 2. Unary operators bash has that this shell doesn't support yet (-v,-R,-N,-o) still quietly return false, as before. Another PR for #334 touches the same file for integer operands, so a small merge conflict is possible.Related Issues
Fixes #321
Documentation PR
N/A
Type of Change
Bug fix
Testing
I added differential cases for the issue's forms and the longer expression, plus an integration test for the exit status and error text. They failed on main and pass with the fix. After review I added a case of valid expressions that must stay quiet, which caught two regressions in my first version. The full test suite passes.
cargo test --workspace --all-targets,pytest tests/python,npm test)cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.