adds nice git interface
This commit is contained in:
@@ -2,19 +2,38 @@ 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 staged: [[String]] = []
|
||||
var unstaged: [[String]] = []
|
||||
var commits: [String] = []
|
||||
var discarded: [[String]] = []
|
||||
var pushes = 0
|
||||
var queued: [(GitSnapshot?, Duration)] = []
|
||||
var current: GitSnapshot?
|
||||
var failCommit = false
|
||||
var pushResult = PushResult(ok: true, message: "main -> main")
|
||||
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>
|
||||
@@ -24,18 +43,25 @@ final class FakeGit: GitServicing, @unchecked Sendable {
|
||||
}
|
||||
|
||||
var loads: Int { state.current.loads }
|
||||
var staged: [[String]] { state.current.staged }
|
||||
var unstaged: [[String]] { state.current.unstaged }
|
||||
var commits: [String] { state.current.commits }
|
||||
var discarded: [[String]] { state.current.discarded }
|
||||
var pushes: Int { state.current.pushes }
|
||||
/// 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 setFailCommit(_ value: Bool) { state.mutate { $0.failCommit = value } }
|
||||
func setPushResult(_ value: PushResult) { state.mutate { $0.pushResult = value } }
|
||||
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()
|
||||
@@ -44,24 +70,22 @@ final class FakeGit: GitServicing, @unchecked Sendable {
|
||||
return snapshot
|
||||
}
|
||||
|
||||
func stage(_ paths: [String]) async throws { state.mutate { $0.staged.append(paths) } }
|
||||
func unstage(_ paths: [String]) async throws { state.mutate { $0.unstaged.append(paths) } }
|
||||
|
||||
func commit(message: String) async throws {
|
||||
try state.mutate { state in
|
||||
if state.failCommit { throw FakeError() }
|
||||
state.commits.append(message)
|
||||
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 push() async -> PushResult {
|
||||
state.mutate { state in
|
||||
state.pushes += 1
|
||||
return state.pushResult
|
||||
}
|
||||
}
|
||||
|
||||
func discard(_ paths: [String]) async throws { state.mutate { $0.discarded.append(paths) } }
|
||||
func branches() async -> GitBranchList { state.current.branches }
|
||||
func author() async -> String? { state.current.author }
|
||||
}
|
||||
|
||||
/// A snapshot with one change per row: (path, status, staged).
|
||||
|
||||
@@ -9,10 +9,10 @@ enum TestDerivation {
|
||||
guard let snapshot else { return GitWorkingState() }
|
||||
let changes = snapshot.changes.map {
|
||||
Change(path: $0.path, status: $0.status, added: $0.updated.isEmpty ? 0 : 1,
|
||||
removed: $0.original.isEmpty ? 0 : 1, staged: $0.staged)
|
||||
removed: $0.original.isEmpty ? 0 : 1, staged: $0.staged, originalPath: $0.originalPath)
|
||||
}
|
||||
return GitWorkingState(branch: snapshot.branch, ahead: snapshot.ahead, canPush: snapshot.canPush,
|
||||
changes: changes, stagedPaths: Set(changes.filter(\.staged).map(\.path)),
|
||||
behind: snapshot.behind, upstream: snapshot.upstream, changes: changes, stagedPaths: Set(changes.filter(\.staged).map(\.path)),
|
||||
isRepository: true)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user