mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
Add tests
This commit is contained in:
88
test/cli/run/bunfig.test.ts
Normal file
88
test/cli/run/bunfig.test.ts
Normal file
@@ -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");
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user