Repository navigation
Expand file tree
/
Copy pathenv-example.test.ts
More file actions
42 lines (36 loc) · 1.61 KB
/
Copy pathenv-example.test.ts
File metadata and controls
42 lines (36 loc) · 1.61 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
// @vitest-environment node
import { describe, expect, it } from "vitest";
import { readdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
const root = process.cwd();
// Set by Next.js or the Dockerfile rather than by operators.
const PLATFORM_VARS = new Set(["NODE_ENV", "NEXT_RUNTIME", "NEXT_PUBLIC_DISPATCH_VERSION", "PORT"]);
function sourceFiles(dir: string): string[] {
return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
const path = join(dir, entry.name);
if (entry.isDirectory()) return entry.name === "__tests__" ? [] : sourceFiles(path);
return /\.(ts|tsx)$/.test(entry.name) && !/\.test\.tsx?$/.test(entry.name) ? [path] : [];
});
}
function envVarsReadByCode(): Set<string> {
const names = new Set<string>();
// process.env.NAME, process.env["NAME"], and env.NAME on an injected env object.
const pattern = /(?:\bprocess\.env\.|\bprocess\.env\[["']|(?<![\w.])env\??\.)([A-Z][A-Z0-9_]+)/g;
for (const file of sourceFiles(join(root, "src"))) {
for (const match of readFileSync(file, "utf8").matchAll(pattern)) names.add(match[1]);
}
return names;
}
function envVarsDocumented(): Set<string> {
const text = readFileSync(join(root, ".env.example"), "utf8");
return new Set([...text.matchAll(/^#?\s*([A-Z][A-Z0-9_]+)=/gm)].map((m) => m[1]));
}
describe(".env.example", () => {
it("documents every env var the code reads", () => {
const documented = envVarsDocumented();
const missing = [...envVarsReadByCode()]
.filter((name) => !documented.has(name) && !PLATFORM_VARS.has(name))
.sort();
expect(missing).toEqual([]);
});
});