diff options
author | 2023-08-01 23:04:05 -0300 | |
---|---|---|
committer | 2023-08-01 19:04:05 -0700 | |
commit | 9cb3f3386e24d52d1c6a5949278ab613ba2624e3 (patch) | |
tree | 306ebc3a7a72d1d7d50bbfcb8e36af8164888401 /test | |
parent | 6c40d6f2f51474c6a38d509417db903385f48f2b (diff) | |
download | bun-9cb3f3386e24d52d1c6a5949278ab613ba2624e3.tar.gz bun-9cb3f3386e24d52d1c6a5949278ab613ba2624e3.tar.zst bun-9cb3f3386e24d52d1c6a5949278ab613ba2624e3.zip |
Fix expanding on .env files (#3918)
* fix expanding
* refactor locks
* bound checks
* fix comment
* one more test
* oops
Diffstat (limited to 'test')
-rw-r--r-- | test/cli/run/env.test.ts | 68 |
1 files changed, 67 insertions, 1 deletions
diff --git a/test/cli/run/env.test.ts b/test/cli/run/env.test.ts index 6e4d83d44..159793e27 100644 --- a/test/cli/run/env.test.ts +++ b/test/cli/run/env.test.ts @@ -1,5 +1,22 @@ import { describe, expect, test } from "bun:test"; -import { bunRun, bunTest, tempDirWithFiles } from "harness"; +import { bunRun, bunTest, tempDirWithFiles, bunExe, bunEnv } from "harness"; +import path from "path"; + +function bunRunWithoutTrim(file: string, env?: Record<string, string>) { + const result = Bun.spawnSync([bunExe(), file], { + cwd: path.dirname(file), + env: { + ...bunEnv, + NODE_ENV: undefined, + ...env, + }, + }); + if (!result.success) throw new Error(result.stderr.toString("utf8")); + return { + stdout: result.stdout.toString("utf8"), + stderr: result.stderr.toString("utf8").trim(), + }; +} describe(".env file is loaded", () => { test(".env", () => { @@ -338,3 +355,52 @@ test(".env in a folder doesn't throw an error", () => { const { stdout } = bunRun(`${dir}/index.ts`); expect(stdout).toBe("hey"); }); + +test("#3911", () => { + const dir = tempDirWithFiles("dotenv", { + ".env": 'KEY="a\\nb"', + "index.ts": "console.log(process.env.KEY);", + }); + const { stdout } = bunRun(`${dir}/index.ts`); + expect(stdout).toBe("a\nb"); +}); + +describe("boundary tests", () => { + test("src boundary", () => { + const dir = tempDirWithFiles("dotenv", { + ".env": 'KEY="a\\n"', + "index.ts": "console.log(process.env.KEY);", + }); + const { stdout } = bunRunWithoutTrim(`${dir}/index.ts`); + // should be "a\n" but console.log adds a newline + expect(stdout).toBe("a\n\n"); + + const dir2 = tempDirWithFiles("dotenv", { + ".env": 'KEY="a\\n', + "index.ts": "console.log(process.env.KEY);", + }); + const { stdout: stdout2 } = bunRunWithoutTrim(`${dir2}/index.ts`); + // should be "a\n but console.log adds a newline + expect(stdout2).toBe('"a\n\n'); + }); + + test("buffer boundary", () => { + const expected = "a".repeat(4094); + let content = expected + "a"; + const dir = tempDirWithFiles("dotenv", { + ".env": `KEY="${content}"`, + "index.ts": "console.log(process.env.KEY);", + }); + const { stdout } = bunRun(`${dir}/index.ts`); + + content = expected + "\\n"; + const dir2 = tempDirWithFiles("dotenv", { + ".env": `KEY="${content}"`, + "index.ts": "console.log(process.env.KEY);", + }); + const { stdout: stdout2 } = bunRun(`${dir2}/index.ts`); + // should be truncated + expect(stdout).toBe(expected); + expect(stdout2).toBe(expected); + }); +}); |