49 lines
1.5 KiB
JavaScript
49 lines
1.5 KiB
JavaScript
"use strict";
|
|
|
|
const fs = require("node:fs");
|
|
const os = require("node:os");
|
|
const path = require("node:path");
|
|
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const { loadDotEnv } = require("../src/envLoader");
|
|
|
|
test("loadDotEnv parses quoted values and keeps existing env", () => {
|
|
const suffix = `${Date.now()}_${Math.random().toString(16).slice(2)}`;
|
|
const plainKey = `AIRPLANE_TEST_PLAIN_${suffix}`;
|
|
const quotedKey = `AIRPLANE_TEST_QUOTED_${suffix}`;
|
|
const singleQuotedKey = `AIRPLANE_TEST_SINGLE_${suffix}`;
|
|
const existingKey = `AIRPLANE_TEST_EXISTING_${suffix}`;
|
|
const invalidKey = "INVALID-NAME";
|
|
|
|
const envPath = path.join(os.tmpdir(), `airplane_${suffix}.env`);
|
|
const envContent = [
|
|
`# comment`,
|
|
`${plainKey}=hello`,
|
|
`${quotedKey}=\"line1\\nline2\"`,
|
|
`${singleQuotedKey}='with space'`,
|
|
`${existingKey}=from_file`,
|
|
`${invalidKey}=ignored`,
|
|
``,
|
|
].join("\n");
|
|
|
|
process.env[existingKey] = "from_process";
|
|
fs.writeFileSync(envPath, envContent, "utf8");
|
|
|
|
try {
|
|
loadDotEnv(envPath);
|
|
|
|
assert.equal(process.env[plainKey], "hello");
|
|
assert.equal(process.env[quotedKey], "line1\nline2");
|
|
assert.equal(process.env[singleQuotedKey], "with space");
|
|
assert.equal(process.env[existingKey], "from_process");
|
|
} finally {
|
|
delete process.env[plainKey];
|
|
delete process.env[quotedKey];
|
|
delete process.env[singleQuotedKey];
|
|
delete process.env[existingKey];
|
|
if (fs.existsSync(envPath)) {
|
|
fs.unlinkSync(envPath);
|
|
}
|
|
}
|
|
});
|