Skip to content

Commit 83d848d

Browse files
David Sheldrickds300
authored andcommitted
wip whitespace continues to be the bane of my existence
1 parent 1345136 commit 83d848d

File tree

4 files changed

+298
-199
lines changed

4 files changed

+298
-199
lines changed

src/__tests__/propertyBasedTests.test.ts

Lines changed: 85 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
1818
`
1919

2020
function makeFileContents() {
21-
return generate({
22-
length: Math.floor(Math.random() * 5),
23-
charset: fileCharSet,
24-
})
21+
return (
22+
generate({
23+
length: Math.floor(Math.random() * 5),
24+
charset: fileCharSet,
25+
}) || ""
26+
)
2527
}
2628

2729
function makeFileName(ext?: boolean) {
2830
const name = generate({
2931
length: Math.ceil(Math.random() * 12),
30-
charset:
31-
"abcdefghijklmnopqrstuvwxyzABCDEFGGHIJKLMNOPQRSTUVWXYZ-_0987654321",
32+
charset: "abcdefghijklmnopqrstuvwxyz-_0987654321",
3233
})
3334

3435
return ext ? name + "." + generate(Math.ceil(Math.random() * 4)) : name
@@ -234,6 +235,8 @@ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
234235

235236
const patchFileContents = patchResult.stdout.toString()
236237

238+
// console.log(patchFileContents)
239+
237240
try {
238241
it("works forwards", () => {
239242
setWorkingFiles({ ...testCase.cleanFiles })
@@ -263,7 +266,15 @@ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
263266
setWorkingFiles({ ...testCase.modifiedFiles })
264267
const result = patch(patchFileContents, { reverse: true })
265268
executeEffects(result)
266-
expect(getWorkingFiles()).toEqual(testCase.cleanFiles)
269+
try {
270+
expect(getWorkingFiles()).toEqual(testCase.cleanFiles)
271+
} catch (e) {
272+
console.error("TEST CASE FAILED", {
273+
testCase,
274+
workingFiles: getWorkingFiles(),
275+
})
276+
throw e
277+
}
267278
})
268279
} catch (e) {
269280
console.error("TEST CASE FAILED", JSON.stringify(testCase))
@@ -272,9 +283,73 @@ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
272283
})
273284
}
274285

275-
for (let i = 0; i < 1; i++) {
276-
executeTest(makeTestCase(), i)
277-
}
286+
// for (let i = 0; i < 100; i++) {
287+
// executeTest(makeTestCase(), i)
288+
// }
289+
290+
executeTest(
291+
{
292+
cleanFiles: {
293+
nugs: "a",
294+
},
295+
modifiedFiles: {
296+
nugs: "a\n\n",
297+
},
298+
},
299+
4,
300+
)
301+
302+
// executeTest(
303+
// {
304+
// cleanFiles: {
305+
// b: "\n",
306+
// },
307+
// modifiedFiles: {
308+
// b: "ba\n",
309+
// },
310+
// },
311+
// 3,
312+
// )
313+
314+
// executeTest({
315+
// cleanFiles: {
316+
// "banana": "WMo^",
317+
// },
318+
// modifiedFiles: {
319+
// "banana": "\n\n",
320+
// },
321+
// }, 4)
322+
323+
// executeTest(
324+
// {
325+
// cleanFiles: {
326+
// b: "a",
327+
// },
328+
// modifiedFiles: { b: "a", c: "a\n" },
329+
// },
330+
// 4,
331+
// )
332+
333+
// executeTest(
334+
// {
335+
// cleanFiles: {
336+
// "c-qZ0Qznn1.RWOZ": "$xs\rwim\t}pJ(;£BZxc\\bg9k|zvBufcaa",
337+
// "tK/NEDQ-hff.iaQK": ";4l",
338+
// "KbYXh8-Dk3J/vcjQ.mz": "+4:",
339+
// "r6LXXaS/DO3VbFBswE6.WmHQ": "rX]bnT%j+,\t\r~xc&`lLh^\\n*-J$z<4xu",
340+
// "Fa/lQgW3c/G8LsUj-YFoS.4hoY": "NS",
341+
// },
342+
// modifiedFiles: {
343+
// "c-qZ0Qznn1.RWOZ": "$xs\rwim\t}pJ(;£BZxc\\bg9k|zvBufcaa",
344+
// "tK/NEDQ-hff.iaQK": ";4l",
345+
// "KbYXh8-Dk3J/vcjQ.mz": "+4:",
346+
// "r6LXXaS/DO3VbFBswE6.WmHQ": "",
347+
// "Fa/lQgW3c/G8LsUj-YFoS.4hoY": "NS",
348+
// wW1UMkaGn: "F",
349+
// },
350+
// },
351+
// 3,
352+
// )
278353

279354
// executeTest(
280355
// {
@@ -317,60 +392,3 @@ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
317392
// 5,
318393
// )
319394
})
320-
/*
321-
oh fuck
322-
What's the problem ?
323-
The problem is like that line endings are not being applied in one situation
324-
- when the patch file is reversed by me
325-
- when the clean file has a line ending but the modified file does not
326-
327-
When reversed, the patch file is wrong because the same scenario manually
328-
reversed is a-ok here's the bad patch file:
329-
330-
```patch
331-
diff --git a/1dkfI.J b/1dkfI.J
332-
index cfc60af..2d950c4 100644
333-
--- a/1dkfI.J
334-
+++ b/1dkfI.J
335-
@@ -1,0 +1,0 @@
336-
-nout
337-
+lineend
338-
\ No newline at end of file
339-
```
340-
341-
here's the good patch file:
342-
343-
```patch
344-
diff --git a/1dkfI.J b/1dkfI.J
345-
index 2d950c4..cfc60af 100644
346-
--- a/1dkfI.J
347-
+++ b/1dkfI.J
348-
@@ -1 +1 @@
349-
-nout
350-
\ No newline at end of file
351-
+lineend
352-
```
353-
354-
here's the differences:
355-
356-
- the bad patch file has length 0s in the hunk header
357-
That's probably not an issue, since the default value for lengths is 0
358-
- The bad patch file has the \ no newline at end of file comment in the wrong place
359-
But that's probably not an issue since my code ignores it
360-
361-
```patch
362-
@@ -1,0 +1,0 @@
363-
-nout
364-
+lineend
365-
\ No newline at end of file
366-
```
367-
368-
here's the good patch file:
369-
370-
```patch
371-
@@ -1 +1 @@
372-
-nout
373-
\ No newline at end of file
374-
+lineend
375-
```
376-
*/

src/applyPatch.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,30 @@ import * as fs from "fs-extra"
22
import { ParsedPatchFile, FilePatch } from "./parsePatch"
33

44
export type Effect =
5-
| { type: "file deletion"; path: string; lines: string[] }
6-
| { type: "rename"; fromPath: string; toPath: string }
7-
| { type: "file creation"; path: string; lines: string[] }
8-
| { type: "replace"; path: string; lines: string[] }
5+
| {
6+
type: "file deletion"
7+
path: string
8+
lines: string[]
9+
mode: number
10+
noNewlineAtEndOfFile: boolean
11+
}
12+
| {
13+
type: "rename"
14+
fromPath: string
15+
toPath: string
16+
}
17+
| {
18+
type: "file creation"
19+
path: string
20+
lines: string[]
21+
mode: number
22+
noNewlineAtEndOfFile: boolean
23+
}
24+
| {
25+
type: "replace"
26+
path: string
27+
lines: string[]
28+
}
929

1030
export function executeEffects(effects: Effect[]) {
1131
effects.forEach(eff => {
@@ -17,7 +37,11 @@ export function executeEffects(effects: Effect[]) {
1737
fs.moveSync(eff.fromPath, eff.toPath)
1838
break
1939
case "file creation":
20-
fs.writeFileSync(eff.path, eff.lines.join("\n"))
40+
fs.writeFileSync(
41+
eff.path,
42+
eff.lines.join("\n") + (eff.noNewlineAtEndOfFile ? "" : "\n"),
43+
{ mode: eff.mode },
44+
)
2145
break
2246
case "replace":
2347
fs.writeFileSync(eff.path, eff.lines.join("\n"))
@@ -87,13 +111,11 @@ function applyPatch({ parts, path }: FilePatch): Effect {
87111
contextIndex += part.lines.length
88112
if (i === parts.length) {
89113
if (part.noNewlineAtEndOfFile) {
90-
while (!fileLines[fileLines.length - 1]) {
114+
if (!fileLines[fileLines.length - 1]) {
91115
fileLines.pop()
92116
}
93-
} else {
94-
if (fileLines[fileLines.length - 1]) {
95-
fileLines.push("")
96-
}
117+
} else if (fileLines[fileLines.length - 1] !== "") {
118+
fileLines.push("")
97119
}
98120
}
99121
break

0 commit comments

Comments
 (0)