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 unstaged U 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: .unmerged, staged: false)]) } // 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 behind count comes from the header", arguments: [ ("main...origin/main [ahead 1, behind 2]", 2), ("main...origin/main [behind 5]", 5), ("main...origin/main [ahead 3]", 0), ("main...origin/main", 0), ("feature/x", 0), ("HEAD (no branch)", 0), ]) func behind(header: String, count: Int) { #expect(Porcelain.parseBehind(header: header) == count) } @Test("the upstream comes from the header", arguments: [ ("main...origin/main [ahead 1, behind 2]", "origin/main"), ("feature/x...origin/feature/x", "origin/feature/x"), ("main...origin/main [gone]", "origin/main"), ("main", nil), ("No commits yet on main", nil), ("HEAD (no branch)", nil), ] as [(String, String?)]) func upstream(header: String, name: String?) { #expect(Porcelain.parseUpstream(header: header) == name) } @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.behind == 0) #expect(parsed.upstream == "origin/main") #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("a header ahead and behind carries both counts and the upstream") func aheadAndBehind() { let parsed = Porcelain.parseStatus("## main...origin/main [ahead 1, behind 2]\0") #expect(parsed == ParsedStatus(branch: "main", ahead: 1, canPush: true, behind: 2, upstream: "origin/main", entries: [])) } @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 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") } }