restores Helder in all its glory
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
import HelderCore
|
||||
import Testing
|
||||
|
||||
@Suite struct GitDerivationTests {
|
||||
private func change(_ path: String, _ status: GitStatus, staged: Bool, _ original: String, _ updated: String) -> GitFileChange {
|
||||
GitFileChange(path: path, status: status, staged: staged, original: original, updated: updated)
|
||||
}
|
||||
|
||||
@Test("a change over the diff cap marks its row and whole-file diffs as too big")
|
||||
func tooBig() {
|
||||
let snapshot = GitSnapshot(branch: "main", ahead: 0, canPush: false, changes: [
|
||||
GitFileChange(path: "dump.sql", status: .modified, staged: true, original: "", updated: "", isTooBig: false),
|
||||
GitFileChange(path: "dump.sql", status: .modified, staged: false, original: "", updated: "", isTooBig: true),
|
||||
change("a.txt", .modified, staged: false, "1\n", "2\n"),
|
||||
])
|
||||
let state = GitDerivation.derive(snapshot)
|
||||
#expect(state.changes.map(\.path) == ["dump.sql", "dump.sql", "a.txt"])
|
||||
#expect(state.rowDiffs["s:dump.sql"]?.isTooBig == false)
|
||||
#expect(state.rowDiffs["w:dump.sql"]?.isTooBig == true)
|
||||
#expect(state.fileDiffs["dump.sql"]?.isTooBig == true)
|
||||
#expect(state.fileDiffs["a.txt"]?.isTooBig == false)
|
||||
}
|
||||
|
||||
@Test("no snapshot means not a repository")
|
||||
func notARepository() {
|
||||
let state = GitDerivation.derive(nil)
|
||||
#expect(!state.isRepository)
|
||||
#expect(state.branch == "—")
|
||||
#expect(state.changes.isEmpty)
|
||||
}
|
||||
|
||||
@Test("one row per change, with its line counts and row diff")
|
||||
func rows() {
|
||||
let snapshot = GitSnapshot(branch: "main", ahead: 1, canPush: true, changes: [
|
||||
change("a.txt", .modified, staged: false, "1\n2\n3\n", "1\nX\n3\n4\n"),
|
||||
])
|
||||
let state = GitDerivation.derive(snapshot)
|
||||
#expect(state.isRepository)
|
||||
#expect(state.branch == "main")
|
||||
#expect(state.ahead == 1)
|
||||
#expect(state.canPush)
|
||||
#expect(state.changes == [Change(path: "a.txt", status: .modified, added: 2, removed: 1, staged: false)])
|
||||
#expect(state.rowDiffs["w:a.txt"]?.added == 2)
|
||||
#expect(state.fileDiffs["a.txt"]?.updated == "1\nX\n3\n4\n")
|
||||
#expect(state.stagedPaths.isEmpty)
|
||||
}
|
||||
|
||||
@Test("a staged-then-edited file compares HEAD with disk for the whole-file views")
|
||||
func twoRowsWholeFile() {
|
||||
let snapshot = GitSnapshot(branch: "main", ahead: 0, canPush: false, changes: [
|
||||
change("a.txt", .modified, staged: true, "1\n2\n3\n", "1\nSTAGED\n3\n"),
|
||||
change("a.txt", .modified, staged: false, "1\nSTAGED\n3\n", "1\nSTAGED\n3\n4\n"),
|
||||
])
|
||||
let state = GitDerivation.derive(snapshot)
|
||||
#expect(state.changes.map(\.id) == ["s:a.txt", "w:a.txt"])
|
||||
#expect(state.stagedPaths == ["a.txt"])
|
||||
let whole = state.fileDiffs["a.txt"]
|
||||
#expect(whole?.original == "1\n2\n3\n")
|
||||
#expect(whole?.updated == "1\nSTAGED\n3\n4\n")
|
||||
#expect(state.rowDiffs["s:a.txt"]?.updated == "1\nSTAGED\n3\n")
|
||||
#expect(state.rowDiffs["w:a.txt"]?.original == "1\nSTAGED\n3\n")
|
||||
}
|
||||
|
||||
@Test("the unstaged row owns the file status: staged as modified but deleted on disk reads deleted")
|
||||
func unstagedOwnsStatus() {
|
||||
let snapshot = GitSnapshot(branch: "main", ahead: 0, canPush: false, changes: [
|
||||
change("a.txt", .modified, staged: true, "old\n", "new\n"),
|
||||
change("a.txt", .deleted, staged: false, "new\n", ""),
|
||||
])
|
||||
let state = GitDerivation.derive(snapshot)
|
||||
#expect(state.fileDiffs["a.txt"]?.isDeletedFile == true)
|
||||
#expect(state.fileDiffs["a.txt"]?.original == "old\n")
|
||||
#expect(state.changes.last?.isDeleted == true)
|
||||
}
|
||||
|
||||
@Test("the unstaged row first and the staged row second still pair HEAD with disk")
|
||||
func reversedRowOrder() {
|
||||
let snapshot = GitSnapshot(branch: "main", ahead: 0, canPush: false, changes: [
|
||||
change("a.txt", .modified, staged: false, "index\n", "disk\n"),
|
||||
change("a.txt", .added, staged: true, "", "index\n"),
|
||||
])
|
||||
let whole = GitDerivation.derive(snapshot).fileDiffs["a.txt"]
|
||||
#expect(whole?.original == "")
|
||||
#expect(whole?.updated == "disk\n")
|
||||
#expect(whole?.isAddedFile == false)
|
||||
}
|
||||
|
||||
@Test("a renamed row keeps the path it was renamed from")
|
||||
func renameSource() {
|
||||
var renamed = change("new.txt", .renamed, staged: true, "a\n", "a\n")
|
||||
renamed.originalPath = "old.txt"
|
||||
let state = GitDerivation.derive(GitSnapshot(branch: "main", ahead: 0, canPush: false, changes: [renamed]))
|
||||
#expect(state.changes.first?.originalPath == "old.txt")
|
||||
#expect(state.fileDiffs["new.txt"] == state.rowDiffs["s:new.txt"])
|
||||
}
|
||||
|
||||
@Test("a new file is an added file diff")
|
||||
func newFile() {
|
||||
let snapshot = GitSnapshot(branch: "main", ahead: 0, canPush: true, changes: [change("n.txt", .added, staged: false, "", "fresh\n")])
|
||||
let state = GitDerivation.derive(snapshot)
|
||||
#expect(state.fileDiffs["n.txt"]?.isAddedFile == true)
|
||||
#expect(state.totals == (added: 1, removed: 0))
|
||||
#expect(state.unstagedPaths == ["n.txt"])
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct SourceControlTextTests {
|
||||
private func change(_ path: String, staged: Bool = false) -> Change {
|
||||
Change(path: path, status: .modified, added: 1, removed: 0, staged: staged)
|
||||
}
|
||||
|
||||
@Test("one group per folder, even when git returns the folder in separate runs")
|
||||
func oneGroupPerFolder() {
|
||||
let groups = SourceControlText.groupByDir([
|
||||
change("tests/Feature/AlphaTest.php"), change("app/Models/User.php"), change("tests/Feature/BetaTest.php"),
|
||||
])
|
||||
#expect(groups.map(\.dir) == ["tests/Feature", "app/Models"])
|
||||
#expect(groups[0].rows.map(\.path) == ["tests/Feature/AlphaTest.php", "tests/Feature/BetaTest.php"])
|
||||
}
|
||||
|
||||
@Test("every row is kept, so the keyboard cursor walks the full list")
|
||||
func keepsEveryRow() {
|
||||
let list = ["a/one.ts", "b/two.ts", "a/three.ts", "four.ts"].map { change($0) }
|
||||
let flat = SourceControlText.groupByDir(list).flatMap(\.rows)
|
||||
#expect(flat.count == list.count)
|
||||
#expect(Set(flat.map(\.id)).count == list.count)
|
||||
}
|
||||
|
||||
@Test("a project-root file sits in the empty-string group")
|
||||
func rootGroup() {
|
||||
#expect(SourceControlText.groupByDir([change("README.md")]) == [ChangeGroup(dir: "", rows: [change("README.md")])])
|
||||
}
|
||||
|
||||
@Test("navigation walks staged groups before unstaged ones")
|
||||
func navigationOrder() {
|
||||
let order = SourceControlText.navigationOrder([change("b/x"), change("a/y", staged: true), change("b/z")])
|
||||
#expect(order.map(\.path) == ["a/y", "b/x", "b/z"])
|
||||
}
|
||||
|
||||
@Test("a dictated commit message becomes one lowercase line")
|
||||
func commitMessage() {
|
||||
#expect(SourceControlText.normalizeCommitMessage("Fix The Bug\n In Parser \r\n") == "fix the bug in parser ")
|
||||
}
|
||||
|
||||
@Test("the push tooltip counts commits")
|
||||
func pushTooltip() {
|
||||
#expect(SourceControlText.pushTooltip(canPush: false, ahead: 0) == "Nothing to push (⌘P)")
|
||||
#expect(SourceControlText.pushTooltip(canPush: true, ahead: 1) == "Push 1 commit to remote (⌘P)")
|
||||
#expect(SourceControlText.pushTooltip(canPush: true, ahead: 3) == "Push 3 commits to remote (⌘P)")
|
||||
#expect(SourceControlText.pushTooltip(canPush: true, ahead: 0) == "Push the branch to remote (⌘P)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
import HelderCore
|
||||
import Testing
|
||||
|
||||
@Suite struct PorcelainTests {
|
||||
// MARK: classify
|
||||
|
||||
@Test("the index column is the staged row, the working column the unstaged row")
|
||||
func columns() {
|
||||
#expect(Porcelain.classify(index: "M", working: " ") == [GitRowSpec(status: .modified, staged: true)])
|
||||
#expect(Porcelain.classify(index: " ", working: "M") == [GitRowSpec(status: .modified, staged: false)])
|
||||
#expect(Porcelain.classify(index: "A", working: " ") == [GitRowSpec(status: .added, staged: true)])
|
||||
#expect(Porcelain.classify(index: "D", working: " ") == [GitRowSpec(status: .deleted, staged: true)])
|
||||
#expect(Porcelain.classify(index: "R", working: " ") == [GitRowSpec(status: .renamed, staged: true)])
|
||||
}
|
||||
|
||||
@Test("an untracked file is one unstaged addition")
|
||||
func untracked() {
|
||||
#expect(Porcelain.classify(index: "?", working: "?") == [GitRowSpec(status: .added, staged: false)])
|
||||
}
|
||||
|
||||
@Test("a staged-then-edited file is two rows", arguments: [
|
||||
("M", "M", GitStatus.modified, GitStatus.modified),
|
||||
("A", "M", .added, .modified),
|
||||
("M", "D", .modified, .deleted),
|
||||
] as [(Character, Character, GitStatus, GitStatus)])
|
||||
func twoRows(index: Character, working: Character, staged: GitStatus, unstaged: GitStatus) {
|
||||
#expect(Porcelain.classify(index: index, working: working) == [
|
||||
GitRowSpec(status: staged, staged: true), GitRowSpec(status: unstaged, staged: false),
|
||||
])
|
||||
}
|
||||
|
||||
@Test("a merge conflict stays one row", arguments: [("U", "U"), ("A", "A"), ("D", "D"), ("U", "D")] as [(Character, Character)])
|
||||
func conflict(index: Character, working: Character) {
|
||||
#expect(Porcelain.classify(index: index, working: working) == [GitRowSpec(status: .modified, staged: true)])
|
||||
}
|
||||
|
||||
// MARK: headers
|
||||
|
||||
@Test("the ahead count comes from the header", arguments: [
|
||||
("main...origin/main [ahead 3]", 3, true),
|
||||
("main...origin/main [ahead 1, behind 2]", 1, true),
|
||||
("main...origin/main", 0, false),
|
||||
("main...origin/main [behind 2]", 0, false),
|
||||
("feature/x", 0, true),
|
||||
("No commits yet on main", 0, false),
|
||||
("HEAD (no branch)", 0, false),
|
||||
])
|
||||
func ahead(header: String, count: Int, canPush: Bool) {
|
||||
let result = Porcelain.parseAhead(header: header)
|
||||
#expect(result.ahead == count)
|
||||
#expect(result.canPush == canPush)
|
||||
}
|
||||
|
||||
@Test("the branch name comes from the header", arguments: [
|
||||
("main...origin/main [ahead 1]", "main"),
|
||||
("main", "main"),
|
||||
("feature/x...origin/feature/x", "feature/x"),
|
||||
("No commits yet on trunk", "trunk"),
|
||||
("HEAD (no branch)", "HEAD"),
|
||||
("", "HEAD"),
|
||||
])
|
||||
func branch(header: String, name: String) {
|
||||
#expect(Porcelain.parseBranch(header: header) == name)
|
||||
}
|
||||
|
||||
// MARK: parseStatus
|
||||
|
||||
@Test("status output is NUL-separated with the header first")
|
||||
func parseStatus() {
|
||||
let raw = "## main...origin/main [ahead 2]\0 M src/a.ts\0?? new file.txt\0MM both.php\0"
|
||||
let parsed = Porcelain.parseStatus(raw)
|
||||
#expect(parsed.branch == "main")
|
||||
#expect(parsed.ahead == 2)
|
||||
#expect(parsed.canPush)
|
||||
#expect(parsed.entries == [
|
||||
StatusEntry(index: " ", working: "M", path: "src/a.ts"),
|
||||
StatusEntry(index: "?", working: "?", path: "new file.txt"),
|
||||
StatusEntry(index: "M", working: "M", path: "both.php"),
|
||||
])
|
||||
}
|
||||
|
||||
@Test("a rename shows its destination and skips the source field")
|
||||
func renameSkipsSource() {
|
||||
let parsed = Porcelain.parseStatus("## main\0R new.ts\0old.ts\0 M other.ts\0")
|
||||
#expect(parsed.entries.map(\.path) == ["new.ts", "other.ts"])
|
||||
#expect(parsed.entries[0].index == "R")
|
||||
}
|
||||
|
||||
@Test("a rename carries its source path, a plain change carries none")
|
||||
func renameCarriesSource() {
|
||||
let parsed = Porcelain.parseStatus("## main\0R new.ts\0old.ts\0 M other.ts\0")
|
||||
#expect(parsed.entries[0] == StatusEntry(index: "R", working: " ", path: "new.ts", originalPath: "old.ts"))
|
||||
#expect(parsed.entries[1].originalPath == nil)
|
||||
}
|
||||
|
||||
@Test("a copy in the working column also skips its source field")
|
||||
func copySkipsSource() {
|
||||
let parsed = Porcelain.parseStatus(" C copy.ts\0orig.ts\0")
|
||||
#expect(parsed.entries.map(\.path) == ["copy.ts"])
|
||||
}
|
||||
|
||||
@Test("without a header the branch reads HEAD and nothing can be pushed")
|
||||
func noHeader() {
|
||||
let parsed = Porcelain.parseStatus(" M a.txt\0")
|
||||
#expect(parsed.branch == "HEAD")
|
||||
#expect(!parsed.canPush)
|
||||
#expect(parsed.entries.count == 1)
|
||||
}
|
||||
|
||||
@Test("empty output is a clean tree")
|
||||
func empty() {
|
||||
#expect(Porcelain.parseStatus("") == ParsedStatus(branch: "HEAD", ahead: 0, canPush: false, entries: []))
|
||||
}
|
||||
|
||||
@Test("paths keep spaces, unicode and no quoting")
|
||||
func unquotedPaths() {
|
||||
let parsed = Porcelain.parseStatus(" M dir/naïve file 😀.md\0")
|
||||
#expect(parsed.entries.first?.path == "dir/naïve file 😀.md")
|
||||
}
|
||||
|
||||
// MARK: push
|
||||
|
||||
@Test("a push toast prefers the ref line, a failure its last line")
|
||||
func pushSummary() {
|
||||
let ok = Porcelain.pushSummary(exitCode: 0, stdout: "", stderr: "To github.com:x/y.git\n abc..def main -> main\n")
|
||||
#expect(ok == PushResult(ok: true, message: "abc..def main -> main"))
|
||||
let failed = Porcelain.pushSummary(exitCode: 1, stdout: "", stderr: "error: failed to push\nhint: pull first\n")
|
||||
#expect(failed == PushResult(ok: false, message: "hint: pull first"))
|
||||
}
|
||||
|
||||
@Test("a missing upstream is detected from git's hint")
|
||||
func needsUpstream() {
|
||||
#expect(Porcelain.needsUpstream(stderr: "fatal: The current branch x has no upstream branch."))
|
||||
#expect(!Porcelain.needsUpstream(stderr: "rejected"))
|
||||
}
|
||||
|
||||
@Test("row ids keep the two halves of one file apart")
|
||||
func rowIDs() {
|
||||
#expect(Change.rowID(path: "a.txt", staged: true) == "s:a.txt")
|
||||
#expect(Change.rowID(path: "a.txt", staged: false) == "w:a.txt")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user