Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions bin/purge.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,29 @@ export LANG=C
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../lib/core/common.sh"

# Set up cleanup trap for temporary files
trap cleanup_temp_files EXIT INT TERM
source "$SCRIPT_DIR/../lib/core/log.sh"
source "$SCRIPT_DIR/../lib/clean/project.sh"

# Configuration
CURRENT_SECTION=""
PURGE_CLEANUP_DONE=false

cleanup_purge() {
local signal="${1:-EXIT}"
local exit_code="${2:-$?}"

if [[ "$PURGE_CLEANUP_DONE" == "true" ]]; then
return 0
fi
PURGE_CLEANUP_DONE=true

cleanup_temp_files
show_cursor 2> /dev/null || true
}

trap 'cleanup_purge EXIT $?' EXIT
trap 'cleanup_purge INT 130; exit 130' INT
trap 'cleanup_purge TERM 143; exit 143' TERM

# IMPORTANT: This file overrides start_section / end_section / note_activity
# from lib/core/base.sh by virtue of being sourced after it. The purge variant
Expand Down Expand Up @@ -300,7 +316,7 @@ show_help() {
# Main entry point
main() {
# Set up signal handling
trap 'show_cursor; exit 130' INT TERM
trap 'cleanup_purge INT 130; exit 130' INT TERM

# Parse arguments
for arg in "$@"; do
Expand Down
18 changes: 18 additions & 0 deletions tests/purge.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,24 @@ EOF
[[ "$output" != *"Unknown option"* ]]
}

@test "mo purge restores cursor when purge exits after hiding it" {
run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" MOLE_SKIP_MAIN=1 bash --noprofile --norc <<'EOF'
set -euo pipefail
source "$PROJECT_ROOT/bin/purge.sh"

hide_cursor() { printf 'hide\n'; }
show_cursor() { printf 'show\n'; }
start_purge() { :; }
perform_purge() { return 42; }

main
EOF

[ "$status" -eq 42 ]
[[ "$output" == *"hide"* ]]
[[ "$output" == *"show"* ]]
}

@test "mo purge: creates cache directory for stats" {
if ! command -v gtimeout >/dev/null 2>&1 && ! command -v timeout >/dev/null 2>&1; then
skip "gtimeout/timeout not available"
Expand Down