43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import { existsSync, readFileSync } from 'node:fs'
|
|
import { resolve } from 'node:path'
|
|
|
|
/**
|
|
* Minimal dependency-free `.env` loader for the E2E suite.
|
|
*
|
|
* Reads `tests/.env.test` (or `./.env.test`) if present and populates
|
|
* `process.env` WITHOUT overriding values already provided by the real
|
|
* environment — so `TEST_BASE_URL=... npx playwright test` still wins.
|
|
*/
|
|
export function loadTestEnv(): void {
|
|
const candidates = [
|
|
resolve(process.cwd(), 'tests/.env.test'),
|
|
resolve(process.cwd(), '.env.test'),
|
|
]
|
|
const file = candidates.find((candidate) => existsSync(candidate))
|
|
if (!file) return
|
|
|
|
const content = readFileSync(file, 'utf8')
|
|
|
|
for (const rawLine of content.split(/\r?\n/)) {
|
|
const line = rawLine.trim()
|
|
if (!line || line.startsWith('#')) continue
|
|
|
|
const separatorIndex = line.indexOf('=')
|
|
if (separatorIndex === -1) continue
|
|
|
|
const key = line.slice(0, separatorIndex).trim()
|
|
if (!key) continue
|
|
|
|
let value = line.slice(separatorIndex + 1).trim()
|
|
if (
|
|
(value.startsWith('"') && value.endsWith('"')) ||
|
|
(value.startsWith("'") && value.endsWith("'"))
|
|
) {
|
|
value = value.slice(1, -1)
|
|
}
|
|
|
|
if (process.env[key] === undefined) {
|
|
process.env[key] = value
|
|
}
|
|
}
|
|
}
|