fix full-suite isolation regressions
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { afterEach, expect, mock, test } from "bun:test"
|
||||
import { expect, test } from "bun:test"
|
||||
import type { PathLike } from "node:fs"
|
||||
import { mkdtemp, readdir, readFile, rm, writeFile } from "node:fs/promises"
|
||||
import { tmpdir } from "node:os"
|
||||
import { join } from "node:path"
|
||||
@@ -7,10 +8,6 @@ async function createTempDirectory(prefix: string): Promise<string> {
|
||||
return await mkdtemp(join(tmpdir(), prefix))
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
mock.restore()
|
||||
})
|
||||
|
||||
test("withLock serializes concurrent work", async () => {
|
||||
// given
|
||||
const { withLock } = await import("./locks")
|
||||
@@ -50,24 +47,20 @@ test("withLock serializes concurrent work", async () => {
|
||||
|
||||
test("atomicWrite leaves no partial file when rename fails", async () => {
|
||||
// given
|
||||
const fsPromises = await import("node:fs/promises")
|
||||
const rootDirectory = await createTempDirectory("locks-atomic-")
|
||||
const targetPath = join(rootDirectory, "target.txt")
|
||||
await writeFile(targetPath, "old content")
|
||||
const renameCalls: string[] = []
|
||||
|
||||
mock.module("node:fs/promises", () => ({
|
||||
...fsPromises,
|
||||
rename: async (from: string, to: string) => {
|
||||
renameCalls.push(`${from}->${to}`)
|
||||
throw new Error("rename failed")
|
||||
},
|
||||
}))
|
||||
|
||||
const { atomicWrite } = await import("./locks")
|
||||
|
||||
// when
|
||||
const result = atomicWrite(targetPath, "new content")
|
||||
const result = atomicWrite(targetPath, "new content", {
|
||||
rename: async (from: PathLike, to: PathLike) => {
|
||||
renameCalls.push(`${from}->${to}`)
|
||||
throw new Error("rename failed")
|
||||
},
|
||||
})
|
||||
|
||||
// then
|
||||
expect(result).rejects.toThrow("rename failed")
|
||||
@@ -76,7 +69,6 @@ test("atomicWrite leaves no partial file when rename fails", async () => {
|
||||
|
||||
const directoryEntries = await readdir(rootDirectory)
|
||||
expect(directoryEntries.some((entry) => entry.startsWith("target.txt.tmp."))).toBe(false)
|
||||
mock.restore()
|
||||
await rm(rootDirectory, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
|
||||
@@ -108,6 +108,7 @@ export async function reapStaleLock(lockPath: string): Promise<void> {
|
||||
export async function atomicWrite(
|
||||
filePath: string,
|
||||
content: string | Buffer,
|
||||
deps: { rename: typeof rename } = { rename },
|
||||
): Promise<void> {
|
||||
const tmpPath = `${filePath}.tmp.${randomUUID()}`
|
||||
|
||||
@@ -119,7 +120,7 @@ export async function atomicWrite(
|
||||
} finally {
|
||||
await fileHandle.close()
|
||||
}
|
||||
await rename(tmpPath, filePath)
|
||||
await deps.rename(tmpPath, filePath)
|
||||
} catch (error) {
|
||||
await rm(tmpPath, { force: true })
|
||||
throw error
|
||||
|
||||
Reference in New Issue
Block a user