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.

Test the update round over stores of its own

Țugui Dragoș594bcc6f93cf
diff --git a/Peel/Model/AppLibrary.swift b/Peel/Model/AppLibrary.swift
index c9be689d..12aba621 100644
--- a/Peel/Model/AppLibrary.swift
+++ b/Peel/Model/AppLibrary.swift
@@ -40,14 +40,14 @@ struct VisibleApps {
}
private enum UpdateMemoryStore {
- static func load() -> [String: UpdateMemory] {
- guard let data = UserDefaults.standard.data(forKey: SettingsKey.updateMemory) else { return [:] }
+ static func load(from defaults: UserDefaults) -> [String: UpdateMemory] {
+ guard let data = defaults.data(forKey: SettingsKey.updateMemory) else { return [:] }
return (try? JSONDecoder().decode([String: UpdateMemory].self, from: data)) ?? [:]
}
- static func save(_ memory: [String: UpdateMemory]) {
+ static func save(_ memory: [String: UpdateMemory], to defaults: UserDefaults) {
guard let data = try? JSONEncoder().encode(memory) else { return }
- UserDefaults.standard.set(data, forKey: SettingsKey.updateMemory)
+ defaults.set(data, forKey: SettingsKey.updateMemory)
}
}
@@ -129,14 +129,11 @@ final class AppLibrary {
/// The apps selected with their checkboxes, to be removed together. Kept apart from `selection`, so
/// choosing a row to look at never clears the batch.
var picked: Set<InstalledApp.ID> = []
- private(set) var ignoredApps = Set(
- UserDefaults.standard.stringArray(forKey: SettingsKey.ignoredUpdateApps) ?? []
- ) {
+ private(set) var ignoredApps: Set<String> {
didSet { updatesRevision += 1 }
}
- private(set) var skippedVersions = UserDefaults.standard.dictionary(forKey: SettingsKey.skippedUpdateVersions)
- as? [String: String] ?? [:] {
+ private(set) var skippedVersions: [String: String] {
didSet { updatesRevision += 1 }
}
var sort = AppSort.name
@@ -147,10 +144,12 @@ final class AppLibrary {
/// True when any of the list's filters is on, the same three `visible(matching:)` applies.
var isFiltering: Bool { !sources.isEmpty || selectedDeveloper != nil || showsOnlyUnused }
- private var memory = UpdateMemoryStore.load()
+ private var memory: [String: UpdateMemory]
/// What is new in each waiting update, read at launch, so a page shows it with no connection too.
private(set) var releaseNotes = ReleaseNotesMemory()
- private let releaseNotesStore = ReleaseNotesStore()
+ private let releaseNotesStore: ReleaseNotesStore
+ /// Where the update round's memory and the person's update preferences are kept.
+ private let defaults: UserDefaults
/// Apps shown from outside the scanned folders, such as one in Downloads or in the Trash. Kept apart,
/// because the next reading of the folders doesn't find them and would drop their row and their page.
private var revealed: [InstalledApp] = []
@@ -161,9 +160,24 @@ final class AppLibrary {
/// finish last with a list that lacks an app installed in between.
let scanRun = ScanRun()
private var lastRead: ContinuousClock.Instant?
- private let updateChecker = UpdateChecker()
+ private let updateChecker: UpdateChecker
private let teamRegistry = TeamRegistry()
+ /// The stores are Peel's own unless a test passes its own, so a test of the update round writes nothing of the
+ /// person's.
+ init(
+ defaults: UserDefaults = .standard,
+ updateChecker: UpdateChecker = UpdateChecker(),
+ releaseNotesStore: ReleaseNotesStore = ReleaseNotesStore()
+ ) {
+ self.defaults = defaults
+ self.updateChecker = updateChecker
+ self.releaseNotesStore = releaseNotesStore
+ ignoredApps = Set(defaults.stringArray(forKey: SettingsKey.ignoredUpdateApps) ?? [])
+ skippedVersions = defaults.dictionary(forKey: SettingsKey.skippedUpdateVersions) as? [String: String] ?? [:]
+ memory = UpdateMemoryStore.load(from: defaults)
+ }
+
/// The apps the page beside the list shows: the chosen rows, or the selected batch when no row is chosen.
private var shown: Set<InstalledApp.ID> {
selection.isEmpty ? picked : selection
@@ -310,7 +324,7 @@ final class AppLibrary {
let recalled = UpdateMemory.recalled(from: memory, for: listed)
if recalled != memory {
memory = recalled
- UpdateMemoryStore.save(memory)
+ UpdateMemoryStore.save(memory, to: defaults)
}
return changed
}
@@ -501,7 +515,7 @@ final class AppLibrary {
func checkForUpdates(_ targets: [InstalledApp], force: Bool = false) async {
// Read here, where every caller passes. Settings promises that with this off, Peel contacts nothing on
// its own, so even Rescan on an app's page only rescans files.
- guard UserDefaults.standard.isOn(SettingsKey.checksForAppUpdates, whenNeverSet: true) else { return }
+ guard defaults.isOn(SettingsKey.checksForAppUpdates, whenNeverSet: true) else { return }
let now = Date.now
// An app is checked on a schedule kept by its identifier, so an app with none is left to Homebrew's answer.
// Peel's own copies are left to Sparkle, which checks the one running and is the only way it updates.
@@ -583,7 +597,7 @@ final class AppLibrary {
}
}
guard !Task.isCancelled, updateRounds.isCurrent(round) else { return }
- UpdateMemoryStore.save(memory)
+ UpdateMemoryStore.save(memory, to: defaults)
await ask(notesToAsk, checker: checker)
var waiting: [String: String] = [:]
for app in apps {
@@ -664,7 +678,7 @@ final class AppLibrary {
var menuBarUpdateCount: Int { menuBarUpdates.count }
var updateSource: UpdateSource {
- UpdateSource(rawValue: UserDefaults.standard.string(forKey: SettingsKey.updateSource) ?? "") ?? .automatic
+ UpdateSource(rawValue: defaults.string(forKey: SettingsKey.updateSource) ?? "") ?? .automatic
}
/// Forgets every update answer and schedule, then checks every app again. The answers shown were found
@@ -674,7 +688,7 @@ final class AppLibrary {
updateStatuses = [:]
lastUpdateChecks = [:]
memory = [:]
- UpdateMemoryStore.save(memory)
+ UpdateMemoryStore.save(memory, to: defaults)
await checkForUpdates(apps, force: true)
}
@@ -694,7 +708,7 @@ final class AppLibrary {
references.remove(app.reference)
}
ignoredApps = references
- UserDefaults.standard.set(Array(references).sorted(), forKey: SettingsKey.ignoredUpdateApps)
+ defaults.set(Array(references).sorted(), forKey: SettingsKey.ignoredUpdateApps)
}
func skippedVersion(for app: InstalledApp) -> String? {
@@ -703,7 +717,7 @@ final class AppLibrary {
func skip(version: String, for app: InstalledApp) {
skippedVersions[app.reference] = version
- UserDefaults.standard.set(skippedVersions, forKey: SettingsKey.skippedUpdateVersions)
+ defaults.set(skippedVersions, forKey: SettingsKey.skippedUpdateVersions)
}
/// Undoes both a skipped version and "never check this app", by `InstalledApp.reference`: what was muted may not
@@ -711,8 +725,8 @@ final class AppLibrary {
func unmute(_ reference: String) {
ignoredApps.remove(reference)
skippedVersions.removeValue(forKey: reference)
- UserDefaults.standard.set(ignoredApps.sorted(), forKey: SettingsKey.ignoredUpdateApps)
- UserDefaults.standard.set(skippedVersions, forKey: SettingsKey.skippedUpdateVersions)
+ defaults.set(ignoredApps.sorted(), forKey: SettingsKey.ignoredUpdateApps)
+ defaults.set(skippedVersions, forKey: SettingsKey.skippedUpdateVersions)
}
/// Called often, including every time the Applications list appears. Apps are matched to casks again only
@@ -757,7 +771,7 @@ final class AppLibrary {
for app in apps where outlived.contains(app.id) {
if let identifier = app.bundleIdentifier { memory[identifier] = nil }
}
- UpdateMemoryStore.save(memory)
+ UpdateMemoryStore.save(memory, to: defaults)
}
/// Records an update for each app whose cask Homebrew marks outdated. Reading Homebrew costs no network
diff --git a/PeelTests/AppLibraryTests.swift b/PeelTests/AppLibraryTests.swift
new file mode 100644
index 00000000..556ac908
--- /dev/null
+++ b/PeelTests/AppLibraryTests.swift
@@ -0,0 +1,71 @@
+import Foundation
+@testable import Peel
+import PeelCore
+import Synchronization
+import Testing
+
+/// Answers every request with one appcast, and sends nothing anywhere.
+private nonisolated final class AppcastProtocol: URLProtocol, @unchecked Sendable {
+ static let body = Mutex(Data())
+
+ override class func canInit(with request: URLRequest) -> Bool { true }
+ override class func canonicalRequest(for request: URLRequest) -> URLRequest { request }
+ override func stopLoading() {}
+
+ override func startLoading() {
+ guard let url = request.url,
+ let response = HTTPURLResponse(url: url, statusCode: 200, httpVersion: "HTTP/1.1", headerFields: nil)
+ else { return }
+ client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
+ client?.urlProtocol(self, didLoad: Self.body.withLock { $0 })
+ client?.urlProtocolDidFinishLoading(self)
+ }
+}
+
+struct AppLibraryTests {
+ @Test func aRoundRemembersItsAnswerInTheStoresItWasGiven() async throws {
+ let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
+ let bundle = folder.appending(path: "Updated.app", directoryHint: .isDirectory)
+ try FileManager.default.createDirectory(
+ at: bundle.appending(path: "Contents"), withIntermediateDirectories: true
+ )
+ let info: [String: Any] = [
+ "CFBundleIdentifier": "org.example.updated", "CFBundleName": "Updated", "CFBundlePackageType": "APPL",
+ "CFBundleShortVersionString": "1.0", "CFBundleVersion": "100",
+ "SUFeedURL": "https://feed.example.com/appcast.xml",
+ ]
+ try PropertyListSerialization.data(fromPropertyList: info, format: .xml, options: 0)
+ .write(to: bundle.appending(path: "Contents/Info.plist"))
+ defer { try? FileManager.default.removeItem(at: folder) }
+ let suite = "org.example.peel-tests.\(UUID().uuidString)"
+ let defaults = try #require(UserDefaults(suiteName: suite))
+ defer { defaults.removePersistentDomain(forName: suite) }
+ let configuration = URLSessionConfiguration.ephemeral
+ configuration.protocolClasses = [AppcastProtocol.self]
+ AppcastProtocol.body.withLock {
+ $0 = Data("""
+ <rss xmlns:sparkle="http://www.andymatuschak.org/xml-namespaces/sparkle"><channel><item>\
+ <enclosure url="https://feed.example.com/u.zip" sparkle:version="101" sparkle:shortVersionString="1.1" />\
+ </item></channel></rss>
+ """.utf8)
+ }
+ let notes = ReleaseNotesStore(url: folder.appending(path: "release-notes.json"))
+ let standardBefore = UserDefaults.standard.data(forKey: SettingsKey.updateMemory)
+ let library = AppLibrary(
+ defaults: defaults,
+ updateChecker: UpdateChecker(session: URLSession(configuration: configuration)),
+ releaseNotesStore: notes
+ )
+
+ #expect(await library.reveal([bundle]))
+ let app = try #require(library.apps.first { $0.bundleIdentifier == "org.example.updated" })
+ await library.checkForUpdates([app], force: true)
+
+ #expect(library.hasUpdate(app))
+ #expect(library.shownUpdateStatus(of: app)?.version == "1.1")
+ let remembered = try #require(defaults.data(forKey: SettingsKey.updateMemory))
+ #expect(String(decoding: remembered, as: UTF8.self).contains("org.example.updated"))
+ #expect(FileManager.default.fileExists(atPath: notes.url.path(percentEncoded: false)))
+ #expect(UserDefaults.standard.data(forKey: SettingsKey.updateMemory) == standardBefore)
+ }
+}