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.

Keep what Homebrew knows when one of its definitions can't be read

Țugui Dragoș97a5e053c15b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index be543878..a6a7833c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -458,6 +458,8 @@ its changes the way [Keep a Changelog](https://keepachangelog.com/) suggests.
- An app whose own agent launchd keeps running can be removed, here and with `peel uninstall`: asked to quit, it only
started again, and the question came back. Moving it stops the job, and Put Back starts it again.
+- A cask definition Homebrew can't read costs only its own app: before, one in a tap took away what every other cask
+ says about the apps it names, and the files only a cask names left their pages.
- Without Full Disk Access, an app's page no longer makes macOS ask about your photos, since `~/Library/Photos` isn't
looked into.
- On macOS 27, the list keeps its place when the first update arrives or the last one leaves.
diff --git a/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swift b/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swift
index a74c379a..72173af3 100644
--- a/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swift
@@ -943,22 +943,42 @@ public enum Homebrew {
)
}
- /// Asks about several formulae in one call. A single unknown name makes the whole call fail, so only names from
- /// `formulaNames()` are ever passed in.
+ /// Asks about several formulae. Only names from `formulaNames()` are ever passed in.
@concurrent
public static func formulae(named names: [String]) async -> [HomebrewPackage] {
- guard !names.isEmpty else { return [] }
- guard let output = try? await answer(["info", "--json=v2", "--formula"] + names) else { return [] }
- return parseInstalled(Data(output.utf8))?.filter { $0.kind == .formula } ?? []
+ await info("--formula", about: names).filter { $0.kind == .formula }
}
- /// Asks about several casks in one call, which is quicker than asking about each on its own. A single
- /// unknown name makes the whole call fail, so only names from `caskTokens()` are ever passed in.
+ /// Asks about several casks. Only names from `caskTokens()` are ever passed in.
@concurrent
public static func casks(named tokens: [String]) async -> [HomebrewPackage] {
- guard !tokens.isEmpty else { return [] }
- guard let output = try? await answer(["info", "--json=v2", "--cask"] + tokens) else { return [] }
- return parseInstalled(Data(output.utf8))?.filter { $0.kind == .cask } ?? []
+ await info("--cask", about: tokens).filter { $0.kind == .cask }
+ }
+
+ /// Past this many calls Homebrew is refusing more than a name or two, and what is left stays unasked. One name
+ /// it can't read among 200 takes 17.
+ private static let mostCallsForOneQuestion = 32
+
+ /// Asks about `names` in one call, which is quicker than a call for each. Homebrew refuses a whole call over one
+ /// definition it can't read, so a call it refuses is asked again in halves, which leaves out only what it can't
+ /// read. A call that got no answer at all isn't asked again: Homebrew didn't run, or ran out of time.
+ private static func info(_ kind: String, about names: [String]) async -> [HomebrewPackage] {
+ var found: [HomebrewPackage] = []
+ var pending = names.isEmpty ? [] : [names[...]]
+ var calls = 0
+ while let asked = pending.popLast(), calls < mostCallsForOneQuestion, !Task.isCancelled {
+ calls += 1
+ guard let attempt = try? await execute(
+ ["info", "--json=v2", kind] + asked, autoUpdate: false, timeout: longestAnswer
+ ) else { break }
+ if attempt.status == 0 {
+ found += parseInstalled(Data(attempt.standardOutput.utf8)) ?? []
+ } else if asked.count > 1 {
+ let middle = asked.startIndex + asked.count / 2
+ pending += [asked[middle...], asked[..<middle]]
+ }
+ }
+ return found
}
/// Upgrades `package` with no time limit, since Homebrew builds some packages on this Mac and that can take
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/KnownCasksTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/KnownCasksTests.swift
index 0b594097..8111b8bb 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/KnownCasksTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/KnownCasksTests.swift
@@ -193,6 +193,98 @@ struct KnownCasksTests {
#expect(await CaskEvidence.knownCasks(for: []).isEmpty)
}
+ /// A `brew` that writes each call to `calls` and answers `info` about every name, unless one is in `unreadable`.
+ private func brew(refusing unreadable: String, in directory: borrowing TemporaryDirectory) throws -> URL {
+ let calls = directory.url.appending(path: "calls").path(percentEncoded: false)
+ let script = """
+ #!/bin/sh
+ echo "$*" >> '\(calls)'
+ kind=$3
+ shift 3
+ for name in "$@"; do
+ case " \(unreadable) " in *" $name "*|*" * "*) echo "Error: '$name' is unreadable" >&2; exit 1;; esac
+ done
+ printf '{"formulae": ['
+ separator=''
+ if [ "$kind" = --formula ]; then
+ for name in "$@"; do
+ printf '%s{"name": "%s", "installed": [], "versions": {"stable": "1"}, "outdated": false, "pinned": false, "dependencies": []}' "$separator" "$name"
+ separator=,
+ done
+ fi
+ printf '], "casks": ['
+ if [ "$kind" = --cask ]; then
+ for name in "$@"; do
+ printf '%s{"token": "%s", "version": "1", "outdated": false}' "$separator" "$name"
+ separator=,
+ done
+ fi
+ printf ']}'
+ """
+ let url = try directory.file("brew", contents: Data(script.utf8))
+ chmod(url.path(percentEncoded: false), 0o755)
+ return url
+ }
+
+ @Test func aCaskHomebrewCantReadCostsOnlyItself() async throws {
+ let directory = try TemporaryDirectory()
+ let stand = try brew(refusing: "broken", in: directory)
+
+ let found = await Homebrew.$standIn.withValue(stand) {
+ await Homebrew.casks(named: ["first", "second", "broken", "third", "fourth"])
+ }
+
+ #expect(Set(found.map(\.name)) == ["first", "second", "third", "fourth"])
+ #expect(found.allSatisfy { $0.kind == .cask })
+ }
+
+ @Test func aFormulaHomebrewCantReadCostsOnlyItself() async throws {
+ let directory = try TemporaryDirectory()
+ let stand = try brew(refusing: "broken", in: directory)
+
+ let found = await Homebrew.$standIn.withValue(stand) {
+ await Homebrew.formulae(named: ["broken", "first", "second"])
+ }
+
+ #expect(Set(found.map(\.name)) == ["first", "second"])
+ #expect(found.allSatisfy { $0.kind == .formula })
+ }
+
+ @Test func casksThatCanAllBeReadTakeOneCall() async throws {
+ let directory = try TemporaryDirectory()
+ let stand = try brew(refusing: "broken", in: directory)
+
+ let found = await Homebrew.$standIn.withValue(stand) { await Homebrew.casks(named: ["first", "second"]) }
+
+ #expect(found.count == 2)
+ let calls = try String(contentsOf: directory.url.appending(path: "calls"), encoding: .utf8)
+ #expect(calls.split(whereSeparator: \.isNewline).count == 1)
+ }
+
+ @Test func aHomebrewThatRefusesEverythingIsNotAskedAboutEachName() async throws {
+ let directory = try TemporaryDirectory()
+ let stand = try brew(refusing: "*", in: directory)
+
+ let found = await Homebrew.$standIn.withValue(stand) {
+ await Homebrew.casks(named: (0..<200).map { "cask\($0)" })
+ }
+
+ #expect(found.isEmpty)
+ let calls = try String(contentsOf: directory.url.appending(path: "calls"), encoding: .utf8)
+ #expect(calls.split(whereSeparator: \.isNewline).count <= 32)
+ }
+
+ @Test func aHomebrewThatDoesNotRunIsAskedOnce() async throws {
+ let directory = try TemporaryDirectory()
+
+ let found = await Homebrew.$standIn.withValue(directory.url.appending(path: "no-brew")) {
+ await Homebrew.casks(named: ["first", "second", "third"])
+ }
+
+ #expect(found.isEmpty)
+ #expect(!directory.url.appending(path: "calls").isThere)
+ }
+
/// Reads Homebrew's local copy of the cask definitions and finds nothing without Homebrew. The result
/// depends on the machine running the test, so only its shape is checked: casks only, and no guessed token
/// Homebrew does not know. That nothing is downloaded rests on `hasLocalDefinitions`, which a test cannot see.