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

azureDevOpsDiff.test.ts

418 lines13.7 KB

1import { unquoteGitPatchPath } from "@t3tools/shared/gitPatchPath";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 azureDevOpsFilePatch,
6 azureDevOpsUnreadableFilePatch,
7 byteLength,
8 formatAzureDevOpsDiffCursor,
9 MAX_FILE_DIFF_EDITS,
10 parseAzureDevOpsDiffCursor,
11} from "./azureDevOpsDiff.ts";
12import type { AzureDevOpsChangeEntry } from "./azureDevOpsPullRequestJson.ts";
13​
14function change(overrides: Partial<AzureDevOpsChangeEntry> = {}): AzureDevOpsChangeEntry {
15 return {
16 path: "README.md",
17 oldPath: "README.md",
18 changeKind: "change",
19 objectId: "8f80",
20 originalObjectId: "0ca4",
21 ...overrides,
22 };
24​
25function texts(oldContents: string, newContents: string, binary = false) {
26 return { oldContents, newContents, binary };
28​
29describe("azureDevOpsFilePatch", () => {
30 it("writes a changed file as the unified patch every diff viewer already reads", () => {
31 const patch = azureDevOpsFilePatch({
32 change: change(),
33 texts: texts("one\ntwo\nthree\n", "one\ntwo again\nthree\n"),
34 });
35​
36 expect(patch.truncated).toBe(false);
37 expect(patch.section).toBe(
38 [
39 "diff --git a/README.md b/README.md",
40 "--- a/README.md",
41 "+++ b/README.md",
42 "@@ -1,3 +1,3 @@",
43 " one",
44 "-two",
45 "+two again",
46 " three",
47 "",
48 ].join("\n"),
49 );
50 });
51​
52 it("names the side a new file does not have as /dev/null", () => {
53 const patch = azureDevOpsFilePatch({
54 change: change({ path: "DEMO.md", oldPath: "DEMO.md", changeKind: "new" }),
55 texts: texts("", "hello\n"),
56 });
57​
58 expect(patch.section).toContain("new file mode 100644");
59 expect(patch.section).toContain("--- /dev/null");
60 expect(patch.section).toContain("+++ b/DEMO.md");
61 expect(patch.section).toContain("@@ -0,0 +1 @@");
62 expect(patch.section).toContain("+hello");
63 });
64​
65 it("names the side a deleted file no longer has as /dev/null", () => {
66 const patch = azureDevOpsFilePatch({
67 change: change({ path: "OLD.md", oldPath: "OLD.md", changeKind: "deleted" }),
68 texts: texts("gone\n", ""),
69 });
70​
71 expect(patch.section).toContain("deleted file mode 100644");
72 expect(patch.section).toContain("--- a/OLD.md");
73 expect(patch.section).toContain("+++ /dev/null");
74 expect(patch.section).toContain("@@ -1 +0,0 @@");
75 expect(patch.section).toContain("-gone");
76 });
77​
78 it("keeps the carriage returns of a file with Windows line endings", () => {
79 const patch = azureDevOpsFilePatch({
80 change: change(),
81 texts: texts("one\r\ntwo\r\n", "one\r\ntwo again\r\n"),
82 });
83​
84 expect(patch.section).toContain("-two\r");
85 expect(patch.section).toContain("+two again\r");
86 });
87​
88 it("keeps a file that only moved, which has no hunks to give", () => {
89 const patch = azureDevOpsFilePatch({
90 change: change({ path: "docs/new.md", oldPath: "docs/old.md", changeKind: "rename-pure" }),
91 texts: texts("same\n", "same\n"),
92 });
93​
94 expect(patch.truncated).toBe(false);
95 expect(patch.section).toBe(
96 [
97 "diff --git a/docs/old.md b/docs/new.md",
98 "rename from docs/old.md",
99 "rename to docs/new.md",
100 "--- a/docs/old.md",
101 "+++ b/docs/new.md",
102 "",
103 ].join("\n"),
104 );
105 });
106​
107 it("reports a binary file as changed rather than spelling it out", () => {
108 const patch = azureDevOpsFilePatch({
109 change: change({ path: "logo.png", oldPath: "logo.png" }),
110 texts: texts("PNG\u0000old", "PNG\u0000new"),
111 });
112​
113 expect(patch.truncated).toBe(true);
114 expect(patch.section).toContain("Binary files a/logo.png and b/logo.png differ");
115 });
116​
117 it("shows an overlong file as changed without its hunks", () => {
118 const patch = azureDevOpsFilePatch({
119 change: change({ path: "bundle.js", oldPath: "bundle.js" }),
120 texts: texts("a\n".repeat(400_000), "b\n".repeat(400_000)),
121 });
122​
123 expect(patch.truncated).toBe(true);
124 expect(patch.section).toBe(
125 ["diff --git a/bundle.js b/bundle.js", "--- a/bundle.js", "+++ b/bundle.js", ""].join("\n"),
126 );
127 });
128​
129 it("takes the host's word that a file is binary, whatever its bytes look like", () => {
130 const patch = azureDevOpsFilePatch({
131 change: change({ path: "logo.png", oldPath: "logo.png" }),
132 texts: texts("b2xk", "bmV3", true),
133 });
134​
135 expect(patch.truncated).toBe(true);
136 expect(patch.section).toContain("Binary files a/logo.png and b/logo.png differ");
137 });
138​
139 it("counts an overlong file in bytes rather than in characters", () => {
140 const patch = azureDevOpsFilePatch({
141 change: change({ path: "notes.md", oldPath: "notes.md" }),
142 texts: texts("\u4e00".repeat(200_000), "\u4e8c".repeat(200_000)),
143 });
144​
145 expect(patch.truncated).toBe(true);
146 expect(patch.section).toBe(
147 ["diff --git a/notes.md b/notes.md", "--- a/notes.md", "+++ b/notes.md", ""].join("\n"),
148 );
149 });
150​
151 const lineRange = (count: number, prefix: string) =>
152 Array.from({ length: count }, (_, line) => `${prefix} ${line}`).join("\n");
153​
154 it("lists a file too far apart to diff without its hunks", () => {
155 const lines = (prefix: string) =>
156 Array.from({ length: MAX_FILE_DIFF_EDITS }, (_, line) => `${prefix} ${line}`).join("\n");
157 const patch = azureDevOpsFilePatch({
158 change: change({ path: "generated.ts", oldPath: "generated.ts" }),
159 texts: texts(`${lines("old")}\n`, `${lines("new")}\n`),
160 });
161​
162 expect(patch.truncated).toBe(true);
163 expect(patch.abandoned).toBe(true);
164 expect(patch.section).toBe(
165 [
166 "diff --git a/generated.ts b/generated.ts",
167 "--- a/generated.ts",
168 "+++ b/generated.ts",
169 "",
170 ].join("\n"),
171 );
172 });
173​
174 it("writes out a wholly new file however many lines it has", () => {
175 const contents = `${Array.from({ length: 15_000 }, () => "x".repeat(29)).join("\n")}\n`;
176 const patch = azureDevOpsFilePatch({
177 change: change({ path: "DEMO.md", oldPath: "DEMO.md", changeKind: "new" }),
178 texts: texts("", contents),
179 });
180​
181 expect(byteLength(contents)).toBeLessThan(512 * 1024);
182 expect(patch.truncated).toBe(false);
183 expect(patch.abandoned).toBe(false);
184 expect(patch.edits).toBe(15_000);
185 expect(patch.section).toContain("@@ -0,0 +1,15000 @@");
186 expect(patch.section.match(/^\+/gmu)).toHaveLength(15_001);
187 });
188​
189 it("keeps a wholly new file too heavy to write out listed without its hunks", () => {
190 const contents = `${Array.from({ length: 200_000 }, () => "x").join("\n")}\n`;
191 const patch = azureDevOpsFilePatch({
192 change: change({ path: "bundle.min.js", oldPath: "bundle.min.js", changeKind: "new" }),
193 texts: texts("", contents),
194 });
195​
196 expect(byteLength(contents)).toBeLessThan(512 * 1024);
197 expect(patch.truncated).toBe(true);
198 expect(patch.abandoned).toBe(false);
199 expect(patch.edits).toBe(200_000);
200 expect(patch.section).toBe(
201 [
202 "diff --git a/bundle.min.js b/bundle.min.js",
203 "new file mode 100644",
204 "--- /dev/null",
205 "+++ b/bundle.min.js",
206 "",
207 ].join("\n"),
208 );
209 });
210​
211 it("writes out a wholly deleted file however many lines it had", () => {
212 const contents = `${lineRange(20_000, "line")}\n`;
213 const patch = azureDevOpsFilePatch({
214 change: change({ path: "OLD.md", oldPath: "OLD.md", changeKind: "deleted" }),
215 texts: texts(contents, ""),
216 });
217​
218 expect(patch.truncated).toBe(false);
219 expect(patch.abandoned).toBe(false);
220 expect(patch.edits).toBe(20_000);
221 expect(patch.section).toContain("@@ -1,20000 +0,0 @@");
222 expect(patch.section.match(/^-line /gmu)).toHaveLength(20_000);
223 expect(patch.section.match(/^-/gmu)).toHaveLength(20_001);
224 });
225​
226 it("gives an empty new file no hunk to read", () => {
227 const patch = azureDevOpsFilePatch({
228 change: change({ path: "EMPTY.md", oldPath: "EMPTY.md", changeKind: "new" }),
229 texts: texts("", ""),
230 });
231​
232 expect(patch.edits).toBe(0);
233 expect(patch.section).not.toContain("@@");
234 });
235​
236 it("marks a wholly new file whose last line has no newline after it", () => {
237 const patch = azureDevOpsFilePatch({
238 change: change({ path: "NOTES.md", oldPath: "NOTES.md", changeKind: "new" }),
239 texts: texts("", "one\ntwo"),
240 });
241​
242 expect(patch.section).toBe(
243 [
244 "diff --git a/NOTES.md b/NOTES.md",
245 "new file mode 100644",
246 "--- /dev/null",
247 "+++ b/NOTES.md",
248 "@@ -0,0 +1,2 @@",
249 "+one",
250 "+two",
251 "\\ No newline at end of file",
252 "",
253 ].join("\n"),
254 );
255 });
256​
257 it("keeps a file it did diff out of the giving up", () => {
258 const patch = azureDevOpsFilePatch({
259 change: change(),
260 texts: texts("one\ntwo\n", "one\ntwo again\n"),
261 });
262​
263 expect(patch.abandoned).toBe(false);
264 });
265​
266 it("counts what the diff worked out, which is what the file cost to diff", () => {
267 const patch = azureDevOpsFilePatch({
268 change: change(),
269 texts: texts("one\ntwo\nthree\nfour\n", "one\ntwo again\nthree\nfour\n"),
270 });
271​
272 expect(patch.edits).toBe(2);
273 });
274​
275 it("counts nothing for a file it never diffed", () => {
276 const patch = azureDevOpsFilePatch({
277 change: change({ path: "logo.png", oldPath: "logo.png" }),
278 texts: texts("PNG\u0000old", "PNG\u0000new"),
279 });
280​
281 expect(patch.edits).toBe(0);
282 });
283​
284 it("lists a file whose hunks outweigh its sides without them", () => {
285 const line = `${"a".repeat(400 * 1024)}\n`;
286 const patch = azureDevOpsFilePatch({
287 change: change({ path: "min.js", oldPath: "min.js" }),
288 texts: texts(line, `${"b".repeat(400 * 1024)}\n`),
289 });
290​
291 expect(patch.edits).toBeLessThan(MAX_FILE_DIFF_EDITS);
292 expect(patch.truncated).toBe(true);
293 expect(patch.section).toBe(
294 ["diff --git a/min.js b/min.js", "--- a/min.js", "+++ b/min.js", ""].join("\n"),
295 );
296 expect(byteLength(patch.section)).toBeLessThan(byteLength(line));
297 });
298​
299 it("marks a file that does not end in a newline, as git does", () => {
300 const patch = azureDevOpsFilePatch({
301 change: change(),
302 texts: texts("one\n", "two"),
303 });
304​
305 expect(patch.section).toContain("\\ No newline at end of file");
306 });
307});
308​
309describe("azureDevOpsUnreadableFilePatch", () => {
310 it("keeps a file the host would not hand over, listed without its hunks", () => {
311 const patch = azureDevOpsUnreadableFilePatch(change({ path: "huge.bin", oldPath: "huge.bin" }));
312​
313 expect(patch.truncated).toBe(true);
314 expect(patch.section).toBe(
315 ["diff --git a/huge.bin b/huge.bin", "--- a/huge.bin", "+++ b/huge.bin", ""].join("\n"),
316 );
317 });
318});
319​
320describe("a file Azure names something a patch header cannot carry plainly", () => {
321 it("writes each side as git's quoted form, the side letter inside the quotes", () => {
322 const patch = azureDevOpsFilePatch({
323 change: change({ path: "notes\treadme.md", oldPath: "notes\treadme.md" }),
324 texts: texts("one\n", "two\n"),
325 });
326​
327 expect(patch.section).toBe(
328 [
329 'diff --git "a/notes\\treadme.md" "b/notes\\treadme.md"',
330 '--- "a/notes\\treadme.md"',
331 '+++ "b/notes\\treadme.md"',
332 "@@ -1 +1 @@",
333 "-one",
334 "+two",
335 "",
336 ].join("\n"),
337 );
338 });
339​
340 it("keeps a name holding a newline on the one header line it belongs to", () => {
341 const patch = azureDevOpsFilePatch({
342 change: change({ path: "line\nfile.txt", oldPath: "line\nfile.txt" }),
343 texts: texts("one\n", "two\n"),
344 });
345​
346 expect(patch.section.split("\n").slice(0, 3)).toEqual([
347 'diff --git "a/line\\nfile.txt" "b/line\\nfile.txt"',
348 '--- "a/line\\nfile.txt"',
349 '+++ "b/line\\nfile.txt"',
350 ]);
351 });
352​
353 it("quotes the names a rename states, which carry no side letter", () => {
354 const patch = azureDevOpsFilePatch({
355 change: change({
356 path: "docs/new\tname.md",
357 oldPath: "docs/old\tname.md",
358 changeKind: "rename-pure",
359 }),
360 texts: texts("same\n", "same\n"),
361 });
362​
363 expect(patch.section).toBe(
364 [
365 'diff --git "a/docs/old\\tname.md" "b/docs/new\\tname.md"',
366 'rename from "docs/old\\tname.md"',
367 'rename to "docs/new\\tname.md"',
368 '--- "a/docs/old\\tname.md"',
369 '+++ "b/docs/new\\tname.md"',
370 "",
371 ].join("\n"),
372 );
373 });
374​
375 it("quotes the sides of the one line a binary file gets", () => {
376 const patch = azureDevOpsFilePatch({
377 change: change({ path: "logo\tmark.png", oldPath: "logo\tmark.png" }),
378 texts: texts("PNG\u0000old", "PNG\u0000new"),
379 });
380​
381 expect(patch.section).toContain(
382 'Binary files "a/logo\\tmark.png" and "b/logo\\tmark.png" differ',
383 );
384 });
385​
386 it("hands a reader of the header back the name Azure gave", () => {
387 const path = 'every\t\n"kind"\\of.md';
388 const patch = azureDevOpsFilePatch({
389 change: change({ path, oldPath: path }),
390 texts: texts("one\n", "two\n"),
391 });
392 const [header, oldLine, newLine] = patch.section.split("\n");
393​
394 expect(header).not.toContain("\t");
395 expect(unquoteGitPatchPath(oldLine?.slice(4) ?? "")).toBe(`a/${path}`);
396 expect(unquoteGitPatchPath(newLine?.slice(4) ?? "")).toBe(`b/${path}`);
397 });
398});
399​
400describe("a diff cursor", () => {
401 it("carries the push it was taken against back to the next slice", () => {
402 const cursor = formatAzureDevOpsDiffCursor({ iterationId: 3, fileIndex: 12 });
403​
404 expect(parseAzureDevOpsDiffCursor(cursor)).toEqual({ iterationId: 3, fileIndex: 12 });
405 });
406​
407 it("reads anything it did not write as no position at all", () => {
408 for (const raw of [undefined, null, "", "abc", "1", "0:4", "1:-2", "1:2:3"]) {
409 expect(parseAzureDevOpsDiffCursor(raw)).toBeNull();
410 }
411 });
412​
413 it("refuses a half it did not write rather than reading it as the first file", () => {
414 for (const raw of ["1:", ":4", "1: ", " 1:4", "1:0x2", "0x1:2", "1e2:0", "1:4.0"]) {
415 expect(parseAzureDevOpsDiffCursor(raw)).toBeNull();
416 }
417 });
418});