Skip to content
pi-c Artifact [1fe52049c9]
Login

Artifact [1fe52049c9]

Artifact 1fe52049c9b164abe50a57c9cba74855c17ccef8b08b999e176db904a28d3b4a:


#!/bin/sh
# pic update (docs/manual/updates.md) against stand-ins for zit, quarry and make: a green build is
# installed over the pic that runs it, atomically; a build or a test that fails installs nothing and
# is not tried again until trunk moves (or --force); a checkout with changes of its own, offline,
# autoUpdate false, the half hour between looks and a second update at once all do nothing;
# without a checkout and without quarry or zit it says how to set one up.
set -eu
PIC=${PIC:?set PIC to the pic binary}
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-autoupdate.XXXXXX")
T=$(cd "$T" && pwd -P)
bg=
cleanup() {
    [ -n "$bg" ] && kill "$bg" 2> /dev/null || :
    rm -rf -- "$T"
}
trap cleanup EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
for v in $(env | sed -nE 's/^([A-Za-z0-9_]*_(API_KEY|TOKEN))=.*/\1/p'); do unset "$v"; done
FX=$T/fx
mkdir -p "$T/home/.pic/src/pi-c" "$T/bin" "$T/fakebin" "$FX"
export HOME="$T/home" PIC_NO_AUTO_UPDATE=1 FX
: > "$HOME/.pic/src/pi-c/Makefile"
printf 'trunk\n' > "$FX/branch"
cat > "$T/fakebin/zit" << 'EOF2'
#!/bin/sh
echo "$@" >> "$FX/zit.log"
case "$1" in
status) printf 'repository:   x\ncheckout:     %s Tue 2026-11-24\n' "$(cat "$FX/tip")"; [ ! -f "$FX/dirty" ] || cat "$FX/dirty" ;;
branch) cat "$FX/branch" ;;
clone) exit 1 ;;
esac
exit 0
EOF2
cat > "$T/fakebin/quarry" << 'EOF2'
#!/bin/sh
echo "$@" >> "$FX/quarry.log"
exit 1
EOF2
cat > "$T/fakebin/make" << 'EOF2'
#!/bin/sh
echo "$@" >> "$FX/make.log"
for a in "$@"; do
    case "$a" in
    build/pic/release/pic)
        [ ! -f "$FX/build_fail" ] || exit 2
        mkdir -p build/pic/release
        printf '#!/bin/sh\necho "pic fake-%s"\n' "$(cat "$FX/tip")" > build/pic/release/pic
        chmod 755 build/pic/release/pic ;;
    test-smoke | test-release)
        while [ -f "$FX/hold" ] && [ ! -f "$FX/release" ]; do sleep 0.05; done
        exit "$(cat "$FX/test_exit" 2> /dev/null || echo 0)" ;;
    esac
done
exit 0
EOF2
chmod +x "$T/fakebin/zit" "$T/fakebin/quarry" "$T/fakebin/make"
export PATH="$T/fakebin:$PATH"
fresh() { rm -f "$T/bin/pic"; cp "$PIC" "$T/bin/pic"; chmod 755 "$T/bin/pic"; } # a new file: a signed binary rewritten in place is killed
upd() { fresh; "$T/bin/pic" update "$@"; }
status() { "$PIC" update --status; }
lines() { wc -l < "$FX/$1" 2> /dev/null | tr -d ' ' || echo 0; }
: > "$FX/zit.log" && : > "$FX/make.log"
PIC_REAL=$T/pic.real
cp "$PIC" "$PIC_REAL"

# ---- a green build is installed over the running pic ----------------------------------------------
printf 'aaa111\n' > "$FX/tip"
upd > "$T/out1" 2>&1 || fail "green: exit $?: $(cat "$T/out1")"
grep -qF 'fake-aaa111' "$T/bin/pic" || fail "green: the new pic was not installed over the running one"
[ -x "$T/bin/pic" ] || fail "green: the installed file is not executable"
ls "$T/bin" | grep -q 'update\.' && fail "green: a temporary file is left: $(ls "$T/bin")"
grep -qF 'test-smoke' "$FX/make.log" || fail "green: the smoke tests did not run: $(cat "$FX/make.log")"
grep -qF 'pull' "$FX/zit.log" || fail "green: trunk was not pulled"
status | grep -q 'installed.*aaa111' || fail "green: status: $(status)"
printf 'PASS: a green build is built, tested and installed over the pic that runs the update\n'

# ---- the same trunk again does nothing -------------------------------------------------------------
n=$(lines make.log)
upd > "$T/out2" 2>&1 || fail "again: exit $?"
cmp -s "$T/bin/pic" "$PIC" || fail "again: the pic was replaced"
[ "$(lines make.log)" = "$n" ] || fail "again: it built the same trunk again"
status | grep -q 'uptodate' || fail "again: status: $(status)"
printf 'PASS: a trunk that is already installed is not built again\n'

# ---- tests fail: nothing installed, not tried again until trunk moves -----------------------------
printf 'bbb222\n' > "$FX/tip"
echo 1 > "$FX/test_exit"
if upd > "$T/out3" 2>&1; then fail "red: exit 0"; fi
cmp -s "$T/bin/pic" "$PIC" || fail "red: a pic that failed its tests was installed"
status | grep -q 'failed.*tests.*bbb222' || fail "red: status: $(status)"
n=$(lines make.log)
if upd > "$T/out4" 2>&1; then fail "red again: exit 0"; fi
[ "$(lines make.log)" = "$n" ] || fail "red again: it built a trunk that failed before"
grep -q 'newer trunk' "$T/out4" || fail "red again: $(cat "$T/out4")"
if upd --force > "$T/out5" 2>&1; then fail "red force: exit 0"; fi
[ "$(lines make.log)" -gt "$n" ] || fail "red force: --force did not try again"
rm "$FX/test_exit"
printf 'PASS: a failing test installs nothing, and is not tried again until trunk moves or --force\n'

# ---- --full runs the whole release suite ----------------------------------------------------------
printf 'bbb223\n' > "$FX/tip"
upd --force > "$T/outf" 2>&1 || fail "full: exit $?: $(cat "$T/outf")"
grep -qF 'test-release' "$FX/make.log" && fail "full: the whole suite ran without --full"
fresh && "$T/bin/pic" update --full --force > "$T/outf2" 2>&1 || fail "full: exit $?: $(cat "$T/outf2")"
grep -qF 'test-release' "$FX/make.log" || fail "full: --full did not run test-release"
printf 'PASS: the smoke set is the gate, and --full runs the whole release suite\n'

# ---- the build fails ------------------------------------------------------------------------------
printf 'ccc333\n' > "$FX/tip"
: > "$FX/build_fail"
if upd > "$T/out6" 2>&1; then fail "build: exit 0"; fi
cmp -s "$T/bin/pic" "$PIC" || fail "build: installed after a failed build"
status | grep -q 'failed.*build.*ccc333' || fail "build: status: $(status)"
rm "$FX/build_fail"
printf 'PASS: a failing build installs nothing\n'

# ---- a checkout with changes of its own is left alone ---------------------------------------------
printf 'ddd444\n' > "$FX/tip"
printf 'EDITED     src/main.c\n' > "$FX/dirty"
n=$(grep -c pull "$FX/zit.log")
upd > "$T/out7" 2>&1 && fail "dirty: exit 0"
[ "$(grep -c pull "$FX/zit.log")" = "$n" ] || fail "dirty: pulled"
status | grep -q 'skipped.*changes of its own' || fail "dirty: status: $(status)"
rm "$FX/dirty"
printf 'PASS: a checkout with changes of its own is not touched\n'

# ---- offline, autoUpdate false and the half hour --------------------------------------------------
"$PIC" settings offline true > /dev/null
n=$(lines zit.log)
upd > "$T/out8" 2>&1 && fail "offline: exit 0"
[ "$(lines zit.log)" = "$n" ] || fail "offline: zit ran"
status | grep -q 'skipped.*offline' || fail "offline: status: $(status)"
"$PIC" settings offline false > /dev/null
sed -i.bak 's/^checked=.*/checked=1/' "$HOME/.pic/update.status" && rm -f "$HOME/.pic/update.status.bak"
"$PIC" settings autoUpdate false > /dev/null
fresh && "$T/bin/pic" update --auto
[ "$(lines zit.log)" = "$n" ] || fail "autoUpdate false: --auto ran"
"$PIC" settings autoUpdate true > /dev/null
fresh && "$T/bin/pic" update --auto
[ "$(lines zit.log)" -gt "$n" ] || fail "autoUpdate true: --auto did not run"
n=$(lines zit.log)
fresh && "$T/bin/pic" update --auto
[ "$(lines zit.log)" = "$n" ] || fail "--auto again at once ran again (the half hour)"
printf 'PASS: offline, autoUpdate false and the half hour between looks each do nothing\n'

# ---- one update at a time -------------------------------------------------------------------------
printf 'eee555\n' > "$FX/tip"
: > "$FX/hold"
fresh
"$T/bin/pic" update > "$T/out9" 2>&1 &
bg=$!
i=0
before=$(grep -c test-smoke "$FX/make.log")
until [ "$(grep -c test-smoke "$FX/make.log")" -gt "$before" ]; do
    i=$((i + 1)); [ "$i" -lt 600 ] || fail "lock: the first update never reached its tests"; sleep 0.05
done
"$PIC" update > "$T/out10" 2>&1 || fail "lock: second update exit $?"
grep -q 'another update is running' "$T/out10" || fail "lock: $(cat "$T/out10")"
: > "$FX/release"
wait "$bg" || fail "lock: the first update failed: $(cat "$T/out9")"
bg=
grep -qF 'fake-eee555' "$T/bin/pic" || fail "lock: the first update did not install"
printf 'PASS: a second update while one runs says so and does nothing\n'

# ---- no checkout, no quarry or zit to make one ----------------------------------------------------
rm -rf "$HOME/.pic/src"
n=$(lines zit.log)
upd > "$T/out11" 2>&1 && fail "noclone: exit 0"
grep -q 'autoUpdate.source' "$T/out11" || fail "noclone: $(cat "$T/out11")"
grep -q 'repo clone' "$FX/quarry.log" 2>/dev/null || fail "noclone: quarry was not tried"
printf 'PASS: with no checkout and no way to make one it says how to set one up\n'