Files
bun.sh/test/js/node/process/process-loadenvfile.test.ts
Claude Bot 794073d62f Disable environment variable expansion in process.loadEnvFile
- Change Parser.parse expand parameter from true to false
- Update test to verify that variable expansion is disabled
- Variables like $VAR and ${VAR} are now treated as literal strings
- All tests still pass (7/7)

This ensures process.loadEnvFile behaves as a simple key-value parser
without performing variable substitution.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-28 03:58:19 +00:00

138 lines
3.1 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import { unlinkSync, writeFileSync } from "fs";
import { tmpdir } from "os";
import { join } from "path";
describe("process.loadEnvFile", () => {
it("should load environment variables from a .env file", () => {
const tempDir = tmpdir();
const envFile = join(tempDir, "test.env");
// Create a test .env file
const envContent = `
FOO=bar
BAZ=qux
MULTILINE="line1
line2"
QUOTED='single quoted'
EMPTY=
`;
writeFileSync(envFile, envContent);
try {
const result = process.loadEnvFile(envFile);
expect(result).toEqual({
FOO: "bar",
BAZ: "qux",
MULTILINE: "line1\nline2",
QUOTED: "single quoted",
EMPTY: "",
});
} finally {
unlinkSync(envFile);
}
});
it("should NOT expand environment variables (expansion disabled)", () => {
const tempDir = tmpdir();
const envFile = join(tempDir, "test-no-expand.env");
// Create a test .env file with variable expansion syntax
const envContent = `
BASE_URL=https://example.com
API_URL=$BASE_URL/api
FULL_URL=\${API_URL}/v1
WITH_DEFAULT=\${MISSING_VAR:-default}
`;
writeFileSync(envFile, envContent);
try {
const result = process.loadEnvFile(envFile);
// Variable expansion should be disabled, so variables should remain as literal strings
expect(result).toEqual({
BASE_URL: "https://example.com",
API_URL: "$BASE_URL/api", // Should NOT be expanded
FULL_URL: "${API_URL}/v1", // Should NOT be expanded
WITH_DEFAULT: "${MISSING_VAR:-default}", // Should NOT be expanded
});
} finally {
unlinkSync(envFile);
}
});
it("should handle export statements", () => {
const tempDir = tmpdir();
const envFile = join(tempDir, "test-export.env");
const envContent = `
export NODE_ENV=development
export PORT=3000
DEBUG=1
`;
writeFileSync(envFile, envContent);
try {
const result = process.loadEnvFile(envFile);
expect(result).toEqual({
NODE_ENV: "development",
PORT: "3000",
DEBUG: "1",
});
} finally {
unlinkSync(envFile);
}
});
it("should handle comments and empty lines", () => {
const tempDir = tmpdir();
const envFile = join(tempDir, "test-comments.env");
const envContent = `
# This is a comment
FOO=bar
# Another comment
BAZ=qux
`;
writeFileSync(envFile, envContent);
try {
const result = process.loadEnvFile(envFile);
expect(result).toEqual({
FOO: "bar",
BAZ: "qux",
});
} finally {
unlinkSync(envFile);
}
});
it("should throw an error for non-existent files", () => {
expect(() => {
process.loadEnvFile("/non/existent/file.env");
}).toThrow();
});
it("should throw an error when no path is provided", () => {
expect(() => {
// @ts-ignore
process.loadEnvFile();
}).toThrow();
});
it("should throw an error when path is not a string", () => {
expect(() => {
// @ts-ignore
process.loadEnvFile(123);
}).toThrow();
});
});