|
1 | 1 | // @effect-diagnostics nodeBuiltinImport:off |
| 2 | +import * as NodeFSP from "node:fs/promises"; |
2 | 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
3 | 4 | import { it, afterEach, describe, expect } from "@effect/vitest"; |
4 | 5 | import * as Effect from "effect/Effect"; |
@@ -132,6 +133,113 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => { |
132 | 133 | }), |
133 | 134 | ); |
134 | 135 |
|
| 136 | + it.effect("lists symlinks as what they point to, hiding ones that cannot be opened", () => |
| 137 | + Effect.gen(function* () { |
| 138 | + const platform = yield* HostProcess.Platform; |
| 139 | + if (platform === "win32") return; |
| 140 | + const cwd = yield* makeTempDir({ git: true }); |
| 141 | + const outside = yield* makeTempDir(); |
| 142 | + const fileSystem = yield* FileSystem.FileSystem; |
| 143 | + const path = yield* Path.Path; |
| 144 | + yield* writeTextFile(cwd, "real.txt"); |
| 145 | + yield* writeTextFile(cwd, "realdir/nested.txt"); |
| 146 | + yield* writeTextFile(outside, "outside.txt"); |
| 147 | + yield* fileSystem.symlink("real.txt", path.join(cwd, "link.txt")); |
| 148 | + yield* fileSystem.symlink("realdir", path.join(cwd, "linkdir")); |
| 149 | + yield* fileSystem.symlink(outside, path.join(cwd, "outside-link")); |
| 150 | + yield* fileSystem.symlink(".git", path.join(cwd, "git-link")); |
| 151 | + yield* fileSystem.symlink("missing", path.join(cwd, "dangling")); |
| 152 | + |
| 153 | + const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 154 | + const root = yield* workspaceEntries.list({ cwd, directoryPath: "" }); |
| 155 | + expect(root.entries.toSorted((a, b) => a.path.localeCompare(b.path))).toEqual([ |
| 156 | + { path: "link.txt", kind: "file" }, |
| 157 | + { path: "linkdir", kind: "directory" }, |
| 158 | + { path: "real.txt", kind: "file" }, |
| 159 | + { path: "realdir", kind: "directory" }, |
| 160 | + ]); |
| 161 | + expect(yield* workspaceEntries.list({ cwd, directoryPath: "linkdir" })).toEqual({ |
| 162 | + entries: [{ path: "linkdir/nested.txt", kind: "file" }], |
| 163 | + truncated: false, |
| 164 | + }); |
| 165 | + }), |
| 166 | + ); |
| 167 | + |
| 168 | + it.effect("hides folder links that lead back to a folder on the listed path", () => |
| 169 | + // "Expand all folders" loads every folder it expands, so a link that loops would |
| 170 | + // keep nesting the same entries without end. |
| 171 | + Effect.gen(function* () { |
| 172 | + const platform = yield* HostProcess.Platform; |
| 173 | + if (platform === "win32") return; |
| 174 | + const cwd = yield* makeTempDir(); |
| 175 | + const fileSystem = yield* FileSystem.FileSystem; |
| 176 | + const path = yield* Path.Path; |
| 177 | + yield* writeTextFile(cwd, "a/file.txt"); |
| 178 | + yield* writeTextFile(cwd, "b/file.txt"); |
| 179 | + yield* fileSystem.symlink(".", path.join(cwd, "self")); |
| 180 | + yield* fileSystem.symlink("..", path.join(cwd, "a/up")); |
| 181 | + yield* fileSystem.symlink("../b", path.join(cwd, "a/to-b")); |
| 182 | + yield* fileSystem.symlink("../a", path.join(cwd, "b/to-a")); |
| 183 | + |
| 184 | + const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 185 | + const paths = (directoryPath: string) => |
| 186 | + workspaceEntries |
| 187 | + .list({ cwd, directoryPath }) |
| 188 | + .pipe(Effect.map(({ entries }) => entries.map((entry) => entry.path).toSorted())); |
| 189 | + expect(yield* paths("")).toEqual(["a", "b"]); |
| 190 | + // A link into a sibling folder is fine on its own... |
| 191 | + expect(yield* paths("a")).toEqual(["a/file.txt", "a/to-b"]); |
| 192 | + // ...but once followed, the sibling's link back is hidden. |
| 193 | + expect(yield* paths("a/to-b")).toEqual(["a/to-b/file.txt"]); |
| 194 | + }), |
| 195 | + ); |
| 196 | + |
| 197 | + it.effect("resolves a folder of many symlinks in bounded batches", () => |
| 198 | + Effect.gen(function* () { |
| 199 | + const platform = yield* HostProcess.Platform; |
| 200 | + if (platform === "win32") return; |
| 201 | + const cwd = yield* makeTempDir(); |
| 202 | + const fileSystem = yield* FileSystem.FileSystem; |
| 203 | + const path = yield* Path.Path; |
| 204 | + // Shaped like a pnpm `node_modules`: one link per package into `.pnpm`. |
| 205 | + const directoryPath = [...Array.from({ length: 80 }, () => "nested"), "node_modules"].join( |
| 206 | + "/", |
| 207 | + ); |
| 208 | + const packages = Array.from({ length: 200 }, (_, index) => `pkg-${index}`); |
| 209 | + yield* Effect.forEach( |
| 210 | + packages, |
| 211 | + (name) => |
| 212 | + Effect.gen(function* () { |
| 213 | + yield* writeTextFile(cwd, `${directoryPath}/.pnpm/${name}/index.js`); |
| 214 | + yield* fileSystem.symlink(`.pnpm/${name}`, path.join(cwd, directoryPath, name)); |
| 215 | + }), |
| 216 | + { concurrency: 32, discard: true }, |
| 217 | + ); |
| 218 | + const realpath = NodeFSP.realpath; |
| 219 | + let inFlight = 0; |
| 220 | + let maxInFlight = 0; |
| 221 | + vi.spyOn(NodeFSP, "realpath").mockImplementation((async (target: string) => { |
| 222 | + maxInFlight = Math.max(maxInFlight, ++inFlight); |
| 223 | + try { |
| 224 | + return await realpath(target); |
| 225 | + } finally { |
| 226 | + inFlight--; |
| 227 | + } |
| 228 | + }) as typeof NodeFSP.realpath); |
| 229 | + |
| 230 | + const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 231 | + const { entries } = yield* workspaceEntries.list({ cwd, directoryPath }); |
| 232 | + expect(entries.map((entry) => entry.path).toSorted()).toEqual( |
| 233 | + [ |
| 234 | + `${directoryPath}/.pnpm`, |
| 235 | + ...packages.map((name) => `${directoryPath}/${name}`), |
| 236 | + ].toSorted(), |
| 237 | + ); |
| 238 | + expect(entries.every((entry) => entry.kind === "directory")).toBe(true); |
| 239 | + expect(maxInFlight).toBeLessThanOrEqual(64); |
| 240 | + }), |
| 241 | + ); |
| 242 | + |
135 | 243 | it.effect("labels the children of wide folders exactly as git does for each child", () => |
136 | 244 | Effect.gen(function* () { |
137 | 245 | // Above 1,000 children, listing first asks whether the whole folder is ignored. |
|
0 commit comments