From 717451b2a1f8a9a4cbc2ef5832bf64c51fbbbb0f Mon Sep 17 00:00:00 2001 From: Hassan Raza Date: Sat, 30 May 2026 14:46:08 +0500 Subject: [PATCH] fix(update): target invoked install path --- mole | 42 ++++++------ tests/update.bats | 169 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 191 insertions(+), 20 deletions(-) create mode 100644 tests/update.bats diff --git a/mole b/mole index 548363601..a51cfac63 100755 --- a/mole +++ b/mole @@ -128,6 +128,13 @@ resolve_mole_source_path() { [[ -n "$mole_path" ]] && printf '%s\n' "$mole_path" } +resolve_current_mole_path() { + local mole_path + mole_path=$(resolve_mole_source_path || true) + [[ -n "$mole_path" ]] || return 1 + printf '%s\n' "$mole_path" +} + is_homebrew_mole_path() { local mole_path="$1" local has_brew="$2" @@ -164,31 +171,19 @@ is_homebrew_mole_path() { return 1 } -# Install detection (Homebrew vs manual). -# Default path checks only the active script, keeping source-tree commands fast. -# Pass --include-path when update/remove flows intentionally need to inspect -# an installed `mole` found through PATH. +# Install detection follows the invoked Mole script, not PATH. is_homebrew_install() { - local include_path_lookup="${1:-}" local has_brew=false if command -v brew > /dev/null 2>&1; then has_brew=true fi local mole_path - mole_path=$(resolve_mole_source_path || true) + mole_path=$(resolve_current_mole_path || true) if is_homebrew_mole_path "$mole_path" "$has_brew"; then return 0 fi - if [[ "$include_path_lookup" == "--include-path" ]]; then - local path_mole - path_mole=$(command -v mole 2> /dev/null || true) - if [[ -n "$path_mole" && "$path_mole" != "$mole_path" ]] && is_homebrew_mole_path "$path_mole" "$has_brew"; then - return 0 - fi - fi - return 1 } @@ -391,7 +386,7 @@ update_mole() { } trap '_update_cleanup; update_interrupted=true; echo ""; exit 130' INT TERM - if is_homebrew_install --include-path; then + if is_homebrew_install; then if [[ "$nightly_update" == "true" ]]; then local review_icon="${ICON_REVIEW:-☞}" log_error "Nightly update is only available for script installations. Homebrew installs follow stable releases." @@ -402,6 +397,18 @@ update_mole() { exit 0 fi + local mole_path + if ! mole_path=$(resolve_current_mole_path); then + log_error "Unable to resolve current Mole path" + exit 1 + fi + + local install_dir + if ! install_dir="$(cd "$(dirname "$mole_path")" && pwd)"; then + log_error "Unable to resolve current Mole install directory" + exit 1 + fi + local latest="" local download_label="Downloading latest version..." local install_label="Installing update..." @@ -500,11 +507,6 @@ update_mole() { if [[ -t 1 ]]; then stop_inline_spinner; fi chmod +x "$tmp_installer" - local mole_path - mole_path="$(command -v mole 2> /dev/null || echo "$0")" - local install_dir - install_dir="$(cd "$(dirname "$mole_path")" && pwd)" - local requires_sudo="false" if [[ ! -w "$install_dir" ]]; then requires_sudo="true" diff --git a/tests/update.bats b/tests/update.bats new file mode 100644 index 000000000..afe187361 --- /dev/null +++ b/tests/update.bats @@ -0,0 +1,169 @@ +#!/usr/bin/env bats + +setup_file() { + PROJECT_ROOT="$(cd "${BATS_TEST_DIRNAME}/.." && pwd)" + export PROJECT_ROOT +} + +setup() { + HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-update-home.XXXXXX")" + TEST_ROOT="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-update-case.XXXXXX")" + export HOME TEST_ROOT +} + +teardown() { + case "${HOME:-}" in + "${BATS_TEST_DIRNAME}/tmp-update-home."*) rm -rf "$HOME" ;; + esac + case "${TEST_ROOT:-}" in + "${BATS_TEST_DIRNAME}/tmp-update-case."*) rm -rf "$TEST_ROOT" ;; + esac +} + +make_manual_mole_install() { + local install_dir="$1" + local config_dir="$2" + local version="$3" + mkdir -p "$install_dir" "$config_dir" + sed \ + -e "s|^SCRIPT_DIR=.*|SCRIPT_DIR=\"$config_dir\"|" \ + -e "s/^VERSION=\".*\"$/VERSION=\"$version\"/" \ + "$PROJECT_ROOT/mole" > "$install_dir/mole" + cp "$PROJECT_ROOT/mo" "$install_dir/mo" + cp -R "$PROJECT_ROOT/lib" "$config_dir/lib" + chmod +x "$install_dir/mole" "$install_dir/mo" +} + +make_homebrew_shadow() { + local bin_dir="$1" + local cellar_mole="$2" + mkdir -p "$bin_dir" "$(dirname "$cellar_mole")" + cp "$PROJECT_ROOT/mole" "$cellar_mole" + cp -R "$PROJECT_ROOT/lib" "$bin_dir/lib" + chmod +x "$cellar_mole" + ln -sf "$cellar_mole" "$bin_dir/mole" + ln -sf "$cellar_mole" "$bin_dir/mo" + + cat > "$bin_dir/brew" <<'SCRIPT' +#!/usr/bin/env bash +printf '%s\n' "$*" >> "$BREW_LOG" +case "${1:-}" in + list) + if [[ "${2:-}" == "--versions" ]]; then + printf 'mole 9.9.9\n' + fi + exit 0 + ;; + update | upgrade) + exit 0 + ;; +esac +exit 0 +SCRIPT + chmod +x "$bin_dir/brew" +} + +make_update_curl_stub() { + local bin_dir="$1" + local latest_version="$2" + cat > "$bin_dir/curl" <