| 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 | local name="$1" |
| 29 | cat >"${src_dir}/${name}" <<EOF |
| 30 | #!/usr/bin/env bash |
| 31 | set -euo pipefail |
| 32 | if [[ "\${1:-}" == "--version" ]]; then |
| 33 | printf '%s\n' '${name} 0.9.1 (${source_sha})' |
| 34 | printf '%s\n' '${name}' >>"\${DOGFOOD_TEST_MARKER}" |
| 35 | exit 0 |
| 36 | fi |
| 37 | exit 2 |
| 38 | EOF |
| 39 | chmod +x "${src_dir}/${name}" |
| 40 | } |
| 41 | |
| 42 | make_binary codewhale |
| 43 | make_binary codew |
| 44 | make_binary codewhale-tui |
| 45 | |
| 46 | # Reproduce the old dogfood state: codew was a symlink to the dispatcher. |
| 47 | printf 'old dispatcher\n' >"${dest_dir}/codewhale" |
| 48 | ln -s "${dest_dir}/codewhale" "${dest_dir}/codew" |
| 49 | |
| 50 | cat >"${fake_bin}/zsh" <<'EOF' |
| 51 | #!/usr/bin/env bash |
| 52 | set -euo pipefail |
| 53 | [[ "${1:-}" == "-lc" ]] |
| 54 | case "${2:-}" in |
| 55 | "command -v codewhale") printf '%s\n' "${DOGFOOD_TEST_DEST}/codewhale" ;; |
| 56 | "command -v codew") printf '%s\n' "${DOGFOOD_TEST_DEST}/codew" ;; |
| 57 | "command -v codewhale-tui") printf '%s\n' "${DOGFOOD_TEST_DEST}/codewhale-tui" ;; |
| 58 | "codewhale --version") exec "${DOGFOOD_TEST_DEST}/codewhale" --version ;; |
| 59 | "codew --version") exec "${DOGFOOD_TEST_DEST}/codew" --version ;; |
| 60 | "codewhale-tui --version") exec "${DOGFOOD_TEST_DEST}/codewhale-tui" --version ;; |
| 61 | *) exit 2 ;; |
| 62 | esac |
| 63 | EOF |
| 64 | chmod +x "${fake_bin}/zsh" |
| 65 | |
| 66 | HOME="${tmp_dir}/home" \ |
| 67 | PATH="${fake_bin}:${PATH}" \ |
| 68 | DOGFOOD_TEST_DEST="${dest_dir}" \ |
| 69 | DOGFOOD_TEST_MARKER="${marker}" \ |
| 70 | CODEWHALE_INSTALL_DIRS="${dest_dir}" \ |
| 71 | CODEWHALE_DOGFOOD_RECEIPT_DIR="${receipt_dir}" \ |
| 72 | "${fixture}/scripts/release/install-dogfood.sh" "${src_dir}" >/dev/null |
| 73 | |
| 74 | for name in codewhale codew codewhale-tui; do |
| 75 | cmp -s "${src_dir}/${name}" "${dest_dir}/${name}" || { |
| 76 | echo "installed ${name} differs from the built fixture" >&2 |
| 77 | exit 1 |
| 78 | } |
| 79 | done |
| 80 | |
| 81 | if [[ -L "${dest_dir}/codew" ]]; then |
| 82 | echo "dogfood install left codew as a symlink" >&2 |
| 83 | exit 1 |
| 84 | fi |
| 85 | |
| 86 | [[ "$(grep -c '^codew$' "${marker}")" -ge 2 ]] || { |
| 87 | echo "native codew was not exercised before and after installation" >&2 |
| 88 | exit 1 |
| 89 | } |
| 90 | |
| 91 | receipt="$(find "${receipt_dir}" -type f -name '*.txt' -print -quit)" |
| 92 | [[ -n "${receipt}" ]] |
| 93 | grep -Fq "codew_sha256=" "${receipt}" |
| 94 | grep -Fq "fresh_shell_codew=${dest_dir}/codew" "${receipt}" |
| 95 | grep -Fq "installed_path=${dest_dir}/codew" "${receipt}" |
| 96 | |
| 97 | echo "install-dogfood tests passed" |
| 98 |