Refactor VCS driver tests into shared contract harness
- Add a reusable VCS driver contract suite - Rework Git driver tests to use real repo fixtures
This commit is contained in:
1 parent
127e91bc86
commit
ccbad67c43
2 files changed
+212
-118
No files matched your search
@@ -1,125 +1,60 @@
|
||||
import { it } from "@effect/vitest";
|
||||
import * as NodeServices from "@effect/platform-node/NodeServices";
|
||||
import { Effect, Layer } from "effect";
|
||||
import { describe, expect } from "vitest";
|
||||
import { Effect, FileSystem, Layer, Path, PlatformError } from "effect";
|
||||
|
||||
import { GitCommandError } from "@t3tools/contracts";
|
||||
import { ServerConfig } from "../config.ts";
|
||||
import { VcsDriver } from "./VcsDriver.ts";
|
||||
import * as GitVcsDriver from "./GitVcsDriver.ts";
|
||||
import * as VcsProcess from "./VcsProcess.ts";
|
||||
import { runVcsDriverContractSuite } from "./testing/VcsDriverContractHarness.ts";
|
||||
|
||||
const splitNullSeparatedPaths = (input: string): string[] =>
|
||||
input
|
||||
.split("\0")
|
||||
.map((value) => value.trim())
|
||||
.filter((value) => value.length > 0);
|
||||
|
||||
const GitVcsDriverTestDependencies = ServerConfig.layerTest(process.cwd(), {
|
||||
prefix: "t3-git-vcs-driver-test-",
|
||||
}).pipe(Layer.provide(NodeServices.layer));
|
||||
|
||||
it.layer(Layer.empty)("GitVcsDriver.vcsLayer", (it) => {
|
||||
describe("workspace helpers", () => {
|
||||
it.effect("filterIgnoredPaths chunks large path lists and preserves kept paths", () =>
|
||||
Effect.gen(function* () {
|
||||
const cwd = "/virtual/repo";
|
||||
const relativePaths = Array.from({ length: 340 }, (_, index) => {
|
||||
const prefix = index % 3 === 0 ? "ignored" : "kept";
|
||||
return `${prefix}/segment-${String(index).padStart(4, "0")}/${"x".repeat(900)}.ts`;
|
||||
});
|
||||
const expectedPaths = relativePaths.filter(
|
||||
(relativePath) => !relativePath.startsWith("ignored/"),
|
||||
);
|
||||
|
||||
const seenChunks: string[][] = [];
|
||||
const layer = GitVcsDriver.vcsLayer.pipe(
|
||||
Layer.provideMerge(GitVcsDriverTestDependencies),
|
||||
Layer.provideMerge(NodeServices.layer),
|
||||
Layer.provide(
|
||||
Layer.succeed(VcsProcess.VcsProcess, {
|
||||
run: (input) => {
|
||||
expect(input.command).toBe("git");
|
||||
expect(input.args).toEqual([
|
||||
"-c",
|
||||
"core.fsmonitor=false",
|
||||
"-c",
|
||||
"core.untrackedCache=false",
|
||||
"check-ignore",
|
||||
"--no-index",
|
||||
"-z",
|
||||
"--stdin",
|
||||
]);
|
||||
|
||||
const chunkPaths = splitNullSeparatedPaths(input.stdin ?? "");
|
||||
seenChunks.push(chunkPaths);
|
||||
const ignoredPaths = chunkPaths.filter((relativePath) =>
|
||||
relativePath.startsWith("ignored/"),
|
||||
);
|
||||
|
||||
return Effect.succeed({
|
||||
exitCode: ignoredPaths.length > 0 ? 0 : 1,
|
||||
stdout: ignoredPaths.length > 0 ? `${ignoredPaths.join("\0")}\0` : "",
|
||||
stderr: "",
|
||||
stdoutTruncated: false,
|
||||
stderrTruncated: false,
|
||||
});
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const result = yield* Effect.gen(function* () {
|
||||
const vcs = yield* VcsDriver;
|
||||
return yield* vcs.filterIgnoredPaths(cwd, relativePaths);
|
||||
}).pipe(Effect.provide(layer));
|
||||
|
||||
expect(seenChunks.length).toBeGreaterThan(1);
|
||||
expect(seenChunks.flat()).toEqual(relativePaths);
|
||||
expect(result).toEqual(expectedPaths);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("listWorkspaceFiles disables fsmonitor and untracked cache helpers", () =>
|
||||
Effect.gen(function* () {
|
||||
const layer = GitVcsDriver.vcsLayer.pipe(
|
||||
Layer.provideMerge(GitVcsDriverTestDependencies),
|
||||
Layer.provideMerge(NodeServices.layer),
|
||||
Layer.provide(
|
||||
Layer.succeed(VcsProcess.VcsProcess, {
|
||||
run: (input) => {
|
||||
expect(input.command).toBe("git");
|
||||
expect(input.args).toEqual([
|
||||
"-c",
|
||||
"core.fsmonitor=false",
|
||||
"-c",
|
||||
"core.untrackedCache=false",
|
||||
"ls-files",
|
||||
"--cached",
|
||||
"--others",
|
||||
"--exclude-standard",
|
||||
"-z",
|
||||
]);
|
||||
return Effect.succeed({
|
||||
exitCode: 0,
|
||||
stdout: "src/index.ts\0README.md\0",
|
||||
stderr: "",
|
||||
stdoutTruncated: false,
|
||||
stderrTruncated: false,
|
||||
});
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const result = yield* Effect.gen(function* () {
|
||||
const vcs = yield* VcsDriver;
|
||||
return yield* vcs.listWorkspaceFiles("/virtual/repo");
|
||||
}).pipe(Effect.provide(layer));
|
||||
|
||||
expect(result.paths).toEqual(["src/index.ts", "README.md"]);
|
||||
expect(result.truncated).toBe(false);
|
||||
expect(result.freshness.source).toBe("live-local");
|
||||
}),
|
||||
);
|
||||
});
|
||||
const ServerConfigLayer = ServerConfig.layerTest(process.cwd(), {
|
||||
prefix: "t3-git-vcs-contract-",
|
||||
});
|
||||
const GitContractLayer = Layer.mergeAll(GitVcsDriver.vcsLayer, GitVcsDriver.layer).pipe(
|
||||
Layer.provide(ServerConfigLayer),
|
||||
Layer.provideMerge(VcsProcess.layer),
|
||||
Layer.provideMerge(NodeServices.layer),
|
||||
);
|
||||
|
||||
const runGit = (cwd: string, args: ReadonlyArray<string>) =>
|
||||
Effect.gen(function* () {
|
||||
const driver = yield* GitVcsDriver.GitVcsDriver;
|
||||
yield* driver.execute({
|
||||
operation: "GitVcsDriver.contract.git",
|
||||
cwd,
|
||||
args,
|
||||
timeoutMs: 10_000,
|
||||
});
|
||||
});
|
||||
|
||||
type GitContractError = GitCommandError | PlatformError.PlatformError;
|
||||
|
||||
runVcsDriverContractSuite<GitVcsDriver.GitVcsDriver, GitContractError>({
|
||||
name: "Git",
|
||||
kind: "git",
|
||||
layer: GitContractLayer,
|
||||
fixture: {
|
||||
createRepo: (cwd) =>
|
||||
Effect.gen(function* () {
|
||||
yield* runGit(cwd, ["init"]);
|
||||
yield* runGit(cwd, ["config", "user.email", "[email protected]"]);
|
||||
yield* runGit(cwd, ["config", "user.name", "Test"]);
|
||||
}),
|
||||
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);
|
||||
}),
|
||||
trackFile: (cwd, relativePath) => runGit(cwd, ["add", relativePath]),
|
||||
commit: (cwd, message) => runGit(cwd, ["commit", "-m", message]),
|
||||
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`);
|
||||
}),
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,159 @@
|
||||
import { realpathSync } from "node:fs";
|
||||
|
||||
import { it } from "@effect/vitest";
|
||||
import { Effect, FileSystem, Layer, Path, type PlatformError, type Scope } from "effect";
|
||||
import { describe, expect } from "vitest";
|
||||
|
||||
import type { VcsDriverKind } from "@t3tools/contracts";
|
||||
import { VcsDriver } from "../VcsDriver.ts";
|
||||
|
||||
export interface VcsDriverFixture<R, E> {
|
||||
readonly createRepo: (cwd: string) => Effect.Effect<void, E, R>;
|
||||
readonly writeFile: (
|
||||
cwd: string,
|
||||
relativePath: string,
|
||||
contents: string,
|
||||
) => Effect.Effect<void, E, R | FileSystem.FileSystem | Path.Path>;
|
||||
readonly trackFile?: (cwd: string, relativePath: string) => Effect.Effect<void, E, R>;
|
||||
readonly commit?: (cwd: string, message: string) => Effect.Effect<void, E, R>;
|
||||
readonly ignorePath: (
|
||||
cwd: string,
|
||||
pattern: string,
|
||||
) => Effect.Effect<void, E, R | FileSystem.FileSystem | Path.Path>;
|
||||
}
|
||||
|
||||
export interface VcsDriverContractSuiteInput<R, E> {
|
||||
readonly name: string;
|
||||
readonly kind: VcsDriverKind;
|
||||
readonly layer: Layer.Layer<VcsDriver | R | FileSystem.FileSystem | Path.Path, E, never>;
|
||||
readonly fixture: VcsDriverFixture<R, E>;
|
||||
}
|
||||
|
||||
export function runVcsDriverContractSuite<R, E>(input: VcsDriverContractSuiteInput<R, E>) {
|
||||
const makeTmpDir = (
|
||||
prefix = `t3-${input.kind}-vcs-contract-`,
|
||||
): Effect.Effect<string, PlatformError.PlatformError, FileSystem.FileSystem | Scope.Scope> =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
return yield* fileSystem.makeTempDirectoryScoped({ prefix });
|
||||
});
|
||||
|
||||
it.layer(input.layer)(`${input.name} VCS driver contract`, (it) => {
|
||||
describe("repository detection", () => {
|
||||
it.effect("returns null outside a repository", () =>
|
||||
Effect.gen(function* () {
|
||||
const cwd = yield* makeTmpDir();
|
||||
const driver = yield* VcsDriver;
|
||||
|
||||
expect(yield* driver.detectRepository(cwd)).toBeNull();
|
||||
expect(yield* driver.isInsideWorkTree(cwd)).toBe(false);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("detects repository identity inside a repository and nested directories", () =>
|
||||
Effect.gen(function* () {
|
||||
const cwd = yield* makeTmpDir();
|
||||
const driver = yield* VcsDriver;
|
||||
|
||||
yield* input.fixture.createRepo(cwd);
|
||||
yield* input.fixture.writeFile(cwd, "src/index.ts", "export const value = 1;\n");
|
||||
const expectedRootPath = realpathSync.native(cwd);
|
||||
|
||||
const identity = yield* driver.detectRepository(cwd);
|
||||
expect(identity).toMatchObject({
|
||||
kind: input.kind,
|
||||
rootPath: expectedRootPath,
|
||||
});
|
||||
expect(identity?.freshness).toMatchObject({
|
||||
source: "live-local",
|
||||
observedAt: expect.any(String),
|
||||
});
|
||||
expect(yield* driver.isInsideWorkTree(cwd)).toBe(true);
|
||||
|
||||
const path = yield* Path.Path;
|
||||
const nestedDir = path.join(cwd, "src");
|
||||
const nestedIdentity = yield* driver.detectRepository(nestedDir);
|
||||
expect(nestedIdentity?.rootPath).toBe(expectedRootPath);
|
||||
expect(yield* driver.isInsideWorkTree(nestedDir)).toBe(true);
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
describe("workspace files", () => {
|
||||
it.effect("lists tracked and untracked non-ignored files", () =>
|
||||
Effect.gen(function* () {
|
||||
const cwd = yield* makeTmpDir();
|
||||
const driver = yield* VcsDriver;
|
||||
|
||||
yield* input.fixture.createRepo(cwd);
|
||||
yield* input.fixture.writeFile(cwd, "tracked.ts", "export const tracked = true;\n");
|
||||
if (input.fixture.trackFile && input.fixture.commit) {
|
||||
yield* input.fixture.trackFile(cwd, "tracked.ts");
|
||||
yield* input.fixture.commit(cwd, "Track file");
|
||||
}
|
||||
yield* input.fixture.writeFile(cwd, "untracked.ts", "export const untracked = true;\n");
|
||||
|
||||
const result = yield* driver.listWorkspaceFiles(cwd);
|
||||
|
||||
expect(result.paths).toContain("tracked.ts");
|
||||
expect(result.paths).toContain("untracked.ts");
|
||||
expect(result.truncated).toBe(false);
|
||||
expect(result.freshness).toMatchObject({
|
||||
source: "live-local",
|
||||
observedAt: expect.any(String),
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("excludes ignored files from workspace listing", () =>
|
||||
Effect.gen(function* () {
|
||||
const cwd = yield* makeTmpDir();
|
||||
const driver = yield* VcsDriver;
|
||||
|
||||
yield* input.fixture.createRepo(cwd);
|
||||
yield* input.fixture.ignorePath(cwd, "*.log");
|
||||
yield* input.fixture.writeFile(cwd, "included.ts", "export const included = true;\n");
|
||||
yield* input.fixture.writeFile(cwd, "debug.log", "ignore me\n");
|
||||
yield* input.fixture.writeFile(cwd, "nested/error.log", "ignore me too\n");
|
||||
|
||||
const result = yield* driver.listWorkspaceFiles(cwd);
|
||||
|
||||
expect(result.paths).toContain("included.ts");
|
||||
expect(result.paths).not.toContain("debug.log");
|
||||
expect(result.paths).not.toContain("nested/error.log");
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
describe("ignored path filtering", () => {
|
||||
it.effect("filters ignored paths", () =>
|
||||
Effect.gen(function* () {
|
||||
const cwd = yield* makeTmpDir();
|
||||
const driver = yield* VcsDriver;
|
||||
|
||||
yield* input.fixture.createRepo(cwd);
|
||||
yield* input.fixture.ignorePath(cwd, "*.log");
|
||||
|
||||
const result = yield* driver.filterIgnoredPaths(cwd, [
|
||||
"keep.ts",
|
||||
"debug.log",
|
||||
"nested/error.log",
|
||||
]);
|
||||
|
||||
expect(result).toEqual(["keep.ts"]);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("returns empty input unchanged", () =>
|
||||
Effect.gen(function* () {
|
||||
const cwd = yield* makeTmpDir();
|
||||
const driver = yield* VcsDriver;
|
||||
|
||||
yield* input.fixture.createRepo(cwd);
|
||||
|
||||
expect(yield* driver.filterIgnoredPaths(cwd, [])).toEqual([]);
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user