Skip to content

Commit 0e83cc9

Browse files
authored
fix: trim space in test comparisons (reubeno#950)
1 parent df9c835 commit 0e83cc9

3 files changed

Lines changed: 88 additions & 3 deletions

File tree

brush-core/src/extendedtests.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ pub(crate) fn apply_unary_predicate_to_str(
126126
}
127127
}
128128
ast::UnaryPredicate::FdIsOpenTerminal => {
129-
if let Ok(fd) = operand.parse::<ShellFd>() {
129+
// Trim whitespace before parsing, matching bash behavior.
130+
if let Ok(fd) = operand.trim().parse::<ShellFd>() {
130131
if let Some(open_file) = params.try_fd(shell, fd) {
131132
Ok(open_file.is_terminal())
132133
} else {
@@ -548,8 +549,9 @@ fn apply_test_binary_arithmetic_predicate(
548549
right: &str,
549550
op: fn(i64, i64) -> bool,
550551
) -> bool {
551-
let left: Result<i64, _> = left.parse();
552-
let right: Result<i64, _> = right.parse();
552+
// We trim leading/trailing whitespace (including newlines) before parsing integers.
553+
let left: Result<i64, _> = left.trim().parse();
554+
let right: Result<i64, _> = right.trim().parse();
553555

554556
if let (Ok(left), Ok(right)) = (left, right) {
555557
op(left, right)

brush-shell/tests/cases/compat/builtins/test.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,47 @@ cases:
311311
test "a b" = "a b" && echo "tabs match"
312312
test 'a"b' = 'a"b' && echo "quotes match"
313313
test "a'b" = "a'b" && echo "single quotes match"
314+
315+
- name: "test: arithmetic comparison with spaces/tabs in operand"
316+
stdin: |
317+
# Leading/trailing spaces
318+
test 0 -eq ' 0'
319+
echo "leading space: $?"
320+
321+
test 0 -eq '0 '
322+
echo "trailing space: $?"
323+
324+
test 0 -eq ' 0 '
325+
echo "both spaces: $?"
326+
327+
# Tabs
328+
test 0 -eq ' 0'
329+
echo "leading tab: $?"
330+
331+
test 0 -eq '0 '
332+
echo "trailing tab: $?"
333+
334+
- name: "test: arithmetic comparison with newline in operand"
335+
min_oracle_version: "5.3"
336+
stdin: |
337+
# Newline trimming seems to have changed in or around bash 5.3
338+
test 0 -eq ' 0
339+
'
340+
echo "newline trailing: $?"
341+
342+
- name: "test: -t operator with whitespace in fd"
343+
stdin: |
344+
# -t operator should trim whitespace before parsing fd number
345+
# These should all behave identically (false since not a terminal)
346+
test -t 0
347+
r1=$?
348+
test -t ' 0'
349+
r2=$?
350+
test -t '0 '
351+
r3=$?
352+
test -t ' 0 '
353+
r4=$?
354+
355+
# All should have the same result
356+
[ $r1 -eq $r2 ] && [ $r2 -eq $r3 ] && [ $r3 -eq $r4 ] && echo "All -t results match"
357+

brush-shell/tests/cases/compat/extended_tests.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,42 @@ cases:
340340
- name: "Regex with newline"
341341
stdin: |
342342
[[ $'\n' =~ . ]] && echo "1. Matches"
343+
344+
- name: "Arithmetic comparisons with whitespace in operands"
345+
stdin: |
346+
# Leading/trailing whitespace should be trimmed before comparison
347+
[[ 0 -eq ' 0' ]] && echo "1. leading space"
348+
[[ 0 -eq '0 ' ]] && echo "2. trailing space"
349+
[[ 0 -eq ' 0 ' ]] && echo "3. both spaces"
350+
351+
# Tabs
352+
[[ 0 -eq ' 0' ]] && echo "4. leading tab"
353+
[[ 0 -eq '0 ' ]] && echo "5. trailing tab"
354+
355+
# Newlines
356+
[[ 0 -eq ' 0
357+
' ]] && echo "6. newline"
358+
359+
# Other operators
360+
[[ 5 -lt ' 10' ]] && echo "7. -lt with space"
361+
[[ 10 -gt ' 5' ]] && echo "8. -gt with space"
362+
[[ 5 -ne ' 6' ]] && echo "9. -ne with space"
363+
[[ 5 -le ' 5' ]] && echo "10. -le with space"
364+
[[ 5 -ge ' 5' ]] && echo "11. -ge with space"
365+
366+
# Negative numbers with whitespace
367+
[[ -5 -eq ' -5' ]] && echo "12. negative with space"
368+
369+
- name: "Terminal fd check with whitespace"
370+
stdin: |
371+
# -t operator should trim whitespace before parsing fd number
372+
# These should all behave identically (false since not a terminal)
373+
[[ -t 0 ]]
374+
r1=$?
375+
[[ -t ' 0' ]]
376+
r2=$?
377+
[[ -t '0 ' ]]
378+
r3=$?
379+
380+
# All should have the same result
381+
[[ $r1 -eq $r2 && $r2 -eq $r3 ]] && echo "All -t results match"

0 commit comments

Comments
 (0)