Files
helder/Tests/HelderUITests/Support/Fakes/FakeGit.swift
T
2026-10-30 13:21:45 +01:00

97 lines
4.1 KiB
Swift

import Foundation
import HelderCore
import HelderServices
/// A scripted `execute` run: the lines it streams (`isError` for stderr) and its exit code.
struct FakeGitRun {
var exitCode: Int32 = 0
var lines: [(text: String, isError: Bool)] = []
/// How long the run takes after its lines.
var delay: Duration = .zero
static let success = FakeGitRun()
var result: GitCommandResult {
func joined(_ isError: Bool) -> String {
lines.filter { $0.isError == isError }.map { $0.text + "\n" }.joined()
}
return GitCommandResult(exitCode: exitCode, stdout: joined(false), stderr: joined(true))
}
}
/// Records every git call; `load` returns queued snapshots first (each after its delay), then the current one.
///
/// `execute` answers with the run scripted for the full argument list, else for its first argument, else success.
final class FakeGit: GitServicing, @unchecked Sendable {
private struct State {
var loads = 0
var queued: [(GitSnapshot?, Duration)] = []
var current: GitSnapshot?
var executed: [[String]] = []
var events: [String] = []
var loadError: Error?
var runsByArguments: [[String]: FakeGitRun] = [:]
var runsByCommand: [String: FakeGitRun] = [:]
var branches = GitBranchList.empty
var author: String?
}
private let state: Locked<State>
init(_ snapshot: GitSnapshot? = nil) {
state = Locked(State(current: snapshot))
}
var loads: Int { state.current.loads }
/// Every `execute` argument list, in call order.
var executed: [[String]] { state.current.executed }
/// `start <args>` and `end <args>` per `execute`, in the order they happened.
var events: [String] { state.current.events }
func set(_ snapshot: GitSnapshot?) { state.mutate { $0.current = snapshot } }
func queue(_ snapshot: GitSnapshot?, after delay: Duration) { state.mutate { $0.queued.append((snapshot, delay)) } }
func setRun(_ run: FakeGitRun, for arguments: [String]) { state.mutate { $0.runsByArguments[arguments] = run } }
func setRun(_ run: FakeGitRun, forCommand command: String) { state.mutate { $0.runsByCommand[command] = run } }
func setBranches(_ value: GitBranchList) { state.mutate { $0.branches = value } }
func setAuthor(_ value: String?) { state.mutate { $0.author = value } }
/// Makes every `load` throw this error, or read normally again with nil.
func setLoadError(_ error: Error?) { state.mutate { $0.loadError = error } }
func load() async throws -> GitSnapshot? {
if let error = state.current.loadError {
state.mutate { $0.loads += 1 }
throw error
}
let (snapshot, delay): (GitSnapshot?, Duration) = state.mutate { state in
state.loads += 1
return state.queued.isEmpty ? (state.current, .zero) : state.queued.removeFirst()
}
if delay > .zero { try? await Task.sleep(for: delay) }
return snapshot
}
func execute(_ arguments: [String], literalPathspecs: Bool, timeout: TimeInterval?,
onLine: @escaping @Sendable (String, Bool) -> Void) async -> GitCommandResult {
let name = arguments.joined(separator: " ")
let run = state.mutate { state in
state.executed.append(arguments)
state.events.append("start \(name)")
return state.runsByArguments[arguments] ?? arguments.first.flatMap { state.runsByCommand[$0] } ?? .success
}
for line in run.lines { onLine(line.text, line.isError) }
if run.delay > .zero { try? await Task.sleep(for: run.delay) }
state.mutate { $0.events.append("end \(name)") }
return run.result
}
func branches() async -> GitBranchList { state.current.branches }
func author() async -> String? { state.current.author }
}
/// A snapshot with one change per row: (path, status, staged).
func snapshot(_ rows: [(String, GitStatus, Bool)], branch: String = "main") -> GitSnapshot {
GitSnapshot(branch: branch, ahead: 0, canPush: false, changes: rows.map {
GitFileChange(path: $0.0, status: $0.1, staged: $0.2, original: "a", updated: "b")
})
}