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

diffFileRevisions.test.ts

259 lines7.5 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 const revisions = parseDiffFileRevisions(
185 patchOf(
186 'diff --git "a/we\\tird-\u{1f680}.ts" "b/we\\tird-\u{1f680}.ts"',
187 "index aaaaaaa..bbbbbbb 100644",
188 "@@ -1 +1 @@",
189 ),
190 );
191​
192 assert.deepStrictEqual([...revisions], [["we\tird-\u{1f680}.ts", "bbbbbbb"]]);
193 });
194​
195 it("drops the tab git ends a name holding a space with", () => {
196 const revisions = parseDiffFileRevisions(
197 patchOf(
198 "diff --git a/with space.txt b/with space.txt",
199 "index 1111111..6178079 100644",
200 "--- a/with space.txt\t",
201 "+++ b/with space.txt\t",
202 "@@ -1 +1 @@",
203 "-a",
204 "+b",
205 ),
206 );
207​
208 assert.deepStrictEqual([...revisions], [["with space.txt", "6178079"]]);
209 });
210​
211 it("drops the timestamp other producers of the format write past that tab", () => {
212 const revisions = parseDiffFileRevisions(
213 patchOf(
214 "diff --git a/stamped.ts b/stamped.ts",
215 "index 4444444..5555555 100644",
216 "--- a/stamped.ts\t2024-01-01 00:00:00.000000000 +0000",
217 "+++ b/stamped.ts\t2024-01-02 00:00:00.000000000 +0000",
218 "@@ -1 +1 @@",
219 "-a",
220 "+b",
221 ),
222 );
223​
224 assert.deepStrictEqual([...revisions], [["stamped.ts", "5555555"]]);
225 });
226});
227​
228describe("parseDiffFilePaths", () => {
229 it("names both sides of each file, with no side for an added or deleted one", () => {
230 const paths = parseDiffFilePaths(
231 [
232 "diff --git a/src/a.ts b/src/a.ts",
233 "--- a/src/a.ts",
234 "+++ b/src/a.ts",
235 "@@ -1 +1 @@",
236 "--- a/not/a/header.ts",
237 "diff --git a/added.md b/added.md",
238 "new file mode 100644",
239 "--- /dev/null",
240 "+++ b/added.md",
241 "diff --git a/gone.md b/gone.md",
242 "deleted file mode 100644",
243 "--- a/gone.md",
244 "+++ /dev/null",
245 'diff --git "a/tab\\tname" b/moved.ts',
246 'rename from "tab\\tname"',
247 "rename to moved.ts",
248 "",
249 ].join("\n"),
250 );
251​
252 assert.deepStrictEqual(paths, [
253 { oldPath: "src/a.ts", newPath: "src/a.ts" },
254 { oldPath: null, newPath: "added.md" },
255 { oldPath: "gone.md", newPath: null },
256 { oldPath: "tab\tname", newPath: "moved.ts" },
257 ]);
258 });
259});