aboutsummaryrefslogtreecommitdiff
path: root/packages/bun-internal-test/src/runner.node.mjs
blob: f540b424f172b404c09c08bd0cbee46412f62703 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import * as action from "@actions/core";
import { spawnSync } from "child_process";
import { fsyncSync, rmSync, writeFileSync, writeSync } from "fs";
import { readdirSync } from "node:fs";
import { resolve } from "node:path";
import { StringDecoder } from "node:string_decoder";
import { relative } from "path";
import { fileURLToPath } from "url";

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

const isAction = !!process.env["GITHUB_ACTION"];

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.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;
    }
  }
}

var failingTests = [];

async function runTest(path) {
  const name = path.replace(cwd, "").slice(1);
  try {
    var {
      stdout,
      stderr,
      status: exitCode,
      error: timedOut,
    } = spawnSync("bun", ["test", path], {
      stdio: ["ignore", "pipe", "pipe"],
      timeout: 1000 * 60 * 3,
      env: {
        ...process.env,
        FORCE_COLOR: "1",
      },
    });
  } catch (e) {
    console.error(e);
  }

  const passed = exitCode === 0 && !timedOut;

  if (!passed) {
    failingTests.push(name);
    if (timedOut) console.error(timedOut);
  }

  if (isAction && !passed) {
    findErrors(stdout);
    findErrors(stderr);
  }

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

  stdout && stdout?.byteLength && dump(stdout);
  stderr && stderr?.byteLength && dump(stderr);

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

function findErrors(data) {
  const text = new StringDecoder().write(new Buffer(data.buffer)).replaceAll(/\u001b\[.*?m/g, "");
  let index = 0;
  do {
    index = text.indexOf("error: ", index);
    if (index === -1) {
      break;
    }

    const messageEnd = text.indexOf("\n", index);
    if (messageEnd === -1) {
      break;
    }
    const message = text.slice(index + 7, messageEnd);
    index = text.indexOf("at ", index);
    if (index === -1) {
      break;
    }
    const startAt = index;
    index = text.indexOf("\n", index);
    if (index === -1) {
      break;
    }
    const at = text.slice(startAt + 3, index);
    let file = at.slice(0, at.indexOf(":"));
    if (file.length === 0) {
      continue;
    }

    const startLine = at.slice(at.indexOf(":") + 1, at.indexOf(":") + 1 + at.slice(at.indexOf(":") + 1).indexOf(":"));
    const startColumn = at.slice(at.indexOf(":") + 1 + at.slice(at.indexOf(":") + 1).indexOf(":") + 1);

    if (file.startsWith("/")) {
      file = relative(cwd, file);
    }

    action.error(message, { file, startLine, startColumn });
  } while (index !== -1);
}
var tests = [];
var testFileNames = [];
for (const path of findTests(resolve(cwd, "test"))) {
  testFileNames.push(path);
  tests.push(runTest(path).catch(console.error));
}
await Promise.allSettled(tests);

rmSync("failing-tests.txt", { force: true });

if (isAction) {
  if (failingTests.length > 0) {
    action.setFailed(`${failingTests.length} files with failing tests`);
  }
  action.setOutput("failing_tests", failingTests.map(a => `- \`${a}\``).join("\n"));
  action.setOutput("failing_tests_count", failingTests.length);
  action.summary.addHeading(`${tests.length} files with tests ran`).addList(testFileNames);
  await action.summary.write();
} else {
  writeFileSync("failing-tests.txt", failingTests.join("\n"));
}

process.exit(failingTests.length);