Files
hash-map/scripts/test-tap-teamcity.ts

100 lines
2.6 KiB
TypeScript
Executable File

#!/usr/bin/env bun
import { TeamcityReporter } from "../tests/TeamcityReporter.ts";
const isTeamCity = process.env.TEAMCITY_VERSION !== undefined;
async function convertTAPToTeamCity() {
if (!isTeamCity) {
// Just pass through if not in TeamCity
const proc = Bun.spawn(["bun", "test", ...process.argv.slice(2)], {
stdout: "inherit",
stderr: "inherit",
});
const exitCode = await proc.exited;
process.exit(exitCode);
return;
}
const proc = Bun.spawn(["bun", "test", "--reporter=tap", ...process.argv.slice(2)], {
stdout: "pipe",
stderr: "pipe",
});
const decoder = new TextDecoder();
let currentSuite = "Tests";
let buffer = "";
let testCount = 0;
TeamcityReporter.reportSuiteStart(currentSuite);
// Read TAP output
for await (const chunk of proc.stdout) {
const text = decoder.decode(chunk);
buffer += text;
const lines = buffer.split("\n");
buffer = lines.pop() || "";
for (const line of lines) {
console.log(line); // Pass through original output
// Parse TAP format
// ok 1 - test description
// not ok 2 - failed test
// # describe block
if (line.startsWith("# ")) {
// Suite/describe block
const suiteName = line.substring(2).trim();
if (suiteName && !suiteName.startsWith("tests") && !suiteName.startsWith("pass")) {
if (testCount > 0) {
TeamcityReporter.reportSuiteEnd(currentSuite);
}
currentSuite = suiteName;
testCount = 0;
TeamcityReporter.reportSuiteStart(currentSuite);
}
} else if (line.match(/^(not )?ok \d+/)) {
// Test result
const match = line.match(/^(not )?ok (\d+)\s*-?\s*(.*)$/);
if (match) {
const [, not, id, description] = match;
const testName = description.trim() || `Test ${id}`;
const fullName = `${currentSuite} > ${testName}`;
testCount++;
TeamcityReporter.reportTestStart(fullName);
if (not) {
// Test failed
TeamcityReporter.reportTestFailed(
fullName,
"Test failed",
line,
{ comparisonFailure: false, expected: "", actual: "" }
);
}
TeamcityReporter.reportTestEnd(fullName);
}
}
}
}
// Process remaining buffer
if (buffer) {
console.log(buffer);
}
const exitCode = await proc.exited;
TeamcityReporter.reportSuiteEnd(currentSuite);
process.exit(exitCode);
}
convertTAPToTeamCity().catch((error) => {
console.error("Error:", error);
process.exit(1);
});