Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

diffFileRevisions.test.ts

327 lines9.7 KB

1import { assert, describe, it } from "@effect/vitest";
2​
3import { parseDiffFilePaths, parseDiffFileRevisions } from "./diffFileRevisions.ts";
4​
5function patchOf(...lines: ReadonlyArray<string>): string {
6 return `${lines.join("\n")}\n`;
7}
8​
9describe("parseDiffFileRevisions", () => {
10 it("reads the head id of a changed file off its index line", () => {
11 const revisions = parseDiffFileRevisions(
12 patchOf(
13 "diff --git a/src/a.ts b/src/a.ts",
14 "index 7f2aa0ab6..b4a2a7c9a 100644",
15 "--- a/src/a.ts",
16 "+++ b/src/a.ts",
17 "@@ -1 +1 @@",
18 "-a",
19 "+b",
20 ),
21 );
22​
23 assert.deepStrictEqual([...revisions], [["src/a.ts", "b4a2a7c9a"]]);
24 });
25​
26 it("names a deletion by the path it had, which is the one still on screen", () => {
27 const revisions = parseDiffFileRevisions(
28 patchOf(
29 "diff --git a/gone.ts b/gone.ts",
30 "deleted file mode 100644",
31 "index 1111111..0000000",
32 "--- a/gone.ts",
33 "+++ /dev/null",
34 "@@ -1 +0,0 @@",
35 "-x",
36 ),
37 );
38​
39 assert.deepStrictEqual([...revisions], [["gone.ts", "0000000"]]);
40 });
41​
42 it("names a rename by where it moved to", () => {
43 const revisions = parseDiffFileRevisions(
44 patchOf(
45 "diff --git a/old.ts b/new.ts",
46 "similarity index 90%",
47 "rename from old.ts",
48 "rename to new.ts",
49 "index 2222222..3333333 100644",
50 "--- a/old.ts",
51 "+++ b/new.ts",
52 "@@ -1 +1 @@",
53 "-a",
54 "+b",
55 ),
56 );
57​
58 assert.deepStrictEqual([...revisions], [["new.ts", "3333333"]]);
59 });
60​
61 it("names a rename that changed nothing, which states no paths of its own", () => {
62 const revisions = parseDiffFileRevisions(
63 patchOf(
64 "diff --git a/old.ts b/new.ts",
65 "similarity index 100%",
66 "rename from old.ts",
67 "rename to new.ts",
68 "index 2222222..2222222 100644",
69 ),
70 );
71​
72 assert.deepStrictEqual([...revisions], [["new.ts", "2222222"]]);
73 });
74​
75 it("keeps a leading U+FEFF in a quoted rename's revision key", () => {
76 const revisions = parseDiffFileRevisions(
77 patchOf(
78 'diff --git a/old.txt "b/\\357\\273\\277note.txt"',
79 "similarity index 100%",
80 "rename from old.txt",
81 'rename to "\\357\\273\\277note.txt"',
82 "index 2222222..2222222 100644",
83 ),
84 );
85​
86 assert.deepStrictEqual([...revisions], [["\uFEFFnote.txt", "2222222"]]);
87 });
88​
89 it("leaves out a file Bitbucket excluded, which it gives no index line for", () => {
90 const revisions = parseDiffFileRevisions(
91 patchOf(
92 "diff --git a/package.json b/package.json",
93 "index 7f2aa0ab6..b4a2a7c9a 100644",
94 "--- a/package.json",
95 "+++ b/package.json",
96 "@@ -1 +1 @@",
97 '- "x": "1"',
98 '+ "x": "2"',
99 "diff --git a/yarn.lock b/yarn.lock",
100 'File excluded by pattern "yarn.lock"',
101 ),
102 );
103​
104 assert.deepStrictEqual([...revisions], [["package.json", "b4a2a7c9a"]]);
105 });
106​
107 it("stops reading headers at the first hunk, so content cannot pose as one", () => {
108 const revisions = parseDiffFileRevisions(
109 patchOf(
110 "diff --git a/notes.md b/notes.md",
111 "index aaaaaaa..bbbbbbb 100644",
112 "--- a/notes.md",
113 "+++ b/notes.md",
114 "@@ -1,2 +1,2 @@",
115 "--- a/decoy.ts",
116 "+++ b/decoy.ts",
117 "+index ccccccc..ddddddd 100644",
118 ),
119 );
120​
121 assert.deepStrictEqual([...revisions], [["notes.md", "bbbbbbb"]]);
122 });
123​
124 it("splits a header whose paths contain the separator, by the sides agreeing", () => {
125 const revisions = parseDiffFileRevisions(
126 patchOf(
127 "diff --git a/one b/two.ts b/one b/two.ts",
128 "index eeeeeee..fffffff 100644",
129 "@@ -1 +1 @@",
130 ),
131 );
132​
133 assert.deepStrictEqual([...revisions], [["one b/two.ts", "fffffff"]]);
134 });
135​
136 it("leaves out an added file that Bitbucket sent no index line for", () => {
137 const revisions = parseDiffFileRevisions(
138 patchOf("diff --git a/added.ts b/added.ts", "new file mode 100644", "--- /dev/null"),
139 );
140​
141 assert.strictEqual(revisions.size, 0);
142 });
143​
144 it("reads nothing out of an empty patch", () => {
145 assert.strictEqual(parseDiffFileRevisions("").size, 0);
146 });
147​
148 it("reads a name git had to quote, here one holding a tab", () => {
149 const revisions = parseDiffFileRevisions(
150 patchOf(
151 'diff --git "a/we\\tird.ts" "b/we\\tird.ts"',
152 "index 4444444..5555555 100644",
153 '--- "a/we\\tird.ts"',
154 '+++ "b/we\\tird.ts"',
155 "@@ -1 +1 @@",
156 "-a",
157 "+b",
158 ),
159 );
160​
161 assert.deepStrictEqual([...revisions], [["we\tird.ts", "5555555"]]);
162 });
163​
164 it("rejoins the octal bytes git writes for a name outside ASCII", () => {
165 const revisions = parseDiffFileRevisions(
166 patchOf(
167 'diff --git "a/caf\\303\\251/r\\303\\251sum\\303\\251.ts" "b/caf\\303\\251/r\\303\\251sum\\303\\251.ts"',
168 "index 6666666..7777777 100644",
169 "@@ -1 +1 @@",
170 ),
171 );
172​
173 assert.deepStrictEqual([...revisions], [["café/résumé.ts", "7777777"]]);
174 });
175​
176 it("splits a rename header where git quoted only the side that needed it", () => {
177 const revisions = parseDiffFileRevisions(
178 patchOf(
179 'diff --git a/old.ts "b/new\\tname.ts"',
180 "similarity index 90%",
181 "rename from old.ts",
182 'rename to "new\\tname.ts"',
183 "index 8888888..9999999 100644",
184 "--- a/old.ts",
185 '+++ "b/new\\tname.ts"',
186 "@@ -1 +1 @@",
187 "-a",
188 "+b",
189 ),
190 );
191​
192 assert.deepStrictEqual([...revisions], [["new\tname.ts", "9999999"]]);
193 });
194​
195 it("names a quoted rename that changed nothing, which states no paths of its own", () => {
196 const revisions = parseDiffFileRevisions(
197 patchOf(
198 'diff --git "a/old\\tname.ts" "b/new\\tname.ts"',
199 "similarity index 100%",
200 'rename from "old\\tname.ts"',
201 'rename to "new\\tname.ts"',
202 "index abcabca..abcabca 100644",
203 ),
204 );
205​
206 assert.deepStrictEqual([...revisions], [["new\tname.ts", "abcabca"]]);
207 });
208​
209 it("keeps a character from outside the basic plane that git left unescaped", () => {
210 // `core.quotePath` off leaves the name's own bytes in place, and git still quotes the header
211 // for the tab. Encoding what it left one unit at a time would split the pair into two halves.
212 const revisions = parseDiffFileRevisions(
213 patchOf(
214 'diff --git "a/we\\tird-\u{1f680}.ts" "b/we\\tird-\u{1f680}.ts"',
215 "index aaaaaaa..bbbbbbb 100644",
216 "@@ -1 +1 @@",
217 ),
218 );
219​
220 assert.deepStrictEqual([...revisions], [["we\tird-\u{1f680}.ts", "bbbbbbb"]]);
221 });
222​
223 it("drops the tab git ends a name holding a space with", () => {
224 const revisions = parseDiffFileRevisions(
225 patchOf(
226 "diff --git a/with space.txt b/with space.txt",
227 "index 1111111..6178079 100644",
228 "--- a/with space.txt\t",
229 "+++ b/with space.txt\t",
230 "@@ -1 +1 @@",
231 "-a",
232 "+b",
233 ),
234 );
235​
236 assert.deepStrictEqual([...revisions], [["with space.txt", "6178079"]]);
237 });
238​
239 it("drops that tab from a quoted name too, where it lands past the closing quote", () => {
240 const revisions = parseDiffFileRevisions(
241 patchOf(
242 'diff --git "a/we ird\\tname.ts" "b/we ird\\tname.ts"',
243 "index 2222222..7777777 100644",
244 '--- "a/we ird\\tname.ts"\t',
245 '+++ "b/we ird\\tname.ts"\t',
246 "@@ -1 +1 @@",
247 "-a",
248 "+b",
249 ),
250 );
251​
252 assert.deepStrictEqual([...revisions], [["we ird\tname.ts", "7777777"]]);
253 });
254​
255 it("reads a deletion whose name holds a space, tab and all", () => {
256 const revisions = parseDiffFileRevisions(
257 patchOf(
258 "diff --git a/with space.txt b/with space.txt",
259 "deleted file mode 100644",
260 "index 3333333..0000000",
261 "--- a/with space.txt\t",
262 "+++ /dev/null",
263 "@@ -1 +0,0 @@",
264 "-a",
265 ),
266 );
267​
268 assert.deepStrictEqual([...revisions], [["with space.txt", "0000000"]]);
269 });
270​
271 it("drops the timestamp other producers of the format write past that tab", () => {
272 const revisions = parseDiffFileRevisions(
273 patchOf(
274 "diff --git a/stamped.ts b/stamped.ts",
275 "index 4444444..5555555 100644",
276 "--- a/stamped.ts\t2024-01-01 00:00:00.000000000 +0000",
277 "+++ b/stamped.ts\t2024-01-02 00:00:00.000000000 +0000",
278 "@@ -1 +1 @@",
279 "-a",
280 "+b",
281 ),
282 );
283​
284 assert.deepStrictEqual([...revisions], [["stamped.ts", "5555555"]]);
285 });
286​
287 it("splits an unquoted header whose names hold a space, by the sides agreeing", () => {
288 const revisions = parseDiffFileRevisions(
289 patchOf("diff --git a/one two b/one two", "index ddddddd..eeeeeee 100644", "@@ -1 +1 @@"),
290 );
291​
292 assert.deepStrictEqual([...revisions], [["one two", "eeeeeee"]]);
293 });
294});
295​
296describe("parseDiffFilePaths", () => {
297 it("names both sides of each file, with no side for an added or deleted one", () => {
298 const paths = parseDiffFilePaths(
299 [
300 "diff --git a/src/a.ts b/src/a.ts",
301 "--- a/src/a.ts",
302 "+++ b/src/a.ts",
303 "@@ -1 +1 @@",
304 "--- a/not/a/header.ts",
305 "diff --git a/added.md b/added.md",
306 "new file mode 100644",
307 "--- /dev/null",
308 "+++ b/added.md",
309 "diff --git a/gone.md b/gone.md",
310 "deleted file mode 100644",
311 "--- a/gone.md",
312 "+++ /dev/null",
313 'diff --git "a/tab\\tname" b/moved.ts',
314 'rename from "tab\\tname"',
315 "rename to moved.ts",
316 "",
317 ].join("\n"),
318 );
319​
320 assert.deepStrictEqual(paths, [
321 { oldPath: "src/a.ts", newPath: "src/a.ts" },
322 { oldPath: null, newPath: "added.md" },
323 { oldPath: "gone.md", newPath: null },
324 { oldPath: "tab\tname", newPath: "moved.ts" },
325 ]);
326 });
327});