aboutsummaryrefslogtreecommitdiff
path: root/test/bun.js/log-test.test.ts
blob: 883daa5c4c708618c3666b67255d049912648b19 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { it, expect } from "bun:test";
import { basename, dirname, join } from "path";
import * as fs from "fs";
import { readableStreamToText, spawn } from "bun";

it("should not log .env when quiet", async () => {
  writeDirectoryTree("/tmp/log-test-silent", {
    ".env": "FOO=bar",
    "bunfig.toml": `logLevel = "error"`,
    "index.ts": "export default console.log('Here');",
  });
  const out = spawn({
    cmd: [process.argv[0], "index.ts"],
    stdout: "pipe",
    stderr: "pipe",
    cwd: "/tmp/log-test-silent",
  });

  out.ref();
  await out.exited;
  const text = await readableStreamToText(out.stderr);
  expect(text).toBe("");
});

it("should log .env by default", async () => {
  writeDirectoryTree("/tmp/log-test-silent", {
    ".env": "FOO=bar",
    "bunfig.toml": ``,
    "index.ts": "export default console.log('Here');",
  });

  const out = spawn({
    cmd: [process.argv[0], "index.ts"],
    stdout: "pipe",
    stderr: "pipe",
    cwd: "/tmp/log-test-silent",
  });

  out.ref();
  await out.exited;
  const text = await readableStreamToText(out.stderr);
  expect(text.includes(".env")).toBe(true);
});

function writeDirectoryTree(base, paths) {
  for (const path of Object.keys(paths)) {
    const content = paths[path];
    const joined = join(base, path);

    try {
      fs.unlinkSync(joined);
    } catch (e) {}

    try {
      fs.mkdirSync(join(base, dirname(path)), { recursive: true });
    } catch (e) {}

    fs.writeFileSync(joined, content);
  }
}