Skip to content

Commit 9b3ac07

Browse files
test(core): cover the placeholder read failure that does not say the file is gone
The guard added in the previous commit reads the placeholder back before removing it, and rethrows a failure that is not ENOENT so a read that proves nothing does not license removing the file. Nothing exercised that arm: the surviving ConditionalExpression mutant on DiffViewProvider.ts:666 is what measured it. This adds the row that drives the read to fail with EACCES and asserts the file is kept, nothing is written over it, and the failure is reported through the console.error call this cleanup site already makes.
1 parent feeaf07 commit 9b3ac07

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

‎src/integrations/editor/__tests__/DiffViewProvider.spec.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2307,6 +2307,64 @@ describe("DiffViewProvider", () => {
23072307
)
23082308
})
23092309

2310+
it("keeps the placeholder when the guard's read fails for a reason that does not say it is gone", async () => {
2311+
// The read that guards the unlink can fail for reasons that say nothing about the file being
2312+
// gone - a permission refusal, for example. Only ENOENT means there was nothing left to
2313+
// remove, so anything else is rethrown: the discard keeps the file and reports it through
2314+
// the console.error call this cleanup site already makes, and the method rejects with that
2315+
// error the way it rejects for an unlink it could not finish.
2316+
const relPath = "unreadable-placeholder.ts"
2317+
const fsPath = `${mockCwd}/${relPath}`
2318+
const mockEditor = {
2319+
document: {
2320+
uri: { fsPath, scheme: "file" },
2321+
getText: vi.fn().mockReturnValue(""),
2322+
isDirty: false,
2323+
save: vi.fn().mockResolvedValue(undefined),
2324+
lineCount: 0,
2325+
},
2326+
selection: { active: { line: 0, character: 0 }, anchor: { line: 0, character: 0 } },
2327+
edit: vi.fn().mockResolvedValue(true),
2328+
revealRange: vi.fn(),
2329+
}
2330+
// Structural double routed through unknown rather than any: only the members open() touches.
2331+
const editor = mockEditor as unknown as vscode.TextEditor
2332+
vi.mocked(vscode.window).visibleTextEditors = [editor]
2333+
vi.mocked(vscode.window.showTextDocument).mockResolvedValue(editor)
2334+
vi.mocked(vscode.workspace.onDidOpenTextDocument).mockImplementation((callback) => {
2335+
setTimeout(() => callback({ uri: { fsPath, scheme: "file" } } as vscode.TextDocument), 0)
2336+
return { dispose: vi.fn() }
2337+
})
2338+
vi.mocked(vscode.window.onDidChangeVisibleTextEditors).mockReturnValue({ dispose: vi.fn() })
2339+
vi.mocked(vscode.window.onDidChangeTextEditorVisibleRanges).mockReturnValue({ dispose: vi.fn() })
2340+
vi.mocked(vscode.languages.getDiagnostics).mockReturnValue([])
2341+
diffViewProvider.editType = "create"
2342+
vi.mocked(fs.readFile).mockResolvedValue("")
2343+
2344+
await diffViewProvider.open(relPath)
2345+
expect(fs.writeFile).toHaveBeenCalledWith(fsPath, "")
2346+
2347+
// The guard's read now fails for a reason that is not 'already gone'.
2348+
const denied: NodeJS.ErrnoException = new Error("EACCES: permission denied, read")
2349+
denied.code = "EACCES"
2350+
vi.mocked(fs.readFile).mockRejectedValue(denied)
2351+
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {})
2352+
2353+
await expect(diffViewProvider.discardUnapprovedStream()).rejects.toThrow(/permission denied, read/)
2354+
2355+
// A read that said nothing about the file being gone does not license removing it.
2356+
expect(fs.unlink).not.toHaveBeenCalledWith(fsPath)
2357+
expect(fs.writeFile).toHaveBeenCalledTimes(1)
2358+
expect(errorSpy).toHaveBeenCalledWith(
2359+
"Error removing abandoned write_to_file artifacts:",
2360+
expect.objectContaining({ code: "EACCES" }),
2361+
)
2362+
// The edit drops its claim on the path with the failure, so a later discard does not
2363+
// report the same kept file twice.
2364+
expect(diffViewProvider["placeholderPath"]).toBeUndefined()
2365+
expect(diffViewProvider["placeholderContent"]).toBeUndefined()
2366+
})
2367+
23102368
it("removes the placeholder that a new-file open() wrote, driven through the public methods", async () => {
23112369
// The ownership lifecycle lives at the public-method layer, so exercise it there:
23122370
// open() writes the empty placeholder and records it, and the discard of an abandoned

0 commit comments

Comments
 (0)