134 lines
6.3 KiB
Swift
134 lines
6.3 KiB
Swift
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("a merge conflict is not counted as staged")
|
|
func conflictIsNotStaged() {
|
|
let state = GitDerivation.derive(GitSnapshot(branch: "main", ahead: 0, canPush: false, changes: [
|
|
change("both.txt", .unmerged, staged: false, "a\n", "<<<<<<< HEAD\na\n=======\nb\n>>>>>>> topic\n"),
|
|
change("ready.txt", .modified, staged: true, "1\n", "2\n"),
|
|
]))
|
|
#expect(state.stagedCount == 1)
|
|
#expect(state.unstagedCount == 1)
|
|
#expect(state.stagedPaths == ["ready.txt"])
|
|
#expect(state.unstagedPaths == ["both.txt"])
|
|
}
|
|
|
|
@Test("behind and upstream pass through from the snapshot")
|
|
func behindAndUpstream() {
|
|
let state = GitDerivation.derive(GitSnapshot(branch: "main", ahead: 1, canPush: true, behind: 2, upstream: "origin/main", changes: []))
|
|
#expect(state.behind == 2)
|
|
#expect(state.upstream == "origin/main")
|
|
#expect(GitDerivation.derive(nil).upstream == nil)
|
|
#expect(GitDerivation.derive(nil).behind == 0)
|
|
}
|
|
|
|
@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 {
|
|
@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 ")
|
|
}
|
|
}
|