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.

Offer a reset's copy of the settings back only once they were cleared

Țugui Dragoș0ff059fb9ccf
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/AppReset.swift b/Packages/PeelCore/Sources/PeelCore/Removal/AppReset.swift
index 9ec46cbb..bc0cf299 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/AppReset.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/AppReset.swift
@@ -93,8 +93,8 @@ public struct AppReset: Sendable {
public var clearedSettings = false
public var privacy: PrivacyReset.Result?
- /// The copy Put Settings Back takes the settings from.
- public var settingsToPutBack: URL? { backup }
+ /// The copy Put Settings Back takes the settings from, only once the reset cleared them.
+ public var settingsToPutBack: URL? { clearedSettings ? backup : nil }
}
/// Copies the settings in `selection`, asks again whether the app is open, since the copy takes a moment, then
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AppResetTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AppResetTests.swift
index 9469808c..149f5b4c 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AppResetTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AppResetTests.swift
@@ -442,4 +442,34 @@ struct AppResetTests {
}
#expect(settings.isThere)
}
+
+ @Test func aResetOffersItsCopyBackOnlyOnceItClearedTheSettings() async throws {
+ let directory = try TemporaryDirectory()
+ let settings = try directory.file("Library/Preferences/com.example.app.plist", bytes: 4096)
+ try directory.file("Library/Caches/com.example.app/cache", bytes: 4096)
+ let reset = await AppReset.prepare(app(), installedApps: [app()], environment: home(directory))
+ let copy = try directory.directory("Copy")
+ let trash = try directory.directory("Trash")
+ let refusingTheSettings = TrashService(environment: home(directory)) { url in
+ guard url.pathExtension != "plist" else { throw CocoaError(.fileWriteNoPermission) }
+ let destination = trash.appending(path: url.lastPathComponent)
+ try FileManager.default.moveItem(at: url, to: destination)
+ return destination
+ }
+
+ let outcome = await reset.run(
+ reset.suggestedSelection,
+ resettingPrivacy: false,
+ through: refusingTheSettings,
+ isOpen: { false },
+ save: { _ in .saved(copy) },
+ resetPrivacy: { _ in .reset }
+ )
+
+ #expect(outcome.result.trashed.count == 1)
+ #expect(outcome.backup == copy)
+ #expect(!outcome.clearedSettings)
+ #expect(outcome.settingsToPutBack == nil)
+ #expect(settings.isThere)
+ }
}