Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/scripts/ci-changes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const INERT = [

// Shared test setup and vendored mobile dependencies affect every package.
const RUNS_EVERYTHING = [
/^packages\/shared\/src\/(HostProcess|testing\/(longTempDir|providerEnv))\.ts$/,
/^packages\/shared\/src\/(HostProcess|testing\/(longTempDir|providerEnv|gitEnv|gitRepositoryEnv))\.ts$/,
/^apps\/mobile\/deps\//,
];

Expand Down
6 changes: 5 additions & 1 deletion apps/desktop/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export default defineConfig({
setupFiles: [
"../../packages/shared/src/testing/longTempDir.ts",
"../../packages/shared/src/testing/providerEnv.ts",
"../../packages/shared/src/testing/gitEnv.ts",
],
},
},
Expand All @@ -187,7 +188,10 @@ export default defineConfig({
include: domTests,
// Guest preloads run in the page; these tests drive them against a DOM.
environment: "jsdom",
setupFiles: ["../../packages/shared/src/testing/providerEnv.ts"],
setupFiles: [
"../../packages/shared/src/testing/providerEnv.ts",
"../../packages/shared/src/testing/gitEnv.ts",
],
},
},
],
Expand Down
6 changes: 5 additions & 1 deletion apps/mobile/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export default defineConfig({
setupFiles: [
"../../packages/shared/src/testing/longTempDir.ts",
"../../packages/shared/src/testing/providerEnv.ts",
"../../packages/shared/src/testing/gitEnv.ts",
],
},
},
Expand Down Expand Up @@ -164,7 +165,10 @@ export default defineConfig({
include: reactTests,
// Native components mock React Native and render through react-dom in these tests.
environment: "jsdom",
setupFiles: ["../../packages/shared/src/testing/providerEnv.ts"],
setupFiles: [
"../../packages/shared/src/testing/providerEnv.ts",
"../../packages/shared/src/testing/gitEnv.ts",
],
},
},
],
Expand Down
1 change: 1 addition & 0 deletions apps/web/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ const unitTestProject = {
setupFiles: [
"../../packages/shared/src/testing/longTempDir.ts",
"../../packages/shared/src/testing/providerEnv.ts",
"../../packages/shared/src/testing/gitEnv.ts",
],
},
} satisfies TestProjectInlineConfiguration;
Expand Down
6 changes: 5 additions & 1 deletion packages/client-runtime/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export default defineConfig({
test: {
environment: "node",
include: ["src/**/*.test.ts"],
setupFiles: ["../shared/src/testing/longTempDir.ts", "../shared/src/testing/providerEnv.ts"],
setupFiles: [
"../shared/src/testing/longTempDir.ts",
"../shared/src/testing/providerEnv.ts",
"../shared/src/testing/gitEnv.ts",
],
},
});
5 changes: 4 additions & 1 deletion packages/provider-testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
}
},
"scripts": {
"test": "vp test run --config ../../vite.config.ts --dir .",
"typecheck": "effect-tsgo tsrs --noEmit"
},
"dependencies": {
Expand All @@ -35,6 +36,8 @@
"effect": "catalog:"
},
"devDependencies": {
"@types/node": "catalog:"
"@effect/vitest": "catalog:",
"@types/node": "catalog:",
"vite-plus": "catalog:"
}
}
83 changes: 83 additions & 0 deletions packages/provider-testing/src/replayWorkspace.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import * as ChildProcess from "effect/process/ChildProcess";
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
import { vi } from "vite-plus/test";

import { checkpointWorkspace } from "./replayWorkspace.ts";

const git = (cwd: string, args: ReadonlyArray<string>) =>
Effect.gen(function* () {
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
return yield* spawner.string(ChildProcess.make("git", args, { cwd }));
});

const repositoryState = (repo: string, worktree: string, worktreeIndex: string) =>
Effect.gen(function* () {
return {
config: yield* git(repo, ["config", "--local", "--list"]),
refs: yield* git(repo, ["for-each-ref"]),
worktrees: yield* git(repo, ["worktree", "list", "--porcelain"]),
worktreeHead: yield* git(worktree, ["rev-parse", "HEAD"]),
worktreeIndex: yield* git(repo, ["hash-object", "--no-filters", worktreeIndex]),
};
});

it.layer(NodeServices.layer)("checkpointWorkspace", (it) => {
// `git bisect run` and `git rebase --exec` export GIT_DIR to what they run, and hooks add
// GIT_INDEX_FILE. In a linked worktree both are absolute paths into the shared repository.
it.effect.each([
{ runner: "git bisect run", selectors: ["GIT_DIR"] },
{ runner: "a git hook", selectors: ["GIT_DIR", "GIT_INDEX_FILE"] },
])("keeps to its own repository when run under $runner", ({ selectors }) =>
Effect.gen(function* () {
yield* Effect.addFinalizer(() => Effect.sync(() => vi.unstubAllEnvs()));
// These commits use the contributor's global config, which must not require a signing key.
vi.stubEnv("GIT_CONFIG_COUNT", "1");
vi.stubEnv("GIT_CONFIG_KEY_0", "commit.gpgsign");
vi.stubEnv("GIT_CONFIG_VALUE_0", "false");
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-replay-workspace-decoy-" });
const repo = path.join(root, "repo");
const worktree = path.join(root, "worktree");
yield* fs.makeDirectory(repo);
yield* git(repo, ["init"]);
yield* git(repo, [
"-c",
"user.name=Decoy",
"-c",
"user.email=decoy@example.com",
"commit",
"--allow-empty",
"-m",
"decoy",
]);
yield* git(repo, ["worktree", "add", "--detach", worktree]);
const worktreeGitDir = (yield* git(worktree, ["rev-parse", "--absolute-git-dir"])).trim();
const worktreeIndex = path.join(worktreeGitDir, "index");
const before = yield* repositoryState(repo, worktree, worktreeIndex);

const inherited: Record<string, string> = {
GIT_DIR: worktreeGitDir,
GIT_INDEX_FILE: worktreeIndex,
};
for (const name of selectors) vi.stubEnv(name, inherited[name]);
const workspace = yield* checkpointWorkspace("decoy-guard", { "src/a.txt": "a\n" });
vi.unstubAllEnvs();

assert.deepStrictEqual(yield* repositoryState(repo, worktree, worktreeIndex), before);
assert.strictEqual(
yield* git(workspace, ["log", "--format=%s%x00%an", "-1"]),
"initial\u0000T3 Code Test\n",
);
assert.strictEqual(
(yield* git(workspace, ["ls-files"])).trim().split("\n").join(","),
"README.md,src/a.txt",
);
}),
);
});
7 changes: 6 additions & 1 deletion packages/provider-testing/src/replayWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import * as PlatformError from "effect/PlatformError";
import * as Schema from "effect/Schema";
import * as ChildProcess from "effect/process/ChildProcess";
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
import * as HostProcess from "@t3tools/shared/HostProcess";
import { unsetInheritedGitRepositoryEnv } from "@t3tools/shared/testing/gitRepositoryEnv";

class ReplayFixtureGitCommandError extends Schema.TaggedError<ReplayFixtureGitCommandError>()(
"ReplayFixtureGitCommandError",
Expand All @@ -29,7 +31,10 @@ function runGit(
> {
return Effect.gen(function* () {
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const exitCode = yield* spawner.exitCode(ChildProcess.make("git", args, { cwd }));
const env = unsetInheritedGitRepositoryEnv(process.env, yield* HostProcess.Platform);
const exitCode = yield* spawner.exitCode(
ChildProcess.make("git", args, { cwd, env, extendEnv: true }),
);
if (Number(exitCode) !== 0) {
return yield* new ReplayFixtureGitCommandError({
command: `git ${args.join(" ")}`,
Expand Down
4 changes: 4 additions & 0 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@
"types": "./src/HostProcess.ts",
"import": "./src/HostProcess.ts"
},
"./testing/gitRepositoryEnv": {
"types": "./src/testing/gitRepositoryEnv.ts",
"import": "./src/testing/gitRepositoryEnv.ts"
},
"./testing/longTempDir": {
"types": "./src/testing/longTempDir.ts",
"import": "./src/testing/longTempDir.ts"
Expand Down
27 changes: 27 additions & 0 deletions packages/shared/src/testing/gitEnv.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, expect, it, onTestFinished, vi } from "vite-plus/test";

import { unsetInheritedGitRepositoryEnv } from "./gitRepositoryEnv.ts";

describe("git test setup", () => {
it("starts tests without the repository selectors git exported to the run", async () => {
onTestFinished(() => {
vi.unstubAllEnvs();
});
vi.stubEnv("GIT_DIR", "/repo/.git/worktrees/wt");
vi.stubEnv("GIT_INDEX_FILE", "/repo/.git/worktrees/wt/index");
vi.stubEnv("GIT_EDITOR", "true");

vi.resetModules();
await import("./gitEnv.ts");

expect(process.env).not.toHaveProperty("GIT_DIR");
expect(process.env).not.toHaveProperty("GIT_INDEX_FILE");
expect(process.env.GIT_EDITOR).toBe("true");
});

it("unsets a selector under the spelling Windows passed it in", () => {
const inherited = { Git_Dir: "C:\\repo\\.git", GIT_EDITOR: "true", PATH: "C:\\bin" };
expect(Object.keys(unsetInheritedGitRepositoryEnv(inherited, "win32"))).toEqual(["Git_Dir"]);
expect(Object.keys(unsetInheritedGitRepositoryEnv(inherited, "linux"))).toEqual([]);
});
});
10 changes: 10 additions & 0 deletions packages/shared/src/testing/gitEnv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as HostProcess from "../HostProcess.ts";
import { unsetInheritedGitRepositoryEnv } from "./gitRepositoryEnv.ts";

// A test run started from `git bisect run`, `git rebase --exec` or a hook inherits GIT_DIR. Every git
// command a test or the code under test starts would then write into that repository.
for (const name of Object.keys(
unsetInheritedGitRepositoryEnv(process.env, HostProcess.Platform.defaultValue()),
)) {
delete process.env[name];
}
24 changes: 24 additions & 0 deletions packages/shared/src/testing/gitRepositoryEnv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Mirrors the server's set in apps/server/src/vcs/VcsProcess.ts, which shared cannot import.
const REPOSITORY_SELECTORS = new Set([
"GIT_DIR",
"GIT_WORK_TREE",
"GIT_COMMON_DIR",
"GIT_INDEX_FILE",
"GIT_OBJECT_DIRECTORY",
"GIT_ALTERNATE_OBJECT_DIRECTORIES",
]);

/**
* `git bisect run`, `git rebase --exec` and hooks export GIT_DIR (hooks also GIT_INDEX_FILE) to what
* they run, so a fixture's git commands would work on that repository instead of the one in their
* cwd. Spread the result into a child's env to unset them under the spelling they were inherited as.
*/
export const unsetInheritedGitRepositoryEnv = (
inherited: NodeJS.ProcessEnv,
platform: NodeJS.Platform,
): Record<string, undefined> =>
Object.fromEntries(
Object.keys(inherited)
.filter((name) => REPOSITORY_SELECTORS.has(platform === "win32" ? name.toUpperCase() : name))
.map((name) => [name, undefined]),
);
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions scripts/check-changed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ describe("check:changed", () => {
it.each([
"packages/shared/src/testing/longTempDir.ts",
"packages/shared/src/testing/providerEnv.ts",
"packages/shared/src/testing/gitEnv.ts",
"packages/shared/src/testing/gitRepositoryEnv.ts",
])("tests every package when the shared test setup file %s changes", (file) => {
assert.strictEqual(decide([file], readWorkspace(), false).outputs.test_packages, "");
});
Expand Down
1 change: 1 addition & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export default defineConfig({
NodeURL.fileURLToPath(
new URL("./packages/shared/src/testing/providerEnv.ts", import.meta.url),
),
NodeURL.fileURLToPath(new URL("./packages/shared/src/testing/gitEnv.ts", import.meta.url)),
],
...(isRepoRootTestRun
? {
Expand Down
Loading