Files
BrewUI/Sources/BrewServicesTestSupport/InstalledPackagesTestSupport.swift
Ivan Dergachev be50a45b1b Update brew before the outdated check
brew info never auto-updates, so its API data can be up to 7 days old
and tap clones never refresh. Automatic loads and the reconcile after an
operation run brew update-if-needed, which applies HOMEBREW_NO_AUTO_UPDATE
and the update interval itself. A user's refresh runs brew update.

Fixes #235
2026-09-28 00:16:26 +04:00

75 lines
2.6 KiB
Swift

//
// InstalledPackagesTestSupport.swift
// BrewServicesTestSupport
//
// Shared boundary fakes for BrewInstalledPackagesRepository slice tests (`AGENTS.md` — Testing).
//
import BrewCLI
import BrewCore
import BrewRepositories
import Foundation
public enum InstalledPackagesTestSupport {
/// Stable fake path passed to `commandRunner` when using `BrewExecutableLocator(overrideURL:)`.
public static let fakeBrewExecutableURL = URL(fileURLWithPath: "/fake/brew")
/// Wired like production slice tests; the default environment is the API path, so no tap refresh runs.
@MainActor
public static func repository(
commandRunner: BrewCommandRunning,
locator: (any BrewExecutableLocating)? = nil,
cache: InstalledInventoryCache? = nil,
) -> BrewInstalledPackagesRepository {
let resolvedCache = cache ?? InstalledInventoryCache()
let resolvedLocator = locator ?? BrewExecutableLocator(overrideURL: fakeBrewExecutableURL)
return BrewInstalledPackagesRepository(
commandRunner: commandRunner,
locator: resolvedLocator,
cache: resolvedCache,
)
}
/// Loads (force-refresh) and returns the resulting packages, failing the test if the repository did not reach `.loaded`.
@MainActor
public static func loadedPackages(
from repository: BrewInstalledPackagesRepository,
) async -> [InstalledBrewPackage] {
await repository.load(forceRefresh: true)
guard case let .loaded(packages) = repository.state else {
return []
}
return packages
}
/// Response for `brew info --installed --json=v2`.
public static func responsesInstalledInfoFailure(
standardOutput: String = "",
standardError: String,
terminationStatus: Int32,
) -> [[String]: CommandOutput] {
[
["info", "--installed", "--json=v2"]: CommandOutput(
standardOutput: standardOutput,
standardError: standardError,
terminationStatus: terminationStatus,
),
]
}
/// Success response for `brew info --installed --json=v2`.
public static func installedInfoJSONResponse(
standardOutput: String,
terminationStatus: Int32 = 0,
standardError: String = "",
) -> [[String]: CommandOutput] {
[
["info", "--installed", "--json=v2"]: CommandOutput(
standardOutput: standardOutput,
standardError: standardError,
terminationStatus: terminationStatus,
),
]
}
}