diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3c5563b..f6c6790 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -72,6 +72,11 @@ jobs: cargo test --workspace cargo test -p lla --features wasm-plugins + - name: Test Unix installer + if: runner.os != 'Windows' + shell: bash + run: ./scripts/test_install_sh.sh + - name: Compile SDK fixtures run: | cargo check --manifest-path sdk/tests/fixtures/minimal_native/Cargo.toml diff --git a/CHANGELOG.md b/CHANGELOG.md index 2156abf..63d92cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- The Unix installer can once again be executed through the documented + `curl ... | bash` command without an unset `BASH_SOURCE` error. - The Windows installer now handles GitHub checksum manifests correctly in Windows PowerShell 5.1 and avoids its legacy Internet Explorer parsing prompt. diff --git a/install.sh b/install.sh index 9347a5b..0f58b57 100755 --- a/install.sh +++ b/install.sh @@ -166,6 +166,6 @@ main() { install_binary } -if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then +if [[ -z "${BASH_SOURCE[0]:-}" || "${BASH_SOURCE[0]}" == "$0" ]]; then main "$@" fi diff --git a/scripts/test_install_sh.sh b/scripts/test_install_sh.sh new file mode 100755 index 0000000..45f49a5 --- /dev/null +++ b/scripts/test_install_sh.sh @@ -0,0 +1,101 @@ +#!/usr/bin/env bash + +set -euo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +test_dir="$(mktemp -d)" +trap 'rm -rf "$test_dir"' EXIT + +mkdir -p "$test_dir/bin" + +cat >"$test_dir/bin/uname" <<'EOF' +#!/usr/bin/env bash +case "${1:-}" in + -s) printf '%s\n' Linux ;; + -m) printf '%s\n' x86_64 ;; + *) exit 1 ;; +esac +EOF + +cat >"$test_dir/bin/curl" <<'EOF' +#!/usr/bin/env bash +set -euo pipefail + +output="" +url="" +while (( $# > 0 )); do + case "$1" in + -o) + output="$2" + shift 2 + ;; + http://*|https://*) + url="$1" + shift + ;; + *) + shift + ;; + esac +done + +case "$url" in + */releases/latest) + printf '%s\n' '{"tag_name":"v0.0.0-test"}' + ;; + */SHA256SUMS) + printf '%s %s\n' "$MOCK_SHA256" lla-linux-amd64 >"$output" + ;; + */lla-linux-amd64) + printf '%s\n' 'fake lla binary' >"$output" + ;; + *) + printf 'Unexpected curl URL: %s\n' "$url" >&2 + exit 1 + ;; +esac +EOF + +cat >"$test_dir/bin/sudo" <<'EOF' +#!/usr/bin/env bash +printf '%s\n' "$*" >>"$MOCK_SUDO_LOG" +EOF + +chmod +x "$test_dir/bin/uname" "$test_dir/bin/curl" "$test_dir/bin/sudo" + +if command -v sha256sum >/dev/null 2>&1; then + mock_sha256="$(printf '%s\n' 'fake lla binary' | sha256sum | awk '{print $1}')" +else + mock_sha256="$(printf '%s\n' 'fake lla binary' | shasum -a 256 | awk '{print $1}')" +fi + +run_installer() { + local mode="$1" + local output_file="$test_dir/${mode}.out" + local sudo_log="$test_dir/${mode}.sudo" + + : >"$sudo_log" + if [[ "$mode" == direct ]]; then + PATH="$test_dir/bin:$PATH" \ + MOCK_SHA256="$mock_sha256" \ + MOCK_SUDO_LOG="$sudo_log" \ + bash "$repo_root/install.sh" >"$output_file" + else + PATH="$test_dir/bin:$PATH" \ + MOCK_SHA256="$mock_sha256" \ + MOCK_SUDO_LOG="$sudo_log" \ + bash -s -- <"$repo_root/install.sh" >"$output_file" + fi + + grep -Fq 'lla v0.0.0-test has been installed successfully!' "$output_file" + grep -Fq 'mv ' "$sudo_log" +} + +bash -n "$repo_root/install.sh" +run_installer direct +run_installer piped + +sourced_output="$(bash -c 'source "$1"; printf sourced' _ "$repo_root/install.sh")" +[[ "$sourced_output" == sourced ]] + +printf '%s\n' 'install.sh tests passed'