forked from freshframework/fresh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_cache_test.ts
More file actions
78 lines (74 loc) · 2.94 KB
/
build_cache_test.ts
File metadata and controls
78 lines (74 loc) · 2.94 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
import { expect } from "@std/expect";
import * as path from "@std/path";
import { ProdBuildCache, type StaticFile } from "./build_cache.ts";
import type { ResolvedFreshConfig } from "./mod.ts";
async function getContent(readResult: Promise<StaticFile | null>) {
const res = await readResult;
if (res === null) return null;
if (res.readable instanceof Uint8Array) throw new Error("not implemented");
return new Response(res.readable).text();
}
Deno.test({
name: "ProdBuildCache - should error if reading outside of staticDir",
fn: async () => {
const tmp = await Deno.makeTempDir();
const config: ResolvedFreshConfig = {
root: tmp,
mode: "production",
basePath: "/",
staticDir: path.join(tmp, "static"),
build: {
outDir: path.join(tmp, "dist"),
},
};
await Deno.mkdir(path.join(tmp, "static", ".well-known"), {
recursive: true,
});
await Deno.mkdir(path.join(tmp, "dist", "static"), {
recursive: true,
});
await Promise.all([
Deno.writeTextFile(
path.join(tmp, "dist", "secret-styles.css"),
"SECRET!",
),
Deno.writeTextFile(path.join(tmp, "SECRETS.txt"), "SECRET!"),
Deno.writeTextFile(path.join(tmp, "dist", "static", "styles.css"), "OK"),
Deno.writeTextFile(
path.join(tmp, "static", ".well-known", "foo.txt"),
"OK",
),
]);
const buildCache = new ProdBuildCache(
config,
new Map(),
new Map([
["../secret-styles.css", { generated: true, hash: "SECRET!" }],
["../SECRETS.txt", { generated: false, hash: "SECRET!" }],
["./../secret-styles.css", { generated: true, hash: "SECRET!" }],
["./../SECRETS.txt", { generated: false, hash: "SECRET!" }],
["styles.css", { generated: true, hash: "OK" }],
[".well-known/foo.txt", { generated: false, hash: "OK" }],
["./styles.css", { generated: true, hash: "OK" }],
["./.well-known/foo.txt", { generated: false, hash: "OK" }],
]),
true,
);
const secret1 = getContent(buildCache.readFile("../styles.css"));
const secret2 = getContent(buildCache.readFile("../SECRETS.txt"));
const secret3 = getContent(buildCache.readFile("./../styles.css"));
const secret4 = getContent(buildCache.readFile("./../SECRETS.txt"));
const public1 = getContent(buildCache.readFile("styles.css"));
const public2 = getContent(buildCache.readFile(".well-known/foo.txt"));
const public3 = getContent(buildCache.readFile("./styles.css"));
const public4 = getContent(buildCache.readFile("./.well-known/foo.txt"));
await expect(secret1).resolves.toBe(null);
await expect(secret2).resolves.toBe(null);
await expect(secret3).resolves.toBe(null);
await expect(secret4).resolves.toBe(null);
await expect(public1).resolves.toBe("OK");
await expect(public2).resolves.toBe("OK");
await expect(public3).resolves.toBe("OK");
await expect(public4).resolves.toBe("OK");
},
});