Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions Sources/BrewRepositories/BrewInstalledPackagesRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,24 @@ public final class BrewInstalledPackagesRepository: InstalledPackagesRepository
}

private func decodeInfoJSON(from standardOutput: String) throws -> BrewInfoJSON {
let data = Data(standardOutput.utf8)
do {
return try JSONDecoder().decode(BrewInfoJSON.self, from: data)
} catch {
throw BrewCommandError.launchFailed(
underlying: String(
localized: "Failed to decode Homebrew JSON output.",
comment: "Installed tab JSON decode failure",
),
)
let decoder = JSONDecoder()
if let payload = try? decoder.decode(BrewInfoJSON.self, from: Data(standardOutput.utf8)) {
return payload
}

for jsonStart in standardOutput.indices where standardOutput[jsonStart] == "{" {
let jsonOutput = standardOutput[jsonStart...]
if let payload = try? decoder.decode(BrewInfoJSON.self, from: Data(jsonOutput.utf8)) {
return payload
}
}

throw BrewCommandError.launchFailed(
underlying: String(
localized: "Failed to decode Homebrew JSON output.",
comment: "Installed tab JSON decode failure",
),
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ import Foundation
import Testing

struct BrewInstalledPackagesRepositoryTests {
@Test @MainActor func `load ignores interactive shell output before installed info json`() async {
let runner = MockBrewCommandRunner(
responses: InstalledPackagesTestSupport.installedInfoJSONResponse(
standardOutput: """
\u{1B}]1337;SetUserVar=badge={"theme":"dark"}\u{7}
{
"formulae": [
{ "name": "wget", "versions": { "stable": "1.0.0" }, "installed": [{ "version": "1.0.0" }] }
],
"casks": []
}
""",
),
)
let repo = InstalledPackagesTestSupport.repository(commandRunner: runner)

let packages = await InstalledPackagesTestSupport.loadedPackages(from: repo)

#expect(packages.map(\.name) == ["wget"])
}

@Test @MainActor func `load returns sorted packages for mixed formula and cask json payload`() async throws {
let json = """
{
Expand Down