29 lines
1.4 KiB
JavaScript
29 lines
1.4 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import test from 'node:test';
|
|
import { missingEnvironment } from './check-env-example.mjs';
|
|
|
|
const root = fileURLToPath(new URL('../', import.meta.url));
|
|
|
|
test('application environment reads are registered with empty example values', () => {
|
|
assert.deepEqual(missingEnvironment(root), []);
|
|
const entries = readFileSync(join(root, '.env.example'), 'utf8').split(/\r?\n/);
|
|
for (const entry of entries) assert.match(entry, /^(?:[A-Za-z_$][\w$]*=|#.*)?$/);
|
|
});
|
|
|
|
test('finds unregistered dotted environment reads across the requested source paths', t => {
|
|
const dir = mkdtempSync(join(tmpdir(), 'env-example-'));
|
|
t.after(() => rmSync(dir, { recursive: true, force: true }));
|
|
for (const section of ['lib', 'app', 'workers', 'hooks']) {
|
|
mkdirSync(join(dir, section));
|
|
writeFileSync(join(dir, section, 'source.ts'), 'process.env.' + section.toUpperCase());
|
|
}
|
|
writeFileSync(join(dir, 'instrumentation.ts'), 'process . env . INIT; process.env.NODE_ENV');
|
|
writeFileSync(join(dir, 'middleware.ts'), 'process.env.MIDDLE; process.env.NEXT_RUNTIME');
|
|
writeFileSync(join(dir, '.env.example'), '# APP=\nLIB=\nUNUSED=\n');
|
|
assert.deepEqual(missingEnvironment(dir), ['APP', 'HOOKS', 'INIT', 'MIDDLE', 'WORKERS']);
|
|
});
|