mirror of
https://github.com/hi-language/transpiler.git
synced 2026-01-14 08:38:37 +00:00
Fix: Implement global try/catch to log all errors
This commit is contained in:
144
test_runner.js
144
test_runner.js
@@ -44,90 +44,98 @@ function generateMarkdownReport(results) {
|
|||||||
return report;
|
return report;
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateBuildFailureReport(error) {
|
function generateCrashReport(error, step) {
|
||||||
let report = `# Hi Language Test Results\n\n`;
|
let report = `# Hi Language Test Results\n\n`;
|
||||||
report += `**Run at:** ${new Date().toISOString()}\n\n`;
|
report += `**Run at:** ${new Date().toISOString()}\n\n`;
|
||||||
report += `| Test Case | Status |\n`;
|
report += `| Test Case | Status |\n`;
|
||||||
report += `|-----------|--------|\n`;
|
report += `|-----------|--------|\n`;
|
||||||
report += `| Build Step | ❌ FAIL |\n`;
|
report += `| ${step} | ❌ FAIL |\n`;
|
||||||
report += `\n---\n\n## Failures\n\n`;
|
report += `\n---\n\n## Failures\n\n`;
|
||||||
report += `### \`Build Step\`\n\n`;
|
report += `### \`${step}\`\n\n`;
|
||||||
report += `**Reason:** Master, failed to build parser from \`grammar.ne\`.\n\n`;
|
report += `**Reason:** Master, the test runner encountered a fatal error.\n\n`;
|
||||||
report += `**Error:**\n\`\`\`\n${error.stderr || error.message}\n\`\`\`\n\n`;
|
report += `**Error:**\n\`\`\`\n${error.stack || error.message}\n\`\`\`\n\n`;
|
||||||
report += `---\n\n`;
|
report += `---\n\n`;
|
||||||
return report;
|
return report;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function run() {
|
async function run() {
|
||||||
try {
|
try {
|
||||||
console.log("Building parser from grammar, Master...");
|
// 1. Build Step
|
||||||
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...");
|
|
||||||
|
|
||||||
for (const file of testFiles) {
|
|
||||||
const testCaseName = basename(file, '.hi');
|
|
||||||
const hiFilePath = join(srcDir, file);
|
|
||||||
const expectedOutputPath = join(expectedDir, `${testCaseName}.txt`);
|
|
||||||
|
|
||||||
const hiCode = readFileSync(hiFilePath, 'utf-8');
|
|
||||||
const expectedOutput = readFileSync(expectedOutputPath, 'utf-8').trim();
|
|
||||||
|
|
||||||
let jsCode = '';
|
|
||||||
try {
|
try {
|
||||||
jsCode = hi2js(hiCode);
|
console.log("Building parser from grammar, Master...");
|
||||||
const actualOutput = execSync('node', {
|
execSync('npm run build-parser');
|
||||||
input: jsCode,
|
} catch (buildError) {
|
||||||
encoding: 'utf-8'
|
throw { step: 'Build Step', error: buildError.stderr || buildError.message };
|
||||||
}).trim();
|
}
|
||||||
|
|
||||||
if (actualOutput === expectedOutput) {
|
// 2. Transpiler Import & Test Execution
|
||||||
results.push({ name: testCaseName, status: '✅ PASS' });
|
const { hi2js } = await import('./transpiler.js');
|
||||||
} else {
|
|
||||||
|
const testFiles = readdirSync(srcDir).filter(file => file.endsWith('.hi'));
|
||||||
|
const results = [];
|
||||||
|
|
||||||
|
console.log("Running Hi language tests, Master...");
|
||||||
|
|
||||||
|
for (const file of testFiles) {
|
||||||
|
const testCaseName = basename(file, '.hi');
|
||||||
|
const hiFilePath = join(srcDir, file);
|
||||||
|
const expectedOutputPath = join(expectedDir, `${testCaseName}.txt`);
|
||||||
|
|
||||||
|
const hiCode = readFileSync(hiFilePath, 'utf-8');
|
||||||
|
const expectedOutput = readFileSync(expectedOutputPath, 'utf-8').trim();
|
||||||
|
|
||||||
|
let jsCode = '';
|
||||||
|
try {
|
||||||
|
jsCode = hi2js(hiCode);
|
||||||
|
const actualOutput = execSync('node', {
|
||||||
|
input: jsCode,
|
||||||
|
encoding: 'utf-8'
|
||||||
|
}).trim();
|
||||||
|
|
||||||
|
if (actualOutput === expectedOutput) {
|
||||||
|
results.push({ name: testCaseName, status: '✅ PASS' });
|
||||||
|
} else {
|
||||||
|
results.push({
|
||||||
|
name: testCaseName,
|
||||||
|
status: '❌ FAIL',
|
||||||
|
reason: 'Output mismatch',
|
||||||
|
expected: expectedOutput,
|
||||||
|
actual: actualOutput,
|
||||||
|
jsCode: jsCode
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
results.push({
|
results.push({
|
||||||
name: testCaseName,
|
name: testCaseName,
|
||||||
status: '❌ FAIL',
|
status: '❌ FAIL',
|
||||||
reason: 'Output mismatch',
|
reason: 'Transpilation or execution error',
|
||||||
expected: expectedOutput,
|
error: error.message,
|
||||||
actual: actualOutput,
|
|
||||||
jsCode: jsCode
|
jsCode: jsCode
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (error) {
|
|
||||||
results.push({
|
|
||||||
name: testCaseName,
|
|
||||||
status: '❌ FAIL',
|
|
||||||
reason: 'Transpilation or execution error',
|
|
||||||
error: error.message,
|
|
||||||
jsCode: jsCode
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const markdownReport = generateMarkdownReport(results);
|
const markdownReport = generateMarkdownReport(results);
|
||||||
writeFileSync(resultsFile, markdownReport);
|
writeFileSync(resultsFile, markdownReport);
|
||||||
|
|
||||||
console.log(`Test run complete. Results written to ${resultsFile}`);
|
console.log(`Test run complete. Results written to ${resultsFile}`);
|
||||||
|
|
||||||
if (results.some(r => r.status.includes('FAIL'))) {
|
if (results.some(r => r.status.includes('FAIL'))) {
|
||||||
console.log("Some tests failed, Master.");
|
console.log("Some tests failed, Master.");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
} else {
|
} else {
|
||||||
console.log("All tests passed, Master.");
|
console.log("All tests passed, Master.");
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// This is the global catch block for any fatal error.
|
||||||
|
const step = e.step || 'Initialization Step';
|
||||||
|
const error = e.error || e;
|
||||||
|
console.error(`Master, a fatal error occurred during the ${step}.`);
|
||||||
|
console.error(error);
|
||||||
|
const report = generateCrashReport(error, step);
|
||||||
|
writeFileSync(resultsFile, report);
|
||||||
|
console.log(`Failure report written to ${resultsFile}`);
|
||||||
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user