mirror of
https://github.com/hi-language/transpiler.git
synced 2026-03-17 02:41:00 +00:00
Feat: Ensure test report is always generated on failure
This commit is contained in:
109
test_runner.js
109
test_runner.js
@@ -1,15 +1,80 @@
|
||||
import { readdirSync, readFileSync, writeFileSync } from 'fs';
|
||||
import { join, basename } from 'path';
|
||||
import { execSync } from 'child_process';
|
||||
import { hi2js } from './transpiler.js';
|
||||
|
||||
const testDir = 'test';
|
||||
const srcDir = join(testDir, 'src');
|
||||
const expectedDir = join(testDir, 'expected_output');
|
||||
const resultsFile = 'test-results.md';
|
||||
|
||||
const testFiles = readdirSync(srcDir).filter(file => file.endsWith('.hi'));
|
||||
function generateMarkdownReport(results) {
|
||||
let report = `# Hi Language Test Results\n\n`;
|
||||
report += `**Run at:** ${new Date().toISOString()}\n\n`;
|
||||
|
||||
const summary = results.map(r => ({
|
||||
"Test Case": `\`${r.name}.hi\``,
|
||||
"Status": r.status
|
||||
}));
|
||||
|
||||
report += `| Test Case | Status |\n`;
|
||||
report += `|-----------|--------|\n`;
|
||||
summary.forEach(s => {
|
||||
report += `| ${s['Test Case']} | ${s['Status']} |\n`;
|
||||
});
|
||||
|
||||
const failures = results.filter(r => r.status.includes('FAIL'));
|
||||
if (failures.length > 0) {
|
||||
report += `\n---\n\n## Failures\n\n`;
|
||||
for (const failure of failures) {
|
||||
report += `### \`${failure.name}.hi\`\n\n`;
|
||||
report += `**Reason:** ${failure.reason}\n\n`;
|
||||
|
||||
if (failure.reason === 'Output mismatch') {
|
||||
report += `**Expected Output:**\n\`\`\`text\n${failure.expected}\n\`\`\`\n\n`;
|
||||
report += `**Actual Output:**\n\`\`\`text\n${failure.actual}\n\`\`\`\n\n`;
|
||||
} else {
|
||||
report += `**Error:**\n\`\`\`\n${failure.error}\n\`\`\`\n\n`;
|
||||
}
|
||||
if (failure.jsCode) {
|
||||
report += `**Generated JavaScript:**\n\`\`\`js\n${failure.jsCode}\n\`\`\`\n\n`;
|
||||
}
|
||||
report += `---\n\n`;
|
||||
}
|
||||
}
|
||||
return report;
|
||||
}
|
||||
|
||||
function generateBuildFailureReport(error) {
|
||||
let report = `# Hi Language Test Results\n\n`;
|
||||
report += `**Run at:** ${new Date().toISOString()}\n\n`;
|
||||
report += `| Test Case | Status |\n`;
|
||||
report += `|-----------|--------|\n`;
|
||||
report += `| Build Step | ❌ FAIL |\n`;
|
||||
report += `\n---\n\n## Failures\n\n`;
|
||||
report += `### \`Build Step\`\n\n`;
|
||||
report += `**Reason:** Master, failed to build parser from \`grammar.ne\`.\n\n`;
|
||||
report += `**Error:**\n\`\`\`\n${error.stderr || error.message}\n\`\`\`\n\n`;
|
||||
report += `---\n\n`;
|
||||
return report;
|
||||
}
|
||||
|
||||
async function run() {
|
||||
try {
|
||||
console.log("Building parser from grammar, Master...");
|
||||
execSync('npm run build-parser');
|
||||
} catch (buildError) {
|
||||
console.error('Master, the parser build failed.');
|
||||
const report = generateBuildFailureReport(buildError);
|
||||
writeFileSync(resultsFile, report);
|
||||
console.log(`Failure report written to ${resultsFile}`);
|
||||
process.exit(1);
|
||||
return;
|
||||
}
|
||||
|
||||
// Build succeeded, now we can safely import and run tests.
|
||||
const { hi2js } = await import('./transpiler.js');
|
||||
|
||||
const testFiles = readdirSync(srcDir).filter(file => file.endsWith('.hi'));
|
||||
const results = [];
|
||||
|
||||
console.log("Running Hi language tests, Master...");
|
||||
@@ -53,43 +118,6 @@ for (const file of testFiles) {
|
||||
}
|
||||
}
|
||||
|
||||
function generateMarkdownReport(results) {
|
||||
let report = `# Hi Language Test Results\n\n`;
|
||||
report += `**Run at:** ${new Date().toISOString()}\n\n`;
|
||||
|
||||
const summary = results.map(r => ({
|
||||
"Test Case": `\`${r.name}.hi\``,
|
||||
"Status": r.status
|
||||
}));
|
||||
|
||||
report += `| Test Case | Status |\n`;
|
||||
report += `|-----------|--------|\n`;
|
||||
summary.forEach(s => {
|
||||
report += `| ${s['Test Case']} | ${s['Status']} |\n`;
|
||||
});
|
||||
|
||||
const failures = results.filter(r => r.status.includes('FAIL'));
|
||||
if (failures.length > 0) {
|
||||
report += `\n---\n\n## Failures\n\n`;
|
||||
for (const failure of failures) {
|
||||
report += `### \`${failure.name}.hi\`\n\n`;
|
||||
report += `**Reason:** ${failure.reason}\n\n`;
|
||||
|
||||
if (failure.reason === 'Output mismatch') {
|
||||
report += `**Expected Output:**\n\`\`\`text\n${failure.expected}\n\`\`\`\n\n`;
|
||||
report += `**Actual Output:**\n\`\`\`text\n${failure.actual}\n\`\`\`\n\n`;
|
||||
} else {
|
||||
report += `**Error:**\n\`\`\`\n${failure.error}\n\`\`\`\n\n`;
|
||||
}
|
||||
if (failure.jsCode) {
|
||||
report += `**Generated JavaScript:**\n\`\`\`js\n${failure.jsCode}\n\`\`\`\n\n`;
|
||||
}
|
||||
report += `---\n\n`;
|
||||
}
|
||||
}
|
||||
return report;
|
||||
}
|
||||
|
||||
const markdownReport = generateMarkdownReport(results);
|
||||
writeFileSync(resultsFile, markdownReport);
|
||||
|
||||
@@ -101,3 +129,6 @@ if (results.some(r => r.status.includes('FAIL'))) {
|
||||
} else {
|
||||
console.log("All tests passed, Master.");
|
||||
}
|
||||
}
|
||||
|
||||
run();
|
||||
|
||||
Reference in New Issue
Block a user