186 lines
7.0 KiB
TypeScript
186 lines
7.0 KiB
TypeScript
import * as NodeServices from "@effect/platform-node/NodeServices";
|
|
import { assert, it } from "@effect/vitest";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Exit from "effect/Exit";
|
|
import * as FileSystem from "effect/FileSystem";
|
|
import * as Layer from "effect/Layer";
|
|
import * as Path from "effect/Path";
|
|
import * as PlatformError from "effect/PlatformError";
|
|
|
|
import { writeFileStringAtomically } from "./atomicWrite.ts";
|
|
|
|
it.layer(NodeServices.layer)("writeFileStringAtomically", (it) => {
|
|
it.effect("keeps a symlinked file linked and rewrites its destination", () =>
|
|
Effect.gen(function* () {
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-atomic-write-" });
|
|
const destination = path.join(root, "dotfiles", "settings.json");
|
|
const link = path.join(root, "home", "settings.json");
|
|
yield* fs.makeDirectory(path.dirname(destination), { recursive: true });
|
|
yield* fs.makeDirectory(path.dirname(link), { recursive: true });
|
|
yield* fs.writeFileString(destination, "before");
|
|
yield* fs.symlink(destination, link);
|
|
|
|
yield* writeFileStringAtomically({ filePath: link, contents: "after" });
|
|
|
|
assert.strictEqual(yield* fs.readLink(link), destination);
|
|
assert.strictEqual(yield* fs.readFileString(destination), "after");
|
|
}),
|
|
);
|
|
|
|
it.effect("keeps a dangling symlink linked and creates its destination", () =>
|
|
Effect.gen(function* () {
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-atomic-write-" });
|
|
const destination = path.join(root, "dotfiles", "settings.json");
|
|
const link = path.join(root, "home", "settings.json");
|
|
yield* fs.makeDirectory(path.dirname(link), { recursive: true });
|
|
yield* fs.symlink(destination, link);
|
|
|
|
yield* writeFileStringAtomically({ filePath: link, contents: "fresh" });
|
|
|
|
assert.strictEqual(yield* fs.readLink(link), destination);
|
|
assert.strictEqual(yield* fs.readFileString(destination), "fresh");
|
|
}),
|
|
);
|
|
|
|
it.effect("fails on a symlink cycle without replacing either link", () =>
|
|
Effect.gen(function* () {
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-atomic-write-" });
|
|
const first = path.join(root, "first.json");
|
|
const second = path.join(root, "second.json");
|
|
yield* fs.symlink(second, first);
|
|
yield* fs.symlink(first, second);
|
|
|
|
const result = yield* Effect.exit(
|
|
writeFileStringAtomically({ filePath: first, contents: "after" }),
|
|
);
|
|
|
|
assert.isTrue(Exit.isFailure(result));
|
|
assert.strictEqual(yield* fs.readLink(first), second);
|
|
assert.strictEqual(yield* fs.readLink(second), first);
|
|
}),
|
|
);
|
|
|
|
it.effect("resolves a relative link through a symlinked parent directory", () =>
|
|
Effect.gen(function* () {
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-atomic-write-" });
|
|
const destination = path.join(root, "dotfiles", "config", "settings.json");
|
|
const linkedState = path.join(root, "dotfiles", "state");
|
|
const home = path.join(root, "home");
|
|
const link = path.join(home, "state", "settings.json");
|
|
yield* fs.makeDirectory(path.dirname(destination), { recursive: true });
|
|
yield* fs.makeDirectory(linkedState, { recursive: true });
|
|
yield* fs.makeDirectory(home, { recursive: true });
|
|
yield* fs.symlink(linkedState, path.join(home, "state"));
|
|
yield* fs.writeFileString(destination, "before");
|
|
yield* fs.symlink("../config/settings.json", link);
|
|
|
|
yield* writeFileStringAtomically({ filePath: link, contents: "after" });
|
|
|
|
assert.strictEqual(yield* fs.readLink(link), "../config/settings.json");
|
|
assert.strictEqual(yield* fs.readFileString(destination), "after");
|
|
}),
|
|
);
|
|
|
|
it.effect("creates a missing file and its directory", () =>
|
|
Effect.gen(function* () {
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-atomic-write-" });
|
|
const filePath = path.join(root, "nested", "settings.json");
|
|
|
|
yield* writeFileStringAtomically({ filePath, contents: "fresh" });
|
|
|
|
assert.strictEqual(yield* fs.readFileString(filePath), "fresh");
|
|
}),
|
|
);
|
|
});
|
|
|
|
it.effect("surfaces an unreadable link instead of writing over it", () =>
|
|
Effect.gen(function* () {
|
|
const readLinkFailure = PlatformError.systemError({
|
|
_tag: "Unknown",
|
|
module: "FileSystem",
|
|
method: "readLink",
|
|
pathOrDescriptor: "/home/settings.json",
|
|
});
|
|
|
|
const result = yield* Effect.exit(
|
|
writeFileStringAtomically({ filePath: "/home/settings.json", contents: "after" }),
|
|
);
|
|
|
|
assert.deepStrictEqual(result, Exit.fail(readLinkFailure));
|
|
}).pipe(
|
|
Effect.provide(
|
|
Layer.mergeAll(
|
|
Path.layer,
|
|
FileSystem.layerNoop({
|
|
readLink: () =>
|
|
Effect.fail(
|
|
PlatformError.systemError({
|
|
_tag: "Unknown",
|
|
module: "FileSystem",
|
|
method: "readLink",
|
|
pathOrDescriptor: "/home/settings.json",
|
|
}),
|
|
),
|
|
rename: () => Effect.die("an unreadable link must not be replaced"),
|
|
}),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
|
|
it.effect("succeeds when the write lands but its temp directory cannot be removed", () =>
|
|
Effect.gen(function* () {
|
|
const renamed: Array<string> = [];
|
|
const removed: Array<string> = [];
|
|
const fileSystem = FileSystem.layerNoop({
|
|
// The target does not exist yet, so it is written in place.
|
|
readLink: (path) =>
|
|
Effect.fail(
|
|
PlatformError.systemError({
|
|
_tag: "NotFound",
|
|
module: "FileSystem",
|
|
method: "readLink",
|
|
pathOrDescriptor: path,
|
|
}),
|
|
),
|
|
makeDirectory: () => Effect.void,
|
|
makeTempDirectory: () => Effect.succeed("/home/settings.json.abc123"),
|
|
writeFileString: () => Effect.void,
|
|
rename: (_from, to) => Effect.sync(() => void renamed.push(to)),
|
|
remove: (path) =>
|
|
Effect.sync(() => void removed.push(path)).pipe(
|
|
Effect.andThen(
|
|
Effect.fail(
|
|
PlatformError.systemError({
|
|
_tag: "PermissionDenied",
|
|
module: "FileSystem",
|
|
method: "remove",
|
|
pathOrDescriptor: path,
|
|
}),
|
|
),
|
|
),
|
|
),
|
|
});
|
|
|
|
const result = yield* Effect.exit(
|
|
writeFileStringAtomically({ filePath: "/home/settings.json", contents: "after" }).pipe(
|
|
Effect.provide(Layer.mergeAll(Path.layer, fileSystem)),
|
|
),
|
|
);
|
|
|
|
assert.deepStrictEqual(result, Exit.void);
|
|
assert.deepStrictEqual(renamed, ["/home/settings.json"]);
|
|
assert.deepStrictEqual(removed, ["/home/settings.json.abc123"]);
|
|
}),
|
|
);
|