// The FileSystem call order each AtomicFile entry point documents in its TSDoc, recorded through memfs spies (and a // wrapped opened file for writeAll and sync), so a test that places a race between two calls can rely on it and the // docs cannot drift from the code. import { assert, describe, it } from "@effect/vitest"; import type { MemoryFileSystemFaultMethod, MemoryFileSystemFaults, MemoryFileSystemSeed, MemoryFileSystemSpies, } from "@effected/memfs"; import { MemoryFileSystem } from "@effected/memfs"; import { Effect, PlatformError } from "effect"; import { AtomicFile } from "../src/index.js"; /** Every FileSystem member the entry points could call; derived members route through these. */ const MEMBERS: ReadonlyArray = [ "access", "chmod", "chown", "copy", "copyFile", "link", "lstat", "makeDirectory", "makeTempDirectory", "makeTempDirectoryScoped", "makeTempFile", "makeTempFileScoped", "open", "readDirectory", "readFile", "readLink", "realPath", "remove", "rename", "stat", "symlink", "truncate", "utimes", "writeFile", ]; /** The random temp name, so a recorded call reads the same on every run. */ const stable = (value: unknown) => String(value).replace(/\.effected-[0-9a-z]+\.tmp$/, "TEMP"); /** * A layer over `seed` that records each FileSystem call as `member path` (`rename from to`), and each `writeAll` and * `sync` on an opened file, in the order they execute. `faults` adds failures on top. */ const recording = (seed: MemoryFileSystemSeed, faults: MemoryFileSystemFaults = {}) => { const calls: Array = []; const spies = Object.fromEntries( MEMBERS.map((member) => [ member, (...args: ReadonlyArray) => void calls.push( member === "rename" ? `rename ${stable(args[0])} ${stable(args[1])}` : `${member} ${stable(args[0])}`, ), ]), ) as MemoryFileSystemSpies; const layer = MemoryFileSystem.layerWithPath(seed, { spies, faults: (base) => ({ open: (path, options) => Effect.map(base.open(path, options), (file) => MemoryFileSystem.wrapFile(file, { writeAll: () => { calls.push("writeAll"); return undefined; }, sync: () => { calls.push("sync"); return undefined; }, }), ), ...faults, }), }); return { calls, layer }; }; const TARGET = "/work/config.json"; const TEMP = "/work/TEMP"; const ROOT = "/root"; const NESTED = "/root/sub/config.json"; /** The confined write's check for NESTED: every directory below the root, the directory's realPath, the target. */ const check = ["lstat /root/sub", "realPath /root/sub", `lstat ${NESTED}`]; describe("AtomicFile.write call order", () => { it.effect('unconfined, "follow", an existing target', () => { const { calls, layer } = recording({ [TARGET]: "old" }); return Effect.gen(function* () { yield* AtomicFile.write(TARGET, "new"); assert.deepStrictEqual(calls, [ `realPath ${TARGET}`, `stat ${TARGET}`, `access ${TARGET}`, `open ${TEMP}`, "writeAll", "sync", `chmod ${TEMP}`, `rename ${TEMP} ${TARGET}`, ]); }).pipe(Effect.provide(layer)); }); it.effect('unconfined, "follow", an absent target: readLink, then stat, and no access or chmod', () => { const { calls, layer } = recording({ "/work": MemoryFileSystem.directory() }); return Effect.gen(function* () { yield* AtomicFile.write(TARGET, "new"); assert.deepStrictEqual(calls, [ `realPath ${TARGET}`, `readLink ${TARGET}`, `stat ${TARGET}`, `open ${TEMP}`, "writeAll", "sync", `rename ${TEMP} ${TARGET}`, ]); }).pipe(Effect.provide(layer)); }); it.effect('unconfined, "follow", a dangling link is written through with writeFile alone', () => { const { calls, layer } = recording({ [TARGET]: MemoryFileSystem.symlink("missing") }); return Effect.gen(function* () { yield* AtomicFile.write(TARGET, "new"); assert.deepStrictEqual(calls, [`realPath ${TARGET}`, `readLink ${TARGET}`, `writeFile ${TARGET}`]); }).pipe(Effect.provide(layer)); }); it.effect('unconfined, "replace", an existing target', () => { const { calls, layer } = recording({ [TARGET]: "old" }); return Effect.gen(function* () { yield* AtomicFile.write(TARGET, "new", { links: "replace" }); assert.deepStrictEqual(calls, [ `lstat ${TARGET}`, `access ${TARGET}`, `open ${TEMP}`, "writeAll", "sync", `chmod ${TEMP}`, `rename ${TEMP} ${TARGET}`, ]); }).pipe(Effect.provide(layer)); }); it.effect("confined, an existing target: the check before the open, the chmod and the rename", () => { const { calls, layer } = recording({ [NESTED]: "old" }); return Effect.gen(function* () { yield* AtomicFile.write(NESTED, "new", { confineTo: ROOT }); assert.deepStrictEqual(calls, [ `realPath ${ROOT}`, ...check, `lstat ${NESTED}`, `access ${NESTED}`, ...check, "open /root/sub/TEMP", "writeAll", "sync", ...check, "chmod /root/sub/TEMP", ...check, `rename /root/sub/TEMP ${NESTED}`, ]); }).pipe(Effect.provide(layer)); }); it.effect('confined, "replace": the check skips the lstat of the target', () => { const { calls, layer } = recording({ [NESTED]: "old" }); const short = check.slice(0, 2); return Effect.gen(function* () { yield* AtomicFile.write(NESTED, "new", { confineTo: ROOT, links: "replace" }); assert.deepStrictEqual(calls, [ `realPath ${ROOT}`, ...short, `lstat ${NESTED}`, `access ${NESTED}`, ...short, "open /root/sub/TEMP", "writeAll", "sync", ...short, "chmod /root/sub/TEMP", ...short, `rename /root/sub/TEMP ${NESTED}`, ]); }).pipe(Effect.provide(layer)); }); it.effect("a failure after the open ends with remove of the temp", () => { const { calls, layer } = recording( { [TARGET]: "old" }, { rename: (from) => Effect.fail( PlatformError.systemError({ _tag: "Unknown", module: "FileSystem", method: "rename", pathOrDescriptor: from, }), ), }, ); return Effect.gen(function* () { yield* Effect.flip(AtomicFile.write(TARGET, "new")); assert.deepStrictEqual(calls.slice(-2), [`rename ${TEMP} ${TARGET}`, `remove ${TEMP}`]); }).pipe(Effect.provide(layer)); }); }); describe("AtomicFile.checkConfined call order", () => { it.effect("a confined target: realPath of the root, then the check", () => { const { calls, layer } = recording({ [NESTED]: "old" }); return Effect.gen(function* () { yield* AtomicFile.checkConfined(NESTED, { confineTo: ROOT }); assert.deepStrictEqual(calls, [`realPath ${ROOT}`, ...check]); }).pipe(Effect.provide(layer)); }); it.effect( "a linked directory: the check stops at it, then realPath of it for target and of the target for resolved", () => { const { calls, layer } = recording({ "/root/inner/real.json": "inner", "/root/alias": MemoryFileSystem.symlink("/root/inner"), }); return Effect.gen(function* () { yield* AtomicFile.checkConfined("/root/alias/real.json", { confineTo: ROOT }); assert.deepStrictEqual(calls, [ `realPath ${ROOT}`, "lstat /root/alias", "realPath /root/alias", "realPath /root/alias/real.json", ]); }).pipe(Effect.provide(layer)); }, ); it.effect("a linked target: the check, then one realPath of the target for both target and resolved", () => { const { calls, layer } = recording({ "/outside/x": "x", [NESTED]: MemoryFileSystem.symlink("/outside/x") }); return Effect.gen(function* () { yield* AtomicFile.checkConfined(NESTED, { confineTo: ROOT }); assert.deepStrictEqual(calls, [`realPath ${ROOT}`, ...check, `realPath ${NESTED}`]); }).pipe(Effect.provide(layer)); }); it.effect("an outside target: the root's realPath, then one realPath of the target for resolved", () => { const { calls, layer } = recording({ "/outside/x": "x", [ROOT]: MemoryFileSystem.directory() }); return Effect.gen(function* () { yield* AtomicFile.checkConfined("/outside/x", { confineTo: ROOT }); assert.deepStrictEqual(calls, [`realPath ${ROOT}`, "realPath /outside/x"]); }).pipe(Effect.provide(layer)); }); }); describe("AtomicFile.checkReadable call order", () => { it.effect("unset links: exactly checkConfined's calls", () => { const { calls, layer } = recording({ [NESTED]: "old" }); return Effect.gen(function* () { yield* AtomicFile.checkReadable(NESTED, { confineTo: ROOT }); assert.deepStrictEqual(calls, [`realPath ${ROOT}`, ...check]); }).pipe(Effect.provide(layer)); }); for (const [label, target] of [ ["confined", "/root/alias.md"], ["refused", "/root/out.md"], ] as const) { it.effect(`links: "resolve", ${label}: realPath of the root and the target, nothing after`, () => { const { calls, layer } = recording({ "/root/a/one.md": "one", "/outside/x.md": "x", "/root/alias.md": MemoryFileSystem.symlink("a/one.md"), "/root/out.md": MemoryFileSystem.symlink("/outside/x.md"), }); return Effect.gen(function* () { yield* AtomicFile.checkReadable(target, { confineTo: ROOT, links: "resolve" }); assert.deepStrictEqual(calls, [`realPath ${ROOT}`, `realPath ${target}`]); }).pipe(Effect.provide(layer)); }); } }); describe("AtomicFile.makeParents call order", () => { it.effect( "an existing level is lstat'ed; a missing one is made, lstat'ed and realPath'ed; last the directory", () => { const { calls, layer } = recording({ "/root/a": MemoryFileSystem.directory() }); return Effect.gen(function* () { yield* AtomicFile.makeParents("/root/a/b/file.json", { confineTo: ROOT }); assert.deepStrictEqual(calls, [ `realPath ${ROOT}`, "lstat /root/a", "lstat /root/a/b", "makeDirectory /root/a/b", "lstat /root/a/b", "realPath /root/a/b", "realPath /root/a/b", ]); }).pipe(Effect.provide(layer)); }, ); }); describe("AtomicFile.readText call order", () => { it.effect("unconfined: readFile alone, never open", () => { const { calls, layer } = recording({ [TARGET]: "text" }); return Effect.gen(function* () { yield* AtomicFile.readText(TARGET); assert.deepStrictEqual(calls, [`readFile ${TARGET}`]); }).pipe(Effect.provide(layer)); }); it.effect("confined: the check, readFile of the real target, the check again", () => { const { calls, layer } = recording({ [NESTED]: "text" }); return Effect.gen(function* () { yield* AtomicFile.readText(NESTED, { confineTo: ROOT }); assert.deepStrictEqual(calls, [`realPath ${ROOT}`, ...check, `readFile ${NESTED}`, ...check]); }).pipe(Effect.provide(layer)); }); it.effect('links: "resolve": realPath of the root and the target, readFile of the real path, realPath again', () => { const { calls, layer } = recording({ "/root/a/one.md": "one", "/root/alias.md": MemoryFileSystem.symlink("a/one.md"), }); return Effect.gen(function* () { yield* AtomicFile.readText("/root/alias.md", { confineTo: ROOT, links: "resolve" }); assert.deepStrictEqual(calls, [ `realPath ${ROOT}`, "realPath /root/alias.md", "readFile /root/a/one.md", "realPath /root/alias.md", ]); }).pipe(Effect.provide(layer)); }); });