Skip to content

Commit b8d8777

Browse files
committed
support testNamePattern option
1 parent c2d4d02 commit b8d8777

File tree

7 files changed

+37
-446
lines changed

7 files changed

+37
-446
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/dist
66

77
/build*
8-
/transform/listFunctions.mjs
8+
/transform/*.mjs
99
/transform/tsconfig.tsbuildinfo
1010

1111
/coverage

src/core/execute.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@ import { supplyDefaultFunction } from "../utils/index.js";
1010
import { parseImportFunctionInfo } from "../utils/wasmparser.js";
1111
import { ExecutionRecorder } from "./executionRecorder.js";
1212
import { CoverageRecorder } from "./covRecorder.js";
13+
import assert from "node:assert";
1314

1415
const readFile = promises.readFile;
1516

16-
async function nodeExecutor(wasm: string, outFolder: string, imports?: Imports): Promise<ExecutionRecorder> {
17+
async function nodeExecutor(
18+
wasm: string,
19+
outFolder: string,
20+
matchedTestNames?: string[],
21+
imports?: Imports
22+
): Promise<ExecutionRecorder> {
1723
const wasi = new WASI({
1824
args: ["node", basename(wasm)],
1925
env: process.env,
@@ -46,12 +52,23 @@ async function nodeExecutor(wasm: string, outFolder: string, imports?: Imports):
4652
try {
4753
wasi.start(ins);
4854
const execTestFunction = ins.exports["executeTestFunction"];
49-
if (typeof execTestFunction === "function") {
50-
for (const fncs of executionRecorder.registerFunctions) {
51-
const functions = fncs[1];
52-
(execTestFunction as (a: number) => void)(functions);
55+
assert(typeof execTestFunction === "function");
56+
if (matchedTestNames === undefined) {
57+
// means execute all testFunctions
58+
for (const functionInfo of executionRecorder.registerFunctions) {
59+
const functionIndex = functionInfo[1];
60+
(execTestFunction as (a: number) => void)(functionIndex);
5361
mockInstrumentFunc["mockFunctionStatus.clear"]();
5462
}
63+
} else {
64+
for (const functionInfo of executionRecorder.registerFunctions) {
65+
const [testName, functionIndex] = functionInfo;
66+
if (matchedTestNames.includes(testName)) {
67+
console.log(testName);
68+
(execTestFunction as (a: number) => void)(functionIndex);
69+
mockInstrumentFunc["mockFunctionStatus.clear"]();
70+
}
71+
}
5572
}
5673
} catch (error) {
5774
if (error instanceof Error) {
@@ -66,14 +83,15 @@ async function nodeExecutor(wasm: string, outFolder: string, imports?: Imports):
6683
export async function execWasmBinarys(
6784
outFolder: string,
6885
instrumentResult: InstrumentResult[],
86+
matchedTestNames?: string[],
6987
imports?: Imports
7088
): Promise<AssertResult> {
7189
const assertRes = new AssertResult();
7290
ensureDirSync(outFolder);
7391
await Promise.all<void>(
7492
instrumentResult.map(async (res): Promise<void> => {
7593
const { instrumentedWasm, expectInfo } = res;
76-
const recorder: ExecutionRecorder = await nodeExecutor(instrumentedWasm, outFolder, imports);
94+
const recorder: ExecutionRecorder = await nodeExecutor(instrumentedWasm, outFolder, matchedTestNames, imports);
7795
await assertRes.merge(recorder, expectInfo);
7896
})
7997
);

src/core/executionRecorder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ export class ExecutionRecorder implements IAssertResult {
1414
this._currentTestDescriptions.pop();
1515
}
1616
registerTestFunction(fncIndex: number): void {
17-
const testCaseFullName = this._currentTestDescriptions.join(" - ");
17+
const testCaseFullName = this._currentTestDescriptions.join(" ");
1818
this.registerFunctions.push([testCaseFullName, fncIndex]);
1919
}
2020
collectCheckResult(result: boolean, codeInfoIndex: number, actualValue: string, expectValue: string): void {
2121
this.total++;
2222
if (!result) {
2323
this.fail++;
24-
const testCaseFullName = this._currentTestDescriptions.join(" - ");
24+
const testCaseFullName = this._currentTestDescriptions.join(" ");
2525
const assertMessage: AssertMessage = [codeInfoIndex.toString(), actualValue, expectValue];
2626
this.failed_info[testCaseFullName] = this.failed_info[testCaseFullName] || [];
2727
this.failed_info[testCaseFullName].push(assertMessage);

src/core/precompile.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { join, relative, resolve } from "node:path";
88
import { getIncludeFiles } from "../utils/pathResolver.js";
99
import { SourceFunctionInfo, UnittestPackage } from "../interface.js";
1010
import { projectRoot } from "../utils/projectRoot.js";
11+
import assert from "node:assert";
1112

1213
export async function precompile(
1314
includes: string[],
@@ -40,6 +41,8 @@ export async function precompile(
4041
}
4142
}
4243
}
44+
45+
assert(matchedTestFiles.size > 0, `No matched testname using ${testNamePattern}`);
4346
return {
4447
testCodePaths: Array.from(matchedTestFiles),
4548
matchedTestNames: matchedTestNames,

src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ export async function start_unit_test(options: TestOption): Promise<boolean> {
6666
const instrumentResult = await instrument(wasmPaths, sourcePaths, options.collectCoverage);
6767
console.log(chalk.blueBright("instrument: ") + chalk.bold.greenBright("OK"));
6868

69-
const executedResult = await execWasmBinarys(options.tempFolder, instrumentResult, options.imports);
69+
const executedResult = await execWasmBinarys(
70+
options.tempFolder,
71+
instrumentResult,
72+
unittestPackage.matchedTestNames,
73+
options.imports
74+
);
7075
console.log(chalk.blueBright("execute testcases: ") + chalk.bold.greenBright("OK"));
7176

7277
logAssertResult(executedResult);

0 commit comments

Comments
 (0)