Skip to content
suit Artifact [7f4fea22aa]
Login

Artifact [7f4fea22aa]

Artifact 7f4fea22aa953d578a51738bb1add8a0f9c44d8fc7b8b7518aa44f945a2092be:


#!/bin/sh
# Shell tests for the suit command line. Each case prints "ok NAME" or
# "FAIL NAME"; the exit status is nonzero if any case failed.
# SUIT names the binary under test (default build/suit).

SUIT=${SUIT:-build/suit}
case $SUIT in /*) ;; *) SUIT=$PWD/$SUIT ;; esac
ROOT=$PWD
mkdir -p build/tmp
T=$(mktemp -d "$ROOT/build/tmp/run.XXXXXX") || exit 1
trap 'rm -rf "$T"' EXIT INT TERM

total=0
failed=0

pass() { total=$((total + 1)); echo "ok $1"; }
bad() {
    total=$((total + 1)); failed=$((failed + 1))
    echo "FAIL $1${2:+: $2}"
}

# conf NAME: read stdin into $T/NAME.conf
conf() { cat > "$T/$1.conf"; }

# ---- valid configs --------------------------------------------------------

expect_dump() { # NAME CONF GOLDEN
    if out=$("$SUIT" check "$2" 2>"$T/err"); then
        if [ "$out" = "$(cat "$3")" ]; then pass "$1"
        else bad "$1" "dump differs from $3"; fi
    else
        bad "$1" "exit $?: $(cat "$T/err")"
    fi
}

expect_dump example-fixture test/fixtures/example.conf test/fixtures/example.dump
expect_dump every-kind-and-key test/fixtures/everything.conf test/fixtures/everything.dump

expect_text() { # NAME EXPECTED   (config on stdin)
    conf "$1"
    out=$("$SUIT" check "$T/$1.conf" 2>"$T/err"); rc=$?
    if [ $rc -eq 0 ] && [ "$out" = "$2" ]; then pass "$1"
    else bad "$1" "exit $rc, got: $out $(cat "$T/err")"; fi
}

printf '' | expect_text empty-file ''
expect_text only-comments '' <<'X'
# one

   # indented
X
expect_text comment-after-space 'harness a
harness a record = x' <<'X'
[harness a] # trailing
record = x # trailing
X
expect_text hash-inside-word-kept 'harness a
harness a event E = a#b' <<'X'
[harness a]
event E = a#b
X
expect_text key-and-value-trimmed 'outfit a
outfit a skills = x y z*' <<'X'
[outfit a]
   skills    =    x	  y   z*   
X
expect_text multiword-key-collapsed 'outfit a
outfit a slot hook/Stop = f.sh' <<'X'
[outfit a]
slot    hook/Stop = f.sh
X
printf '[harness a]\r\nrecord = x\r\n' > "$T/crlf.conf"
if [ "$("$SUIT" check "$T/crlf.conf")" = 'harness a
harness a record = x' ]; then pass crlf-line-endings; else bad crlf-line-endings; fi
printf '[harness a]\nrecord = x' > "$T/nonl.conf"
if "$SUIT" check "$T/nonl.conf" >/dev/null 2>&1; then pass no-trailing-newline; else bad no-trailing-newline; fi
expect_text forward-include 'outfit a
outfit a include = b
outfit b' <<'X'
[outfit a]
include = b
[outfit b]
X
expect_text dump-order-stable 'harness a
harness b
outfit a
outfit b
wear user
upstream u' <<'X'
[wear user]
[upstream u]
[outfit b]
[outfit a]
[harness b]
[harness a]
X

# ---- strict errors --------------------------------------------------------

expect_err() { # NAME LINE MESSAGE-PART   (config on stdin)
    conf "$1"
    out=$("$SUIT" check "$T/$1.conf" 2>"$T/err"); rc=$?
    want="$T/$1.conf:$2: "
    if [ $rc -ne 2 ]; then bad "$1" "exit $rc, want 2"
    elif [ -n "$out" ]; then bad "$1" "printed a partial dump"
    elif [ "$(wc -l < "$T/err" | tr -d ' ')" != 1 ]; then bad "$1" "stderr is not one line: $(cat "$T/err")"
    elif ! grep -qF -- "$want" "$T/err"; then bad "$1" "want '$want' in: $(cat "$T/err")"
    elif ! grep -qF -- "$3" "$T/err"; then bad "$1" "want '$3' in: $(cat "$T/err")"
    else pass "$1"; fi
}

expect_err err-unknown-kind 3 "unknown section kind" <<'X'
[harness a]
record = x
[gadget a]
X
expect_err err-unknown-key 2 "unknown key 'nope'" <<'X'
[harness a]
nope = 1
X
expect_err err-key-in-wrong-kind 2 "unknown key 'skills'" <<'X'
[harness a]
skills = x
X
expect_err err-unknown-harness-key 2 "unknown key 'skills.galaxy'" <<'X'
[harness a]
skills.galaxy = x
X
expect_err err-duplicate-section 3 "duplicate section (first at line 1)" <<'X'
[harness a]

[harness a]
X
expect_err err-duplicate-wear-section 4 "duplicate section" <<'X'
[wear user machine=m]
outfits = a
[outfit a]
[wear user machine=m]
X
expect_err err-duplicate-key 3 "duplicate key 'skills.user' (first at line 2)" <<'X'
[harness a]
skills.user = x
skills.user = y
X
expect_err err-duplicate-slot-key 4 "duplicate key 'slot s'" <<'X'
[outfit a]
slot s = x
only = h
slot   s = y
X
expect_err err-entry-before-section 1 "entry before any section header" <<'X'
record = x
[harness a]
X
expect_err err-header-no-close 2 "malformed section header" <<'X'
# c
[harness a
X
expect_err err-header-empty 1 "malformed section header" <<'X'
[]
X
expect_err err-header-trailing-text 1 "malformed section header" <<'X'
[harness a] extra
X
expect_err err-header-missing-name 1 "exactly one NAME" <<'X'
[harness]
X
expect_err err-header-two-names 1 "exactly one NAME" <<'X'
[outfit a b]
X
expect_err err-need-missing-name 2 "HARNESS and a NAME" <<'X'
[harness a]
[need a]
X
expect_err err-include-cycle 4 "include cycle: a -> b -> a" <<'X'
[outfit a]
include = b
[outfit b]
include = a
X
expect_err err-include-self 2 "include cycle: a -> a" <<'X'
[outfit a]
include = a
X
expect_err err-include-cycle-long 6 "include cycle: a -> b -> c -> a" <<'X'
[outfit a]
include = b
[outfit b]
include = c
[outfit c]
include = a
X
expect_err err-include-unknown 3 "include of unknown outfit 'ghost'" <<'X'
[outfit a]
skills = x
include = b ghost
[outfit b]
X
expect_err err-wear-unknown-outfit 2 "wear names unknown outfit 'ghost'" <<'X'
[wear user]
outfits = ghost
X
expect_err err-only-unknown-harness 3 "only names unknown harness 'ghost'" <<'X'
[harness real]
[outfit a]
only = ghost
X
expect_err err-need-unknown-harness 1 "need names unknown harness 'ghost'" <<'X'
[need ghost n]
X
expect_err err-wear-unknown-scope 1 "unknown scope 'galaxy'" <<'X'
[wear galaxy]
X
expect_err err-wear-shared-needs-target 1 "needs a TARGET" <<'X'
[wear shared]
X
expect_err err-wear-personal-needs-target 1 "needs a TARGET" <<'X'
[wear personal machine=m]
X
expect_err err-wear-user-forbids-target 1 "takes no TARGET" <<'X'
[wear user github.com/o/n]
X
expect_err err-wear-enterprise-forbids-target 1 "takes no TARGET" <<'X'
[wear enterprise github.com/o/n]
X
expect_err err-wear-machine-on-enterprise 1 "machine= is allowed only on user and personal" <<'X'
[wear enterprise machine=m]
X
expect_err err-wear-machine-on-shared 1 "machine= is allowed only on user and personal" <<'X'
[wear shared github.com/o/n machine=m]
X
expect_err err-publish-on-user 2 "publish is allowed only on wear shared" <<'X'
[wear user]
publish = yes
X
expect_err err-publish-not-yesno 2 "must be yes or no" <<'X'
[wear shared github.com/o/n]
publish = maybe
X
expect_err err-trust-bad 2 "must be review or auto" <<'X'
[upstream u]
trust = always
X
expect_err err-empty-value 2 "empty value for 'record'" <<'X'
[harness a]
record =
X
expect_err err-no-equals 2 "expected KEY = VALUE" <<'X'
[harness a]
record=x
X
expect_err err-only-two-words 3 "takes one name" <<'X'
[harness a]
[outfit o]
only = a b
X
expect_err err-bad-list-name 2 "invalid name" <<'X'
[outfit o]
skills = ok bad/name
X
expect_err err-glob-in-include 2 "invalid name" <<'X'
[outfit o]
include = o*
X
expect_err err-two-word-event-name 2 "invalid key" <<'X'
[harness a]
event two words = x
X
printf '[harness a]\nrecord = x\0y\n' > "$T/nul.conf"
"$SUIT" check "$T/nul.conf" >"$T/out" 2>"$T/err"; rc=$?
if [ $rc -eq 2 ] && grep -qF "nul.conf:2: NUL byte in line" "$T/err" && [ ! -s "$T/out" ]; then pass err-nul-byte; else bad err-nul-byte "$(cat "$T/err")"; fi
"$SUIT" check "$T/does-not-exist.conf" >"$T/out" 2>"$T/err"; rc=$?
if [ $rc -eq 2 ] && grep -qF "does-not-exist.conf:0: cannot open" "$T/err"; then pass err-missing-file; else bad err-missing-file "exit $rc $(cat "$T/err")"; fi
"$SUIT" check >/dev/null 2>&1; [ $? -eq 2 ] && pass usage-check-no-file || bad usage-check-no-file

# ---- limits ---------------------------------------------------------------

gen() { # N FORMAT   -> N lines of FORMAT with %d = 1..N on stdout
    awk -v n="$1" -v f="$2" 'BEGIN { for (i = 1; i <= n; i++) printf f "\n", i }'
}

limit_case() { # NAME N FORMAT LIMIT-WORD   (HEAD env: lines to put first)
    name=$1; n=$2; fmt=$3; word=$4
    { printf '%s' "${HEAD:-}"; gen "$n" "$fmt"; } > "$T/$name-at.conf"
    { printf '%s' "${HEAD:-}"; gen $((n + 1)) "$fmt"; } > "$T/$name-over.conf"
    if "$SUIT" check "$T/$name-at.conf" >/dev/null 2>"$T/err"; then pass "$name-at-limit"
    else bad "$name-at-limit" "$(cat "$T/err")"; fi
    hl=0; [ -n "${HEAD:-}" ] && hl=$(printf '%s' "$HEAD" | wc -l | tr -d ' ')
    "$SUIT" check "$T/$name-over.conf" >/dev/null 2>"$T/err"; rc=$?
    if [ $rc -eq 2 ] && grep -qF "$name-over.conf:$((n + 1 + hl)): $word" "$T/err"; then pass "$name-over-limit"
    else bad "$name-over-limit" "exit $rc, $(cat "$T/err")"; fi
}

limit_case limit-harness 64 '[harness h%d]' "too many harness sections (limit 64)"
limit_case limit-outfit 512 '[outfit o%d]' "too many outfit sections (limit 512)"
limit_case limit-wear 256 '[wear personal github.com/o/n%d]' "too many wear sections (limit 256)"
HEAD='[harness h]
' limit_case limit-need 256 '[need h n%d]' "too many need sections (limit 256)"
limit_case limit-upstream 128 '[upstream u%d]' "too many upstream sections (limit 128)"
HEAD='[harness h]
' limit_case limit-entries 8192 'record = r%d' "too many entries (limit 8192)"

awk 'BEGIN { s = "# "; while (length(s) < 4096) s = s "x"; print s }' > "$T/line-at.conf"
awk 'BEGIN { s = "# "; while (length(s) < 4097) s = s "x"; print s }' > "$T/line-over.conf"
if "$SUIT" check "$T/line-at.conf" >/dev/null 2>&1; then pass limit-line-at-limit; else bad limit-line-at-limit; fi
"$SUIT" check "$T/line-over.conf" >/dev/null 2>"$T/err"
if grep -qF "line-over.conf:1: line is 4097 bytes, limit is 4096" "$T/err"; then pass limit-line-over-limit; else bad limit-line-over-limit "$(cat "$T/err")"; fi

# 1048576 bytes exactly is allowed; one more is not. 32768 lines of 32 bytes.
awk 'BEGIN { for (i = 0; i < 32768; i++) print "# 123456789012345678901234567890" }' > "$T/file-at.conf"
printf '\n' >> "$T/file-at.conf"
head -c 1048576 "$T/file-at.conf" > "$T/file-at2.conf"
cp "$T/file-at2.conf" "$T/file-over.conf"; printf '\n' >> "$T/file-over.conf"
if "$SUIT" check "$T/file-at2.conf" >/dev/null 2>"$T/err"; then pass limit-file-at-limit; else bad limit-file-at-limit "$(cat "$T/err")"; fi
"$SUIT" check "$T/file-over.conf" >/dev/null 2>"$T/err"; rc=$?
if [ $rc -eq 2 ] && grep -qF "file-over.conf:0: file is over the limit of 1048576 bytes" "$T/err"; then pass limit-file-over-limit; else bad limit-file-over-limit "exit $rc $(cat "$T/err")"; fi

# ---- stamp and run --------------------------------------------------------

S=$T/scratch
mkdir -p "$S/files"
for i in 1 2 3; do echo "file $i" > "$S/files/f$i"; done
export SUIT_HOME="$S/home"

"$SUIT" run >"$T/out" 2>"$T/err"; rc=$?
if [ $rc -eq 11 ] && [ ! -s "$T/out" ] && [ "$(wc -l < "$T/err" | tr -d ' ')" = 1 ] && grep -qF "no stamp at $SUIT_HOME/stamp" "$T/err"; then pass run-no-stamp-exits-11
else bad run-no-stamp-exits-11 "exit $rc $(cat "$T/err")"; fi
[ ! -e "$SUIT_HOME" ] && pass run-no-stamp-creates-nothing || bad run-no-stamp-creates-nothing

"$SUIT" stamp "$S/files/f1" "$S/files/f2" "$S/files/with space" "$S/files/gone" >"$T/out" 2>"$T/err"; rc=$?
[ $rc -eq 0 ] && [ ! -s "$T/out" ] && pass stamp-writes || bad stamp-writes "exit $rc $(cat "$T/err")"
if [ "$(ls "$SUIT_HOME")" = stamp ]; then pass stamp-leaves-no-temp-file; else bad stamp-leaves-no-temp-file "$(ls "$SUIT_HOME")"; fi
if [ "$(wc -l < "$SUIT_HOME/stamp" | tr -d ' ')" = 4 ]; then pass stamp-one-line-per-path; else bad stamp-one-line-per-path; fi
if grep -Eq "^7 [0-9]+ [0-9]+ $S/files/f1\$" "$SUIT_HOME/stamp"; then pass stamp-line-format; else bad stamp-line-format "$(cat "$SUIT_HOME/stamp")"; fi
if grep -qxF -- "- - - $S/files/gone" "$SUIT_HOME/stamp"; then pass stamp-missing-path-recorded; else bad stamp-missing-path-recorded; fi
want=$(stat -f %z "$S/files/f1" 2>/dev/null || stat -c %s "$S/files/f1")
case $(grep "files/f1\$" "$SUIT_HOME/stamp") in "$want "*) pass stamp-size-matches-stat ;; *) bad stamp-size-matches-stat ;; esac

"$SUIT" run >"$T/out" 2>"$T/err"; rc=$?
if [ $rc -eq 0 ] && [ ! -s "$T/out" ] && [ ! -s "$T/err" ]; then pass run-unchanged-silent-exit-0; else bad run-unchanged-silent-exit-0 "exit $rc"; fi

echo more >> "$S/files/f1"
"$SUIT" run >"$T/out" 2>"$T/err"; rc=$?
if [ $rc -eq 10 ] && [ "$(cat "$T/out")" = "changed: $S/files/f1" ]; then pass run-size-change-exit-10; else bad run-size-change-exit-10 "exit $rc $(cat "$T/out")"; fi
"$SUIT" stamp "$S/files/f1" "$S/files/f2" "$S/files/gone" >/dev/null 2>&1

touch -t 200001010000 "$S/files/f2"
"$SUIT" run >"$T/out" 2>&1; rc=$?
if [ $rc -eq 10 ] && [ "$(cat "$T/out")" = "changed: $S/files/f2" ]; then pass run-mtime-only-change-exit-10; else bad run-mtime-only-change-exit-10 "exit $rc $(cat "$T/out")"; fi
"$SUIT" stamp "$S/files/f1" "$S/files/f2" "$S/files/gone" >/dev/null 2>&1

echo new > "$S/files/gone"
"$SUIT" run >"$T/out" 2>&1; rc=$?
if [ $rc -eq 10 ] && [ "$(cat "$T/out")" = "changed: $S/files/gone" ]; then pass run-missing-path-appears-exit-10; else bad run-missing-path-appears-exit-10 "exit $rc $(cat "$T/out")"; fi
"$SUIT" stamp "$S/files/f1" "$S/files/f2" "$S/files/gone" >/dev/null 2>&1

rm "$S/files/f1" "$S/files/f2"
"$SUIT" run >"$T/out" 2>&1; rc=$?
if [ $rc -eq 10 ] && [ "$(sort "$T/out")" = "changed: $S/files/f1
changed: $S/files/f2" ]; then pass run-reports-every-changed-path; else bad run-reports-every-changed-path "exit $rc $(cat "$T/out")"; fi

"$SUIT" stamp "$S/files/gone" >/dev/null 2>&1
printf '1 2 3\n' > "$SUIT_HOME/stamp"
"$SUIT" run >"$T/out" 2>"$T/err"; rc=$?
if [ $rc -eq 12 ] && grep -qF "stamp:1: expected SIZE MTIME_SEC MTIME_NSEC PATH" "$T/err"; then pass run-corrupt-stamp-exit-12; else bad run-corrupt-stamp-exit-12 "exit $rc $(cat "$T/err")"; fi
printf '5 1 1 /x' > "$SUIT_HOME/stamp"
"$SUIT" run >"$T/out" 2>"$T/err"; rc=$?
if [ $rc -eq 12 ] && grep -qF "stamp:1: line has no newline" "$T/err"; then pass run-truncated-stamp-exit-12; else bad run-truncated-stamp-exit-12 "exit $rc $(cat "$T/err")"; fi
: > "$SUIT_HOME/stamp"
"$SUIT" run >/dev/null 2>&1; [ $? -eq 0 ] && pass run-empty-stamp-exit-0 || bad run-empty-stamp-exit-0

"$SUIT" stamp "$SUIT_HOME/nonexistent/../x" >/dev/null 2>&1; [ $? -eq 0 ] && pass stamp-odd-missing-path-ok || bad stamp-odd-missing-path-ok
"$SUIT" stamp >/dev/null 2>&1; [ $? -eq 2 ] && pass usage-stamp-no-paths || bad usage-stamp-no-paths
"$SUIT" run extra >/dev/null 2>&1; [ $? -eq 2 ] && pass usage-run-extra-arg || bad usage-run-extra-arg
"$SUIT" >/dev/null 2>&1; [ $? -eq 2 ] && pass usage-no-command || bad usage-no-command
"$SUIT" frobnicate >/dev/null 2>&1; [ $? -eq 2 ] && pass usage-unknown-command || bad usage-unknown-command

# ---- the no-change run writes nothing ------------------------------------

snap() { # DIR: every path with mode, size, mtime to the nanosecond, and a content sum
    if stat -f %m / >/dev/null 2>&1; then
        find "$1" -exec stat -f '%N %Sp %z %Fm' {} + | sort
    else
        find "$1" -exec stat -c '%n %A %s %.9Y' {} + | sort
    fi
    find "$1" -type f -exec cksum {} + | sort
}

W=$T/nowrite
mkdir -p "$W/tree/sub"
i=0
while [ $i -lt 100 ]; do
    echo "content $i" > "$W/tree/sub/file$i"
    i=$((i + 1))
done
export SUIT_HOME="$W/home"
"$SUIT" stamp "$W"/tree/sub/file* >/dev/null 2>&1
snap "$W" > "$T/before"
"$SUIT" run >"$T/out" 2>"$T/err"; rc=$?
snap "$W" > "$T/after"
if [ $rc -eq 0 ] && [ ! -s "$T/out" ] && [ ! -s "$T/err" ]; then pass no-write-run-silent-exit-0; else bad no-write-run-silent-exit-0 "exit $rc"; fi
if [ -s "$T/before" ] && cmp -s "$T/before" "$T/after"; then pass no-write-tree-unchanged; else bad no-write-tree-unchanged "$(diff "$T/before" "$T/after" | head -5)"; fi
n=$(wc -l < "$T/before" | tr -d ' ')
[ "$n" -gt 200 ] && pass no-write-snapshot-covers-all-files || bad no-write-snapshot-covers-all-files "only $n lines"
unset SUIT_HOME

# ---- bench arguments ------------------------------------------------------

for args in "-n 0" "-n x" "-n 10001" "-q" "-n" "-e"; do
    # shellcheck disable=SC2086
    "$SUIT" bench $args >/dev/null 2>&1; rc=$?
    [ $rc -eq 2 ] && pass "bench-rejects-[$args]" || bad "bench-rejects-[$args]" "exit $rc"
done
SUIT_HOME="$T/no-such-home" "$SUIT" bench -n 3 >"$T/out" 2>"$T/err"; rc=$?
if [ $rc -eq 1 ] && grep -qF "exited with status 11" "$T/err"; then pass bench-without-stamp-fails; else bad bench-without-stamp-fails "exit $rc $(cat "$T/err")"; fi

SUIT_HOME="$T/no-such-home" "$SUIT" bench -n 3 -e "$T/no-empty" >"$T/out" 2>"$T/err"; rc=$?
if [ $rc -eq 1 ] && grep -qF "cannot spawn $T/no-empty" "$T/err"; then pass bench-missing-empty-program-fails; else bad bench-missing-empty-program-fails "exit $rc $(cat "$T/err")"; fi

echo "run.sh: $total cases, $failed failed"
[ "$failed" -eq 0 ]