diff --git a/.github/scripts/ci-changes.cjs b/.github/scripts/ci-changes.cjs index 7b7332903ed9f..f9c8174f854f2 100644 --- a/.github/scripts/ci-changes.cjs +++ b/.github/scripts/ci-changes.cjs @@ -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\//, ]; diff --git a/apps/desktop/vite.config.ts b/apps/desktop/vite.config.ts index 3e6fd9531106b..f4e04317e4924 100644 --- a/apps/desktop/vite.config.ts +++ b/apps/desktop/vite.config.ts @@ -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", ], }, }, @@ -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", + ], }, }, ], diff --git a/apps/mobile/vite.config.ts b/apps/mobile/vite.config.ts index 0ed29995b982a..05261f9cb1848 100644 --- a/apps/mobile/vite.config.ts +++ b/apps/mobile/vite.config.ts @@ -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", ], }, }, @@ -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", + ], }, }, ], diff --git a/apps/web/vite.config.ts b/apps/web/vite.config.ts index 1916d2dd54fc0..073fca7943d3e 100644 --- a/apps/web/vite.config.ts +++ b/apps/web/vite.config.ts @@ -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; diff --git a/packages/client-runtime/vite.config.ts b/packages/client-runtime/vite.config.ts index dbb29b7a4ef6c..18cae2d8d8c95 100644 --- a/packages/client-runtime/vite.config.ts +++ b/packages/client-runtime/vite.config.ts @@ -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", + ], }, }); diff --git a/packages/provider-testing/package.json b/packages/provider-testing/package.json index 1f0e3e02fd922..b2da89012680c 100644 --- a/packages/provider-testing/package.json +++ b/packages/provider-testing/package.json @@ -25,6 +25,7 @@ } }, "scripts": { + "test": "vp test run --config ../../vite.config.ts --dir .", "typecheck": "effect-tsgo tsrs --noEmit" }, "dependencies": { @@ -35,6 +36,8 @@ "effect": "catalog:" }, "devDependencies": { - "@types/node": "catalog:" + "@effect/vitest": "catalog:", + "@types/node": "catalog:", + "vite-plus": "catalog:" } } diff --git a/packages/provider-testing/src/replayWorkspace.test.ts b/packages/provider-testing/src/replayWorkspace.test.ts new file mode 100644 index 0000000000000..6c952542c4ea6 --- /dev/null +++ b/packages/provider-testing/src/replayWorkspace.test.ts @@ -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) => + 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 = { + 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", + ); + }), + ); +}); diff --git a/packages/provider-testing/src/replayWorkspace.ts b/packages/provider-testing/src/replayWorkspace.ts index 4216ebaedc53f..1b60b685c788a 100644 --- a/packages/provider-testing/src/replayWorkspace.ts +++ b/packages/provider-testing/src/replayWorkspace.ts @@ -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", @@ -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(" ")}`, diff --git a/packages/shared/package.json b/packages/shared/package.json index 781d1a4e125e1..68a0b589f8809 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -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" diff --git a/packages/shared/src/testing/gitEnv.test.ts b/packages/shared/src/testing/gitEnv.test.ts new file mode 100644 index 0000000000000..370a5147279f4 --- /dev/null +++ b/packages/shared/src/testing/gitEnv.test.ts @@ -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([]); + }); +}); diff --git a/packages/shared/src/testing/gitEnv.ts b/packages/shared/src/testing/gitEnv.ts new file mode 100644 index 0000000000000..9fa63ab9312f2 --- /dev/null +++ b/packages/shared/src/testing/gitEnv.ts @@ -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]; +} diff --git a/packages/shared/src/testing/gitRepositoryEnv.ts b/packages/shared/src/testing/gitRepositoryEnv.ts new file mode 100644 index 0000000000000..630f81d9ebb4b --- /dev/null +++ b/packages/shared/src/testing/gitRepositoryEnv.ts @@ -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 => + Object.fromEntries( + Object.keys(inherited) + .filter((name) => REPOSITORY_SELECTORS.has(platform === "win32" ? name.toUpperCase() : name)) + .map((name) => [name, undefined]), + ); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index af0be20508e16..3d44f4e0d55ec 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2060,9 +2060,15 @@ importers: specifier: 4.1.3 version: 4.1.3 devDependencies: + '@effect/vitest': + specifier: 4.1.2 + version: 4.1.2(patch_hash=299510b71c36537a832a67e9d160a5f112a5f326a29e95c41cb45fc8234e47c7)(@astrojs/compiler-rs@0.5.1)(@types/node@24.12.4)(@vitest/browser-playwright@5.0.5)(@vitest/ui@5.0.5)(@volar/typescript@2.4.28(typescript@6.0.3))(bufferutil@4.1.0)(effect@4.1.3)(esbuild@0.28.2)(jiti@2.7.0)(jsdom@30.1.0(@noble/hashes@1.8.0))(playwright@1.64.1)(terser@5.48.0)(typescript@6.0.3)(unrun@0.2.39)(utf-8-validate@6.0.6)(yaml@2.9.0) '@types/node': specifier: 24.12.4 version: 24.12.4 + vite-plus: + specifier: 1.4.0 + version: 1.4.0(@astrojs/compiler-rs@0.5.1)(@types/node@24.12.4)(@vitest/browser-playwright@5.0.5)(@vitest/ui@5.0.5)(@volar/typescript@2.4.28(typescript@6.0.3))(bufferutil@4.1.0)(esbuild@0.28.2)(jiti@2.7.0)(jsdom@30.1.0(@noble/hashes@1.8.0))(playwright@1.64.1)(terser@5.48.0)(typescript@6.0.3)(unrun@0.2.39)(utf-8-validate@6.0.6)(yaml@2.9.0) packages/secret-provider-core: dependencies: diff --git a/scripts/check-changed.test.ts b/scripts/check-changed.test.ts index 37bcc389fc229..5874eef8de515 100644 --- a/scripts/check-changed.test.ts +++ b/scripts/check-changed.test.ts @@ -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, ""); }); diff --git a/vite.config.ts b/vite.config.ts index fce0242597545..7533acd18b177 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -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 ? {