diff --git a/test/unit/wt-sh.bats b/test/unit/wt-sh.bats new file mode 100644 index 0000000..7d120cb --- /dev/null +++ b/test/unit/wt-sh.bats @@ -0,0 +1,35 @@ +#!/usr/bin/env bats + +# Unit tests for wt.sh + +setup() { + load '../test_helper/common' + setup_test_env +} + +teardown() { + teardown_test_env +} + +# ============================================================================= +# Tests for _WT_ROOT consistency +# ============================================================================= + +@test "_WT_ROOT default in wt.sh matches INSTALL_DIR in install.sh" { + # Extract the default path from wt.sh: _WT_ROOT="${_WT_ROOT:-$HOME/.wt}" + local wt_line + wt_line=$(grep '_WT_ROOT=.*HOME' "$PROJECT_ROOT/wt.sh" | head -1) + # Pull out the $HOME/... portion + local wt_path + wt_path=$(echo "$wt_line" | sed 's/.*\(\$HOME\/[^}"]*\).*/\1/') + + # Extract INSTALL_DIR from install.sh: INSTALL_DIR="$HOME/.wt" + local install_line + install_line=$(grep 'INSTALL_DIR=.*HOME' "$PROJECT_ROOT/install.sh" | head -1) + local install_path + install_path=$(echo "$install_line" | sed 's/.*"\(\$HOME\/[^"]*\)".*/\1/') + + [ -n "$wt_path" ] + [ -n "$install_path" ] + [ "$wt_path" = "$install_path" ] +} diff --git a/wt.sh b/wt.sh index f1ee957..5e75d46 100755 --- a/wt.sh +++ b/wt.sh @@ -11,18 +11,9 @@ # Usage: source this file, then run `wt help` for details. # -# Capture script path at top level (works in both bash and zsh) -# In bash, BASH_SOURCE[0] gives the sourced file path. -# In zsh, %x prompt expansion reliably gives the current source file path, -# even when FUNCTION_ARGZERO is unset (where $0 would return "-zsh"). -if [[ -n "${BASH_SOURCE[0]:-}" ]]; then - _WT_SCRIPT_PATH="${BASH_SOURCE[0]}" -elif [[ -n "${ZSH_VERSION:-}" ]]; then - # %x prompt expansion reliably gives the current source file path - _WT_SCRIPT_PATH="$(print -P '%x')" -else - _WT_SCRIPT_PATH="$0" -fi +# Installation root — all wt files live under this directory. +# Override by setting _WT_ROOT before sourcing (e.g., for development). +_WT_ROOT="${_WT_ROOT:-$HOME/.wt}" # Ensure this file is sourced, not executed; exit with error if executed directly _wt_ensure_sourced() { @@ -30,54 +21,26 @@ _wt_ensure_sourced() { # In zsh, check if we're being sourced by examining ZSH_EVAL_CONTEXT # When sourced, it contains "toplevel:file" or similar patterns with "file" if [[ "${ZSH_EVAL_CONTEXT:-}" != *:file:* && "${ZSH_EVAL_CONTEXT:-}" != *:file ]]; then - # Not sourced - but this check can be unreliable, so also check $0 - if [[ "$0" == "$_WT_SCRIPT_PATH" ]]; then - echo "Error: This file must be sourced, not executed." >&2 - echo "" >&2 - echo "Add this to your ~/.zshrc:" >&2 - echo " source $_WT_SCRIPT_PATH" >&2 - echo "" >&2 - echo "Then use: wt [args]" >&2 - exit 1 - fi + echo "Error: This file must be sourced, not executed." >&2 + echo "" >&2 + echo "Add this to your ~/.zshrc:" >&2 + echo " source $_WT_ROOT/wt.sh" >&2 + echo "" >&2 + echo "Then use: wt [args]" >&2 + exit 1 fi elif [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then # Bash: BASH_SOURCE[0] equals $0 when executed directly echo "Error: This file must be sourced, not executed." >&2 echo "" >&2 echo "Add this to your ~/.bashrc:" >&2 - echo " source $_WT_SCRIPT_PATH" >&2 + echo " source $_WT_ROOT/wt.sh" >&2 echo "" >&2 echo "Then use: wt [args]" >&2 exit 1 fi } -# Resolve the root directory where wt.sh lives -_wt_resolve_root() { - local source="$_WT_SCRIPT_PATH" - local root - # Resolve symlinks to find the real location - while [[ -L "$source" ]]; do - local dir - dir="$(command cd -P "$(dirname "$source")" && pwd)" - source="$(readlink "$source")" - # If source is relative, resolve it relative to the symlink's directory - [[ "$source" != /* ]] && source="$dir/$source" - done - root="$(command cd -P "$(dirname "$source")" && pwd)" - - # Validate: root must contain bin/ and lib/ directories - if [[ -d "$root/bin" && -d "$root/lib" ]]; then - echo "$root" - elif [[ -d "$HOME/.wt/bin" && -d "$HOME/.wt/lib" ]]; then - echo "$HOME/.wt" - else - echo "wt: cannot determine installation root" >&2 - return 1 - fi -} - # helper for sourcing a library file from lib/ directory # Args: $1 = library name, $2 = "optional" to skip error if not found _wt_source_lib() { @@ -86,8 +49,6 @@ _wt_source_lib() { if [[ -f "$_WT_ROOT/lib/$lib" ]]; then . "$_WT_ROOT/lib/$lib" - elif [[ -f "$HOME/.wt/lib/$lib" ]]; then - . "$HOME/.wt/lib/$lib" elif [[ "$required" != "optional" ]]; then echo "wt: cannot find required library: $lib" >&2 return 1 @@ -167,7 +128,6 @@ _wt_source_shell_completion() { # ═══════════════════════════════════════════════════════════════════════════════ _wt_ensure_sourced -_WT_ROOT="$(_wt_resolve_root)" _wt_source_lib wt-common _wt_source_lib wt-help _wt_source_shell_completion # completion for `wt` and `wt-*` commands