gunk-dev/the-valley

valley-self-update.sh

source4b9dfcca2bc0main's published tip history
5.9 KiB
1# shellcheck shell=bash
2# Paths and fixtures come from the derivation environment.
3# shellcheck disable=SC2154
4export HOME="$TMPDIR/home" XDG_STATE_HOME="$TMPDIR/state"
5mkdir -p "$HOME"
6export GIT_CONFIG_NOSYSTEM=1
7git config --global user.name valley-check
8git config --global user.email valley-check@localhost
9git config --global init.defaultBranch main
10
11fail() {
12 echo "valley-self-update: $*" >&2
13 exit 1
14}
15
16# The package prints the version nix stamped on it. The script run
17# straight from a checkout says it has none.
18[ "$(valley --version)" = "valley $version" ] \
19 || fail "valley --version says '$(valley --version)', not 'valley $version'"
20bash "$cliScript" --version | grep -q '^valley unstamped' \
21 || fail "the bare script claims a stamp: $(bash "$cliScript" --version)"
22valley help self-update | grep -q -- '--rollback' \
23 || fail "valley help self-update does not describe --rollback"
24
25# The repository self-update follows: a bare one, as git config names it.
26upstream="$TMPDIR/upstream.git"
27git init --quiet --bare "$upstream"
28git clone --quiet "$upstream" "$TMPDIR/work" 2> /dev/null
29push() {
30 git -C "$TMPDIR/work" commit --quiet --allow-empty -m "$1"
31 git -C "$TMPDIR/work" push --quiet origin HEAD:main
32 git -C "$TMPDIR/work" rev-parse HEAD
33}
34git config --global valley.selfUpdate.repo "$upstream"
35
36# A stand-in for nix, first on PATH, since a build has no nix to build
37# with. Its build of a commit is a directory holding this package's share/
38# and a valley that runs the real, unstamped script under the version
39# stamp nix would give that commit. So the smoke test runs the real CLI
40# over the policy the package ships. A commit whose message is `broken`
41# builds a valley pointed at a schema that does not exist, so its smoke
42# test fails at valley checks. The profile is a JSON file of elements.
43export STAND_IN="$TMPDIR/nix" UPSTREAM="$upstream" VALLEY_SCRIPT="$valleyScript" VALLEY_PACKAGE="$valley"
44mkdir -p "$STAND_IN/bin" "$STAND_IN/store"
45echo '{}' > "$STAND_IN/profile.json"
46printf '#!%s\n' "$(command -v bash)" > "$STAND_IN/bin/nix"
47cat >> "$STAND_IN/bin/nix" <<'EOF'
48set -euo pipefail
49while [ "${1:-}" = --extra-experimental-features ]; do shift 2; done
50profile="$STAND_IN/profile.json"
51case "$1" in
52 build)
53 ref="${*: -1}"
54 rev="${ref##*&rev=}"
55 rev="${rev%%#*}"
56 out="$STAND_IN/store/$rev-valley-0.1.0-${rev:0:7}"
57 mkdir -p "$out/bin"
58 ln -sfn "$VALLEY_PACKAGE/share" "$out/share"
59 schema=""
60 if [ "$(git --git-dir="$UPSTREAM" log -1 --format=%s "$rev")" = broken ]; then
61 schema="export VALLEY_VERIFICATION_SCHEMA=/nonexistent/verification.cue"
62 fi
63 printf '#!/bin/sh\nexport VALLEY_VERSION=0.1.0-%s\n%s\nexec %s "$@"\n' \
64 "${rev:0:7}" "$schema" "$VALLEY_SCRIPT/bin/valley" > "$out/bin/valley"
65 chmod +x "$out/bin/valley"
66 echo "$out"
67 ;;
68 profile)
69 case "$2" in
70 list) jq '{elements: .}' "$profile" ;;
71 remove) jq --arg n "$3" 'del(.[$n])' "$profile" > "$profile.new" ;;
72 add)
73 if jq -e '.valley' "$profile" > /dev/null; then
74 echo "error: a valley is installed already" >&2
75 exit 1
76 fi
77 jq --arg p "$3" '. + {valley: {storePaths: [$p]}}' "$profile" > "$profile.new"
78 ;;
79 esac
80 [ ! -e "$profile.new" ] || mv "$profile.new" "$profile"
81 ;;
82 *) exit 2 ;;
83esac
84EOF
85chmod +x "$STAND_IN/bin/nix"
86export PATH="$STAND_IN/bin:$PATH"
87state="$XDG_STATE_HOME/valley/self-update.json"
88installed() { jq -r '.valley.storePaths[0] // empty' "$STAND_IN/profile.json"; }
89
90# A first run builds main's tip, smoke-tests it and installs it.
91c1="$(push one)"
92valley self-update > run.txt || fail "the first run failed: $(cat run.txt)"
93grep -q "smoke test passed: valley 0.1.0-${c1:0:7}" run.txt || fail "no smoke test: $(cat run.txt)"
94grep -q "installed ${c1:0:7} into the nix profile" run.txt || fail "nothing installed: $(cat run.txt)"
95[ "$("$(installed)/bin/valley" --version)" = "valley 0.1.0-${c1:0:7}" ] \
96 || fail "the profile holds $(installed)"
97[ "$(jq -r .current.commit "$state")" = "$c1" ] || fail "state: $(cat "$state")"
98
99# A second run finds it up to date, quietly unless -v. A repository's own
100# git config names no other repository.
101[ -z "$(valley self-update)" ] || fail "an up-to-date run printed something"
102git -C "$TMPDIR/work" config valley.selfUpdate.repo /elsewhere.git
103(cd "$TMPDIR/work" && valley self-update -v) | grep -q "main is at ${c1:0:7}: valley is up to date" \
104 || fail "-v does not say valley is up to date"
105
106# A commit whose build fails the smoke test is recorded, installs
107# nothing, and is skipped until the branch moves.
108c2="$(push broken)"
109if valley self-update > run.txt 2>&1; then fail "a broken build installed: $(cat run.txt)"; fi
110grep -q "smoke test: valley checks" run.txt || fail "not the smoke test: $(cat run.txt)"
111[ "$(jq -r .failed.commit "$state")" = "$c2" ] || fail "no failure recorded: $(cat "$state")"
112[ "$(jq -r .current.commit "$state")" = "$c1" ] || fail "state: $(cat "$state")"
113valley self-update | grep -q "skipping ${c2:0:7}" || fail "the failed commit was not skipped"
114
115# The home-manager module renders a timer, and a service whose script runs
116# the first valley on its PATH with the repository and branch it was
117# given.
118jq -e '.timers."valley-self-update".Timer == {OnBootSec: "2min", OnUnitActiveSec: "5min"}' \
119 "$unitsPath" > /dev/null || fail "the timer: $(cat "$unitsPath")"
120jq -r '.services."valley-self-update".Service.Environment[0]' "$unitsPath" \
121 | grep -q '^PATH=/home/someone/.nix-profile/bin:/etc/profiles/per-user/someone/bin:' \
122 || fail "the service's PATH: $(cat "$unitsPath")"
123mkdir -p "$TMPDIR/stub"
124printf '#!/bin/sh\nprintf "%%s\\n" "$@"\n' > "$TMPDIR/stub/valley"
125chmod +x "$TMPDIR/stub/valley"
126PATH="$TMPDIR/stub:$PATH" "$(jq -r '.services."valley-self-update".Service.ExecStart' "$unitsPath")" > args.txt
127printf '%s\n' self-update --repo git@classic-laddie:the-valley --branch main | cmp - args.txt \
128 || fail "the service runs valley with: $(cat args.txt)"
129
130touch "$out"