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

azureDevOpsDiff.test.ts

241 lines7.5 KB

1import { unquoteGitPatchPath } from "@t3tools/shared/gitPatchPath";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 azureDevOpsFilePatch,
6 byteLength,
7 MAX_FILE_DIFF_EDITS,
8 parseAzureDevOpsDiffCursor,
9} from "./azureDevOpsDiff.ts";
10import type { AzureDevOpsChangeEntry } from "./azureDevOpsPullRequestJson.ts";
11​
12function change(overrides: Partial<AzureDevOpsChangeEntry> = {}): AzureDevOpsChangeEntry {
13 return {
14 path: "README.md",
15 oldPath: "README.md",
16 changeKind: "change",
17 objectId: "8f80",
18 originalObjectId: "0ca4",
19 ...overrides,
20 };
22​
23function texts(oldContents: string, newContents: string, binary = false) {
24 return { oldContents, newContents, binary };
26​
27describe("azureDevOpsFilePatch", () => {
28 it("writes a changed file as the unified patch every diff viewer already reads", () => {
29 const patch = azureDevOpsFilePatch({
30 change: change(),
31 texts: texts("one\ntwo\nthree\n", "one\ntwo again\nthree\n"),
32 });
33​
34 expect(patch.truncated).toBe(false);
35 expect(patch.section).toBe(
36 [
37 "diff --git a/README.md b/README.md",
38 "--- a/README.md",
39 "+++ b/README.md",
40 "@@ -1,3 +1,3 @@",
41 " one",
42 "-two",
43 "+two again",
44 " three",
45 "",
46 ].join("\n"),
47 );
48 });
49​
50 it("names the side a deleted file no longer has as /dev/null", () => {
51 const patch = azureDevOpsFilePatch({
52 change: change({ path: "OLD.md", oldPath: "OLD.md", changeKind: "deleted" }),
53 texts: texts("gone\n", ""),
54 });
55​
56 expect(patch.section).toContain("deleted file mode 100644");
57 expect(patch.section).toContain("--- a/OLD.md");
58 expect(patch.section).toContain("+++ /dev/null");
59 expect(patch.section).toContain("@@ -1 +0,0 @@");
60 expect(patch.section).toContain("-gone");
61 });
62​
63 it("keeps the carriage returns of a file with Windows line endings", () => {
64 const patch = azureDevOpsFilePatch({
65 change: change(),
66 texts: texts("one\r\ntwo\r\n", "one\r\ntwo again\r\n"),
67 });
68​
69 expect(patch.section).toContain("-two\r");
70 expect(patch.section).toContain("+two again\r");
71 });
72​
73 it("keeps a file that only moved, which has no hunks to give", () => {
74 const patch = azureDevOpsFilePatch({
75 change: change({ path: "docs/new.md", oldPath: "docs/old.md", changeKind: "rename-pure" }),
76 texts: texts("same\n", "same\n"),
77 });
78​
79 expect(patch.truncated).toBe(false);
80 expect(patch.section).toBe(
81 [
82 "diff --git a/docs/old.md b/docs/new.md",
83 "rename from docs/old.md",
84 "rename to docs/new.md",
85 "--- a/docs/old.md",
86 "+++ b/docs/new.md",
87 "",
88 ].join("\n"),
89 );
90 });
91​
92 it("reports a binary file as changed rather than spelling it out", () => {
93 const patch = azureDevOpsFilePatch({
94 change: change({ path: "logo.png", oldPath: "logo.png" }),
95 texts: texts("PNG\u0000old", "PNG\u0000new"),
96 });
97​
98 expect(patch.truncated).toBe(true);
99 expect(patch.section).toContain("Binary files a/logo.png and b/logo.png differ");
100 });
101​
102 it("takes the host's word that a file is binary, whatever its bytes look like", () => {
103 const patch = azureDevOpsFilePatch({
104 change: change({ path: "logo.png", oldPath: "logo.png" }),
105 texts: texts("b2xk", "bmV3", true),
106 });
107​
108 expect(patch.truncated).toBe(true);
109 expect(patch.section).toContain("Binary files a/logo.png and b/logo.png differ");
110 });
111​
112 it("counts an overlong file in bytes rather than in characters", () => {
113 const patch = azureDevOpsFilePatch({
114 change: change({ path: "notes.md", oldPath: "notes.md" }),
115 texts: texts("\u4e00".repeat(200_000), "\u4e8c".repeat(200_000)),
116 });
117​
118 expect(patch.truncated).toBe(true);
119 expect(patch.section).toBe(
120 ["diff --git a/notes.md b/notes.md", "--- a/notes.md", "+++ b/notes.md", ""].join("\n"),
121 );
122 });
123​
124 it("lists a file too far apart to diff without its hunks", () => {
125 const lines = (prefix: string) =>
126 Array.from({ length: MAX_FILE_DIFF_EDITS }, (_, line) => `${prefix} ${line}`).join("\n");
127 const patch = azureDevOpsFilePatch({
128 change: change({ path: "generated.ts", oldPath: "generated.ts" }),
129 texts: texts(`${lines("old")}\n`, `${lines("new")}\n`),
130 });
131​
132 expect(patch.truncated).toBe(true);
133 expect(patch.abandoned).toBe(true);
134 expect(patch.section).toBe(
135 [
136 "diff --git a/generated.ts b/generated.ts",
137 "--- a/generated.ts",
138 "+++ b/generated.ts",
139 "",
140 ].join("\n"),
141 );
142 });
143​
144 it("keeps a wholly new file too heavy to write out listed without its hunks", () => {
145 const contents = `${Array.from({ length: 200_000 }, () => "x").join("\n")}\n`;
146 const patch = azureDevOpsFilePatch({
147 change: change({ path: "bundle.min.js", oldPath: "bundle.min.js", changeKind: "new" }),
148 texts: texts("", contents),
149 });
150​
151 expect(byteLength(contents)).toBeLessThan(512 * 1024);
152 expect(patch.truncated).toBe(true);
153 expect(patch.abandoned).toBe(false);
154 expect(patch.edits).toBe(200_000);
155 expect(patch.section).toBe(
156 [
157 "diff --git a/bundle.min.js b/bundle.min.js",
158 "new file mode 100644",
159 "--- /dev/null",
160 "+++ b/bundle.min.js",
161 "",
162 ].join("\n"),
163 );
164 });
165​
166 it("gives an empty new file no hunk to read", () => {
167 const patch = azureDevOpsFilePatch({
168 change: change({ path: "EMPTY.md", oldPath: "EMPTY.md", changeKind: "new" }),
169 texts: texts("", ""),
170 });
171​
172 expect(patch.edits).toBe(0);
173 expect(patch.section).not.toContain("@@");
174 });
175​
176 it("marks a wholly new file whose last line has no newline after it", () => {
177 const patch = azureDevOpsFilePatch({
178 change: change({ path: "NOTES.md", oldPath: "NOTES.md", changeKind: "new" }),
179 texts: texts("", "one\ntwo"),
180 });
181​
182 expect(patch.section).toBe(
183 [
184 "diff --git a/NOTES.md b/NOTES.md",
185 "new file mode 100644",
186 "--- /dev/null",
187 "+++ b/NOTES.md",
188 "@@ -0,0 +1,2 @@",
189 "+one",
190 "+two",
191 "\\ No newline at end of file",
192 "",
193 ].join("\n"),
194 );
195 });
196});
197​
198describe("a file Azure names something a patch header cannot carry plainly", () => {
199 it("quotes the names a rename states, which carry no side letter", () => {
200 const patch = azureDevOpsFilePatch({
201 change: change({
202 path: "docs/new\tname.md",
203 oldPath: "docs/old\tname.md",
204 changeKind: "rename-pure",
205 }),
206 texts: texts("same\n", "same\n"),
207 });
208​
209 expect(patch.section).toBe(
210 [
211 'diff --git "a/docs/old\\tname.md" "b/docs/new\\tname.md"',
212 'rename from "docs/old\\tname.md"',
213 'rename to "docs/new\\tname.md"',
214 '--- "a/docs/old\\tname.md"',
215 '+++ "b/docs/new\\tname.md"',
216 "",
217 ].join("\n"),
218 );
219 });
220​
221 it("hands a reader of the header back the name Azure gave", () => {
222 const path = 'every\t\n"kind"\\of.md';
223 const patch = azureDevOpsFilePatch({
224 change: change({ path, oldPath: path }),
225 texts: texts("one\n", "two\n"),
226 });
227 const [header, oldLine, newLine] = patch.section.split("\n");
228​
229 expect(header).not.toContain("\t");
230 expect(unquoteGitPatchPath(oldLine?.slice(4) ?? "")).toBe(`a/${path}`);
231 expect(unquoteGitPatchPath(newLine?.slice(4) ?? "")).toBe(`b/${path}`);
232 });
233});
234​
235describe("a diff cursor", () => {
236 it("reads anything it did not write as no position at all", () => {
237 for (const raw of [undefined, null, "", "abc", "1", "0:4", "1:-2", "1:2:3"]) {
238 expect(parseAzureDevOpsDiffCursor(raw)).toBeNull();
239 }
240 });
241});