-
Notifications
You must be signed in to change notification settings - Fork 742
Expand file tree
/
Copy pathtest_utils.ts
More file actions
261 lines (230 loc) · 5.97 KB
/
test_utils.ts
File metadata and controls
261 lines (230 loc) · 5.97 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
import { createBuilder } from "vite";
import * as path from "@std/path";
import { walk } from "@std/fs/walk";
import { withTmpDir } from "../../fresh/src/test_utils.ts";
import { withChildProcessServer } from "../../fresh/tests/test_utils.tsx";
let ensureEsbuildSignedPromise: Promise<void> | null = null;
async function ensureEsbuildSigned() {
if (Deno.build.os !== "darwin") {
return;
}
if (ensureEsbuildSignedPromise) {
return await ensureEsbuildSignedPromise;
}
ensureEsbuildSignedPromise = (async () => {
let esbuildPath: string;
try {
esbuildPath = path.fromFileUrl(
import.meta.resolve("npm:esbuild/bin/esbuild"),
);
} catch (err) {
throw new Error(
`Failed to resolve esbuild binary location: ${
err instanceof Error ? err.message : String(err)
}`,
);
}
try {
const command = new Deno.Command("codesign", {
args: ["--force", "--sign", "-", esbuildPath],
stdout: "null",
stderr: "piped",
});
const { code, stderr } = await command.output();
if (code !== 0) {
const message = new TextDecoder().decode(stderr).trim();
throw new Error(
`codesign exited with code ${code}${message ? ": " + message : ""}`,
);
}
} catch (err) {
ensureEsbuildSignedPromise = null;
if (err instanceof Deno.errors.NotFound) {
throw new Error(
"codesign tool was not found. Install Xcode command-line tools or codesign the esbuild binary manually.",
);
}
throw err;
}
})();
await ensureEsbuildSignedPromise;
}
export const DEMO_DIR = path.join(import.meta.dirname!, "..", "demo");
export const FIXTURE_DIR = path.join(import.meta.dirname!, "fixtures");
export async function updateFile(
filePath: string,
fn: (text: string) => string | Promise<string>,
) {
const original = await Deno.readTextFile(filePath);
const result = await fn(original);
await Deno.writeTextFile(filePath, result);
return {
async [Symbol.asyncDispose]() {
await Deno.writeTextFile(filePath, original);
},
};
}
async function copyDir(from: string, to: string) {
const entries = walk(from, {
includeFiles: true,
includeDirs: false,
skip: [/([\\/]+(_fresh|node_modules|vendor)[\\/]+|[\\/]+vite\.config\.ts)/],
});
for await (const entry of entries) {
if (entry.isFile) {
const relative = path.relative(from, entry.path);
const target = path.join(to, relative);
try {
await Deno.mkdir(path.dirname(target), { recursive: true });
} catch (err) {
if (!(err instanceof Deno.errors.NotFound)) {
throw err;
}
}
await Deno.copyFile(entry.path, target);
}
}
}
export async function prepareDevServer(fixtureDir: string) {
const tmp = await withTmpDir({
dir: path.join(import.meta.dirname!, ".."),
prefix: "tmp_vite_",
});
await copyDir(fixtureDir, tmp.dir);
await Deno.writeTextFile(
path.join(tmp.dir, "vite.config.ts"),
`import { defineConfig } from "vite";
import { fresh } from "@fresh/plugin-vite";
export default defineConfig({
plugins: [
fresh(),
],
});
`,
);
return tmp;
}
export async function launchDevServer(
dir: string,
fn: (address: string, dir: string) => void | Promise<void>,
env: Record<string, string> = {},
) {
await ensureEsbuildSigned();
await withChildProcessServer(
{
cwd: dir,
args: ["run", "-A", "--cached-only", "npm:vite", "--port", "0"],
env,
},
async (address) => await fn(address, dir),
);
}
export async function spawnDevServer(
dir: string,
env: Record<string, string> = {},
) {
await ensureEsbuildSigned();
const boot = Promise.withResolvers<void>();
const p = Promise.withResolvers<void>();
let serverAddress = "";
const server = withChildProcessServer(
{
cwd: dir,
args: ["run", "-A", "--cached-only", "npm:vite", "--port", "0"],
env,
},
async (address) => {
serverAddress = address;
boot.resolve();
await p.promise;
},
);
await boot.promise;
return {
dir,
promise: server,
address: () => {
return serverAddress;
},
async [Symbol.asyncDispose]() {
await p.resolve();
},
};
}
export async function withDevServer(
fixtureDir: string,
fn: (address: string, dir: string) => void | Promise<void>,
env: Record<string, string> = {},
) {
await using tmp = await prepareDevServer(fixtureDir);
await launchDevServer(tmp.dir, fn, env);
}
export async function buildVite(
fixtureDir: string,
options?: { base?: string },
) {
await ensureEsbuildSigned();
const tmp = await withTmpDir({
dir: path.join(import.meta.dirname!, ".."),
prefix: "tmp_vite_",
});
const builder = await createBuilder({
logLevel: "error",
root: fixtureDir,
base: options?.base,
build: {
emptyOutDir: true,
},
environments: {
ssr: {
build: {
outDir: path.join(tmp.dir, "_fresh", "server"),
},
},
client: {
build: {
outDir: path.join(tmp.dir, "_fresh", "client"),
},
},
},
});
await builder.buildApp();
return {
tmp: tmp.dir,
async [Symbol.asyncDispose]() {
return await tmp[Symbol.asyncDispose]();
},
};
}
export function usingEnv(name: string, value: string) {
const prev = Deno.env.get(name);
Deno.env.set(name, value);
return {
[Symbol.dispose]: () => {
if (prev === undefined) {
Deno.env.delete(name);
} else {
Deno.env.set(name, prev);
}
},
};
}
export interface ProdOptions {
cwd: string;
args?: string[];
bin?: string;
env?: Record<string, string>;
}
export async function launchProd(
options: ProdOptions,
fn: (address: string) => void | Promise<void>,
) {
return await withChildProcessServer(
{
cwd: options.cwd,
args: options.args ??
["serve", "-A", "--cached-only", "--port", "0", "_fresh/server.js"],
},
fn,
);
}