返回 CodeWhale
install-dogfood.test.sh
根目录 / scripts / release / install-dogfood.test.sh
1 #!/usr/bin/env bash
2 set -euo pipefail
3
4 repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
5 tmp_dir="$(mktemp -d)"
6 trap 'rm -rf "${tmp_dir}"' EXIT
7
8 fixture="${tmp_dir}/repo"
9 src_dir="${fixture}/target/release"
10 dest_dir="${tmp_dir}/installed"
11 receipt_dir="${tmp_dir}/receipts"
12 fake_bin="${tmp_dir}/bin"
13 marker="${tmp_dir}/invocations.log"
14
15 mkdir -p "${fixture}/scripts/release" "${src_dir}" "${dest_dir}" "${fake_bin}"
16 cp "${repo_root}/scripts/release/install-dogfood.sh" \
17 "${fixture}/scripts/release/install-dogfood.sh"
18 printf 'target/\n' >"${fixture}/.gitignore"
19
20 git -C "${fixture}" init --quiet
21 git -C "${fixture}" config user.name "Dogfood Test"
22 git -C "${fixture}" config user.email "dogfood-test@example.invalid"
23 git -C "${fixture}" add .gitignore scripts/release/install-dogfood.sh
24 git -C "${fixture}" -c commit.gpgsign=false commit --quiet -m "fixture"
25 source_sha="$(git -C "${fixture}" rev-parse HEAD)"
26
27 make_binary() {
28 cat >"${src_dir}/codewhale" <<EOF
29 #!/usr/bin/env bash
30 set -euo pipefail
31 if [[ "\${1:-}" == "--version" ]]; then
32 printf '%s\n' 'codewhale 0.9.1 (${source_sha})'
33 printf '%s\n' "\${0##*/}" >>"\${DOGFOOD_TEST_MARKER}"
34 exit 0
35 fi
36 exit 2
37 EOF
38 chmod +x "${src_dir}/codewhale"
39 }
40
41 make_binary
42
43 # Reproduce the old dogfood state: codew was a symlink to the dispatcher.
44 printf 'old dispatcher\n' >"${dest_dir}/codewhale"
45 ln -s "${dest_dir}/codewhale" "${dest_dir}/codew"
46
47 cat >"${fake_bin}/zsh" <<'EOF'
48 #!/usr/bin/env bash
49 set -euo pipefail
50 [[ "${1:-}" == "-lc" ]]
51 case "${2:-}" in
52 "command -v codewhale") printf '%s\n' "${DOGFOOD_TEST_DEST}/codewhale" ;;
53 "command -v codew") printf '%s\n' "${DOGFOOD_TEST_DEST}/codew" ;;
54 "codewhale --version") exec "${DOGFOOD_TEST_DEST}/codewhale" --version ;;
55 "codew --version") exec "${DOGFOOD_TEST_DEST}/codew" --version ;;
56 *) exit 2 ;;
57 esac
58 EOF
59 chmod +x "${fake_bin}/zsh"
60
61 HOME="${tmp_dir}/home" \
62 PATH="${fake_bin}:${PATH}" \
63 DOGFOOD_TEST_DEST="${dest_dir}" \
64 DOGFOOD_TEST_MARKER="${marker}" \
65 CODEWHALE_INSTALL_DIRS="${dest_dir}" \
66 CODEWHALE_DOGFOOD_RECEIPT_DIR="${receipt_dir}" \
67 "${fixture}/scripts/release/install-dogfood.sh" "${src_dir}" >/dev/null
68
69 for name in codewhale codew; do
70 cmp -s "${src_dir}/codewhale" "${dest_dir}/${name}" || {
71 echo "installed ${name} differs from the built fixture" >&2
72 exit 1
73 }
74 done
75
76 if [[ -L "${dest_dir}/codew" ]]; then
77 echo "dogfood install left codew as a symlink" >&2
78 exit 1
79 fi
80
81 [[ "$(grep -c '^codew$' "${marker}")" -ge 1 ]] || {
82 echo "the installed codew command name was not exercised" >&2
83 exit 1
84 }
85
86 receipt="$(find "${receipt_dir}" -type f -name '*.txt' -print -quit)"
87 [[ -n "${receipt}" ]]
88 grep -Fq "codew_sha256=" "${receipt}"
89 grep -Fq "fresh_shell_codew=${dest_dir}/codew" "${receipt}"
90 grep -Fq "installed_path=${dest_dir}/codew" "${receipt}"
91
92 echo "install-dogfood tests passed"
93
94 # A shell fixture cannot expose codesign's filename-derived identities. Run
95 # the actual installer on a native Mach-O and require one signed artifact for
96 # both command names, as the updater's sibling-ownership guard requires.
97 if [[ "$(uname -s)" == "Darwin" ]]; then
98 cat >"${tmp_dir}/native.c" <<EOF
99 #include <stdio.h>
100 #include <string.h>
101 int main(int argc, char **argv) {
102 if (argc == 2 && strcmp(argv[1], "--version") == 0) {
103 puts("codewhale 0.9.1 (${source_sha})");
104 return 0;
105 }
106 return 2;
107 }
108 EOF
109 /usr/bin/cc "${tmp_dir}/native.c" -o "${src_dir}/codewhale"
110 PATH="${fake_bin}:${PATH}" \
111 DOGFOOD_TEST_DEST="${dest_dir}" \
112 DOGFOOD_TEST_MARKER="${marker}" \
113 CODEWHALE_INSTALL_DIRS="${dest_dir}" \
114 CODEWHALE_DOGFOOD_RECEIPT_DIR="${receipt_dir}" \
115 "${fixture}/scripts/release/install-dogfood.sh" "${src_dir}" >/dev/null
116 cmp -s "${dest_dir}/codewhale" "${dest_dir}/codew" || {
117 echo "native dogfood aliases acquired different code signatures" >&2
118 exit 1
119 }
120 codesign --verify --strict "${dest_dir}/codewhale"
121 codesign --verify --strict "${dest_dir}/codew"
122 echo "native macOS dogfood identity test passed"
123 fi
124
124 lines BASH