|
| 1 | +import Testing |
| 2 | + |
| 3 | +@testable import gitdiff |
| 4 | + |
| 5 | +internal func makeLargeMultiHunkDiff(hunks: Int, linesPerHunk: Int) -> String { |
| 6 | + var parts: [String] = [] |
| 7 | + parts.append("diff --git a/large.txt b/large.txt") |
| 8 | + parts.append("index 7777777..8888888 100644") |
| 9 | + parts.append("--- a/large.txt") |
| 10 | + parts.append("+++ b/large.txt") |
| 11 | + var oldStart = 1 |
| 12 | + var newStart = 1 |
| 13 | + for _ in 0..<hunks { |
| 14 | + parts.append("@@ -\(oldStart),\(linesPerHunk) +\(newStart),\(linesPerHunk) @@") |
| 15 | + // Add alternating context/removed/added to keep parser busy |
| 16 | + for j in 0..<linesPerHunk { |
| 17 | + if j % 3 == 0 { |
| 18 | + parts.append(" context line \(j)") |
| 19 | + oldStart += 1 |
| 20 | + newStart += 1 |
| 21 | + } else if j % 3 == 1 { |
| 22 | + parts.append("-removed line \(j)") |
| 23 | + oldStart += 1 |
| 24 | + } else { |
| 25 | + parts.append("+added line \(j)") |
| 26 | + newStart += 1 |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + return parts.joined(separator: "\n") |
| 31 | +} |
| 32 | +struct DiffParserTests { |
| 33 | + // MARK: - Helpers |
| 34 | + private func makeSimpleSingleHunkDiff() -> String { |
| 35 | + return """ |
| 36 | + diff --git a/foo.txt b/foo.txt |
| 37 | + index 1111111..2222222 100644 |
| 38 | + --- a/foo.txt |
| 39 | + +++ b/foo.txt |
| 40 | + @@ -1,2 +1,3 @@ |
| 41 | + line1 |
| 42 | + -line2 |
| 43 | + +line2 changed |
| 44 | + +line3 |
| 45 | + """ |
| 46 | + } |
| 47 | + |
| 48 | + private func makeTwoHunksDiff() -> String { |
| 49 | + return """ |
| 50 | + diff --git a/bar.txt b/bar.txt |
| 51 | + index 3333333..4444444 100644 |
| 52 | + --- a/bar.txt |
| 53 | + +++ b/bar.txt |
| 54 | + @@ -1,2 +1,2 @@ |
| 55 | + a |
| 56 | + -b |
| 57 | + +B |
| 58 | + @@ -5,2 +5,3 @@ |
| 59 | + five |
| 60 | + -six |
| 61 | + +six! |
| 62 | + +seven |
| 63 | + """ |
| 64 | + } |
| 65 | + |
| 66 | + private func makeBinaryDiff() -> String { |
| 67 | + return """ |
| 68 | + diff --git a/bin/file.bin b/bin/file.bin |
| 69 | + index abcdef1..abcdef2 100644 |
| 70 | + Binary files a/bin/file.bin and b/bin/file.bin differ |
| 71 | + """ |
| 72 | + } |
| 73 | + |
| 74 | + private func makeRenameDiff() -> String { |
| 75 | + return """ |
| 76 | + diff --git a/old.txt b/new.txt |
| 77 | + similarity index 100% |
| 78 | + rename from old.txt |
| 79 | + rename to new.txt |
| 80 | + index 5555555..6666666 100644 |
| 81 | + --- a/old.txt |
| 82 | + +++ b/new.txt |
| 83 | + """ |
| 84 | + } |
| 85 | + |
| 86 | + // MARK: - Tests |
| 87 | + |
| 88 | + @Test |
| 89 | + func testParseEmptyReturnsEmpty() async throws { |
| 90 | + let files = try await DiffParser.parse("") |
| 91 | + #expect(files.count == 0) |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + func testParseSingleFileSingleHunk() async throws { |
| 96 | + let diff = makeSimpleSingleHunkDiff() |
| 97 | + let files = try await DiffParser.parse(diff) |
| 98 | + #expect(files.count == 1) |
| 99 | + let file = try #require(files.first) |
| 100 | + #expect(file.oldPath == "foo.txt") |
| 101 | + #expect(file.newPath == "foo.txt") |
| 102 | + #expect(file.isBinary == false) |
| 103 | + #expect(file.isRenamed == false) |
| 104 | + #expect(file.hunks.count == 1) |
| 105 | + let hunk = try #require(file.hunks.first) |
| 106 | + #expect(hunk.header.trimmingCharacters(in: .whitespaces) == "@@ -1,2 +1,3 @@") |
| 107 | + #expect(hunk.lines.count == 4) |
| 108 | + #expect(hunk.lines[0].type == .context) |
| 109 | + #expect(hunk.lines[0].content == "line1") |
| 110 | + #expect(hunk.lines[1].type == .removed) |
| 111 | + #expect(hunk.lines[1].content == "line2") |
| 112 | + #expect(hunk.lines[2].type == .added) |
| 113 | + #expect(hunk.lines[2].content == "line2 changed") |
| 114 | + #expect(hunk.lines[3].type == .added) |
| 115 | + #expect(hunk.lines[3].content == "line3") |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + func testParseMultipleHunksInOneFile() async throws { |
| 120 | + let diff = makeTwoHunksDiff() |
| 121 | + let files = try await DiffParser.parse(diff) |
| 122 | + #expect(files.count == 1) |
| 123 | + let file = try #require(files.first) |
| 124 | + #expect(file.hunks.count == 2) |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + func testParseBinaryFile() async throws { |
| 129 | + let diff = makeBinaryDiff() |
| 130 | + let files = try await DiffParser.parse(diff) |
| 131 | + #expect(files.count == 1) |
| 132 | + let file = try #require(files.first) |
| 133 | + #expect(file.isBinary) |
| 134 | + #expect(file.hunks.count == 0) |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + func testParseRename() async throws { |
| 139 | + let diff = makeRenameDiff() |
| 140 | + let files = try await DiffParser.parse(diff) |
| 141 | + #expect(files.count == 1) |
| 142 | + let file = try #require(files.first) |
| 143 | + #expect(file.isRenamed) |
| 144 | + #expect(file.oldPath == "old.txt") |
| 145 | + #expect(file.newPath == "new.txt") |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + func testCancellationThrowsCancellationError() async throws { |
| 150 | + // Build a large diff to ensure the task doesn't finish instantly |
| 151 | + let diff = makeLargeMultiHunkDiff(hunks: 40, linesPerHunk: 30) |
| 152 | + let task = Task { () -> [DiffFile] in try await DiffParser.parse(diff) } |
| 153 | + // Yield and then cancel shortly after to trigger cooperative cancellation |
| 154 | + await Task.yield() |
| 155 | + task.cancel() |
| 156 | + await #expect(throws: CancellationError.self) { |
| 157 | + _ = try await task.value |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments