aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Colin McDonnell <colinmcd94@gmail.com> 2023-08-15 18:15:43 -0700
committerGravatar Colin McDonnell <colinmcd94@gmail.com> 2023-08-21 17:49:58 -0700
commit8a3f85c81a0db9bae40ed366263dbf997304f9a8 (patch)
tree35e4a3138522cd1b84182ff9260f10ef5f9fda1e
parentedb6ed62a479adfa12defb5c52cb0693aa2a5cfe (diff)
downloadbun-8a3f85c81a0db9bae40ed366263dbf997304f9a8.tar.gz
bun-8a3f85c81a0db9bae40ed366263dbf997304f9a8.tar.zst
bun-8a3f85c81a0db9bae40ed366263dbf997304f9a8.zip
Add tests
-rw-r--r--test/cli/run/bunfig.test.ts88
-rw-r--r--test/harness.ts10
2 files changed, 96 insertions, 2 deletions
diff --git a/test/cli/run/bunfig.test.ts b/test/cli/run/bunfig.test.ts
new file mode 100644
index 000000000..2c0a0d1fb
--- /dev/null
+++ b/test/cli/run/bunfig.test.ts
@@ -0,0 +1,88 @@
+import { describe, expect, test } from "bun:test";
+import { bunRun, tempDirWithFiles } from "harness";
+
+describe("config", () => {
+ test("read bun.json", () => {
+ {
+ const dir = tempDirWithFiles("dotenv", {
+ "bun.json": `{"define": { "caterpillar": "'butterfly'" }}`,
+ "index.ts": "console.log(caterpillar);",
+ });
+ const { stdout } = bunRun(`${dir}/index.ts`);
+ // should be "a\n but console.log adds a newline
+ expect(stdout).toBe("butterfly");
+ }
+ });
+
+ test("read bunfig.toml", () => {
+ {
+ const dir = tempDirWithFiles("dotenv", {
+ "bunfig.toml": `[define]\n"caterpillar" = "'butterfly'"`,
+ "index.ts": "console.log(caterpillar);",
+ });
+ const { stdout } = bunRun(`${dir}/index.ts`);
+ // should be "a\n but console.log adds a newline
+ expect(stdout).toBe("butterfly");
+ }
+ });
+
+ test("ignore bunfig.toml if bun.json is found", () => {
+ {
+ const dir = tempDirWithFiles("dotenv", {
+ "bun.json": `{"define": { "caterpillar": "'correct'" }}`,
+ "bunfig.toml": `[define]\n"caterpillar" = "'wrong'"`,
+ "index.ts": "console.log(caterpillar);",
+ });
+ const { stdout } = bunRun(`${dir}/index.ts`);
+ // should be "a\n but console.log adds a newline
+ expect(stdout).toBe("correct");
+ }
+ });
+
+ test("read --config *.json", () => {
+ {
+ const dir = tempDirWithFiles("dotenv", {
+ "bun2.json": `{"define": { "caterpillar": "'butterfly'" }}`,
+ "index.ts": "console.log(caterpillar);",
+ });
+ const { stdout } = bunRun(`${dir}/index.ts`, {}, { flags: ["--config", "bun2.json"] });
+ // should be "a\n but console.log adds a newline
+ expect(stdout).toBe("butterfly");
+ }
+ });
+
+ test("read -c *.json", () => {
+ {
+ const dir = tempDirWithFiles("dotenv", {
+ "bun2.json": `{"define": { "caterpillar": "'butterfly'" }}`,
+ "index.ts": "console.log(caterpillar);",
+ });
+ const { stdout } = bunRun(`${dir}/index.ts`, {}, { flags: ["-c", "bun2.json"] });
+ // should be "a\n but console.log adds a newline
+ expect(stdout).toBe("butterfly");
+ }
+ });
+
+ test("read --config *.toml", () => {
+ {
+ const dir = tempDirWithFiles("dotenv", {
+ "bun2.toml": `[define]\n"caterpillar" = "'butterfly'"`,
+ "index.ts": "console.log(caterpillar);",
+ });
+ const { stdout } = bunRun(`${dir}/index.ts`, {}, { flags: ["--config", "bun2.toml"] });
+ // should be "a\n but console.log adds a newline
+ expect(stdout).toBe("butterfly");
+ }
+ });
+ test("read -c *.toml", () => {
+ {
+ const dir = tempDirWithFiles("dotenv", {
+ "bun2.toml": `[define]\n"caterpillar" = "'butterfly'"`,
+ "index.ts": "console.log(caterpillar);",
+ });
+ const { stdout } = bunRun(`${dir}/index.ts`, {}, { flags: ["-c", "bun2.toml"] });
+ // should be "a\n but console.log adds a newline
+ expect(stdout).toBe("butterfly");
+ }
+ });
+});
diff --git a/test/harness.ts b/test/harness.ts
index 2da12e094..9643aff3d 100644
--- a/test/harness.ts
+++ b/test/harness.ts
@@ -101,9 +101,15 @@ export function tempDirWithFiles(basename: string, files: Record<string, string
return dir;
}
-export function bunRun(file: string, env?: Record<string, string>) {
+export function bunRun(
+ file: string,
+ env?: Record<string, string>,
+ params?: {
+ flags?: string[];
+ },
+) {
var path = require("path");
- const result = Bun.spawnSync([bunExe(), file], {
+ const result = Bun.spawnSync([bunExe(), ...(params?.flags ?? []), file], {
cwd: path.dirname(file),
env: {
...bunEnv,