Files
ThothII/scripts/test-install-tht.sh

86 lines
2.8 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
repository_root=$(cd "$(dirname "$0")/.." && pwd)
installer="$repository_root/scripts/install-tht.sh"
temporary_root=$(mktemp -d)
trap 'rm -rf "$temporary_root"' EXIT HUP INT TERM
fail() {
echo "test-install-tht.sh: $*" >&2
exit 1
}
test -x "$installer" || fail "install-tht.sh must be executable for the documented invocation"
sha256() {
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$1" | awk '{print $1}'
else
shasum -a 256 "$1" | awk '{print $1}'
fi
}
write_artifact() {
local path=$1
local version=$2
cat >"$path" <<EOF
#!/usr/bin/env bash
set -euo pipefail
case "\${1:-}" in
--help|-h) echo "Usage: tht" ;;
version) echo "$version" ;;
*) echo "unexpected argument: \${1:-}" >&2; exit 2 ;;
esac
EOF
chmod 0644 "$path"
}
test_directory="$temporary_root/command directory"
artifact_one="$temporary_root/tht-one"
artifact_two="$temporary_root/tht-two"
write_artifact "$artifact_one" "tht test version one"
write_artifact "$artifact_two" "tht test version two"
system_path=$PATH
installer_bash=${THT_TEST_BASH:-/bin/bash}
test -x "$installer_bash" || fail "THT_TEST_BASH must name an executable Bash interpreter"
run_installer() {
local artifact=$1
THT_INSTALL_DIRECTORY="$test_directory" \
THT_BUILD_ARTIFACT="$artifact" \
THT_BUILD_ARTIFACT_SHA256="$(sha256 "$artifact")" \
PATH="$test_directory:$system_path" \
"$installer_bash" "$installer"
}
run_installer "$artifact_one"
test -x "$test_directory/tht" || fail "installed tht is not executable"
first_inode=$(stat -f '%i' "$test_directory/tht" 2>/dev/null || stat -c '%i' "$test_directory/tht")
first_version=$(PATH="$test_directory:$system_path" tht version)
test "$first_version" = "tht test version one" || fail "installed command did not run version one"
run_installer "$artifact_two"
second_inode=$(stat -f '%i' "$test_directory/tht" 2>/dev/null || stat -c '%i' "$test_directory/tht")
second_version=$(PATH="$test_directory:$system_path" tht version)
test "$second_version" = "tht test version two" || fail "replacement did not install version two"
test "$first_inode" != "$second_inode" || fail "replacement did not atomically replace the destination"
run_installer "$artifact_two"
third_version=$(PATH="$test_directory:$system_path" tht version)
test "$third_version" = "tht test version two" || fail "re-running installer was not idempotent"
if THT_INSTALL_DIRECTORY="$test_directory" \
THT_BUILD_ARTIFACT="$artifact_one" \
THT_BUILD_ARTIFACT_SHA256="not-a-checksum" \
PATH="$test_directory:$system_path" \
bash "$installer" >/dev/null 2>&1; then
fail "installer accepted a checksum mismatch"
fi
after_failed_checksum=$(PATH="$test_directory:$system_path" tht version)
test "$after_failed_checksum" = "tht test version two" || fail "failed install replaced the existing command"
echo "shell tht installer tests passed."