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.

A refused move of a background item is written down

Țugui Dragoș63969bbfaa29
diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift
index 9f816aa3..fd73a91e 100644
--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift
+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift
@@ -79,7 +79,8 @@ public enum BackgroundItemActions {
}
/// Moves the job's property list to the Trash, then unloads the job. The move comes first, so a refused move
- /// never leaves the job stopped with its file still in place.
+ /// never leaves the job stopped with its file still in place. A refused move comes back in the result, to be
+ /// written down like any other refusal.
@concurrent
@discardableResult
public static func moveToTrash(_ item: BackgroundItem, exclusions: Exclusions = .none) async throws(Failure) -> TrashResult {
@@ -102,12 +103,9 @@ public enum BackgroundItemActions {
throw .requiresPrivileges
}
let result = await trash.trash([plist], usingHelperFor: item.removalRequiresPrivileges ? [plist] : [])
- if let failure = result.failures.first {
- throw .trash(failure.reason)
- }
// `TrashService` stops a job only when `PrivilegedPathPolicy.isValidLabel` accepts its label. The user
// chose this job, so it is also stopped here by its own label. A job that is already gone is not a failure.
- if item.state != .notLoaded {
+ if !result.trashed.isEmpty, item.state != .notLoaded {
await stop(item)
}
return result
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
index f20db622..0b077ea0 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
@@ -72,13 +72,14 @@ struct BackgroundItemActionsTests {
#expect(result.trashed.count == 1)
#expect(steps.withLock { $0 } == ["move", "stop"])
- // Refused: nothing is stopped, and the reason is the one the guard gave.
+ // Refused: nothing is stopped, and the refusal comes back in the result, with the reason the guard gave, so
+ // it is written down like any other.
steps.withLock { $0 = [] }
- await #expect(throws: BackgroundItemActions.Failure.trash(.protectedLocation)) {
- try await BackgroundItemActions.moveToTrash(
- item(.userLibrary, state: .running(pid: 7), plist: plist), isHelperEnabled: false, trash: service(refusing: true), stop: stop
- )
- }
+ let refused = try await BackgroundItemActions.moveToTrash(
+ item(.userLibrary, state: .running(pid: 7), plist: plist), isHelperEnabled: false, trash: service(refusing: true), stop: stop
+ )
+ #expect(refused.trashed.isEmpty)
+ #expect(refused.failures.map(\.reason) == [.protectedLocation])
#expect(steps.withLock { $0 } == ["move"])
// A job launchd does not hold has nothing to stop.
diff --git a/Peel/Model/BackgroundItemLibrary.swift b/Peel/Model/BackgroundItemLibrary.swift
index e3828647..11659da6 100644
--- a/Peel/Model/BackgroundItemLibrary.swift
+++ b/Peel/Model/BackgroundItemLibrary.swift
@@ -55,7 +55,11 @@ final class BackgroundItemLibrary {
case .stop: try await BackgroundItemActions.stop(item)
case .enable: try await BackgroundItemActions.setEnabled(true, for: item)
case .disable: try await BackgroundItemActions.setEnabled(false, for: item)
- case .moveToTrash: result = try await BackgroundItemActions.moveToTrash(item, exclusions: ExclusionsStore.shared.exclusions)
+ case .moveToTrash:
+ result = try await BackgroundItemActions.moveToTrash(item, exclusions: ExclusionsStore.shared.exclusions)
+ if let refusal = result.failures.first {
+ failure = ActionFailure(action: action, reason: .trash(refusal.reason))
+ }
}
} catch {
failure = ActionFailure(action: action, reason: error)