diff --git a/pkg/shell/cmds/shell_test.go b/pkg/shell/cmds/shell_test.go index ecdd7f6..5995f81 100644 --- a/pkg/shell/cmds/shell_test.go +++ b/pkg/shell/cmds/shell_test.go @@ -94,6 +94,57 @@ func TestVariableAssignment(t *testing.T) { } } +func TestArithmeticExpansionRejected(t *testing.T) { + for _, command := range []string{ + `echo $((2+3))`, + `x=$((2+3)); echo "x=[$x] exit=$?"`, + `L=5; echo "a=[$((L-1))] b=[$((2+3))]"`, + "sed -i \"$((2-1))a\\\nz\" /docs/arithmetic.txt", + `head -n $((5-1)) /docs/arithmetic.txt > /docs/arithmetic.txt`, + `echo changed > /docs/arithmetic.txt; echo $((2+3))`, + `echo $(echo $((2+3))) > /docs/arithmetic.txt`, + `echo ${UNSET:-$((2+3))} > /docs/arithmetic.txt`, + `for n in $((2+3)); do echo changed > /docs/arithmetic.txt; done`, + `echo changed > "$((2+3))"`, + `export N=$((2+3))`, + } { + t.Run(command, func(t *testing.T) { + fs := testFS() + const original = "a\nb\nc\n" + fs.AddFile("/docs/arithmetic.txt", original) + out, errOut, code := execCmd(t, fs, command) + if code == 0 || out != "" || !strings.Contains(errOut, "arithmetic expansion") || !strings.Contains(errOut, "use expr") { + t.Fatalf("code=%d stdout=%q stderr=%q, want arithmetic expansion error", code, out, errOut) + } + content, err := fs.ReadFile("/docs/arithmetic.txt") + if err != nil || string(content) != original { + t.Fatalf("file changed: content=%q err=%v", content, err) + } + }) + } +} + +func TestArithmeticExpansionLiteralAndCommandSubstitution(t *testing.T) { + for _, command := range []string{ + `echo '$((2+3))'`, + `echo "\$((2+3))"`, + `TEXT='$((2+3))'; echo "$TEXT"`, + "cat <= len(s) || s[i-1] == ')') { + if t.err == nil { + t.err = fmt.Errorf("arithmetic expansion $((...)) is not supported; use expr") + } + return nil + } body := s[start:i] if i < len(s) { i++ // skip closing )