aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorGravatar Alex Lam S.L <alexlamsl@gmail.com> 2023-06-23 03:05:54 +0300
committerGravatar GitHub <noreply@github.com> 2023-06-22 17:05:54 -0700
commitca1fe3c602a19878e8cd3545494d6b5af7ed13c9 (patch)
treef747af80f504d177e486fd765446f1c8f5ff7bc4 /test
parent4a13a53058eca1c3d96847277878a19192dd2a34 (diff)
downloadbun-ca1fe3c602a19878e8cd3545494d6b5af7ed13c9.tar.gz
bun-ca1fe3c602a19878e8cd3545494d6b5af7ed13c9.tar.zst
bun-ca1fe3c602a19878e8cd3545494d6b5af7ed13c9.zip
revamp dotEnv parser (#3347)
- fixes `strings.indexOfAny()` - fixes OOB array access fixes #411 fixes #2823 fixes #3042
Diffstat (limited to 'test')
-rw-r--r--test/cli/install/bunx.test.ts16
-rw-r--r--test/cli/run/env.test.ts82
2 files changed, 86 insertions, 12 deletions
diff --git a/test/cli/install/bunx.test.ts b/test/cli/install/bunx.test.ts
index 87ad2f8b4..3605f5b6b 100644
--- a/test/cli/install/bunx.test.ts
+++ b/test/cli/install/bunx.test.ts
@@ -1,7 +1,6 @@
-import { spawn } from "bun";
+import { file, spawn } from "bun";
import { afterEach, beforeEach, expect, it } from "bun:test";
import { bunExe, bunEnv as env } from "harness";
-import { realpathSync } from "fs";
import { mkdtemp, realpath, rm, writeFile } from "fs/promises";
import { tmpdir } from "os";
import { join } from "path";
@@ -10,7 +9,7 @@ import { readdirSorted } from "./dummy.registry";
let x_dir: string;
beforeEach(async () => {
- x_dir = realpathSync(await mkdtemp(join(tmpdir(), "bun-x.test")));
+ x_dir = await realpath(await mkdtemp(join(tmpdir(), "bun-x.test")));
});
afterEach(async () => {
await rm(x_dir, { force: true, recursive: true });
@@ -167,6 +166,16 @@ for (const entry of await decompress(Buffer.from(buffer))) {
expect(stderr).toBeDefined();
const err = await new Response(stderr).text();
expect(err).toBe("");
+ expect(await readdirSorted(x_dir)).toEqual([".cache", "test.js"]);
+ expect(await readdirSorted(join(x_dir, ".cache"))).toContain("decompress");
+ expect(await readdirSorted(join(x_dir, ".cache", "decompress"))).toEqual(["4.2.1"]);
+ expect(await readdirSorted(join(x_dir, ".cache", "decompress", "4.2.1"))).toEqual([
+ "index.js",
+ "license",
+ "package.json",
+ "readme.md",
+ ]);
+ expect(await file(join(x_dir, ".cache", "decompress", "4.2.1", "index.js")).text()).toContain("\nmodule.exports = ");
expect(stdout).toBeDefined();
const out = await new Response(stdout).text();
expect(out.split(/\r?\n/)).toEqual([
@@ -176,7 +185,6 @@ for (const entry of await decompress(Buffer.from(buffer))) {
"",
]);
expect(await exited).toBe(0);
- expect(await readdirSorted(x_dir)).toEqual([".cache", "test.js"]);
});
it("should execute from current working directory", async () => {
diff --git a/test/cli/run/env.test.ts b/test/cli/run/env.test.ts
index ddc316f05..0cab610a5 100644
--- a/test/cli/run/env.test.ts
+++ b/test/cli/run/env.test.ts
@@ -194,6 +194,51 @@ describe("dotenv priority", () => {
});
});
+test(".env colon assign", () => {
+ const dir = tempDirWithFiles("dotenv-colon", {
+ ".env": "FOO: foo",
+ "index.ts": "console.log(process.env.FOO);",
+ });
+ const { stdout } = bunRun(`${dir}/index.ts`);
+ expect(stdout).toBe("foo");
+});
+
+test(".env export assign", () => {
+ const dir = tempDirWithFiles("dotenv-export", {
+ ".env": "export FOO = foo\nexport = bar",
+ "index.ts": "console.log(process.env.FOO, process.env.export);",
+ });
+ const { stdout } = bunRun(`${dir}/index.ts`);
+ expect(stdout).toBe("foo bar");
+});
+
+test(".env value expansion", () => {
+ const dir = tempDirWithFiles("dotenv-expand", {
+ ".env": "FOO=foo\nBAR=$FOO bar\nMOO=${FOO} ${BAR:-fail} ${MOZ:-moo}",
+ "index.ts": "console.log([process.env.FOO, process.env.BAR, process.env.MOO].join('|'));",
+ });
+ const { stdout } = bunRun(`${dir}/index.ts`);
+ expect(stdout).toBe("foo|foo bar|foo foo bar moo");
+});
+
+test(".env comments", () => {
+ const dir = tempDirWithFiles("dotenv-comments", {
+ ".env": "#FOZ\nFOO = foo#FAIL\nBAR='bar' #BAZ",
+ "index.ts": "console.log(process.env.FOO, process.env.BAR);",
+ });
+ const { stdout } = bunRun(`${dir}/index.ts`);
+ expect(stdout).toBe("foo bar");
+});
+
+test(".env escaped dollar sign", () => {
+ const dir = tempDirWithFiles("dotenv-dollar", {
+ ".env": "FOO=foo\nBAR=\\$FOO",
+ "index.ts": "console.log(process.env.FOO, process.env.BAR);",
+ });
+ const { stdout } = bunRun(`${dir}/index.ts`);
+ expect(stdout).toBe("foo $FOO");
+});
+
test(".env doesnt crash with 159 bytes", () => {
const dir = tempDirWithFiles("dotenv-159", {
".env":
@@ -217,28 +262,49 @@ test(".env doesnt crash with 159 bytes", () => {
);
});
-test.todo(".env space edgecase (issue #411)", () => {
+test(".env with >768 entries", () => {
+ const dir = tempDirWithFiles("dotenv-many-entries", {
+ ".env": new Array(2000)
+ .fill(null)
+ .map((_, i) => `TEST_VAR${i}=TEST_VAL${i}`)
+ .join("\n"),
+ "index.ts": "console.log(process.env.TEST_VAR47);",
+ });
+ const { stdout } = bunRun(`${dir}/index.ts`);
+ expect(stdout).toBe("TEST_VAL47");
+});
+
+test(".env space edgecase (issue #411)", () => {
const dir = tempDirWithFiles("dotenv-issue-411", {
".env": "VARNAME=A B",
- "index.ts": "console.log('[' + process.env.VARNAME + ']'); ",
+ "index.ts": "console.log('[' + process.env.VARNAME + ']');",
});
const { stdout } = bunRun(`${dir}/index.ts`);
expect(stdout).toBe("[A B]");
});
-test.todo(".env special characters 1 (issue #2823)", () => {
- const dir = tempDirWithFiles("dotenv-issue-411", {
- ".env": 'A="a$t"\n',
- "index.ts": "console.log('[' + process.env.A + ']'); ",
+test(".env special characters 1 (issue #2823)", () => {
+ const dir = tempDirWithFiles("dotenv-issue-2823", {
+ ".env": 'A="a$t"\nC=`c\\$v`',
+ "index.ts": "console.log('[' + process.env.A + ']', '[' + process.env.C + ']');",
});
const { stdout } = bunRun(`${dir}/index.ts`);
- expect(stdout).toBe("[a$t]");
+ expect(stdout).toBe("[a] [c$v]");
});
test.todo("env escaped quote (issue #2484)", () => {
- const dir = tempDirWithFiles("dotenv-issue-411", {
+ const dir = tempDirWithFiles("env-issue-2484", {
"index.ts": "console.log(process.env.VALUE, process.env.VALUE2);",
});
const { stdout } = bunRun(`${dir}/index.ts`, { VALUE: `\\"`, VALUE2: `\\\\"` });
expect(stdout).toBe('\\" \\\\"');
});
+
+test(".env Windows-style newline (issue #3042)", () => {
+ const dir = tempDirWithFiles("dotenv-issue-3042", {
+ ".env": "FOO=\rBAR='bar\r\rbaz'\r\nMOO=moo\r",
+ "index.ts": "console.log([process.env.FOO, process.env.BAR, process.env.MOO].join('|'));",
+ });
+ const { stdout } = bunRun(`${dir}/index.ts`);
+ expect(stdout).toBe("|bar\n\nbaz|moo");
+});