RustForge

foundry

Foundry OS: the operating system built on the Rustbelt kernel

foundry / rootfs / scripts / dash-test.sh
2168 bytes ยท raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# dash-test.sh: POSIX shell checks for dash (Foundry OS, milestone M2).
# The self-test runs it as `dash /mnt/disk0/scripts/dash-test.sh` (contract
# v1) or `dash /scripts/dash-test.sh /bin` (v2): the argument is the
# directory holding the programs (default /mnt/disk0/bin). Each check prints "dash-test: ok NAME" or "dash-test: FAIL NAME"; the exit
# status is the number of failures.
fails=0 checks=0
bindir=${1:-/mnt/disk0/bin}
check() { # check NAME COMMAND...  (passes when COMMAND succeeds)
    name=$1; shift
    checks=$((checks + 1))
    if "$@"; then echo "dash-test: ok $name"
    else echo "dash-test: FAIL $name"; fails=$((fails + 1)); fi
}
eq() { [ "$1" = "$2" ] || { echo "  got '$1', want '$2'"; return 1; }; }

echo "dash-test: dash running, pid $$"
x=6
check arithmetic eq "$((x * 7))" 42
s=; for i in 1 2 3; do s="$s$i"; done
check for-loop eq "$s" 123
f() { echo "$#:$*"; return 3; }
out=$(f a b c); st=$?
check function-and-return eq "$out/$st" "3:a b c/3"
case foo.txt in *.txt) c=yes ;; *) c=no ;; esac
check case-glob eq "$c" yes
n=0; for p in "$bindir"/*; do n=$((n + 1)); done
echo "dash-test: $n programs in $bindir"
check pipe-to-rust-grep eq "$(printf 'apple\nbanana\n' | grep ban)" banana
echo "written by dash" > /tmp/dash-test.txt
read line < /tmp/dash-test.txt
check redirect-and-read eq "$line" "written by dash"
cat /tmp/no-such-file 2>/dev/null; st=$?
check missing-file-status [ "$st" -ne 0 ]
heredoc=$(cat <<END
here $x
END
)
check here-document eq "$heredoc" "here 6"
(exit 7); st=$?
check subshell-exit-status eq "$st" 7
sleep 1 &
bg=$!
wait "$bg"; st=$?
check background-and-wait eq "$st" 0
tests() { [ -d /tmp ] && [ -x "$bindir/dash" ] && [ ! -e /tmp/nope ]; }
check test-builtins tests
got=; trap 'got=USR1' USR1; kill -USR1 $$
check trap-signal eq "$got" USR1
rm /tmp/dash-test.txt
check rm-rust-tool [ ! -e /tmp/dash-test.txt ]
v=$(sh_var=inner; echo "$sh_var"); check command-substitution eq "$v" inner
set -- one "two words" three
check positional-params eq "$#:$2" "3:two words"
if [ $fails = 0 ]; then echo "dash-test: all $checks checks passed"
else echo "dash-test: $fails of $checks checks FAILED"; fi
exit $fails