Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

entrypoint.test.ts

93 lines2.8 KB

1// @effect-diagnostics nodeBuiltinImport:off - entrypoint detection is a Node filesystem boundary.
2import * as NodeFS from "node:fs";
3import * as NodeOS from "node:os";
4import * as NodePath from "node:path";
5import * as NodeURL from "node:url";
6​
7import { describe, expect, it } from "vite-plus/test";
8​
9import { isEntrypoint } from "./entrypoint.ts";
10import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
11​
12const makeTempDir = () => NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-entrypoint-test-"));
13​
14describe("isEntrypoint", () => {
15 it("uses the runtime answer when Node provides one", () => {
16 expect(
17 isEntrypoint({
18 moduleUrl: "file:///somewhere/bin.mjs",
19 entryPath: "/elsewhere/other.mjs",
20 runtimeMain: true,
21 }),
22 ).toBe(true);
23 expect(
24 isEntrypoint({
25 moduleUrl: "file:///somewhere/bin.mjs",
26 entryPath: "/somewhere/bin.mjs",
27 runtimeMain: false,
28 }),
29 ).toBe(false);
30 });
31​
32 it("matches the entrypoint path when the runtime has no import.meta.main", () => {
33 // Node 22.16, 22.17 and 23.11 are inside `engines.node` but leave it undefined.
34 const dir = makeTempDir();
35 const entry = NodePath.join(dir, "bin.mjs");
36 NodeFS.writeFileSync(entry, "");
37​
38 expect(
39 isEntrypoint({
40 moduleUrl: NodeURL.pathToFileURL(entry).href,
41 entryPath: entry,
42 runtimeMain: undefined,
43 }),
44 ).toBe(true);
45 });
46​
47 it.skipIf(!symlinksSupported)(
48 "matches through a symlinked entrypoint, as npm and npx install it",
49 () => {
50 const dir = makeTempDir();
51 const real = NodePath.join(dir, "bin.mjs");
52 const link = NodePath.join(dir, "t3");
53 NodeFS.writeFileSync(real, "");
54 NodeFS.symlinkSync(real, link);
55​
56 // Node resolves the module URL to the real path, and on macOS the temp
57 // directory itself sits behind a symlink (`/var` -> `/private/var`).
58 expect(
59 isEntrypoint({
60 moduleUrl: NodeURL.pathToFileURL(NodeFS.realpathSync(real)).href,
61 entryPath: link,
62 runtimeMain: undefined,
63 }),
64 ).toBe(true);
65 },
66 );
67​
68 it("stays false for an imported module that is not the entrypoint", () => {
69 const dir = makeTempDir();
70 const entry = NodePath.join(dir, "bin.mjs");
71 const imported = NodePath.join(dir, "cli.mjs");
72 NodeFS.writeFileSync(entry, "");
73 NodeFS.writeFileSync(imported, "");
74​
75 expect(
76 isEntrypoint({
77 moduleUrl: NodeURL.pathToFileURL(imported).href,
78 entryPath: entry,
79 runtimeMain: undefined,
80 }),
81 ).toBe(false);
82 });
83​
84 it("stays false when there is no entrypoint argument", () => {
85 expect(
86 isEntrypoint({
87 moduleUrl: "file:///somewhere/bin.mjs",
88 entryPath: undefined,
89 runtimeMain: undefined,
90 }),
91 ).toBe(false);
92 });
93});