Repository navigation
Expand file tree
/
Copy pathinstall-viteplus.test.ts
More file actions
179 lines (160 loc) · 5.9 KB
/
Copy pathinstall-viteplus.test.ts
File metadata and controls
179 lines (160 loc) · 5.9 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
import { describe, expect, it, vi } from "vite-plus/test";
import { writeFileSync } from "node:fs";
import { join } from "node:path";
import { installVitePlus } from "./install-viteplus.js";
function writeDirsFile(env: Record<string, string>, bin: string): void {
writeFileSync(
env.SETUP_VP_DIRS_FILE,
[
"data\t/test/data",
`bin\t${bin}`,
"cache\t/test/cache",
"config\t/test/config",
"state\t/test/state",
].join("\n"),
);
}
describe("installVitePlus", () => {
it("uses PowerShell installers on Windows and bash installers on Unix", async () => {
const calls: NodeJS.Platform[] = [];
const runInstall = vi.fn(
(_url: string, env: Record<string, string>, platform: NodeJS.Platform = process.platform) => {
calls.push(platform);
writeDirsFile(env, `/test/${platform}/bin`);
return 0;
},
);
await installVitePlus("latest", {
platform: "win32",
env: { PATH: "" },
prependPath: () => undefined,
sleep: async () => undefined,
runInstall,
logWarningFn: () => undefined,
});
await installVitePlus("latest", {
platform: "linux",
env: { PATH: "" },
prependPath: () => undefined,
sleep: async () => undefined,
runInstall,
logWarningFn: () => undefined,
});
expect(calls).toEqual(["win32", "linux"]);
for (const [, installEnv] of runInstall.mock.calls) {
expect(installEnv.VP_NODE_MANAGER).toBeUndefined();
}
expect(runInstall.mock.calls[0]?.[0]).toContain("install.ps1");
expect(runInstall.mock.calls[1]?.[0]).toContain("install.sh");
});
it("installs exact versions with the release-tag script before the latest script", async () => {
// Fail every attempt so the full URL order is observable.
const runInstall = vi.fn((_url: string, _env: Record<string, string>) => 1);
const warnings: string[] = [];
await expect(
installVitePlus("0.2.9", {
platform: "linux",
env: {
PATH: "",
VP_VPDIRS_AWARE: "1",
SETUP_VP_DIRS_FILE: "/tmp/stale-vp-dirs",
},
prependPath: () => undefined,
sleep: async () => undefined,
runInstall,
logWarningFn: (message) => warnings.push(message),
}),
).rejects.toThrow(/after 8 attempts across 4 URL\(s\)/);
const urls = runInstall.mock.calls.map((call) => call[0]);
expect(urls[0]).toContain("/v0.2.9/packages/cli/install.sh");
expect(urls[1]).toContain("jsdelivr");
expect(urls[4]).toBe("https://viteplus.dev/install.sh");
expect(warnings.some((message) => message.includes("Falling back to the latest"))).toBe(true);
const installEnv = runInstall.mock.calls[0]?.[1] as Record<string, string>;
expect(installEnv.VP_VPDIRS_AWARE).toBeUndefined();
expect(installEnv.SETUP_VP_DIRS_FILE).toBeUndefined();
});
it("routes pkg.pr.new commit builds through VP_PR_VERSION", async () => {
const runInstall = vi.fn((_url: string, env: Record<string, string>) => {
writeDirsFile(env, "/test/data/bin");
return 0;
});
const sha = "a".repeat(40);
await installVitePlus(`0.0.0-commit.${sha}`, {
platform: "linux",
env: { PATH: "" },
prependPath: () => undefined,
sleep: async () => undefined,
runInstall,
logWarningFn: () => undefined,
});
const installCalls = runInstall.mock.calls as unknown as Array<
[string, Record<string, string>, NodeJS.Platform?]
>;
expect(installCalls[0]?.[1]?.VP_PR_VERSION).toBe(sha);
expect(installCalls[0]?.[1]?.VP_VPDIRS_AWARE).toBe("1");
expect(installCalls[0]?.[1]?.SETUP_VP_DIRS_FILE).toMatch(/setup-vp-dirs-.*\.txt$/);
});
it("uses the installed version to resolve the latest dist-tag", async () => {
const prependPath = vi.fn();
const env = { HOME: "/home/runner", PATH: "/usr/bin" };
const runInstall = vi.fn((_url: string, installEnv: Record<string, string>) => {
writeFileSync(installEnv.SETUP_VP_DIRS_FILE, "vp v0.2.9\n");
return 0;
});
await installVitePlus("latest", {
platform: "linux",
env,
prependPath,
sleep: async () => undefined,
runInstall,
logWarningFn: () => undefined,
});
const bin = join("/home/runner", ".vite-plus", "bin");
expect(prependPath).toHaveBeenCalledWith(bin);
expect(env.PATH).toBe(`${bin}:/usr/bin`);
});
it.each([
{ version: "0.3.0", output: "vp v0.2.9\n" },
{ version: `0.0.0-commit.${"a".repeat(40)}`, output: "bin\t/test/data/bin\n" },
{ version: "latest", output: "vp v0.3.0\n" },
])("fails when $version does not report valid VpDirs", async ({ version, output }) => {
const prependPath = vi.fn();
const runInstall = vi.fn((_url: string, env: Record<string, string>) => {
if (output !== undefined) writeFileSync(env.SETUP_VP_DIRS_FILE, output);
return 0;
});
await expect(
installVitePlus(version, {
platform: "linux",
env: { PATH: "/usr/bin" },
prependPath,
sleep: async () => undefined,
runInstall,
logWarningFn: () => undefined,
}),
).rejects.toThrow(
"Vite+ was installed successfully, but setup-vp could not resolve its VpDirs.",
);
expect(runInstall).toHaveBeenCalledTimes(1);
expect(prependPath).not.toHaveBeenCalled();
});
it("prepends the bin directory reported by the installed payload", async () => {
const prependPath = vi.fn();
const env = { PATH: "/usr/bin" };
const runInstall = vi.fn((_url: string, installEnv: Record<string, string>) => {
writeDirsFile(installEnv, "/test/data/bin");
return 0;
});
await installVitePlus("latest", {
platform: "linux",
env,
prependPath,
sleep: async () => undefined,
runInstall,
logWarningFn: () => undefined,
});
expect(prependPath).toHaveBeenCalledWith("/test/data/bin");
expect(env.PATH).toBe("/test/data/bin:/usr/bin");
});
});