Add tests

This commit is contained in:
Colin McDonnell
2023-08-15 18:15:43 -07:00
parent edb6ed62a4
commit 8a3f85c81a
2 changed files with 96 additions and 2 deletions

View 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");
}
});
});

View File

@@ -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,