fix(core): enforce patch EOF anchors (#38039)

This commit is contained in:
Aiden Cline 2026-07-21 10:15:51 -05:00 committed by GitHub
commit 3ea8895299
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 58 additions and 7 deletions

View file

@ -113,6 +113,21 @@ describe("Patch", () => {
])
})
test("preserves the end-of-file marker", () => {
expect(
parse(
"*** Begin Patch\n*** Update File: file.txt\n@@\n+quux\n*** End of File\n\n*** End Patch",
),
).toEqual([
{
type: "update",
path: "file.txt",
movePath: undefined,
chunks: [{ oldLines: [], newLines: ["quux"], changeContext: undefined, endOfFile: true }],
},
])
})
test("derives fuzzy line updates while preserving BOM", () => {
const update = Patch.derive("update.txt", [{ oldLines: [" old "], newLines: ["new"] }], "\uFEFFold\n")
expect(update).toEqual({ content: "new\n", bom: true })
@ -174,6 +189,16 @@ describe("Patch", () => {
).toBe("marker\nmiddle\nmarker changed\nend\n")
})
test("does not fall back to a non-EOF match", () => {
expect(() =>
Patch.derive(
"update.txt",
[{ oldLines: ["marker", "end"], newLines: ["changed", "end"], endOfFile: true }],
"marker\nend\nmiddle\n",
),
).toThrow("Failed to find expected lines")
})
test("matches V1 lenient parsing of malformed hunk bodies", () => {
expect(parse("*** Begin Patch\n*** Add File: add.txt\nmissing plus\n*** End Patch")).toEqual([
{ type: "add", path: "add.txt", contents: "" },