Skip to content

Commit

Permalink
Prevent double stack trace for assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdz committed Sep 19, 2024
1 parent 7402291 commit 1fde09d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function ASSERT(b, m = '', ...rest) {
console.log(...(rest.length ? rest : ['<assert had no further args>']));
}
console.trace(n + '; ' + rest.join(', '));
throw new Error(`${n}; args: ${JSON.stringify(rest)}`);
throw new Error(`PREVAL ASSERT: ${n}; args: ${JSON.stringify(rest)}`);
}
}
export function ASSERT_LOC(loc) {
Expand Down
18 changes: 15 additions & 3 deletions tests/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
toNormalizedResult, ASSERT,
} from './utils.mjs';
import {
DIM,
VERBOSE_TRACING,
} from '../src/constants.mjs';
import {
Expand Down Expand Up @@ -180,7 +181,11 @@ let fail = 0; // crash
let badNorm = 0; // evaluation of normalized code does not match input
let badFinal = 0; // evaluation of final output does not match input

testCases.forEach((tc, i) => runTestCase({ ...tc, withOutput: testCases.length === 1 && !CONFIG.onlyNormalized }, i));
try {
testCases.forEach((tc, i) => runTestCase({ ...tc, withOutput: testCases.length === 1 && !CONFIG.onlyNormalized }, i));
} catch {
console.log(DIM + 'At least one test crashed hard.' + RESET);
}

if (isMainThread) {
if (CONFIG.fileVerbatim) {
Expand Down Expand Up @@ -1075,8 +1080,15 @@ function runTestCase(
console.log('###################################################', caseIndex + 1, '/', testCases.length, '[', fname, ']');

console.log(`TEST ${RED}FAIL${RESET} Threw an error`);
console.log('(stack should be printed above. uncomment me if not)');
console.log(lastError.stack || lastError);
if (
lastError.message?.includes?.('PREVAL ASSERT:') ||
lastError.includes?.('PREVAL ASSERT:') ||
lastError.stack?.includes('PREVAL ASSERT:')
) {
console.log('(stack should be printed above. uncomment me if not)');
} else {
console.log(lastError.stack || lastError);
}
throw new Error('the test failed...');
}

Expand Down

0 comments on commit 1fde09d

Please sign in to comment.