Repository navigation
Expand file tree
/
Copy pathRepositoryIdentityResolver.test.ts
More file actions
326 lines (278 loc) · 14.3 KB
/
Copy pathRepositoryIdentityResolver.test.ts
File metadata and controls
326 lines (278 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// @effect-diagnostics nodeBuiltinImport:off - realpathSync.native resolves Windows 8.3 short names, which the Effect realPath does not.
import * as NodeFS from "node:fs";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { expect, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import * as ProcessRunner from "../processRunner.ts";
import * as RepositoryIdentityResolver from "./RepositoryIdentityResolver.ts";
const normalizePathSeparators = (value: string) => value.replaceAll("\\", "/");
const normalizeResolvedPath = (value: string) => normalizePathSeparators(value);
const git = (cwd: string, args: ReadonlyArray<string>) =>
Effect.gen(function* () {
const processRunner = yield* ProcessRunner.ProcessRunner;
return yield* processRunner.run({
command: "git",
args: ["-C", cwd, ...args],
});
}).pipe(Effect.provide(ProcessRunner.layer));
it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
it.effect("normalizes equivalent GitHub remotes into a stable repository identity", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const cwd = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-repository-identity-test-",
});
yield* git(cwd, ["init"]);
yield* git(cwd, ["remote", "add", "origin", "git@github.com:T3Tools/t3code.git"]);
const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
const identity = yield* resolver.resolve(cwd);
// Native realpath matches git's long paths when Windows temp dirs use 8.3 names.
const resolvedIdentityRoot =
identity?.rootPath === undefined ? "" : NodeFS.realpathSync.native(identity.rootPath);
const resolvedCwd = NodeFS.realpathSync.native(cwd);
expect(identity).not.toBeNull();
expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
expect(normalizeResolvedPath(resolvedIdentityRoot)).toBe(normalizeResolvedPath(resolvedCwd));
expect(identity?.displayName).toBe("T3Tools/t3code");
expect(identity?.provider).toBe("github");
expect(identity?.owner).toBe("T3Tools");
expect(identity?.name).toBe("t3code");
expect(yield* resolver.resolve(path.join(cwd, ".git"))).toBeNull();
}).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
);
it.effect("identifies the host of an SSH remote that leaves its user to ssh config", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const cwd = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-repository-identity-userless-remote-test-",
});
yield* git(cwd, ["init"]);
yield* git(cwd, ["remote", "add", "origin", "github.com:T3Tools/t3code.git"]);
const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
const identity = yield* resolver.resolve(cwd);
expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
expect(identity?.provider).toBe("github");
expect(identity?.displayName).toBe("T3Tools/t3code");
}).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
);
it.effect("resolves the fetch remote of a partial clone", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const cwd = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-repository-identity-partial-clone-test-",
});
yield* git(cwd, ["init"]);
yield* git(cwd, ["remote", "add", "origin", "git@github.com:T3Tools/t3code.git"]);
yield* git(cwd, ["remote", "set-url", "--push", "origin", "git@github.com:T3Tools/push.git"]);
yield* git(cwd, ["config", "remote.origin.promisor", "true"]);
yield* git(cwd, ["config", "remote.origin.partialclonefilter", "blob:none"]);
const remotes = yield* git(cwd, ["remote", "-v"]);
expect(remotes.stdout).toContain("(fetch) [blob:none]");
const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
const identity = yield* resolver.resolve(cwd);
expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
expect(identity?.locator.remoteUrl).toBe("git@github.com:T3Tools/t3code.git");
expect(identity?.locator.remoteName).toBe("origin");
}).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
);
it.effect("returns the git top-level root path when resolving from a nested workspace", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const repoRoot = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-repository-identity-nested-root-test-",
});
const nestedWorkspace = path.join(repoRoot, "packages", "web");
yield* fileSystem.makeDirectory(nestedWorkspace, { recursive: true });
yield* git(repoRoot, ["init"]);
yield* git(repoRoot, ["remote", "add", "origin", "git@github.com:T3Tools/t3code.git"]);
const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
const identity = yield* resolver.resolve(nestedWorkspace);
const resolvedIdentityRoot =
identity?.rootPath === undefined ? "" : NodeFS.realpathSync.native(identity.rootPath);
const resolvedRepoRoot = NodeFS.realpathSync.native(repoRoot);
expect(identity).not.toBeNull();
expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
expect(normalizeResolvedPath(resolvedIdentityRoot)).toBe(
normalizeResolvedPath(resolvedRepoRoot),
);
}).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
);
it.effect.each(["bare clone", "dot-bare"] as const)(
"resolves a %s to its Git directory and the same identity as a checkout",
(layout) =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const checkout = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-repository-identity-checkout-",
});
const root = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-repository-identity-bare-",
});
const bareGitDir = layout === "dot-bare" ? path.join(root, ".bare") : root;
yield* git(checkout, ["init"]);
yield* git(checkout, ["remote", "add", "origin", "git@github.com:T3Tools/t3code.git"]);
yield* git(root, ["init", "--bare", bareGitDir]);
if (layout === "dot-bare") {
yield* fileSystem.writeFileString(path.join(root, ".git"), "gitdir: ./.bare\n");
}
yield* git(root, [
"--git-dir",
bareGitDir,
"remote",
"add",
"origin",
"https://github.com/T3Tools/t3code.git",
]);
const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
const checkoutIdentity = yield* resolver.resolve(checkout);
expect(checkoutIdentity?.canonicalKey).toBe("github.com/t3tools/t3code");
const resolvedBareGitDir = NodeFS.realpathSync.native(bareGitDir);
const projectRoots = layout === "dot-bare" ? [root, bareGitDir] : [root];
for (const projectRoot of projectRoots) {
const identity = yield* resolver.resolve(projectRoot);
const resolvedIdentityRoot =
identity?.rootPath === undefined ? "" : NodeFS.realpathSync.native(identity.rootPath);
expect(identity).not.toBeNull();
expect(identity?.canonicalKey).toBe(checkoutIdentity?.canonicalKey);
expect(normalizeResolvedPath(resolvedIdentityRoot)).toBe(
normalizeResolvedPath(resolvedBareGitDir),
);
}
yield* git(root, ["--git-dir", bareGitDir, "remote", "remove", "origin"]);
for (const projectRoot of projectRoots) {
expect(yield* resolver.resolve(projectRoot, { refresh: true })).toBeNull();
}
}).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
);
it.effect(
"returns null for non-git, missing and no-remote folders, probing only what can exist",
() =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const nonGitDir = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-repository-identity-non-git-",
});
const gitDir = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-repository-identity-no-remote-",
});
yield* git(gitDir, ["init"]);
const runner = yield* ProcessRunner.ProcessRunner;
const runs: Array<ReadonlyArray<string>> = [];
const resolver = yield* RepositoryIdentityResolver.make().pipe(
Effect.provideService(ProcessRunner.ProcessRunner, {
run: (input) =>
Effect.suspend(() => {
runs.push(input.args);
return runner.run(input);
}),
}),
);
const bareProbes = () =>
runs.filter((args) => args.some((arg) => arg.startsWith("--git-dir=")));
expect(yield* resolver.resolve(path.join(nonGitDir, "deleted"))).toBeNull();
expect(runs).toEqual([]);
expect(yield* resolver.resolve(nonGitDir)).toBeNull();
expect(bareProbes()).toEqual([]);
expect(yield* resolver.resolve(gitDir)).toBeNull();
}).pipe(Effect.provide(ProcessRunner.layer)),
);
it.effect("ignores local-path remotes such as GitButler's gb-local", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const localOnlyDir = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-repository-identity-local-remote-",
});
const mixedDir = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-repository-identity-mixed-remote-",
});
yield* git(localOnlyDir, ["init"]);
yield* git(localOnlyDir, ["remote", "add", "gb-local", "."]);
yield* git(localOnlyDir, ["remote", "add", "backup", "/srv/git/repo.git"]);
yield* git(localOnlyDir, ["remote", "add", "archive", "file:///srv/git/repo.git"]);
yield* git(mixedDir, ["init"]);
yield* git(mixedDir, ["remote", "add", "gb-local", "."]);
yield* git(mixedDir, ["remote", "add", "mirror", "git@github.com:T3Tools/t3code.git"]);
const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
expect(yield* resolver.resolve(localOnlyDir)).toBeNull();
const mixedIdentity = yield* resolver.resolve(mixedDir);
expect(mixedIdentity?.locator.remoteName).toBe("mirror");
expect(mixedIdentity?.canonicalKey).toBe("github.com/t3tools/t3code");
}).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
);
it.effect.each(["add", "replace"] as const)(
"refreshes the primary upstream after %s before cache expiry",
(change) =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const cwd = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-repository-identity-upstream-test-",
});
yield* git(cwd, ["init"]);
yield* git(cwd, ["remote", "add", "origin", "git@github.com:julius/t3code.git"]);
if (change === "replace") {
yield* git(cwd, ["remote", "add", "upstream", "git@github.com:T3Tools/previous.git"]);
}
const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
const initialIdentity = yield* resolver.resolve(cwd);
expect(initialIdentity?.canonicalKey).toBe(
change === "add" ? "github.com/julius/t3code" : "github.com/t3tools/previous",
);
yield* git(cwd, [
"remote",
change === "add" ? "add" : "set-url",
"upstream",
"git@github.com:T3Tools/t3code.git",
]);
expect(yield* resolver.resolve(cwd)).toEqual(initialIdentity);
const identity = yield* resolver.resolve(cwd, { refresh: true });
expect(identity).not.toBeNull();
expect(identity?.locator.remoteName).toBe("upstream");
expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
expect(identity?.displayName).toBe("T3Tools/t3code");
expect(yield* resolver.resolve(cwd)).toEqual(identity);
}).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
);
it.effect("reports a fork's own remote as origin next to the upstream identity", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const cwd = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-repository-identity-fork-test-",
});
yield* git(cwd, ["init"]);
yield* git(cwd, ["remote", "add", "origin", "git@github.com:julius/t3code-fork.git"]);
yield* git(cwd, ["remote", "add", "upstream", "git@github.com:T3Tools/t3code.git"]);
const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
const identity = yield* resolver.resolve(cwd);
expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
expect(identity?.displayName).toBe("T3Tools/t3code");
expect(identity?.origin).toEqual({
canonicalKey: "github.com/julius/t3code-fork",
remoteUrl: "git@github.com:julius/t3code-fork.git",
displayName: "julius/t3code-fork",
});
}).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
);
it.effect("uses the last remote path segment as the repository name for nested groups", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const cwd = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-repository-identity-nested-group-test-",
});
yield* git(cwd, ["init"]);
yield* git(cwd, ["remote", "add", "origin", "git@gitlab.com:T3Tools/platform/t3code.git"]);
const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
const identity = yield* resolver.resolve(cwd);
expect(identity).not.toBeNull();
expect(identity?.canonicalKey).toBe("gitlab.com/t3tools/platform/t3code");
expect(identity?.displayName).toBe("T3Tools/platform/t3code");
expect(identity?.owner).toBe("T3Tools");
expect(identity?.name).toBe("t3code");
}).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
);
});