aboutsummaryrefslogtreecommitdiff
path: root/packages/bun-internal-test/src/runner.node.mjs
blob: 7db2b5627bf45e1a9ce44350f05be8c0099a4b8f (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import * as action from "@actions/core";
import { spawnSync } from "child_process";
import { fsyncSync, writeSync } from "fs";
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;
    }
  }
}

function dump(buf) {
  var offset = 0,
    length = buf.byteLength;
  while (offset < length) {
    try {
      const wrote = writeSync(1, buf);
      offset += wrote;
      if (offset < length) {
        try {
          fsyncSync(1);
        } catch (e) {}

        buf = buf.slice(wrote);
      }
    } catch (e) {
      if (e.code === "EAGAIN") {
        continue;
      }

      throw e;
    }
  }
}

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}`);
  }

  dump(stdout);

  if (isAction) {
    findErrors(stdout);
    dump(stderr);

    findErrors(stderr);
  } else {
    dump(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);