aboutsummaryrefslogtreecommitdiff
path: root/packages/bun-internal-test/src/runner.node.mjs
blob: 540c90a379515821680d2fb0b98c279d4bcd232c (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import * as action from "@actions/core";
import { spawnSync } from "child_process";
import { readdirSync } from "node:fs";
import { resolve } from "node:path";
import { StringDecoder } from "node:string_decoder";
import { fileURLToPath } from "url";

const cwd = resolve(fileURLToPath(import.meta.url), "../../../../");
process.chdir(cwd);

const isAction = !!process.env["GITHUB_ACTION"];
const errorPattern = /error: ([\S\s]*?)(?=\n.*?at (\/.*):(\d+):(\d+))/gim;

function* findTests(dir, query) {
  for (const entry of readdirSync(resolve(dir), { encoding: "utf-8", withFileTypes: true })) {
    const path = resolve(dir, entry.name);
    if (entry.isDirectory()) {
      yield* findTests(path, query);
    } else if (entry.isFile() && entry.name.includes(".test.")) {
      yield path;
    }
  }
}

async function runTest(path) {
  const name = path.replace(cwd, "").slice(1);
  const {
    stdout,
    stderr,
    status: exitCode,
  } = spawnSync("bun", ["test", path], {
    stdio: ["ignore", "pipe", "pipe"],
    timeout: 10_000,
    env: {
      ...process.env,
      FORCE_COLOR: "1",
    },
  });

  if (isAction) {
    const prefix = +exitCode === 0 ? "PASS" : `FAIL`;
    action.startGroup(`${prefix} - ${name}`);
  }

  process.stdout.write(stdout);

  if (isAction) {
    findErrors(stdout);
    process.stderr.write(stderr);
    findErrors(stderr);
  } else {
    process.stderr.write(stderr);
    findErrors(stderr);
  }

  if (isAction) {
    action.endGroup();
  }
}

let failed = false;

function findErrors(data) {
  const text = new StringDecoder().write(new Buffer(data.buffer));
  for (const [message, _, path, line, col] of text.matchAll(errorPattern)) {
    failed = true;
    action.error(message, {
      file: path.replace(cwd, "").slice(1),
      startLine: parseInt(line),
      startColumn: parseInt(col),
    });
  }
}

const tests = [];
for (const path of findTests(resolve(cwd, "test/bun.js"))) {
  tests.push(runTest(path).catch(console.error));
}
await Promise.allSettled(tests);
process.exit(failed ? 1 : 0);