Use effect file system and node services in VCS tests

- Replace ad hoc temp-dir handling with scoped FileSystem APIs
- Provide NodeServices in VCS driver and status broadcaster tests
- Normalize CWDs through FileSystem realpath
This commit is contained in:
Julius Marminge committed 2026-05-02 01:57:03 -07:00
1 parent c3c4aca232
commit e07cc3e3ae
5 files changed
+183 -181

No files matched your search

+65 -63
View File
@@ -1,5 +1,3 @@
import { spawnSync } from "node:child_process";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, it } from "@effect/vitest";
import { Effect, FileSystem, Layer, Path, PlatformError } from "effect";
@@ -28,8 +26,6 @@ const processOutput = (stdout: string, exitCode = 0, stderr = ""): VcsProcessOut
stderrTruncated: false,
});
const hasJj = spawnSync("jj", ["--version"], { stdio: "ignore" }).status === 0;
const runJj = (cwd: string, args: ReadonlyArray<string>) =>
Effect.gen(function* () {
const process = yield* VcsProcess;
@@ -44,34 +40,28 @@ const runJj = (cwd: string, args: ReadonlyArray<string>) =>
type JjContractError = PlatformError.PlatformError | VcsError;
if (hasJj) {
runVcsDriverContractSuite<VcsProcess, JjContractError>({
name: "JJ",
kind: "jj",
layer: JjContractLayer,
fixture: {
createRepo: (cwd) => runJj(cwd, ["git", "init", "--colocate"]),
writeFile: (cwd, relativePath, contents) =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const absolutePath = path.join(cwd, relativePath);
yield* fileSystem.makeDirectory(path.dirname(absolutePath), { recursive: true });
yield* fileSystem.writeFileString(absolutePath, contents);
}),
ignorePath: (cwd, pattern) =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
yield* fileSystem.writeFileString(path.join(cwd, ".gitignore"), `${pattern}\n`);
}),
},
});
} else {
it("skips the JJ VCS driver contract when jj is not installed", () => {
assert.isFalse(hasJj);
});
}
runVcsDriverContractSuite<VcsProcess, JjContractError>({
name: "JJ",
kind: "jj",
layer: JjContractLayer,
fixture: {
createRepo: (cwd) => runJj(cwd, ["git", "init", "--colocate"]),
writeFile: (cwd, relativePath, contents) =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const absolutePath = path.join(cwd, relativePath);
yield* fileSystem.makeDirectory(path.dirname(absolutePath), { recursive: true });
yield* fileSystem.writeFileString(absolutePath, contents);
}),
ignorePath: (cwd, pattern) =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
yield* fileSystem.writeFileString(path.join(cwd, ".gitignore"), `${pattern}\n`);
}),
},
});
describe("JjVcsDriver", () => {
it.effect("detects repository identity with jj root", () => {
@@ -87,13 +77,16 @@ describe("JjVcsDriver", () => {
assert.deepStrictEqual(commandCalls(calls), [["jj", "--no-pager", "root"]]);
}).pipe(
Effect.provide(
Layer.mock(VcsProcess)({
run: (input) =>
Effect.sync(() => {
calls.push(input);
return processOutput("/repo\n");
}),
}),
Layer.mergeAll(
Layer.mock(VcsProcess)({
run: (input) =>
Effect.sync(() => {
calls.push(input);
return processOutput("/repo\n");
}),
}),
NodeServices.layer,
),
),
);
});
@@ -109,13 +102,16 @@ describe("JjVcsDriver", () => {
assert.deepStrictEqual(observedInput?.args, ["--no-pager", "file", "list"]);
}).pipe(
Effect.provide(
Layer.mock(VcsProcess)({
run: (input) =>
Effect.sync(() => {
observedInput = input;
return processOutput("README.md\nsrc/index.ts\n");
}),
}),
Layer.mergeAll(
Layer.mock(VcsProcess)({
run: (input) =>
Effect.sync(() => {
observedInput = input;
return processOutput("README.md\nsrc/index.ts\n");
}),
}),
NodeServices.layer,
),
),
);
});
@@ -144,16 +140,19 @@ describe("JjVcsDriver", () => {
assert.equal(calls[1]?.stdin, "keep.ts\0debug.log\0src/index.ts\0");
}).pipe(
Effect.provide(
Layer.mock(VcsProcess)({
run: (input) =>
Effect.sync(() => {
calls.push(input);
if (input.command === "git" && input.args.includes("check-ignore")) {
return processOutput("debug.log\0");
}
return processOutput("");
}),
}),
Layer.mergeAll(
Layer.mock(VcsProcess)({
run: (input) =>
Effect.sync(() => {
calls.push(input);
if (input.command === "git" && input.args.includes("check-ignore")) {
return processOutput("debug.log\0");
}
return processOutput("");
}),
}),
NodeServices.layer,
),
),
);
});
@@ -178,13 +177,16 @@ describe("JjVcsDriver", () => {
});
}).pipe(
Effect.provide(
Layer.mock(VcsProcess)({
run: (input) =>
Effect.sync(() => {
observedEnv = input.env;
return processOutput("");
}),
}),
Layer.mergeAll(
Layer.mock(VcsProcess)({
run: (input) =>
Effect.sync(() => {
observedEnv = input.env;
return processOutput("");
}),
}),
NodeServices.layer,
),
),
);
});
+24 -24
View File
@@ -1,8 +1,4 @@
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { Effect, Layer } from "effect";
import { Effect, FileSystem, Layer } from "effect";
import { VcsOutputDecodeError, VcsProcessExitError } from "@t3tools/contracts";
import { VcsDriver, type VcsDriverShape } from "./VcsDriver.ts";
@@ -117,7 +113,7 @@ const gitCommand = (
options?: Parameters<typeof processCommand>[5],
) => processCommand(process, "git", operation, cwd, args, options);
function tempDirError(operation: string, cwd: string, detail: string, cause: unknown) {
function fileSystemError(operation: string, cwd: string, detail: string, cause: unknown) {
return new VcsOutputDecodeError({
operation,
command: "git check-ignore",
@@ -127,17 +123,18 @@ function tempDirError(operation: string, cwd: string, detail: string, cause: unk
});
}
const makeTempGitDir = (operation: string, cwd: string) =>
Effect.tryPromise({
try: () => mkdtemp(join(tmpdir(), "t3-jj-check-ignore-")),
catch: (cause) => tempDirError(operation, cwd, "failed to create temp git dir", cause),
});
const removeTempGitDir = (gitDir: string) =>
Effect.promise(() => rm(gitDir, { force: true, recursive: true })).pipe(Effect.ignore);
const makeScopedTempGitDir = (fileSystem: FileSystem.FileSystem, operation: string, cwd: string) =>
fileSystem
.makeTempDirectoryScoped({ prefix: "t3-jj-check-ignore-" })
.pipe(
Effect.mapError((cause) =>
fileSystemError(operation, cwd, "failed to create temp git dir", cause),
),
);
export const makeVcsDriverShape = Effect.fn("makeJjVcsDriverShape")(function* () {
const process = yield* VcsProcess;
const fileSystem = yield* FileSystem.FileSystem;
const capabilities = {
kind: "jj" as const,
supportsWorktrees: true,
@@ -229,15 +226,19 @@ export const makeVcsDriverShape = Effect.fn("makeJjVcsDriverShape")(function* ()
const operation = "JjVcsDriver.filterIgnoredPaths";
const ignoredPaths = new Set<string>();
yield* Effect.acquireUseRelease(
makeTempGitDir(operation, cwd),
Effect.fn("JjVcsDriver.filterIgnoredPaths.withTempGitDir")(function* (gitDir) {
const initResult = yield* gitCommand(process, operation, cwd, [
"--git-dir",
gitDir,
"init",
"--bare",
]);
yield* Effect.scoped(
Effect.gen(function* () {
const gitDir = yield* makeScopedTempGitDir(fileSystem, operation, cwd);
const initResult = yield* gitCommand(
process,
operation,
cwd,
["--git-dir", gitDir, "init", "--bare"],
{
allowNonZeroExit: true,
},
);
if (initResult.exitCode !== 0) {
return yield* new VcsProcessExitError({
operation,
@@ -290,7 +291,6 @@ export const makeVcsDriverShape = Effect.fn("makeJjVcsDriverShape")(function* ()
}
}
}),
removeTempGitDir,
);
if (ignoredPaths.size === 0) {
@@ -1,3 +1,4 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, it } from "@effect/vitest";
import { Effect, Layer } from "effect";
import { ChildProcessSpawner } from "effect/unstable/process";
@@ -46,6 +47,7 @@ describe("VcsDriverRegistry", () => {
}),
}),
),
Layer.provide(NodeServices.layer),
);
return Effect.gen(function* () {
@@ -87,6 +89,7 @@ describe("VcsDriverRegistry", () => {
}),
}),
),
Layer.provide(NodeServices.layer),
);
return Effect.gen(function* () {
@@ -123,6 +126,7 @@ describe("VcsDriverRegistry", () => {
}),
}),
),
Layer.provide(NodeServices.layer),
);
return Effect.gen(function* () {
@@ -50,29 +50,30 @@ function makeTestLayer(state: {
localInvalidationCalls: number;
remoteInvalidationCalls: number;
}) {
return VcsStatusBroadcasterLayer.pipe(
Layer.provide(
Layer.mock(GitWorkflowService)({
localStatus: () =>
Effect.sync(() => {
state.localStatusCalls += 1;
return state.currentLocalStatus;
}),
remoteStatus: () =>
Effect.sync(() => {
state.remoteStatusCalls += 1;
return state.currentRemoteStatus;
}),
invalidateLocalStatus: () =>
Effect.sync(() => {
state.localInvalidationCalls += 1;
}),
invalidateRemoteStatus: () =>
Effect.sync(() => {
state.remoteInvalidationCalls += 1;
}),
const gitWorkflowLayer = Layer.mock(GitWorkflowService)({
localStatus: () =>
Effect.sync(() => {
state.localStatusCalls += 1;
return state.currentLocalStatus;
}),
),
remoteStatus: () =>
Effect.sync(() => {
state.remoteStatusCalls += 1;
return state.currentRemoteStatus;
}),
invalidateLocalStatus: () =>
Effect.sync(() => {
state.localInvalidationCalls += 1;
}),
invalidateRemoteStatus: () =>
Effect.sync(() => {
state.remoteInvalidationCalls += 1;
}),
});
return VcsStatusBroadcasterLayer.pipe(
Layer.provideMerge(NodeServices.layer),
Layer.provide(gitWorkflowLayer),
);
}
@@ -189,31 +190,31 @@ describe("VcsStatusBroadcaster", () => {
localInvalidationCalls: 0,
remoteInvalidationCalls: 0,
};
const gitWorkflowLayer = Layer.mock(GitWorkflowService)({
localStatus: (input) =>
Effect.sync(() => {
seenCwds.push(input.cwd);
state.localStatusCalls += 1;
return state.currentLocalStatus;
}),
remoteStatus: (input) =>
Effect.sync(() => {
seenCwds.push(input.cwd);
state.remoteStatusCalls += 1;
return state.currentRemoteStatus;
}),
invalidateLocalStatus: () =>
Effect.sync(() => {
state.localInvalidationCalls += 1;
}),
invalidateRemoteStatus: () =>
Effect.sync(() => {
state.remoteInvalidationCalls += 1;
}),
} satisfies Partial<GitWorkflowServiceShape>);
const testLayer = VcsStatusBroadcasterLayer.pipe(
Layer.provide(
Layer.mock(GitWorkflowService)({
localStatus: (input) =>
Effect.sync(() => {
seenCwds.push(input.cwd);
state.localStatusCalls += 1;
return state.currentLocalStatus;
}),
remoteStatus: (input) =>
Effect.sync(() => {
seenCwds.push(input.cwd);
state.remoteStatusCalls += 1;
return state.currentRemoteStatus;
}),
invalidateLocalStatus: () =>
Effect.sync(() => {
state.localInvalidationCalls += 1;
}),
invalidateRemoteStatus: () =>
Effect.sync(() => {
state.remoteInvalidationCalls += 1;
}),
} satisfies Partial<GitWorkflowServiceShape>),
),
Layer.provideMerge(NodeServices.layer),
Layer.provide(gitWorkflowLayer),
);
return Effect.gen(function* () {
@@ -236,7 +237,7 @@ describe("VcsStatusBroadcaster", () => {
assert.deepStrictEqual(seenCwds, [realPath, realPath]);
assert.equal(state.localStatusCalls, 1);
assert.equal(state.remoteStatusCalls, 1);
}).pipe(Effect.provide(Layer.mergeAll(testLayer, NodeServices.layer)));
}).pipe(Effect.provide(testLayer));
});
it.effect("streams a local snapshot first and remote updates later", () => {
@@ -290,40 +291,40 @@ describe("VcsStatusBroadcaster", () => {
};
let remoteInterruptedDeferred: Deferred.Deferred<void, never> | null = null;
let remoteStartedDeferred: Deferred.Deferred<void, never> | null = null;
const gitWorkflowLayer = Layer.mock(GitWorkflowService)({
localStatus: () =>
Effect.sync(() => {
state.localStatusCalls += 1;
return state.currentLocalStatus;
}),
remoteStatus: () =>
Effect.sync(() => {
state.remoteStatusCalls += 1;
}).pipe(
Effect.andThen(
remoteStartedDeferred
? Deferred.succeed(remoteStartedDeferred, undefined).pipe(Effect.ignore)
: Effect.void,
),
Effect.andThen(Effect.never as Effect.Effect<VcsStatusRemoteResult | null, never>),
Effect.onInterrupt(() =>
remoteInterruptedDeferred
? Deferred.succeed(remoteInterruptedDeferred, undefined).pipe(Effect.ignore)
: Effect.void,
),
),
invalidateLocalStatus: () =>
Effect.sync(() => {
state.localInvalidationCalls += 1;
}),
invalidateRemoteStatus: () =>
Effect.sync(() => {
state.remoteInvalidationCalls += 1;
}),
} satisfies Partial<GitWorkflowServiceShape>);
const testLayer = VcsStatusBroadcasterLayer.pipe(
Layer.provide(
Layer.mock(GitWorkflowService)({
localStatus: () =>
Effect.sync(() => {
state.localStatusCalls += 1;
return state.currentLocalStatus;
}),
remoteStatus: () =>
Effect.sync(() => {
state.remoteStatusCalls += 1;
}).pipe(
Effect.andThen(
remoteStartedDeferred
? Deferred.succeed(remoteStartedDeferred, undefined).pipe(Effect.ignore)
: Effect.void,
),
Effect.andThen(Effect.never as Effect.Effect<VcsStatusRemoteResult | null, never>),
Effect.onInterrupt(() =>
remoteInterruptedDeferred
? Deferred.succeed(remoteInterruptedDeferred, undefined).pipe(Effect.ignore)
: Effect.void,
),
),
invalidateLocalStatus: () =>
Effect.sync(() => {
state.localInvalidationCalls += 1;
}),
invalidateRemoteStatus: () =>
Effect.sync(() => {
state.remoteInvalidationCalls += 1;
}),
} satisfies Partial<GitWorkflowServiceShape>),
),
Layer.provideMerge(NodeServices.layer),
Layer.provide(gitWorkflowLayer),
);
return Effect.gen(function* () {
+9 -14
View File
@@ -1,9 +1,8 @@
import { realpathSync } from "node:fs";
import {
Duration,
Effect,
Exit,
FileSystem,
Fiber,
Layer,
PubSub,
@@ -69,18 +68,11 @@ function fingerprintStatusPart(status: unknown): string {
return JSON.stringify(status);
}
function normalizeCwd(cwd: string): string {
try {
return realpathSync.native(cwd);
} catch {
return cwd;
}
}
export const layer = Layer.effect(
VcsStatusBroadcaster,
Effect.gen(function* () {
const workflow = yield* GitWorkflowService;
const fileSystem = yield* FileSystem.FileSystem;
const changesPubSub = yield* Effect.acquireRelease(
PubSub.unbounded<VcsStatusChange>(),
(pubsub) => PubSub.shutdown(pubsub),
@@ -91,6 +83,9 @@ export const layer = Layer.effect(
const cacheRef = yield* Ref.make(new Map<string, CachedVcsStatus>());
const pollersRef = yield* SynchronizedRef.make(new Map<string, ActiveRemotePoller>());
const normalizeCwd = (cwd: string) =>
fileSystem.realPath(cwd).pipe(Effect.catch(() => Effect.succeed(cwd)));
const getCachedStatus = Effect.fn("VcsStatusBroadcaster.getCachedStatus")(function* (
cwd: string,
) {
@@ -198,7 +193,7 @@ export const layer = Layer.effect(
const getStatus: VcsStatusBroadcasterShape["getStatus"] = Effect.fn(
"VcsStatusBroadcaster.getStatus",
)(function* (input) {
const cwd = normalizeCwd(input.cwd);
const cwd = yield* normalizeCwd(input.cwd);
const [local, remote] = yield* Effect.all([
getOrLoadLocalStatus(cwd),
getOrLoadRemoteStatus(cwd),
@@ -209,7 +204,7 @@ export const layer = Layer.effect(
const refreshLocalStatus: VcsStatusBroadcasterShape["refreshLocalStatus"] = Effect.fn(
"VcsStatusBroadcaster.refreshLocalStatus",
)(function* (rawCwd) {
const cwd = normalizeCwd(rawCwd);
const cwd = yield* normalizeCwd(rawCwd);
yield* workflow.invalidateLocalStatus(cwd);
const local = yield* workflow.localStatus({ cwd });
return yield* updateCachedLocalStatus(cwd, local, { publish: true });
@@ -226,7 +221,7 @@ export const layer = Layer.effect(
const refreshStatus: VcsStatusBroadcasterShape["refreshStatus"] = Effect.fn(
"VcsStatusBroadcaster.refreshStatus",
)(function* (rawCwd) {
const cwd = normalizeCwd(rawCwd);
const cwd = yield* normalizeCwd(rawCwd);
const [local, remote] = yield* Effect.all([
refreshLocalStatus(cwd),
refreshRemoteStatus(cwd),
@@ -312,7 +307,7 @@ export const layer = Layer.effect(
const streamStatus: VcsStatusBroadcasterShape["streamStatus"] = (input) =>
Stream.unwrap(
Effect.gen(function* () {
const cwd = normalizeCwd(input.cwd);
const cwd = yield* normalizeCwd(input.cwd);
const subscription = yield* PubSub.subscribe(changesPubSub);
const initialLocal = yield* getOrLoadLocalStatus(cwd);
const initialRemote = (yield* getCachedStatus(cwd))?.remote?.value ?? null;