Skip to content

Commit c440742

Browse files
test(mutation): exercise the real rename step in the summary cleanup test
Review follow-up: the rename-failure test now uses an existing regular file as the target (so the initial summary read succeeds) and stubs fs.renameSync to fail once after the temp file is written. It asserts rename ran exactly once, the warning carries the EIO message, the temp file is removed, and the existing summary is untouched.
1 parent 71b59ac commit c440742

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

‎scripts/mutation-summary.test.mjs‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,23 +226,35 @@ test("writeMutationSummary cleans its temp file when the rename fails", (t) => {
226226
const root = makeRoot(t)
227227
const reportRoot = path.join(root, "reports", "mutation")
228228
writeReport(reportRoot, "pkg", { files: { "src/a.ts": { mutants: mutants("Killed") } } })
229-
// The target is a directory, so the atomic rename fails after the temp file exists.
229+
// A regular existing file is the target, so the initial read succeeds and the write really
230+
// reaches the rename step; the rename itself then fails.
230231
const outputFile = path.join(root, "summary.md")
231-
fs.mkdirSync(outputFile, { recursive: true })
232+
fs.writeFileSync(outputFile, "existing summary\n", "utf8")
232233
const logs = []
233234
const originalLog = console.log
235+
const originalRename = fs.renameSync
236+
let renameAttempts = 0
234237
console.log = (message) => {
235238
logs.push(String(message))
236239
}
240+
fs.renameSync = () => {
241+
renameAttempts += 1
242+
throw Object.assign(new Error("EIO: i/o error"), { code: "EIO" })
243+
}
237244
t.after(() => {
238245
console.log = originalLog
246+
fs.renameSync = originalRename
239247
})
240248

241249
assert.doesNotThrow(() => writeMutationSummary({ reportRoot, cwd: root, outputFile }))
242250

243-
assert.match(logs.join("\n"), /Could not write the per-file mutation summary/)
251+
// The write got past the read and the temp write, and the temp file was cleaned up.
252+
assert.equal(renameAttempts, 1)
253+
assert.match(logs.join("\n"), /Could not write the per-file mutation summary: EIO/)
244254
assert.deepEqual(
245255
fs.readdirSync(root).filter((entry) => entry.includes(".tmp")),
246256
[],
247257
)
258+
// The failed rename left the existing summary untouched.
259+
assert.equal(fs.readFileSync(outputFile, "utf8"), "existing summary\n")
248260
})

0 commit comments

Comments
 (0)