Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Keep Undo from offering again what the helper's own rule refuses

Țugui Dragoșa521c3c64cd6
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Support/Output.swift b/Packages/PeelCore/Sources/PeelCommandLine/Support/Output.swift
index fedea610..b36565b3 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Support/Output.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Support/Output.swift
@@ -253,6 +253,7 @@ extension RestoreFailure {
"your exclusions keep Peel from putting anything there; take it off the list, or drag it out of the Trash in Finder"
case .notAllowed(let refusal): "Peel protects that place now (\(refusal.summary)); drag it out of the Trash in Finder"
case .notInATrash: "it isn't in the Trash where Peel put it"
+ case .notAllowedByTheHelper(let message): Output.plain(message)
case .locked: "it is locked; unlock it in Finder's Get Info"
case .folderLocked: "the folder it goes back to is locked; unlock it in Finder's Get Info"
case .folderGone:
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
index 58e3288c..c199a237 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
@@ -212,6 +212,8 @@ public enum RestoreFailure: Sendable, Hashable {
case needsHelper
/// The guard refuses the place it would go back to, for this reason.
case notAllowed(GuardRefusal)
+ /// The helper's own rule refuses the place it would go back to, in the helper's words.
+ case notAllowedByTheHelper(String)
/// The record names something that isn't in a Trash where Peel put it.
case notInATrash
/// It is locked in the Trash, as Finder's Get Info locks it.
@@ -227,7 +229,7 @@ public enum RestoreFailure: Sendable, Hashable {
/// True when asking again can't help: the item left the Trash, or Peel puts nothing back from or to there.
public var isFinal: Bool {
switch self {
- case .missingFromTrash, .notAllowed, .notInATrash: true
+ case .missingFromTrash, .notAllowed, .notAllowedByTheHelper, .notInATrash: true
case .alreadyThere, .needsHelper, .locked, .folderLocked, .folderGone, .needsAppManagement, .notPermitted,
.failed:
false
@@ -794,7 +796,7 @@ public struct TrashService: Sendable {
if Self.isFolderGone(puttingBack: item) { return .folderGone }
let reach = HelperReach(environment: environment)
if let rejection = reach.refusal(ofPuttingBack: item.trashedURL, at: item.originalURL) {
- return .failed(rejection.explanation)
+ return .notAllowedByTheHelper(rejection.explanation)
}
if !canUseHelper { return .needsHelper }
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
index 296c40c4..5c27ff26 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
@@ -609,7 +609,8 @@ struct TrashServiceTests {
let throughTheHelper = service.refusal(ofPuttingBack: item, canUseHelper: true)
#expect(byThePerson == nil)
- #expect(throughTheHelper == .failed(PrivilegedPathPolicy.Rejection.loadsCode.explanation))
+ #expect(throughTheHelper == .notAllowedByTheHelper(PrivilegedPathPolicy.Rejection.loadsCode.explanation))
+ #expect(throughTheHelper?.isFinal == true, "the helper's own rule doesn't pass by asking again")
}
@Test func anItemLockedInTheTrashOrAFolderLockedOnItsWayBackIsSaidSo() async throws {
@@ -694,7 +695,7 @@ struct TrashServiceTests {
defer { try? directory.setPermissions(0o755, of: locked) }
let outside = PrivilegedPathPolicy.Rejection.outsideAllowedLocations.explanation
- #expect(service.refusal(ofPuttingBack: item, canUseHelper: true) == .failed(outside))
+ #expect(service.refusal(ofPuttingBack: item, canUseHelper: true) == .notAllowedByTheHelper(outside))
}
@Test func aMoveRefusedForAnItemLockedInFinderSaysItIsLocked() async throws {
diff --git a/Peel/Views/Presentation.swift b/Peel/Views/Presentation.swift
index e396f0f2..f62707e0 100644
--- a/Peel/Views/Presentation.swift
+++ b/Peel/Views/Presentation.swift
@@ -258,6 +258,7 @@ extension RestoreFailure {
String(localized: "Your exclusions in Settings keep Peel from putting anything back there. Take it off the list to put this back, or drag it out of the Trash in Finder.")
case .notAllowed: String(localized: "Peel protects that place now, so it puts nothing back there. You can drag it out of the Trash in Finder.")
case .notInATrash: String(localized: "It isn’t in the Trash where Peel put it, so Peel leaves it where it is.")
+ case .notAllowedByTheHelper(let message): FixedSentence.translated(message)
case .locked: String(localized: "It is locked. In Finder, choose File > Get Info and deselect Locked, then try again.")
case .folderLocked:
String(localized: "The folder it goes back to is locked. In Finder, choose File > Get Info for that folder and deselect Locked, then try again.")
diff --git a/PeelTests/RemovalOutcomeTests.swift b/PeelTests/RemovalOutcomeTests.swift
index 93dd67e2..4dcb21a4 100644
--- a/PeelTests/RemovalOutcomeTests.swift
+++ b/PeelTests/RemovalOutcomeTests.swift
@@ -47,13 +47,15 @@ struct RemovalOutcomeTests {
#expect(RemovalFailureAlert.details(for: outcome).hasPrefix("What moved is in the Trash"))
}
- private func history(in folder: URL, outcome: RemovalOutcome, staying: String? = nil) -> RemovalHistoryStore {
+ private func history(
+ in folder: URL, outcome: RemovalOutcome, staying: String? = nil, because failure: RestoreFailure = .needsHelper
+ ) -> RemovalHistoryStore {
RemovalHistoryStore(
log: RemovalLog(url: folder.appending(path: "removals.json")),
refusals: RefusalLog(url: folder.appending(path: "refusals.json")),
outcome: outcome,
knowsTheExclusions: { true },
- restoring: { items, _ in items.map { $0.originalURL.lastPathComponent == staying ? .needsHelper : nil } }
+ restoring: { items, _ in items.map { $0.originalURL.lastPathComponent == staying ? failure : nil } }
)
}
@@ -79,6 +81,19 @@ struct RemovalOutcomeTests {
#expect(history.undoTitle != nil, "what stayed can be put back once the helper can")
}
+ @Test func anUndoTheHelpersOwnRuleRefusedIsNotOfferedAgain() async throws {
+ let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
+ defer { try? FileManager.default.removeItem(at: folder) }
+ let outcome = RemovalOutcome()
+ let history = history(in: folder, outcome: outcome, staying: "Synth", because: .notAllowedByTheHelper("x"))
+ await history.record(try moved(["Synth"]), tool: .plugins, source: "Plug-ins", sizes: [:])
+
+ await history.undoLastRemoval()
+
+ #expect(outcome.putBackFailures.map(\.url.lastPathComponent) == ["Synth"])
+ #expect(history.undoTitle == nil)
+ }
+
@Test func undoGoesOnceItsRemovalIsPutBackFromHistory() async throws {
let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
defer { try? FileManager.default.removeItem(at: folder) }