1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-04 13:16:47 +00:00

Complete automated testing

This commit is contained in:
Tw93
2025-10-12 15:43:45 +08:00
parent f9a93f6052
commit 3c56fe0633
13 changed files with 484 additions and 198 deletions

View File

@@ -1,22 +0,0 @@
#!/usr/bin/env bash
# shellcheck disable=SC2329
# Helper stub definitions for uninstall tests
setup_uninstall_stubs() {
request_sudo_access() { return 0; }
start_inline_spinner() { :; }
stop_inline_spinner() { :; }
enter_alt_screen() { :; }
leave_alt_screen() { :; }
hide_cursor() { :; }
show_cursor() { :; }
remove_apps_from_dock() { :; }
pgrep() { return 1; }
pkill() { return 0; }
sudo() { return 0; }
export -f request_sudo_access start_inline_spinner stop_inline_spinner \
enter_alt_screen leave_alt_screen hide_cursor show_cursor \
remove_apps_from_dock pgrep pkill sudo || true
}

View File

@@ -4,26 +4,41 @@ set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
if ! command -v bats >/dev/null 2>&1; then
if command -v shellcheck >/dev/null 2>&1; then
SHELLCHECK_TARGETS=()
while IFS= read -r file; do
SHELLCHECK_TARGETS+=("$file")
done < <(find "$PROJECT_ROOT/tests" -type f \( -name '*.bats' -o -name '*.sh' \) | sort)
if [[ ${#SHELLCHECK_TARGETS[@]} -gt 0 ]]; then
shellcheck --rcfile "$PROJECT_ROOT/.shellcheckrc" "${SHELLCHECK_TARGETS[@]}"
else
echo "No shell files to lint under tests/." >&2
fi
else
echo "shellcheck not found; skipping linting." >&2
fi
if command -v bats >/dev/null 2>&1; then
cd "$PROJECT_ROOT"
if [[ -z "${TERM:-}" ]]; then
export TERM="xterm-256color"
fi
if [[ $# -eq 0 ]]; then
set -- tests
fi
if [[ -t 1 ]]; then
bats -p "$@"
else
TERM="${TERM:-xterm-256color}" bats --tap "$@"
fi
else
cat <<'EOF' >&2
bats is required to run Mole's test suite.
Install via Homebrew with 'brew install bats-core' or via npm with 'npm install -g bats'.
EOF
exit 1
fi
cd "$PROJECT_ROOT"
if [[ -z "${TERM:-}" ]]; then
export TERM="xterm-256color"
fi
if [[ $# -eq 0 ]]; then
set -- tests
fi
if [[ -t 1 ]]; then
bats -p "$@"
else
TERM="${TERM:-xterm-256color}" bats --tap "$@"
fi

View File

@@ -77,8 +77,19 @@ EOF
set -euo pipefail
source "$PROJECT_ROOT/lib/common.sh"
source "$PROJECT_ROOT/lib/batch_uninstall.sh"
source "$PROJECT_ROOT/tests/helpers/uninstall_stubs.sh"
setup_uninstall_stubs
# Test stubs
request_sudo_access() { return 0; }
start_inline_spinner() { :; }
stop_inline_spinner() { :; }
enter_alt_screen() { :; }
leave_alt_screen() { :; }
hide_cursor() { :; }
show_cursor() { :; }
remove_apps_from_dock() { :; }
pgrep() { return 1; }
pkill() { return 0; }
sudo() { return 0; }
app_bundle="$HOME/Applications/TestApp.app"
mkdir -p "$app_bundle"

29
tests/z_shellcheck.bats Normal file
View File

@@ -0,0 +1,29 @@
#!/usr/bin/env bats
setup_file() {
PROJECT_ROOT="$(cd "${BATS_TEST_DIRNAME}/.." && pwd)"
export PROJECT_ROOT
}
@test "shellcheck passes for test scripts" {
if ! command -v shellcheck >/dev/null 2>&1; then
skip "shellcheck not installed"
fi
run env PROJECT_ROOT="$PROJECT_ROOT" bash --noprofile --norc <<'EOF'
set -euo pipefail
cd "$PROJECT_ROOT"
targets=()
while IFS= read -r file; do
targets+=("$file")
done < <(find "$PROJECT_ROOT/tests" -type f \( -name '*.bats' -o -name '*.sh' \) | sort)
if [[ ${#targets[@]} -eq 0 ]]; then
echo "No test shell files found"
exit 0
fi
shellcheck --rcfile "$PROJECT_ROOT/.shellcheckrc" "${targets[@]}"
EOF
printf '%s\n' "$output" >&3
[ "$status" -eq 0 ]
}