|
| 1 | +#!/bin/bash |
| 2 | +set -uo pipefail |
| 3 | + |
| 4 | +LIND_WASM_ROOT="${LIND_WASM_ROOT:-/home/lind/lind-wasm}" |
| 5 | +LINDBOOT_BIN="$LIND_WASM_ROOT/build/lind-boot" |
| 6 | +LINDFS_ROOT="$LIND_WASM_ROOT/lindfs" |
| 7 | + |
| 8 | +cd $LINDFS_ROOT |
| 9 | + |
| 10 | +echo "Starting sed sandbox tests..." |
| 11 | + |
| 12 | +FAILS=0 |
| 13 | + |
| 14 | +# Helper function to validate outputs |
| 15 | +check_result() { |
| 16 | + local test_name="$1" |
| 17 | + local expected="$2" |
| 18 | + local actual="$3" |
| 19 | + |
| 20 | + if [ "$expected" = "$actual" ]; then |
| 21 | + echo "[PASS] $test_name" |
| 22 | + else |
| 23 | + echo "[FAIL] $test_name" |
| 24 | + echo " Expected: '$expected'" |
| 25 | + echo " Actual: '$actual'" |
| 26 | + FAILS=$((FAILS + 1)) |
| 27 | + fi |
| 28 | +} |
| 29 | + |
| 30 | +# 1. Basic Stream Substitution |
| 31 | +ACTUAL=$(echo "Hello World" | lind_run bin/sed 's/World/Sandbox/') |
| 32 | +check_result "Basic Substitution" "Hello Sandbox" "$ACTUAL" |
| 33 | + |
| 34 | +# 2. Global Substitution |
| 35 | +ACTUAL=$(echo "apple banana apple" | lind_run bin/sed 's/a[a-z]*e/orange/g') |
| 36 | +check_result "Global Substitution" "orange banana orange" "$ACTUAL" |
| 37 | + |
| 38 | +# 3. In-Place File Editing (-i) |
| 39 | +# This heavily stresses file-related system calls (open, read, write, rename, unlink) |
| 40 | +echo "Version 1.0" > test_inplace.txt |
| 41 | +lind_run bin/sed -i 's/1.0/2.0/' test_inplace.txt |
| 42 | +ACTUAL=$(cat test_inplace.txt) |
| 43 | +check_result "In-Place Editing" "Version 2.0" "$ACTUAL" |
| 44 | +rm -f test_inplace.txt |
| 45 | + |
| 46 | +# 4. Line Deletion |
| 47 | +ACTUAL=$(printf "keep me\ndelete me\nkeep me too\n" | lind_run bin/sed '/delete/d') |
| 48 | +EXPECTED=$(printf "keep me\nkeep me too") |
| 49 | +check_result "Line Deletion" "$EXPECTED" "$ACTUAL" |
| 50 | + |
| 51 | +# 5. Selective Printing (-n) |
| 52 | +ACTUAL=$(printf "apple\nbanana\ncherry\n" | lind_run bin/sed -n '/banana/p') |
| 53 | +check_result "Selective Printing" "banana" "$ACTUAL" |
| 54 | + |
| 55 | +# 6. Multiple Commands (-e) |
| 56 | +ACTUAL=$(echo "foo and bar" | lind_run bin/sed -e 's/foo/baz/' -e 's/bar/qux/') |
| 57 | +check_result "Multiple Commands" "baz and qux" "$ACTUAL" |
| 58 | + |
| 59 | +# 7. Reading Commands from a Script (-f) |
| 60 | +echo "s/cat/dog/g" > script.sed |
| 61 | +ACTUAL=$(echo "The cat chased the other cat." | lind_run bin/sed -f script.sed) |
| 62 | +check_result "Script File" "The dog chased the other dog." "$ACTUAL" |
| 63 | +rm -f script.sed |
| 64 | + |
| 65 | +echo "-----------------------------------" |
| 66 | +if [ $FAILS -eq 0 ]; then |
| 67 | + echo "Success: All sed tests passed!" |
| 68 | + exit 0 |
| 69 | +else |
| 70 | + echo "Failure: $FAILS test(s) failed." |
| 71 | + exit 1 |
| 72 | +fi |
0 commit comments