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

diffFileRevisions.test.ts

261 lines7.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("reads a name git had to quote, here one holding a tab", () => {
137 const revisions = parseDiffFileRevisions(
138 patchOf(
139 'diff --git "a/we\\tird.ts" "b/we\\tird.ts"',
140 "index 4444444..5555555 100644",
141 '--- "a/we\\tird.ts"',
142 '+++ "b/we\\tird.ts"',
143 "@@ -1 +1 @@",
144 "-a",
145 "+b",
146 ),
147 );
148​
149 assert.deepStrictEqual([...revisions], [["we\tird.ts", "5555555"]]);
150 });
151​
152 it("rejoins the octal bytes git writes for a name outside ASCII", () => {
153 const revisions = parseDiffFileRevisions(
154 patchOf(
155 'diff --git "a/caf\\303\\251/r\\303\\251sum\\303\\251.ts" "b/caf\\303\\251/r\\303\\251sum\\303\\251.ts"',
156 "index 6666666..7777777 100644",
157 "@@ -1 +1 @@",
158 ),
159 );
160​
161 assert.deepStrictEqual([...revisions], [["café/résumé.ts", "7777777"]]);
162 });
163​
164 it("splits a rename header where git quoted only the side that needed it", () => {
165 const revisions = parseDiffFileRevisions(
166 patchOf(
167 'diff --git a/old.ts "b/new\\tname.ts"',
168 "similarity index 90%",
169 "rename from old.ts",
170 'rename to "new\\tname.ts"',
171 "index 8888888..9999999 100644",
172 "--- a/old.ts",
173 '+++ "b/new\\tname.ts"',
174 "@@ -1 +1 @@",
175 "-a",
176 "+b",
177 ),
178 );
179​
180 assert.deepStrictEqual([...revisions], [["new\tname.ts", "9999999"]]);
181 });
182​
183 it("keeps a character from outside the basic plane that git left unescaped", () => {
184 // `core.quotePath` off leaves the name's own bytes in place, and git still quotes the header
185 // for the tab. Encoding what it left one unit at a time would split the pair into two halves.
186 const revisions = parseDiffFileRevisions(
187 patchOf(
188 'diff --git "a/we\\tird-\u{1f680}.ts" "b/we\\tird-\u{1f680}.ts"',
189 "index aaaaaaa..bbbbbbb 100644",
190 "@@ -1 +1 @@",
191 ),
192 );
193​
194 assert.deepStrictEqual([...revisions], [["we\tird-\u{1f680}.ts", "bbbbbbb"]]);
195 });
196​
197 it("drops the tab git ends a name holding a space with", () => {
198 const revisions = parseDiffFileRevisions(
199 patchOf(
200 "diff --git a/with space.txt b/with space.txt",
201 "index 1111111..6178079 100644",
202 "--- a/with space.txt\t",
203 "+++ b/with space.txt\t",
204 "@@ -1 +1 @@",
205 "-a",
206 "+b",
207 ),
208 );
209​
210 assert.deepStrictEqual([...revisions], [["with space.txt", "6178079"]]);
211 });
212​
213 it("drops the timestamp other producers of the format write past that tab", () => {
214 const revisions = parseDiffFileRevisions(
215 patchOf(
216 "diff --git a/stamped.ts b/stamped.ts",
217 "index 4444444..5555555 100644",
218 "--- a/stamped.ts\t2024-01-01 00:00:00.000000000 +0000",
219 "+++ b/stamped.ts\t2024-01-02 00:00:00.000000000 +0000",
220 "@@ -1 +1 @@",
221 "-a",
222 "+b",
223 ),
224 );
225​
226 assert.deepStrictEqual([...revisions], [["stamped.ts", "5555555"]]);
227 });
228});
229​
230describe("parseDiffFilePaths", () => {
231 it("names both sides of each file, with no side for an added or deleted one", () => {
232 const paths = parseDiffFilePaths(
233 [
234 "diff --git a/src/a.ts b/src/a.ts",
235 "--- a/src/a.ts",
236 "+++ b/src/a.ts",
237 "@@ -1 +1 @@",
238 "--- a/not/a/header.ts",
239 "diff --git a/added.md b/added.md",
240 "new file mode 100644",
241 "--- /dev/null",
242 "+++ b/added.md",
243 "diff --git a/gone.md b/gone.md",
244 "deleted file mode 100644",
245 "--- a/gone.md",
246 "+++ /dev/null",
247 'diff --git "a/tab\\tname" b/moved.ts',
248 'rename from "tab\\tname"',
249 "rename to moved.ts",
250 "",
251 ].join("\n"),
252 );
253​
254 assert.deepStrictEqual(paths, [
255 { oldPath: "src/a.ts", newPath: "src/a.ts" },
256 { oldPath: null, newPath: "added.md" },
257 { oldPath: "gone.md", newPath: null },
258 { oldPath: "tab\tname", newPath: "moved.ts" },
259 ]);
260 });
261});