Skip to content

Commit b796fef

Browse files
committed
fix
1 parent 0d78b99 commit b796fef

File tree

4 files changed

+42
-30
lines changed

4 files changed

+42
-30
lines changed

src/executionResult.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,24 +96,28 @@ export class ExecutionResultSummary {
9696
this.total.toString();
9797
log(`\ntest case: ${rate} (success/total)\n`);
9898
if (this.fail !== 0) {
99-
log(chalk.red("Error Message: "));
100-
// sort failedInfos by testcaseName to keep stability for e2e testing
101-
const failedInfosArray = Array.from(this.failedInfos.entries()).sort((a, b) => a[0].localeCompare(b[0]));
102-
for (const [testcaseName, { hasCrash, assertMessages, logMessages }] of failedInfosArray) {
103-
log(` ${testcaseName}: `);
104-
for (const assertMessage of assertMessages) {
105-
log(" " + chalk.yellow(assertMessage));
106-
}
107-
if (hasCrash) {
108-
log(" " + chalk.red("Test Crashed!"));
109-
}
110-
for (const logMessage of logMessages ?? []) {
111-
log(chalk.gray(logMessage));
112-
}
113-
if (logMessages.length > 0) {
114-
// empty line to separate test
115-
log("");
116-
}
99+
this.#printErrorMessage(log);
100+
}
101+
}
102+
103+
#printErrorMessage(log: (msg: string) => void): void {
104+
log(chalk.red("Error Message: "));
105+
// sort failedInfos by testcaseName to keep stability for e2e testing
106+
const failedInfosArray = Array.from(this.failedInfos.entries()).sort((a, b) => a[0].localeCompare(b[0]));
107+
for (const [testcaseName, { hasCrash, assertMessages, logMessages }] of failedInfosArray) {
108+
log(` ${testcaseName}: `);
109+
for (const assertMessage of assertMessages) {
110+
log(" " + chalk.yellow(assertMessage));
111+
}
112+
if (hasCrash) {
113+
log(" " + chalk.red("Test Crashed!"));
114+
}
115+
for (const logMessage of logMessages ?? []) {
116+
log(chalk.gray(logMessage));
117+
}
118+
if (logMessages.length > 0) {
119+
// empty line to separate test
120+
log("");
117121
}
118122
}
119123
}

src/utils/errorTraceHandler.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { readFile } from "node:fs/promises";
44
import { parseSourceMapPath } from "./wasmparser.js";
55
import { BasicSourceMapConsumer, IndexedSourceMapConsumer, SourceMapConsumer } from "source-map";
6+
import chalk from "chalk";
67

78
export interface WebAssemblyCallSite {
89
functionName: string;
@@ -47,7 +48,7 @@ function getOriginLocationWithSourceMap(
4748
}
4849

4950
function getWebAssemblyFunctionName(callSite: NodeJS.CallSite): string {
50-
return callSite.getFunctionName() ?? `wasm-function[${callSite.getFunction()}]`;
51+
return callSite.getFunctionName() ?? `wasm-function[${callSite.getFunction()?.toString() ?? "unknown"}]`;
5152
}
5253

5354
function createWebAssemblyCallSite(
@@ -87,15 +88,24 @@ export interface ExecutionError {
8788
stacks: WebAssemblyCallSite[];
8889
}
8990

90-
async function getSourceMapConsumer(sourceMapPath: string): Promise<BasicSourceMapConsumer | IndexedSourceMapConsumer> {
91-
return new Promise<BasicSourceMapConsumer | IndexedSourceMapConsumer>(async (resolve, reject) => {
92-
let sourceMapContent: string | null = null;
91+
async function getSourceMapConsumer(sourceMapPath: string | null): Promise<SourceMapHandler | null> {
92+
if (sourceMapPath === null) {
93+
return null;
94+
}
95+
const sourceMapContent: string | null = await (async () => {
9396
try {
94-
sourceMapContent = await readFile(sourceMapPath, "utf8");
97+
return await readFile(sourceMapPath, "utf8");
9598
} catch (error) {
96-
reject(error);
97-
return;
99+
if (error instanceof Error) {
100+
console.log(chalk.yellow(`Failed to read source map file: ${sourceMapPath} due to ${error}`));
101+
}
102+
return null;
98103
}
104+
})();
105+
if (sourceMapContent === null) {
106+
return null;
107+
}
108+
return new Promise<SourceMapHandler>((resolve) => {
99109
SourceMapConsumer.with(sourceMapContent, null, (consumer) => {
100110
resolve(consumer);
101111
});
@@ -108,7 +118,7 @@ export async function handleWebAssemblyError(
108118
): Promise<ExecutionError> {
109119
const wasmBuffer = await readFile(wasmPath);
110120
const sourceMapPath = parseSourceMapPath(wasmBuffer.buffer as ArrayBuffer);
111-
const sourceMapConsumer = sourceMapPath ? await getSourceMapConsumer(sourceMapPath) : null;
121+
const sourceMapConsumer: SourceMapHandler | null = await getSourceMapConsumer(sourceMapPath);
112122
let stacks: NodeJS.CallSite[] = [];
113123
const originalPrepareStackTrace = Error.prepareStackTrace;
114124
Error.prepareStackTrace = (_: Error, structuredStackTrace: NodeJS.CallSite[]) => {
@@ -120,6 +130,6 @@ export async function handleWebAssemblyError(
120130
message: error.message,
121131
stacks: stacks
122132
.map((callSite) => createWebAssemblyCallSite(callSite, { wasmPath, sourceMapConsumer }))
123-
.filter((callSite) => callSite != null),
133+
.filter((callSite) => callSite !== null),
124134
};
125135
}

src/utils/wasmparser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export function parseSourceMapPath(buf: ArrayBuffer): string | null {
8989
if (sectionInfo.id !== SectionCode.Custom) {
9090
reader.skipSection();
9191
}
92-
} else if (reader.state == BinaryReaderState.SOURCE_MAPPING_URL) {
92+
} else if (reader.state === BinaryReaderState.SOURCE_MAPPING_URL) {
9393
const sectionInfo = reader.result as ISourceMappingURL;
9494
return new TextDecoder("utf8").decode(sectionInfo.url);
9595
}

tests/ts/test/executionResult.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ test("equal assert failed", async () => {
6262

6363
test("equal crash", async () => {
6464
const executionResult = new ExecutionResultSummary();
65-
const actualString = "A long sentence for testing errorMsg.length > 160 in executionResult.ts merge function";
66-
const expectString = "= A long sentence for testing errorMsg.length > 160 in executionResult.ts merge function ";
6765
const testcaseA: IExecutionResult = {
6866
fail: 1,
6967
total: 1,

0 commit comments

Comments
 (0)