diff --git a/bin/clean.sh b/bin/clean.sh index c0e7d01..b5f16ea 100755 --- a/bin/clean.sh +++ b/bin/clean.sh @@ -717,7 +717,6 @@ start_cleanup() { if [[ "$DRY_RUN" == "true" ]]; then echo -e "${YELLOW}Dry Run Mode${NC}, Preview only, no deletions" echo "" - SYSTEM_CLEAN=false ensure_user_file "$EXPORT_LIST_FILE" cat > "$EXPORT_LIST_FILE" << EOF @@ -732,6 +731,17 @@ start_cleanup() { # EOF + + # Preview system section when sudo is already cached (no password prompt). + if sudo -n true 2> /dev/null; then + SYSTEM_CLEAN=true + echo -e "${GREEN}${ICON_SUCCESS}${NC} Admin access available, system preview included" + echo "" + else + SYSTEM_CLEAN=false + echo -e "${GRAY}${ICON_WARNING} System caches need sudo, run ${NC}sudo -v && mo clean --dry-run${GRAY} for full preview${NC}" + echo "" + fi return fi diff --git a/tests/clean_core.bats b/tests/clean_core.bats index 3994c64..0b78f6a 100644 --- a/tests/clean_core.bats +++ b/tests/clean_core.bats @@ -34,6 +34,42 @@ setup() { [[ "$output" != *"Deep system-level cleanup"* ]] } +@test "mo clean --dry-run includes system preview when sudo is cached" { + local mock_bin="$HOME/bin" + mkdir -p "$mock_bin" + cat > "$mock_bin/sudo" << 'MOCK' +#!/bin/bash +# Shim: sudo -n true succeeds, all other sudo calls are no-ops. +if [[ "$1" == "-n" && "$2" == "true" ]]; then exit 0; fi +if [[ "$1" == "test" ]]; then exit 1; fi +if [[ "$1" == "find" ]]; then exit 0; fi +exit 0 +MOCK + chmod +x "$mock_bin/sudo" + + run env HOME="$HOME" MOLE_TEST_MODE=1 PATH="$mock_bin:$PATH" \ + "$PROJECT_ROOT/mole" clean --dry-run + [ "$status" -eq 0 ] + [[ "$output" == *"system preview included"* ]] +} + +@test "mo clean --dry-run shows hint when sudo is not cached" { + local mock_bin="$HOME/bin" + mkdir -p "$mock_bin" + cat > "$mock_bin/sudo" << 'MOCK' +#!/bin/bash +# Shim: sudo -n always fails (no cached credentials). +exit 1 +MOCK + chmod +x "$mock_bin/sudo" + + run env HOME="$HOME" MOLE_TEST_MODE=1 PATH="$mock_bin:$PATH" \ + "$PROJECT_ROOT/mole" clean --dry-run + [ "$status" -eq 0 ] + [[ "$output" == *"sudo -v"* ]] + [[ "$output" == *"full preview"* ]] +} + @test "mo clean --dry-run reports user cache without deleting it" { mkdir -p "$HOME/Library/Caches/TestApp" echo "cache data" > "$HOME/Library/Caches/TestApp/cache.tmp"