1
0
mirror of https://github.com/tw93/Mole.git synced 2026-03-22 19:05:07 +00:00

Fix --dry-run hiding system cleanup preview (#493)

The dry-run mode hardcoded SYSTEM_CLEAN=false, which meant
the entire system section was silently skipped during preview.
Users had no way to see what /Library/Caches, /private/var/log,
diagnostics, or other sudo-required paths would be cleaned.

Now dry-run checks for a cached sudo session (sudo -n) and
includes the system preview when available. When sudo isn't
cached, a hint tells the user how to get the full preview
without running the whole script as root.
This commit is contained in:
Luke Bullimore
2026-02-25 02:22:56 +00:00
committed by GitHub
parent 7c1fedae12
commit ee1f2bd372
2 changed files with 47 additions and 1 deletions

View File

@@ -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

View File

@@ -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"