| ... | ... | @@ -1,7 +1,9 @@ |
| 1 | 1 | import { assert, it, describe } from "@effect/vitest"; |
| 2 | 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as FileSystem from "effect/FileSystem"; |
| 4 | 5 | import * as Layer from "effect/Layer"; |
| 6 | import * as Path from "effect/Path"; |
| 5 | 7 | import { ChildProcessSpawner } from "effect/process"; |
| 6 | 8 | |
| 7 | 9 | import * as VcsProcess from "./VcsProcess.ts"; |
| ... | ... | @@ -43,9 +45,10 @@ describe("VcsDriverRegistry", () => { |
| 43 | 45 | }).pipe(Effect.provide(layer)); |
| 44 | 46 | }); |
| 45 | 47 | |
| 46 | | it.effect("caches repository detection for repeated resolves in the same cwd and kind", () => { |
| 47 | | const calls: VcsProcess.VcsProcessInput[] = []; |
| 48 | | const layer = Layer.effect(VcsDriverRegistry.VcsDriverRegistry, VcsDriverRegistry.make).pipe( |
| 48 | // Answers detection like git: the nearest folder at or above the cwd, up to |
| 49 | // `rootDir`, that holds `.git` is the repository. |
| 50 | const makeDiskBackedLayer = (rootDir: string, calls: string[]) => |
| 51 | Layer.effect(VcsDriverRegistry.VcsDriverRegistry, VcsDriverRegistry.make).pipe( |
| 49 | 52 | Layer.provide(NodeServices.layer), |
| 50 | 53 | Layer.provide( |
| 51 | 54 | Layer.mock(VcsProjectConfig.VcsProjectConfig)({ |
| ... | ... | @@ -55,86 +58,139 @@ describe("VcsDriverRegistry", () => { |
| 55 | 58 | Layer.provide( |
| 56 | 59 | Layer.mock(VcsProcess.VcsProcess)({ |
| 57 | 60 | run: (input) => |
| 58 | | Effect.sync(() => { |
| 59 | | calls.push(input); |
| 60 | | const normalizedArgs = |
| 61 | | input.args[0] === "-C" && input.args.length >= 2 ? input.args.slice(2) : input.args; |
| 62 | | const command = normalizedArgs.join(" "); |
| 63 | | if (command === "rev-parse --is-inside-work-tree") { |
| 64 | | return processOutput("true\n"); |
| 61 | Effect.gen(function* () { |
| 62 | const fs = yield* FileSystem.FileSystem; |
| 63 | const path = yield* Path.Path; |
| 64 | const command = normalizeGitArgs(input.args).join(" "); |
| 65 | calls.push(command); |
| 66 | let repoDir: string | null = input.cwd; |
| 67 | while (repoDir !== null && !(yield* fs.exists(path.join(repoDir, ".git")))) { |
| 68 | repoDir = repoDir === rootDir ? null : path.dirname(repoDir); |
| 65 | 69 | } |
| 66 | | if (command === "rev-parse --show-toplevel") { |
| 67 | | return processOutput("/repo\n"); |
| 70 | if (repoDir === null) { |
| 71 | return { |
| 72 | ...processOutput(""), |
| 73 | exitCode: ChildProcessSpawner.ExitCode(128), |
| 74 | stderr: "fatal: not a git repository", |
| 75 | }; |
| 68 | 76 | } |
| 77 | if (command === "rev-parse --is-inside-work-tree") return processOutput("true\n"); |
| 78 | if (command === "rev-parse --show-toplevel") return processOutput(`${repoDir}\n`); |
| 69 | 79 | if (command === "rev-parse --git-common-dir") { |
| 70 | | return processOutput("/repo/.git\n"); |
| 80 | return processOutput(`${path.relative(input.cwd, path.join(repoDir, ".git"))}\n`); |
| 71 | 81 | } |
| 72 | 82 | return processOutput(""); |
| 73 | | }), |
| 83 | }).pipe(Effect.provide(NodeServices.layer), Effect.orDie), |
| 74 | 84 | }), |
| 75 | 85 | ), |
| 76 | 86 | ); |
| 77 | 87 | |
| 78 | | return Effect.gen(function* () { |
| 79 | | const registry = yield* VcsDriverRegistry.VcsDriverRegistry; |
| 80 | | const first = yield* registry.resolve({ cwd: "/repo", requestedKind: "git" }); |
| 81 | | const second = yield* registry.resolve({ cwd: "/repo", requestedKind: "git" }); |
| 82 | | |
| 83 | | assert.equal(first.repository.rootPath, "/repo"); |
| 84 | | assert.equal(second.repository.rootPath, "/repo"); |
| 85 | | assert.deepStrictEqual( |
| 86 | | calls.map((call) => normalizeGitArgs(call.args).join(" ")), |
| 87 | | [ |
| 88 | it.effect("caches repository detection for repeated resolves in the same cwd and kind", () => |
| 89 | Effect.gen(function* () { |
| 90 | const fs = yield* FileSystem.FileSystem; |
| 91 | const path = yield* Path.Path; |
| 92 | const repoDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-vcs-registry-" }); |
| 93 | yield* fs.makeDirectory(path.join(repoDir, ".git")); |
| 94 | const calls: string[] = []; |
| 95 | |
| 96 | yield* Effect.gen(function* () { |
| 97 | const registry = yield* VcsDriverRegistry.VcsDriverRegistry; |
| 98 | const first = yield* registry.resolve({ cwd: repoDir, requestedKind: "git" }); |
| 99 | const second = yield* registry.resolve({ cwd: repoDir, requestedKind: "git" }); |
| 100 | |
| 101 | assert.equal(first.repository.rootPath, repoDir); |
| 102 | assert.equal(second.repository.rootPath, repoDir); |
| 103 | assert.deepStrictEqual(calls, [ |
| 88 | 104 | "rev-parse --is-inside-work-tree", |
| 89 | 105 | "rev-parse --show-toplevel", |
| 90 | 106 | "rev-parse --git-common-dir", |
| 91 | | ], |
| 92 | | ); |
| 93 | | }).pipe(Effect.provide(layer)); |
| 94 | | }); |
| 107 | ]); |
| 108 | }).pipe(Effect.provide(makeDiskBackedLayer(repoDir, calls))); |
| 109 | }).pipe(Effect.provide(NodeServices.layer)), |
| 110 | ); |
| 95 | 111 | |
| 96 | | it.effect("detects a repository created after a negative lookup", () => { |
| 97 | | let insideWorkTreeChecks = 0; |
| 98 | | const layer = Layer.effect(VcsDriverRegistry.VcsDriverRegistry, VcsDriverRegistry.make).pipe( |
| 99 | | Layer.provide(NodeServices.layer), |
| 100 | | Layer.provide( |
| 101 | | Layer.mock(VcsProjectConfig.VcsProjectConfig)({ |
| 102 | | resolveKind: (input) => Effect.succeed(input.requestedKind ?? "auto"), |
| 103 | | }), |
| 104 | | ), |
| 105 | | Layer.provide( |
| 106 | | Layer.mock(VcsProcess.VcsProcess)({ |
| 107 | | run: (input) => |
| 108 | | Effect.sync(() => { |
| 109 | | const command = normalizeGitArgs(input.args).join(" "); |
| 110 | | if (command === "rev-parse --is-inside-work-tree") { |
| 111 | | insideWorkTreeChecks += 1; |
| 112 | | return insideWorkTreeChecks === 1 |
| 113 | | ? { |
| 114 | | ...processOutput(""), |
| 115 | | exitCode: ChildProcessSpawner.ExitCode(128), |
| 116 | | stderr: "fatal: not a git repository", |
| 117 | | } |
| 118 | | : processOutput("true\n"); |
| 119 | | } |
| 120 | | if (command === "rev-parse --show-toplevel") { |
| 121 | | return processOutput("/repo\n"); |
| 122 | | } |
| 123 | | if (command === "rev-parse --git-common-dir") { |
| 124 | | return processOutput("/repo/.git\n"); |
| 125 | | } |
| 126 | | return processOutput(""); |
| 127 | | }), |
| 128 | | }), |
| 129 | | ), |
| 130 | | ); |
| 112 | it.effect("detects again when a cached repository is removed from disk", () => |
| 113 | Effect.gen(function* () { |
| 114 | const fs = yield* FileSystem.FileSystem; |
| 115 | const path = yield* Path.Path; |
| 116 | const repoDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-vcs-registry-" }); |
| 117 | yield* fs.makeDirectory(path.join(repoDir, ".git")); |
| 118 | const calls: string[] = []; |
| 131 | 119 | |
| 132 | | return Effect.gen(function* () { |
| 133 | | const registry = yield* VcsDriverRegistry.VcsDriverRegistry; |
| 120 | yield* Effect.gen(function* () { |
| 121 | const registry = yield* VcsDriverRegistry.VcsDriverRegistry; |
| 122 | assert.equal((yield* registry.detect({ cwd: repoDir }))?.repository.rootPath, repoDir); |
| 134 | 123 | |
| 135 | | assert.equal(yield* registry.detect({ cwd: "/repo" }), null); |
| 136 | | assert.equal((yield* registry.detect({ cwd: "/repo" }))?.repository.rootPath, "/repo"); |
| 137 | | assert.equal(insideWorkTreeChecks, 2); |
| 138 | | }).pipe(Effect.provide(layer)); |
| 139 | | }); |
| 124 | yield* fs.remove(path.join(repoDir, ".git"), { recursive: true }); |
| 125 | |
| 126 | assert.equal(yield* registry.detect({ cwd: repoDir }), null); |
| 127 | assert.equal(calls.filter((call) => call === "rev-parse --is-inside-work-tree").length, 2); |
| 128 | }).pipe(Effect.provide(makeDiskBackedLayer(repoDir, calls))); |
| 129 | }).pipe(Effect.provide(NodeServices.layer)), |
| 130 | ); |
| 131 | |
| 132 | it.effect("detects again when git init creates a repository inside a cached one", () => |
| 133 | Effect.gen(function* () { |
| 134 | const fs = yield* FileSystem.FileSystem; |
| 135 | const path = yield* Path.Path; |
| 136 | const parentDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-vcs-registry-" }); |
| 137 | const projectDir = path.join(parentDir, "project"); |
| 138 | yield* fs.makeDirectory(path.join(parentDir, ".git")); |
| 139 | yield* fs.makeDirectory(projectDir); |
| 140 | const calls: string[] = []; |
| 141 | |
| 142 | yield* Effect.gen(function* () { |
| 143 | const registry = yield* VcsDriverRegistry.VcsDriverRegistry; |
| 144 | assert.equal((yield* registry.detect({ cwd: projectDir }))?.repository.rootPath, parentDir); |
| 145 | |
| 146 | yield* fs.makeDirectory(path.join(projectDir, ".git")); |
| 147 | |
| 148 | assert.equal( |
| 149 | (yield* registry.detect({ cwd: projectDir }))?.repository.rootPath, |
| 150 | projectDir, |
| 151 | ); |
| 152 | }).pipe(Effect.provide(makeDiskBackedLayer(parentDir, calls))); |
| 153 | }).pipe(Effect.provide(NodeServices.layer)), |
| 154 | ); |
| 155 | |
| 156 | it.effect("detects again when git init creates a repository between the cwd and its root", () => |
| 157 | Effect.gen(function* () { |
| 158 | const fs = yield* FileSystem.FileSystem; |
| 159 | const path = yield* Path.Path; |
| 160 | const parentDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-vcs-registry-" }); |
| 161 | const middleDir = path.join(parentDir, "a"); |
| 162 | const cwd = path.join(middleDir, "b"); |
| 163 | yield* fs.makeDirectory(path.join(parentDir, ".git")); |
| 164 | yield* fs.makeDirectory(cwd, { recursive: true }); |
| 165 | const calls: string[] = []; |
| 166 | |
| 167 | yield* Effect.gen(function* () { |
| 168 | const registry = yield* VcsDriverRegistry.VcsDriverRegistry; |
| 169 | assert.equal((yield* registry.detect({ cwd }))?.repository.rootPath, parentDir); |
| 170 | |
| 171 | yield* fs.makeDirectory(path.join(middleDir, ".git")); |
| 172 | |
| 173 | assert.equal((yield* registry.detect({ cwd }))?.repository.rootPath, middleDir); |
| 174 | }).pipe(Effect.provide(makeDiskBackedLayer(parentDir, calls))); |
| 175 | }).pipe(Effect.provide(NodeServices.layer)), |
| 176 | ); |
| 177 | |
| 178 | it.effect("detects a repository created after a negative lookup", () => |
| 179 | Effect.gen(function* () { |
| 180 | const fs = yield* FileSystem.FileSystem; |
| 181 | const path = yield* Path.Path; |
| 182 | const repoDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-vcs-registry-" }); |
| 183 | const calls: string[] = []; |
| 184 | |
| 185 | yield* Effect.gen(function* () { |
| 186 | const registry = yield* VcsDriverRegistry.VcsDriverRegistry; |
| 187 | assert.equal(yield* registry.detect({ cwd: repoDir }), null); |
| 188 | |
| 189 | yield* fs.makeDirectory(path.join(repoDir, ".git")); |
| 190 | |
| 191 | assert.equal((yield* registry.detect({ cwd: repoDir }))?.repository.rootPath, repoDir); |
| 192 | assert.equal(calls.filter((call) => call === "rev-parse --is-inside-work-tree").length, 2); |
| 193 | }).pipe(Effect.provide(makeDiskBackedLayer(repoDir, calls))); |
| 194 | }).pipe(Effect.provide(NodeServices.layer)), |
| 195 | ); |
| 140 | 196 | }); |