Files
helder/Tests/HelderCoreTests/Git/GitConsoleTests.swift
T
2026-10-30 13:21:45 +01:00

123 lines
5.3 KiB
Swift

import HelderCore
import Testing
@Suite struct GitConsoleTests {
// MARK: commandLine
@Test("the echo quotes only what needs quotes", arguments: [
(["status"], "$ git status"),
(["commit", "-m", "fix the bar"], "$ git commit -m \"fix the bar\""),
(["commit", "-m", "say \"hi\""], "$ git commit -m \"say \\\"hi\\\"\""),
(["commit", "-m", "it's"], "$ git commit -m \"it's\""),
(["commit", "-m", ""], "$ git commit -m \"\""),
(["add", "--", "pages/[id].vue"], "$ git add -- pages/[id].vue"),
])
func commandLine(arguments: [String], expected: String) {
#expect(GitConsoleText.commandLine(arguments) == expected)
}
// MARK: parseInput
@Test("typed input splits like a shell", arguments: [
("status", ["status"]),
(" git status ", ["status"]),
("git commit -m \"fix the bar\"", ["commit", "-m", "fix the bar"]),
("commit -m 'it is \"fine\"'", ["commit", "-m", "it is \"fine\""]),
("commit -m \"say \\\"hi\\\"\"", ["commit", "-m", "say \"hi\""]),
("commit -m \"a\\nb\"", ["commit", "-m", "a\\nb"]),
("commit -m \"\"", ["commit", "-m", ""]),
("log --format='%h %s'", ["log", "--format=%h %s"]),
("add my\\ file.txt", ["add", "my file.txt"]),
("commit -m \"unclosed", ["commit", "-m", "unclosed"]),
])
func parseInput(input: String, expected: [String]) {
#expect(GitConsoleText.parseInput(input) == expected)
}
@Test("nothing to run is nil", arguments: ["", " ", "git", " git "])
func parseNothing(input: String) {
#expect(GitConsoleText.parseInput(input) == nil)
}
// MARK: syncLabel
@Test("the sync label names the remote", arguments: [
(0, 2, "origin/main", "2 behind origin"),
(1, 2, "origin/main", "1 ahead · 2 behind origin"),
(1, 0, "origin/main", "1 ahead of origin"),
(0, 0, "origin/feature/x", "in sync with origin"),
(0, 0, "upstream/main", "in sync with upstream"),
(3, 0, nil, "no upstream"),
] as [(Int, Int, String?, String)])
func syncLabel(ahead: Int, behind: Int, upstream: String?, expected: String) {
#expect(GitConsoleText.syncLabel(ahead: ahead, behind: behind, upstream: upstream) == expected)
}
// MARK: classifier
@Test("hints and errors are recognised for any command, whatever the stream", arguments: [
("hint: Updates were rejected", GitLineKind.hint),
("error: failed to push some refs", .error),
("fatal: not a git repository", .error),
(" ! [rejected] main -> main (fetch first)", .error),
(" ! [remote rejected] main -> main (pre-receive hook declined)", .error),
("Aborting", .error),
("Please commit your changes or stash them before you switch branches.", .error),
("Already up to date.", .output),
])
func generic(line: String, kind: GitLineKind) {
var classifier = GitOutputClassifier(arguments: ["pull"])
#expect(classifier.classify(line, isError: true) == kind)
var other = GitOutputClassifier(arguments: ["push"])
#expect(other.classify(line, isError: false) == kind)
}
@Test("stderr progress is plain output")
func progressIsOutput() {
var classifier = GitOutputClassifier(arguments: ["push"])
#expect(classifier.classify("Writing objects: 100% (3/3), 280 bytes | 280.00 KiB/s, done.", isError: true) == .output)
#expect(classifier.classify("To github.com:x/y.git", isError: true) == .output)
}
@Test("a push ref update is a success", arguments: [
" a1b2c3d..e4f5a6b main -> main",
" * [new branch] feature/x -> feature/x",
" + a1b2c3d...e4f5a6b main -> main (forced update)",
])
func pushSuccess(line: String) {
var classifier = GitOutputClassifier(arguments: ["push", "-u", "origin", "main"])
#expect(classifier.classify(line, isError: true) == .success)
}
@Test("a ref update outside push is plain output")
func refUpdateOutsidePush() {
var classifier = GitOutputClassifier(arguments: ["fetch"])
#expect(classifier.classify(" a1b2c3d..e4f5a6b main -> origin/main", isError: true) == .output)
}
@Test("status paths take the kind of their section")
func statusSections() {
var classifier = GitOutputClassifier(arguments: ["-c", "color.ui=never", "status"])
let lines: [(String, GitLineKind)] = [
("On branch main", .output),
("Changes to be committed:", .output),
(" (use \"git restore --staged <file>...\" to unstage)", .output),
("\tmodified: a.txt", .staged),
("Changes not staged for commit:", .output),
("\tmodified: b.txt", .unstaged),
("Untracked files:", .output),
("\tnew.txt", .unstaged),
]
for (line, kind) in lines {
#expect(classifier.classify(line, isError: false) == kind, "\(line)")
}
}
@Test("tab lines outside status are plain output")
func tabLinesOutsideStatus() {
var classifier = GitOutputClassifier(arguments: ["commit"])
_ = classifier.classify("Changes to be committed:", isError: false)
#expect(classifier.classify("\tmodified: a.txt", isError: false) == .output)
}
}