| 1 | # shellcheck shell=bash |
| 2 | # Paths and fixtures come from the derivation environment. |
| 3 | # shellcheck disable=SC2154 |
| 4 | export HOME="$TMPDIR/home" XDG_STATE_HOME="$TMPDIR/state" |
| 5 | mkdir -p "$HOME" |
| 6 | export GIT_CONFIG_NOSYSTEM=1 |
| 7 | git config --global user.name valley-check |
| 8 | git config --global user.email valley-check@localhost |
| 9 | git config --global init.defaultBranch main |
| 10 | |
| 11 | fail() { |
| 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'" |
| 20 | bash "$cliScript" --version | grep -q '^valley unstamped' \ |
| 21 | || fail "the bare script claims a stamp: $(bash "$cliScript" --version)" |
| 22 | valley 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. |
| 26 | upstream="$TMPDIR/upstream.git" |
| 27 | git init --quiet --bare "$upstream" |
| 28 | git clone --quiet "$upstream" "$TMPDIR/work" 2> /dev/null |
| 29 | push() { |
| 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 | } |
| 34 | git 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. |
| 43 | export STAND_IN="$TMPDIR/nix" UPSTREAM="$upstream" VALLEY_SCRIPT="$valleyScript" VALLEY_PACKAGE="$valley" |
| 44 | mkdir -p "$STAND_IN/bin" "$STAND_IN/store" |
| 45 | echo '{}' > "$STAND_IN/profile.json" |
| 46 | printf '#!%s\n' "$(command -v bash)" > "$STAND_IN/bin/nix" |
| 47 | cat >> "$STAND_IN/bin/nix" <<'EOF' |
| 48 | set -euo pipefail |
| 49 | while [ "${1:-}" = --extra-experimental-features ]; do shift 2; done |
| 50 | profile="$STAND_IN/profile.json" |
| 51 | case "$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 ;; |
| 83 | esac |
| 84 | EOF |
| 85 | chmod +x "$STAND_IN/bin/nix" |
| 86 | export PATH="$STAND_IN/bin:$PATH" |
| 87 | state="$XDG_STATE_HOME/valley/self-update.json" |
| 88 | installed() { 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. |
| 91 | c1="$(push one)" |
| 92 | valley self-update > run.txt || fail "the first run failed: $(cat run.txt)" |
| 93 | grep -q "smoke test passed: valley 0.1.0-${c1:0:7}" run.txt || fail "no smoke test: $(cat run.txt)" |
| 94 | grep -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" |
| 102 | git -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. |
| 108 | c2="$(push broken)" |
| 109 | if valley self-update > run.txt 2>&1; then fail "a broken build installed: $(cat run.txt)"; fi |
| 110 | grep -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")" |
| 113 | valley 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. |
| 118 | jq -e '.timers."valley-self-update".Timer == {OnBootSec: "2min", OnUnitActiveSec: "5min"}' \ |
| 119 | "$unitsPath" > /dev/null || fail "the timer: $(cat "$unitsPath")" |
| 120 | jq -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")" |
| 123 | mkdir -p "$TMPDIR/stub" |
| 124 | printf '#!/bin/sh\nprintf "%%s\\n" "$@"\n' > "$TMPDIR/stub/valley" |
| 125 | chmod +x "$TMPDIR/stub/valley" |
| 126 | PATH="$TMPDIR/stub:$PATH" "$(jq -r '.services."valley-self-update".Service.ExecStart' "$unitsPath")" > args.txt |
| 127 | printf '%s\n' self-update --repo git@valley-host:the-valley --branch main | cmp - args.txt \ |
| 128 | || fail "the service runs valley with: $(cat args.txt)" |
| 129 | |
| 130 | touch "$out" |