|
1 | 1 | // @effect-diagnostics nodeBuiltinImport:off |
2 | 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | +import * as NodeFSP from "node:fs/promises"; |
3 | 4 | import { it, afterEach, describe, expect } from "@effect/vitest"; |
4 | 5 | import * as Effect from "effect/Effect"; |
5 | 6 | import * as FileSystem from "effect/FileSystem"; |
@@ -568,6 +569,133 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => { |
568 | 569 | }); |
569 | 570 |
|
570 | 571 | describe("browse", () => { |
| 572 | + it.effect( |
| 573 | + "offers directory links in browsing and autocomplete while retaining their symlink paths", |
| 574 | + () => |
| 575 | + Effect.gen(function* () { |
| 576 | + const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 577 | + const path = yield* Path.Path; |
| 578 | + const cwd = yield* makeTempDir({ prefix: "t3code-picker-links-" }); |
| 579 | + const target = yield* makeTempDir({ prefix: "t3code-picker-target-" }); |
| 580 | + yield* writeTextFile(target, "nested-project/package.json", "{}"); |
| 581 | + yield* writeTextFile(cwd, "ordinary/package.json", "{}"); |
| 582 | + const names = ["linked-project", "Templates", "Recent", ".linked-hidden"]; |
| 583 | + for (const name of names) { |
| 584 | + yield* Effect.tryPromise(() => |
| 585 | + NodeFSP.symlink(target, path.join(cwd, name), "junction"), |
| 586 | + ); |
| 587 | + } |
| 588 | + const root = yield* appendSeparator(cwd); |
| 589 | + const directory = yield* workspaceEntries.browse({ partialPath: root }); |
| 590 | + expect(directory).toEqual({ |
| 591 | + parentPath: cwd, |
| 592 | + entries: [...names, "ordinary"] |
| 593 | + .toSorted((a, b) => a.localeCompare(b)) |
| 594 | + .map((name) => ({ |
| 595 | + name, |
| 596 | + fullPath: path.join(cwd, name), |
| 597 | + })), |
| 598 | + }); |
| 599 | + expect(yield* workspaceEntries.browse({ partialPath: `${root}LiNk` })).toEqual({ |
| 600 | + parentPath: cwd, |
| 601 | + entries: [{ name: "linked-project", fullPath: path.join(cwd, "linked-project") }], |
| 602 | + }); |
| 603 | + expect(yield* workspaceEntries.browse({ partialPath: `${root}.link` })).toEqual({ |
| 604 | + parentPath: cwd, |
| 605 | + entries: [{ name: ".linked-hidden", fullPath: path.join(cwd, ".linked-hidden") }], |
| 606 | + }); |
| 607 | + const alias = path.join(cwd, "linked-project"); |
| 608 | + expect( |
| 609 | + yield* workspaceEntries.browse({ partialPath: yield* appendSeparator(alias) }), |
| 610 | + ).toEqual({ |
| 611 | + parentPath: alias, |
| 612 | + entries: [{ name: "nested-project", fullPath: path.join(alias, "nested-project") }], |
| 613 | + }); |
| 614 | + }), |
| 615 | + ); |
| 616 | + |
| 617 | + it.effect( |
| 618 | + "omits file, broken, cyclic, and inaccessible link targets without losing other directories", |
| 619 | + () => |
| 620 | + Effect.gen(function* () { |
| 621 | + const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 622 | + const path = yield* Path.Path; |
| 623 | + const cwd = yield* makeTempDir({ prefix: "t3code-picker-link-errors-" }); |
| 624 | + const target = yield* makeTempDir({ prefix: "t3code-picker-link-target-" }); |
| 625 | + yield* writeTextFile(cwd, "plain-file.txt"); |
| 626 | + yield* writeTextFile(cwd, "ordinary/package.json", "{}"); |
| 627 | + for (const name of [ |
| 628 | + "linked-good", |
| 629 | + "linked-file", |
| 630 | + "linked-broken", |
| 631 | + "linked-loop", |
| 632 | + "linked-denied", |
| 633 | + ]) { |
| 634 | + yield* Effect.tryPromise(() => |
| 635 | + NodeFSP.symlink(target, path.join(cwd, name), "junction"), |
| 636 | + ); |
| 637 | + } |
| 638 | + const originalStat = NodeFSP.stat; |
| 639 | + const fileStat = yield* Effect.tryPromise(() => |
| 640 | + originalStat(path.join(cwd, "plain-file.txt")), |
| 641 | + ); |
| 642 | + const failures = new Map([ |
| 643 | + [path.join(cwd, "linked-broken"), "ENOENT"], |
| 644 | + [path.join(cwd, "linked-loop"), "ELOOP"], |
| 645 | + [path.join(cwd, "linked-denied"), "EACCES"], |
| 646 | + ]); |
| 647 | + // Inject target states so this also runs on Windows without file-symlink privileges. |
| 648 | + using _stat = vi.spyOn(NodeFSP, "stat").mockImplementation((file, options) => { |
| 649 | + const code = failures.get(String(file)); |
| 650 | + if (code) return Promise.reject(Object.assign(new Error(code), { code })); |
| 651 | + if (String(file) === path.join(cwd, "linked-file")) return Promise.resolve(fileStat); |
| 652 | + return originalStat(file, options); |
| 653 | + }); |
| 654 | + const result = yield* workspaceEntries.browse({ |
| 655 | + partialPath: yield* appendSeparator(cwd), |
| 656 | + }); |
| 657 | + expect(result.entries).toEqual([ |
| 658 | + { name: "linked-good", fullPath: path.join(cwd, "linked-good") }, |
| 659 | + { name: "ordinary", fullPath: path.join(cwd, "ordinary") }, |
| 660 | + ]); |
| 661 | + }), |
| 662 | + ); |
| 663 | + |
| 664 | + it.effect("omits direct and chained network links without statting their targets", () => |
| 665 | + Effect.gen(function* () { |
| 666 | + const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 667 | + const path = yield* Path.Path; |
| 668 | + const cwd = yield* makeTempDir({ prefix: "t3code-picker-network-" }); |
| 669 | + const target = yield* makeTempDir({ prefix: "t3code-picker-local-" }); |
| 670 | + const direct = path.join(cwd, "network"); |
| 671 | + const chained = path.join(cwd, "chained"); |
| 672 | + const local = path.join(cwd, "local"); |
| 673 | + for (const link of [direct, local]) { |
| 674 | + yield* Effect.tryPromise(() => NodeFSP.symlink(target, link, "junction")); |
| 675 | + } |
| 676 | + yield* Effect.tryPromise(() => NodeFSP.symlink(direct, chained, "junction")); |
| 677 | + const originalReadlink = NodeFSP.readlink; |
| 678 | + const networkTarget = String.raw`\\host\share`; |
| 679 | + using _readlink = vi |
| 680 | + .spyOn(NodeFSP, "readlink") |
| 681 | + .mockImplementation((file, options) => |
| 682 | + String(file) === direct |
| 683 | + ? Promise.resolve(networkTarget) |
| 684 | + : originalReadlink(file, options), |
| 685 | + ); |
| 686 | + using stat = vi.spyOn(NodeFSP, "stat"); |
| 687 | + using lstat = vi.spyOn(NodeFSP, "lstat"); |
| 688 | + const result = yield* workspaceEntries.browse({ |
| 689 | + partialPath: yield* appendSeparator(cwd), |
| 690 | + }); |
| 691 | + expect(result.entries).toEqual([{ name: "local", fullPath: local }]); |
| 692 | + expect(stat.mock.calls.map(([file]) => String(file))).not.toContain(direct); |
| 693 | + expect(stat.mock.calls.map(([file]) => String(file))).not.toContain(chained); |
| 694 | + expect(stat.mock.calls.map(([file]) => String(file))).not.toContain(networkTarget); |
| 695 | + expect(lstat.mock.calls.map(([file]) => String(file))).not.toContain(networkTarget); |
| 696 | + }), |
| 697 | + ); |
| 698 | + |
571 | 699 | it.effect("shows dot directories in directory mode and hidden-prefix mode", () => |
572 | 700 | Effect.gen(function* () { |
573 | 701 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
|
0 commit comments