fix: do not inline process.env during bun run and bun test (#7614)

* env stuff

* ok

* undo

* remove unused param

* resolve review comment

* dupe

* it compiles now i promise
This commit is contained in:
dave caruso
2023-12-13 06:05:03 -08:00
committed by GitHub
parent 38c6575dc8
commit 5029fc8564
8 changed files with 193 additions and 89 deletions

View File

@@ -1,6 +1,6 @@
import { spawn } from "bun";
import { expect, it } from "bun:test";
import { bunExe, bunEnv } from "harness";
import { bunExe, bunEnv, tempDirWithFiles, bunRun, bunRunAsScript } from "harness";
import { readFileSync, renameSync, rmSync, unlinkSync, writeFileSync } from "fs";
import { join } from "path";

View File

@@ -127,7 +127,7 @@ describe(".env file is loaded", () => {
const { stdout } = bunTest(`${dir}/index.test.ts`);
expect(stdout).toBe("false");
});
test.todo("NODE_ENV is automatically set to test within bun test", () => {
test("NODE_ENV is automatically set to test within bun test", () => {
const dir = tempDirWithFiles("dotenv", {
"index.test.ts": "console.log(process.env.NODE_ENV);",
});
@@ -365,7 +365,7 @@ test(".env special characters 1 (issue #2823)", () => {
expect(stdout).toBe("[a] [c$v]");
});
test.todo("env escaped quote (issue #2484)", () => {
test("env escaped quote (issue #2484)", () => {
const dir = tempDirWithFiles("env-issue-2484", {
"index.ts": "console.log(process.env.VALUE, process.env.VALUE2);",
});
@@ -424,7 +424,8 @@ test("#3911", () => {
});
describe("boundary tests", () => {
test("src boundary", () => {
// TODO: this is a regression in bun ~1.0.15 ish
test.todo("src boundary", () => {
const dir = tempDirWithFiles("dotenv", {
".env": 'KEY="a\\n"',
"index.ts": "console.log(process.env.KEY);",
@@ -579,3 +580,102 @@ describe("--env-file", () => {
expect(res.stdout).toBe("");
});
});
describe("process.env is not inlined", () => {
test("basic case", () => {
const tmp = tempDirWithFiles("env-inlining", {
"index.ts": `process.env.NODE_ENV = "production";
process.env.YOLO = "woo!";
console.log(process.env.NODE_ENV, process.env.YOLO);`,
});
expect(
bunRun(path.join(tmp, "index.ts"), {
NODE_ENV: undefined,
YOLO: "boo",
}).stdout,
).toBe("production woo!");
});
test("pass explicit NODE_ENV case", () => {
const tmp = tempDirWithFiles("env-inlining", {
"index.ts": `console.log(process.env.NODE_ENV);
process.env.NODE_ENV = "development";
process.env.YOLO = "woo!";
console.log(process.env.NODE_ENV, process.env.YOLO);`,
});
expect(
bunRun(path.join(tmp, "index.ts"), {
NODE_ENV: "production",
YOLO: "boo",
}).stdout,
).toBe("production\ndevelopment woo!");
});
test("pass weird NODE_ENV case", () => {
const tmp = tempDirWithFiles("env-inlining", {
"index.ts": `console.log(process.env.NODE_ENV);
process.env.NODE_ENV = "development";
process.env.YOLO = "woo!";
console.log(process.env.NODE_ENV, process.env.YOLO);`,
});
expect(
bunRun(path.join(tmp, "index.ts"), {
NODE_ENV: "buh",
YOLO: "boo",
}).stdout,
).toBe("buh\ndevelopment woo!");
});
test("in bun test", () => {
const tmp = tempDirWithFiles("env-inlining", {
"index.test.ts": `test("my test", () => {
console.log(process.env.NODE_ENV);
process.env.NODE_ENV = "development";
process.env.YOLO = "woo!";
console.log(process.env.NODE_ENV, process.env.YOLO);
});`,
});
expect(
bunTest(path.join(tmp, "index.test.ts"), {
YOLO: "boo",
}).stdout,
).toBe("test\ndevelopment woo!");
});
test("in bun test with explicit setting", () => {
const tmp = tempDirWithFiles("env-inlining", {
"index.test.ts": `test("my test", () => {
console.log(process.env.NODE_ENV);
process.env.NODE_ENV = "development";
process.env.YOLO = "woo!";
console.log(process.env.NODE_ENV, process.env.YOLO);
});`,
});
expect(
bunTest(path.join(tmp, "index.test.ts"), {
YOLO: "boo",
NODE_ENV: "production",
}).stdout,
).toBe("production\ndevelopment woo!");
});
test("in bun test with dynamic access", () => {
const tmp = tempDirWithFiles("env-inlining", {
"index.test.ts": `const dynamic = () => require('process')['e' + String('nv')];
test("my test", () => {
console.log(dynamic().NODE_ENV);
process.env.NODE_ENV = "production";
console.log(dynamic().NODE_ENV);
});`,
});
expect(bunTest(path.join(tmp, "index.test.ts"), {}).stdout).toBe("test\nproduction");
});
test("in bun test with dynamic access + explicit set", () => {
const tmp = tempDirWithFiles("env-inlining", {
"index.test.ts": `const dynamic = () => require('process')['e' + String('nv')];
test("my test", () => {
console.log(dynamic().NODE_ENV);
process.env.NODE_ENV = "production";
console.log(dynamic().NODE_ENV);
});`,
});
expect(bunTest(path.join(tmp, "index.test.ts"), { NODE_ENV: "development" }).stdout).toBe(
"development\nproduction",
);
});
});

View File

@@ -6,10 +6,10 @@ import { bunEnv, bunExe, bunRun } from "harness";
import { tmpdir } from "os";
import { join } from "path";
function dummyFile(size: number, cache_bust: string, value: string) {
function dummyFile(size: number, cache_bust: string, value: string | { code: string }) {
const data = Buffer.alloc(size);
data.write("/*" + cache_bust);
const end = `*/\nconsole.log(${JSON.stringify(value)});`;
const end = `*/\nconsole.log(${(value as any).code ?? JSON.stringify(value)});`;
data.fill("*", 2 + cache_bust.length, size - end.length, "utf-8");
data.write(end, size - end.length, "utf-8");
return data;
@@ -160,4 +160,17 @@ describe("transpiler cache", () => {
chmodSync(join(cache_dir), "777");
});
test("does not inline process.env", () => {
writeFileSync(
join(temp_dir, "a.js"),
dummyFile((50 * 1024 * 1.5) | 0, "1", { code: "process.env.NODE_ENV, process.env.HELLO" }),
);
const a = bunRun(join(temp_dir, "a.js"), { ...env, NODE_ENV: undefined, HELLO: "1" });
expect(a.stdout == "development 1");
assert(existsSync(cache_dir));
expect(newCacheCount()).toBe(1);
const b = bunRun(join(temp_dir, "a.js"), { ...env, NODE_ENV: "production", HELLO: "5" });
expect(b.stdout == "production 5");
expect(newCacheCount()).toBe(0);
});
});