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.

Believe an empty App Store answer about a found update only twice in a row

Țugui Dragoșdfeb96884ede
diff --git a/Packages/PeelCore/Sources/PeelCore/Updates/UpdateChecker.swift b/Packages/PeelCore/Sources/PeelCore/Updates/UpdateChecker.swift
index a6b81707..7ff46ceb 100644
--- a/Packages/PeelCore/Sources/PeelCore/Updates/UpdateChecker.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Updates/UpdateChecker.swift
@@ -63,15 +63,19 @@ public struct UpdateChecker: Sendable {
}
/// Returns the update status of `app`, without installing anything. `preference` decides which source to
- /// believe when an app can be updated in more than one way, and `earlier` is the last answer about this build.
+ /// believe when an app can be updated in more than one way, `earlier` is the last answer about this build, and
+ /// `notListedBefore` says the App Store's last answer about it listed no such app.
@concurrent
public func status(
for app: InstalledApp,
preference: UpdateSource = .automatic,
casks: [HomebrewPackage] = [],
- earlier: UpdateStatus? = nil
+ earlier: UpdateStatus? = nil,
+ notListedBefore: Bool = false
) async -> UpdateStatus {
- await answer(for: app, preference: preference, casks: casks, earlier: earlier).status
+ await answer(
+ for: app, preference: preference, casks: casks, earlier: earlier, notListedBefore: notListedBefore
+ ).status
}
/// Returns the update status, and the developer's name when the App Store answered. An App Store app is
@@ -81,7 +85,8 @@ public struct UpdateChecker: Sendable {
for app: InstalledApp,
preference: UpdateSource = .automatic,
casks: [HomebrewPackage] = [],
- earlier: UpdateStatus? = nil
+ earlier: UpdateStatus? = nil,
+ notListedBefore: Bool = false
) async -> UpdateAnswer {
let homebrew = homebrewStatus(for: app, casks: casks)
if let status = Self.statusWithoutAsking(for: app, preference: preference, homebrew: homebrew) {
@@ -90,11 +95,14 @@ public struct UpdateChecker: Sendable {
// Even with the App Store preferred, Apple is asked only about apps from the App Store. Any other app is
// checked through its own feed, or not at all.
let own = preference == .appStore && app.isFromAppStore
- ? await appStoreAnswer(for: app, earlier: earlier) : await feedAnswer(for: app, earlier: earlier)
+ ? await appStoreAnswer(for: app, earlier: earlier, notListedBefore: notListedBefore)
+ : await feedAnswer(for: app, earlier: earlier, notListedBefore: notListedBefore)
guard !own.wasOffline else { return own }
// A feed can be down, and the App Store may not sell the app here: what Homebrew knows is used then.
if let homebrew, own.status == .unsupported || own.status == .failed {
- return UpdateAnswer(status: homebrew, developer: own.developer, standsInFor: own.status)
+ return UpdateAnswer(
+ status: homebrew, developer: own.developer, standsInFor: own.status, isNotListed: own.isNotListed
+ )
}
return own
}
@@ -182,14 +190,16 @@ public struct UpdateChecker: Sendable {
return Self.homebrewStatus(of: cask, appVersion: app.version, at: clock()) == nil ? cask : nil
}
- private func feedAnswer(for app: InstalledApp, earlier: UpdateStatus?) async -> UpdateAnswer {
+ private func feedAnswer(
+ for app: InstalledApp, earlier: UpdateStatus?, notListedBefore: Bool
+ ) async -> UpdateAnswer {
// An app whose bundle names no version has nothing to compare a release with, so nobody is asked.
guard let installed = app.version else { return UpdateAnswer(status: .unsupported) }
return switch app.updateFeed {
case nil: UpdateAnswer(status: .unsupported)
case .sparkle(let url): await sparkleAnswer(for: app, installed: installed, at: url)
case .electron(let url): await electronAnswer(installed: installed, at: url)
- case .appStore: await appStoreAnswer(for: app, earlier: earlier)
+ case .appStore: await appStoreAnswer(for: app, earlier: earlier, notListedBefore: notListedBefore)
}
}
@@ -230,7 +240,9 @@ public struct UpdateChecker: Sendable {
/// Asks the App Store about `app`, but only when the app has an App Store receipt, whoever calls this.
/// Asking about other apps would send Apple, one identifier at a time, the list of installed apps.
- private func appStoreAnswer(for app: InstalledApp, earlier: UpdateStatus?) async -> UpdateAnswer {
+ private func appStoreAnswer(
+ for app: InstalledApp, earlier: UpdateStatus?, notListedBefore: Bool
+ ) async -> UpdateAnswer {
guard app.isFromAppStore, let installed = app.version, let identifier = app.bundleIdentifier else {
return UpdateAnswer(status: .unsupported)
}
@@ -246,7 +258,12 @@ public struct UpdateChecker: Sendable {
case .unreadable:
return UpdateAnswer(status: .failed)
case .notListed:
- return UpdateAnswer(status: .unsupported)
+ // An app the store just offered an update for is believed unlisted only when the answer before this
+ // one said so too; a first such answer is asked again later, as a failed check.
+ if case .updateAvailable(_, .appStore, _) = earlier, !notListedBefore {
+ return UpdateAnswer(status: .failed, isNotListed: true)
+ }
+ return UpdateAnswer(status: .unsupported, isNotListed: true)
case .listed(let latest, let page, let developer, let notes):
// The App Store never goes back to an older version, so an answer older than the update it gave for this
// build comes from a copy of its catalog that isn't up to date yet, and is asked again later.
@@ -431,6 +448,8 @@ public struct UpdateAnswer: Sendable, Hashable {
public let standsInFor: UpdateStatus?
/// True when this Mac wasn't online, so the check learned nothing about the app.
public let wasOffline: Bool
+ /// True when the App Store answered that it lists no such app.
+ public let isNotListed: Bool
public init(
status: UpdateStatus,
@@ -438,7 +457,8 @@ public struct UpdateAnswer: Sendable, Hashable {
notes: ReleaseNotes? = nil,
notesPage: URL? = nil,
standsInFor: UpdateStatus? = nil,
- wasOffline: Bool = false
+ wasOffline: Bool = false,
+ isNotListed: Bool = false
) {
self.status = status
self.developer = developer
@@ -446,6 +466,7 @@ public struct UpdateAnswer: Sendable, Hashable {
self.notesPage = notesPage
self.standsInFor = standsInFor
self.wasOffline = wasOffline
+ self.isNotListed = isNotListed
}
/// The status to keep after `previous`: a check whose own source failed keeps an update that source found earlier,
diff --git a/Packages/PeelCore/Sources/PeelCore/Updates/UpdateMemory.swift b/Packages/PeelCore/Sources/PeelCore/Updates/UpdateMemory.swift
index 8a903fc2..529a6c89 100644
--- a/Packages/PeelCore/Sources/PeelCore/Updates/UpdateMemory.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Updates/UpdateMemory.swift
@@ -20,6 +20,8 @@ public struct UpdateMemory: Codable, Equatable, Sendable {
public let standsInFor: UpdateStatus?
/// The `UpdateChecker.revision` that gave the answer; nil for one saved before revisions were kept.
public let revision: Int?
+ /// Whether the App Store's answer was that it lists no such app; nil for one saved before this was kept.
+ public let isNotListed: Bool?
public init(
status: UpdateStatus,
@@ -27,13 +29,15 @@ public struct UpdateMemory: Codable, Equatable, Sendable {
checked: Date?,
describing app: InstalledApp,
developer: String? = nil,
- standsInFor: UpdateStatus? = nil
+ standsInFor: UpdateStatus? = nil,
+ isNotListed: Bool = false
) {
self.status = status
self.schedule = schedule
self.checked = checked
self.developer = developer
self.standsInFor = standsInFor
+ self.isNotListed = isNotListed
version = app.version
buildVersion = app.buildVersion
revision = UpdateChecker.revision
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/UpdateCheckerTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/UpdateCheckerTests.swift
index 1b493e1a..91912fee 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/UpdateCheckerTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/UpdateCheckerTests.swift
@@ -453,6 +453,21 @@ private final class OfflineProtocol: URLProtocol, @unchecked Sendable {
#expect(await checker.status(for: notes) == .updateAvailable(version: "1.1", source: .appStore))
}
+ @Test func believesTheAppStoreNoLongerListsAnAppWithAnUpdateFoundOnlyOnTheSecondAnswer() async {
+ let notes = InstalledApp(
+ url: URL(filePath: "/Applications/Notes Pro.app"), bundleIdentifier: "com.example.notes", name: "Notes Pro",
+ version: "1.0", isFromAppStore: true, updateFeed: .appStore(.mac)
+ )
+ let found = UpdateStatus.updateAvailable(version: "1.2", source: .appStore)
+ reply("itunes.apple.com", #"{"resultCount":0,"results":[]}"#)
+
+ let first = await checker.answer(for: notes, earlier: found)
+ #expect(first.status == .failed && first.isNotListed)
+ #expect(first.following(found) == found)
+ #expect(await checker.status(for: notes, earlier: found, notListedBefore: true) == .unsupported)
+ #expect(await checker.status(for: notes) == .unsupported, "with no update found, it is believed at once")
+ }
+
@Test func asksTheMacListingOfAMacAppSoldWithItsIPhoneVersion() async {
let notes = { (version: String) in
InstalledApp(
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/UpdateMemoryTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/UpdateMemoryTests.swift
index 696f8d9d..1a25437d 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/UpdateMemoryTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/UpdateMemoryTests.swift
@@ -118,4 +118,18 @@ struct UpdateMemoryTests {
#expect(old["com.example.foo"]?.describes(app("1.0")) == false)
}
+
+ @Test func keepsAcrossLaunchesThatTheAppStoreListedNoSuchApp() throws {
+ let entry = UpdateMemory(
+ status: .updateAvailable(version: "1.2", source: .appStore), schedule: schedule, checked: nil,
+ describing: app("1.0"), isNotListed: true
+ )
+ let saved = try JSONDecoder().decode(UpdateMemory.self, from: JSONEncoder().encode(entry))
+ #expect(saved.isNotListed == true)
+
+ var object = try #require(JSONSerialization.jsonObject(with: JSONEncoder().encode(entry)) as? [String: Any])
+ object.removeValue(forKey: "isNotListed")
+ let earlier = try JSONDecoder().decode(UpdateMemory.self, from: JSONSerialization.data(withJSONObject: object))
+ #expect(earlier.isNotListed == nil && earlier.status == entry.status)
+ }
}
diff --git a/Peel/Model/AppLibrary.swift b/Peel/Model/AppLibrary.swift
index bf7de1b0..1b328e6d 100644
--- a/Peel/Model/AppLibrary.swift
+++ b/Peel/Model/AppLibrary.swift
@@ -559,13 +559,21 @@ final class AppLibrary {
await withTaskGroup(of: (InstalledApp.ID, UpdateAnswer).self) { group in
var pending = wanted.makeIterator()
- for _ in 0..<Self.concurrentUpdateChecks {
- guard let app = pending.next() else { break }
+ @MainActor func askNext() {
+ guard let app = pending.next() else { return }
let earlier = updateStatuses[app.id]
+ let notListedBefore = remembered(app)?.isNotListed == true
_ = group.addTaskUnlessCancelled {
- (app.id, await checker.answer(for: app, preference: preference, casks: casks, earlier: earlier))
+ let answer = await checker.answer(
+ for: app, preference: preference, casks: casks,
+ earlier: earlier, notListedBefore: notListedBefore
+ )
+ return (app.id, answer)
}
}
+ for _ in 0..<Self.concurrentUpdateChecks {
+ askNext()
+ }
while case let (id, answer)? = await group.next() {
// The update source changed since this round began, and the round that change started answers now.
guard updateRounds.answered(id, in: round) else {
@@ -573,12 +581,7 @@ final class AppLibrary {
continue
}
// Whatever comes of this answer, the next app is asked, so one that left the list costs no place.
- if let app = pending.next() {
- let earlier = updateStatuses[app.id]
- _ = group.addTaskUnlessCancelled {
- (app.id, await checker.answer(for: app, preference: preference, casks: casks, earlier: earlier))
- }
- }
+ askNext()
// In a canceled round every pending request ends as a failure. Recording those would erase
// what was known and put off the next check for hours, and so would recording a check made while
// this Mac wasn't online, which leaves the app due at the next round.
@@ -613,7 +616,8 @@ final class AppLibrary {
checked: answer.status == .failed ? memory[identifier]?.checked : .now,
describing: app,
developer: answer.developer ?? memory[identifier]?.developer,
- standsInFor: standsInFor
+ standsInFor: standsInFor,
+ isNotListed: answer.isNotListed
)
// Only an answer that found this update can say what is new in it; a failed one keeps what was known.
if case .updateAvailable(let version, _, _) = kept, answer.status == kept {
diff --git a/PeelTests/AppLibraryTests.swift b/PeelTests/AppLibraryTests.swift
index 0f16bec4..6223065e 100644
--- a/PeelTests/AppLibraryTests.swift
+++ b/PeelTests/AppLibraryTests.swift
@@ -482,6 +482,28 @@ private nonisolated final class TestClock: Sendable {
#expect(known != nil && remembered() == known, "nothing learned, nothing put off")
}
+ @Test func anAppStoreUpdateStaysUntilTwoAnswersInARowListNoSuchApp() async throws {
+ let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
+ let bundle = try soldApp(in: folder)
+ defer { try? FileManager.default.removeItem(at: folder) }
+ let made = try MadeUpDefaults()
+ defer { made.forget() }
+ let library = library(with: made.defaults, notesIn: folder)
+
+ #expect(await library.reveal([bundle]))
+ let app = try #require(library.apps.first { $0.bundleIdentifier == "org.example.sold" })
+ AppcastProtocol.body.withLock { $0 = listing("1.2") }
+ await library.checkForUpdates([app], force: true)
+ #expect(library.shownUpdateStatus(of: app)?.version == "1.2")
+
+ AppcastProtocol.body.withLock { $0 = Data(#"{"resultCount":0,"results":[]}"#.utf8) }
+ await library.checkForUpdates([app], force: true)
+ #expect(library.shownUpdateStatus(of: app)?.version == "1.2")
+
+ await library.checkForUpdates([app], force: true)
+ #expect(library.shownUpdateStatus(of: app) == .unsupported)
+ }
+
@Test func notesNoServerAnsweredWaitForTheirTurnUnlessThePersonAsks() async throws {
let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
let bundle = try caskApp("Fed", in: folder, feed: "https://feed.example.com/appcast.xml")