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.

Let the Spotlight test wait for Spotlight's answer, not the clock

Țugui Dragoșd28cb607db89
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
index deffae60..54a6e35f 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
@@ -32,25 +32,31 @@ public enum AppInspector {
beginningWith prefixes: [String],
within budget: TimeInterval = spotlightBudget
) async -> Set<String> {
+ guard let query = spotlightQuery(forIdentifiersBeginningWith: prefixes) else { return [] }
+ return await SlowRead.answer(within: budget) { _ in identifiers(answering: query) } ?? []
+ }
+
+ static func spotlightQuery(forIdentifiersBeginningWith prefixes: [String]) -> String? {
let isAnIdentifiersStart = { (prefix: String) in
!prefix.isEmpty && prefix.allSatisfy { $0.isASCII && ($0.isLetter || $0.isNumber || "._-".contains($0)) }
}
- guard !prefixes.isEmpty, prefixes.allSatisfy(isAnIdentifiersStart) else { return [] }
+ guard !prefixes.isEmpty, prefixes.allSatisfy(isAnIdentifiersStart) else { return nil }
let identifiers = prefixes.map { "kMDItemCFBundleIdentifier == \"\($0)*\"c" }.joined(separator: " || ")
- let text = "kMDItemContentTypeTree == \"com.apple.application-bundle\" && (\(identifiers))"
- let found = await SlowRead.answer(within: budget) { _ -> Set<String> in
- guard let query = MDQueryCreate(kCFAllocatorDefault, text as CFString, nil, nil),
- MDQueryExecute(query, CFOptionFlags(kMDQuerySynchronous.rawValue))
- else { return [] }
- return Set((0..<MDQueryGetResultCount(query)).compactMap { index in
- guard let result = MDQueryGetResultAtIndex(query, index) else { return nil }
- return MDItemCopyAttribute(
- Unmanaged<MDItem>.fromOpaque(result).takeUnretainedValue(),
- kMDItemCFBundleIdentifier
- ) as? String
- })
- }
- return found ?? []
+ return "kMDItemContentTypeTree == \"com.apple.application-bundle\" && (\(identifiers))"
+ }
+
+ /// Blocks until Spotlight has answered `query` in full, so it runs on a thread of its own.
+ static func identifiers(answering query: String) -> Set<String> {
+ guard let query = MDQueryCreate(kCFAllocatorDefault, query as CFString, nil, nil),
+ MDQueryExecute(query, CFOptionFlags(kMDQuerySynchronous.rawValue))
+ else { return [] }
+ return Set((0..<MDQueryGetResultCount(query)).compactMap { index in
+ guard let result = MDQueryGetResultAtIndex(query, index) else { return nil }
+ return MDItemCopyAttribute(
+ Unmanaged<MDItem>.fromOpaque(result).takeUnretainedValue(),
+ kMDItemCFBundleIdentifier
+ ) as? String
+ })
}
/// How long a scan waits for Spotlight. It answers a query on one attribute at once unless it is rebuilding its
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift
index 4698b05e..d6de493a 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift
@@ -1,5 +1,6 @@
import Foundation
@testable import PeelCore
+import PeelPrivileged
import Testing
struct AppInspectorTests {
@@ -38,14 +39,15 @@ struct AppInspectorTests {
/// Spotlight names the apps whose identifier begins with any prefix asked, wherever they are: every Mac has the
/// dictation input method in its input methods folder, outside the Applications folders, and TextEdit. A Mac with
- /// Spotlight off answers nothing, and the test waits for one that indexes. The other tests' scans ask Spotlight at
- /// the same time, so this one is given longer than a scan waits.
+ /// Spotlight off answers nothing, so the test waits for a Mac where macOS's own `mdfind` finds TextEdit. It waits
+ /// for Spotlight's whole answer, since a busy Mac answers late, never wrong.
@Test(.enabled("Spotlight indexes this Mac") {
- await !AppInspector.indexedIdentifiers(beginningWith: ["com.apple."], within: 60).isEmpty
+ let query = "kMDItemCFBundleIdentifier == \"com.apple.TextEdit\""
+ return (try? await Subprocess.run("/usr/bin/mdfind", [query], timeout: nil).get())?.text.isEmpty == false
})
func spotlightNamesTheAppsOfOneMaker() async {
let prefixes = ["com.apple.inputmethod.", "com.apple.TextEdit"]
- let found = await AppInspector.indexedIdentifiers(beginningWith: prefixes, within: 60)
+ let found = await Self.indexed(beginningWith: prefixes)
#expect(found.contains("com.apple.inputmethod.ironwood"))
#expect(found.contains("com.apple.TextEdit"))
@@ -53,6 +55,13 @@ struct AppInspectorTests {
#expect(found.allSatisfy { identifier in lowercased.contains { identifier.lowercased().hasPrefix($0) } })
}
+ private static func indexed(beginningWith prefixes: [String]) async -> Set<String> {
+ guard let query = AppInspector.spotlightQuery(forIdentifiersBeginningWith: prefixes) else { return [] }
+ return await withCheckedContinuation { continuation in
+ Thread { continuation.resume(returning: AppInspector.identifiers(answering: query)) }.start()
+ }
+ }
+
/// A bundle writes its own identifier, and a maker's prefix taken from it goes into a Spotlight query. One that
/// could change the query asks nothing.
@Test func aPrefixThatCouldChangeTheQueryAsksNothing() async {