Repository Public
1import Foundation2import Testing3@testable import GitCore45@Suite struct GitParserTests {6 @Test func parsesPorcelainV2() {7 let z = "\0"8 let out = ["# branch.oid 0123abcd", "# branch.head main", "# branch.upstream origin/main", "# branch.ab +2 -1",9 "1 M. N... 100644 100644 100644 aaa bbb staged.txt", "1 .M N... 100644 100644 100644 aaa bbb unstaged.txt",10 "1 MM N... 100644 100644 100644 aaa bbb both.txt", "2 R. N... 100644 100644 100644 aaa bbb R100 new name.txt", "old.txt",11 "u UU N... 100644 100644 100644 100644 a b c conflict.txt", "? untracked.txt", "! ignored.txt"].joined(separator: z) + z12 let s = GitParsers.parseStatus(out)13 #expect(s.branch == "main"); #expect(s.upstream == "origin/main"); #expect(s.ahead == 2); #expect(s.behind == 1)14 #expect(s.entries.count == 7)15 #expect(s.entries[0].kind == .staged); #expect(s.entries[1].kind == .unstaged); #expect(s.entries[2].kind == .both)16 #expect(s.entries[3].kind == .renamed); #expect(s.entries[3].path == "new name.txt"); #expect(s.entries[3].originalPath == "old.txt")17 #expect(s.entries[4].kind == .conflicted); #expect(s.hasConflicts)18 #expect(s.untrackedCount == 1); #expect(!s.isClean)19 #expect(GitParsers.parseStatus("# branch.oid (initial)\0# branch.head (detached)\0").isDetached)20 }2122 @Test func parsesWorktrees() {23 let out = "worktree /repo\nHEAD abc\nbranch refs/heads/main\n\nworktree /repo-wt\nHEAD def\ndetached\nlocked\n\n"24 let w = GitParsers.parseWorktrees(out)25 #expect(w.count == 2); #expect(w[0].branchName == "main"); #expect(w[1].isDetached); #expect(w[1].isLocked)26 }2728 @Test func branchNameValidation() {29 #expect(GitRepository.isValidBranchName("agent/feature-1"))30 #expect(!GitRepository.isValidBranchName("-rf"))31 #expect(!GitRepository.isValidBranchName("a..b"))32 #expect(!GitRepository.isValidBranchName("bad name"))33 }34}3536@Suite struct GitRepositoryTests {37 func makeRepo() async throws -> String {38 let dir = FileManager.default.temporaryDirectory.appendingPathComponent("nexterm-git-\(UUID().uuidString)").path39 try FileManager.default.createDirectory(atPath: dir, withIntermediateDirectories: true)40 try await GitRunner.check(["init", "-q", "-b", "main"], in: dir)41 try await GitRunner.check(["config", "user.email", "t@example.com"], in: dir)42 try await GitRunner.check(["config", "user.name", "T"], in: dir)43 try "hello".write(toFile: dir + "/a.txt", atomically: true, encoding: .utf8)44 try await GitRunner.check(["add", "a.txt"], in: dir)45 try await GitRunner.check(["commit", "-q", "-m", "init"], in: dir)46 return dir47 }4849 @Test func statusLogAndDiff() async throws {50 let dir = try await makeRepo()51 let repo = GitRepository(path: dir)52 var s = try await repo.status()53 #expect(s.branch == "main"); #expect(s.isClean)54 try "changed".write(toFile: dir + "/a.txt", atomically: true, encoding: .utf8)55 try "new".write(toFile: dir + "/b.txt", atomically: true, encoding: .utf8)56 s = try await repo.status()57 #expect(s.unstagedCount == 1); #expect(s.untrackedCount == 1)58 let diff = try await repo.diff(path: "a.txt")59 #expect(diff.contains("-hello")); #expect(diff.contains("+changed"))60 let log = try await repo.log()61 #expect(log.count == 1); #expect(log[0].subject == "init")62 let discovered = await GitRepository.discover(from: dir + "/")63 #expect(discovered.map { URL(fileURLWithPath: $0).resolvingSymlinksInPath().path } == URL(fileURLWithPath: dir).resolvingSymlinksInPath().path)64 }6566 @Test func worktreeLifecycleRefusesDirtyRemoval() async throws {67 let dir = try await makeRepo()68 let repo = GitRepository(path: dir)69 let wt = dir + "-wt-agent1"70 try await repo.addWorktree(at: wt, branch: "agent/1", createBranch: true)71 var list = try await repo.worktrees()72 #expect(list.count == 2)73 #expect(list.contains { $0.branchName == "agent/1" })74 try "wip".write(toFile: wt + "/wip.txt", atomically: true, encoding: .utf8)75 await #expect(throws: GitSafetyError.worktreeHasUncommittedChanges(wt)) {76 try await repo.removeCleanWorktree(at: wt)77 }78 try FileManager.default.removeItem(atPath: wt + "/wip.txt")79 try await repo.removeCleanWorktree(at: wt)80 list = try await repo.worktrees()81 #expect(list.count == 1)82 await #expect(throws: GitSafetyError.invalidBranchName("-x")) { try await repo.addWorktree(at: dir + "-x", branch: "-x", createBranch: true) }83 }8485 @Test func runnerDoesNotHangOnPrompts() async throws {86 let dir = try await makeRepo()87 let r = try await GitRunner.run(["fetch", "https://127.0.0.1:1/nonexistent.git"], in: dir, timeout: 15)88 #expect(!r.succeeded)89 }90}91