Skip to content

Commit fe0f695

Browse files
authored
chore: remove listTestNames transform (#87)
1 parent 146459c commit fe0f695

File tree

12 files changed

+139
-600
lines changed

12 files changed

+139
-600
lines changed

bin/cli.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ if (options.testcase !== undefined) {
5959
const testFiles = options.testFiles || options.testcase;
6060

6161
const onlyFailures = options.onlyFailures || false;
62+
const testNamePattern = options.testNamePattern ?? null;
63+
64+
if (onlyFailures && testNamePattern !== null) {
65+
console.error(chalk.redBright("Cannot use --onlyFailures and --testNamePattern together") + "\n");
66+
exit(3);
67+
}
6268

6369
// if enabled testcase or testNamePattern or onlyFailures, disable collectCoverage by default
6470
const collectCoverage =
@@ -89,7 +95,7 @@ const testOption = {
8995
includes,
9096
excludes,
9197
testFiles,
92-
testNamePattern: options.testNamePattern,
98+
testNamePattern: testNamePattern,
9399
collectCoverage,
94100
onlyFailures,
95101

src/core/execute.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const readFile = promises.readFile;
1717
async function nodeExecutor(
1818
instrumentResult: InstrumentResult,
1919
outFolder: string,
20-
matchedTestNames: string[],
20+
filterByName: (fullTestName: string) => boolean,
2121
imports?: Imports
2222
): Promise<ExecutionResult> {
2323
const wasi = new WASI({
@@ -85,7 +85,7 @@ async function nodeExecutor(
8585
break;
8686
}
8787
const { fullName, functionIndex, setupFunctions, teardownFunctions } = testCase;
88-
if (matchedTestNames.length === 0 || matchedTestNames.includes(fullName)) {
88+
if (filterByName(fullName)) {
8989
await executionRecorder.runTestFunction(
9090
fullName,
9191
() => {
@@ -110,14 +110,14 @@ async function nodeExecutor(
110110
export async function execWasmBinaries(
111111
outFolder: string,
112112
instrumentResults: InstrumentResult[],
113-
matchedTestNames: string[],
113+
filterByName: (fullTestName: string) => boolean,
114114
imports?: Imports
115115
): Promise<ExecutionResultSummary> {
116116
const assertRes = new ExecutionResultSummary();
117117
ensureDirSync(outFolder);
118118
await Promise.all<void>(
119119
instrumentResults.map(async (instrumentResult): Promise<void> => {
120-
const result: ExecutionResult = await nodeExecutor(instrumentResult, outFolder, matchedTestNames, imports);
120+
const result: ExecutionResult = await nodeExecutor(instrumentResult, outFolder, filterByName, imports);
121121
await assertRes.merge(result, instrumentResult.expectInfo);
122122
})
123123
);

src/core/precompile.ts

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,33 @@ import { join, relative, resolve } from "node:path";
77
import { getIncludeFiles } from "../utils/pathResolver.js";
88
import { SourceFunctionInfo, UnittestPackage } from "../interface.js";
99
import { projectRoot } from "../utils/projectRoot.js";
10-
import assert from "node:assert";
1110
import { ascMain } from "../utils/ascWrapper.js";
11+
import assert from "node:assert";
12+
13+
function getFilterByName(testNamePattern: string | null, failedTestNames: string[]): UnittestPackage["filterByName"] {
14+
assert(
15+
!(testNamePattern !== null && failedTestNames.length > 0),
16+
"Cannot use testNamePattern and failedTestNames together"
17+
);
18+
if (testNamePattern !== null) {
19+
const regexPattern = new RegExp(testNamePattern);
20+
return (fullTestName: string): boolean => regexPattern.test(fullTestName);
21+
}
22+
if (failedTestNames.length > 0) {
23+
return (fullTestName: string): boolean => failedTestNames.includes(fullTestName);
24+
}
25+
return (): boolean => true;
26+
}
1227

13-
// eslint-disable-next-line sonarjs/cognitive-complexity
1428
export async function precompile(
1529
includes: string[],
1630
excludes: string[],
1731
testFiles: string[] | undefined, // this field specifed test file names
18-
testNamePattern: string | undefined,
32+
testNamePattern: string | null,
1933
failedTestNames: string[],
2034
collectCoverage: boolean,
2135
flags: string
2236
): Promise<UnittestPackage> {
23-
// if specify testFiles, use testFiles for unittest
24-
// otherwise, get testFiles(*.test.ts) in includes directory
25-
const testCodePaths = testFiles ?? getRelatedFiles(includes, excludes, (path: string) => path.endsWith(".test.ts"));
26-
const matchedTestFiles = new Set<string>();
27-
let matchedTestNames: string[] = [];
28-
29-
if (testNamePattern || failedTestNames.length > 0) {
30-
// if enabled testNamePattern or enabled onlyFailures, need listTestName transform
31-
const testNameInfos = new Map<string, string[]>();
32-
const testNameTransformFunction = join(projectRoot, "transform", "listTestNames.mjs");
33-
for (const testCodePath of testCodePaths) {
34-
await transform(testNameTransformFunction, testCodePath, flags, () => {
35-
testNameInfos.set(testCodePath, testNames);
36-
});
37-
}
38-
if (testNamePattern) {
39-
const regexPattern = new RegExp(testNamePattern);
40-
for (const [fileName, testNames] of testNameInfos) {
41-
for (const testName of testNames) {
42-
if (regexPattern.test(testName)) {
43-
matchedTestNames.push(testName);
44-
matchedTestFiles.add(fileName);
45-
}
46-
}
47-
}
48-
}
49-
50-
if (failedTestNames.length > 0) {
51-
matchedTestNames = failedTestNames;
52-
for (const [fileName, testNames] of testNameInfos) {
53-
for (const testName of testNames) {
54-
if (matchedTestNames.includes(testName)) {
55-
matchedTestFiles.add(fileName);
56-
}
57-
}
58-
}
59-
}
60-
61-
assert(matchedTestFiles.size > 0, "No matched testname");
62-
}
63-
6437
let sourceFunctions: Map<string, SourceFunctionInfo[]> | undefined = undefined;
6538
if (collectCoverage) {
6639
const sourceCodePaths = getRelatedFiles(includes, excludes, (path: string) => !path.endsWith(".test.ts"));
@@ -69,8 +42,10 @@ export async function precompile(
6942
sourceFunctions = await transform(sourceTransformFunction, sourceCodePaths, flags, () => __functionInfos);
7043
}
7144
return {
72-
testCodePaths: matchedTestFiles.size > 0 ? Array.from(matchedTestFiles) : testCodePaths,
73-
matchedTestNames,
45+
// if specify testFiles, use testFiles for unittest
46+
// otherwise, get testFiles(*.test.ts) in includes directory
47+
testCodePaths: testFiles ?? getRelatedFiles(includes, excludes, (path: string) => path.endsWith(".test.ts")),
48+
filterByName: getFilterByName(testNamePattern, failedTestNames),
7449
sourceFunctions: sourceFunctions || new Map<string, SourceFunctionInfo[]>(),
7550
};
7651
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ async function startUniTestImpl(options: TestOption): Promise<number> {
6969
const executedResult = await execWasmBinaries(
7070
options.tempFolder,
7171
instrumentResult,
72-
unittestPackage.matchedTestNames,
72+
unittestPackage.filterByName,
7373
options.imports
7474
);
7575
console.log(chalk.blueBright("execute test files: ") + chalk.bold.greenBright("OK"));

src/interface.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export class CodeCoverage {
174174

175175
export interface UnittestPackage {
176176
readonly testCodePaths: string[];
177-
readonly matchedTestNames: string[];
177+
readonly filterByName: (fullTestName: string) => boolean;
178178
readonly sourceFunctions?: Map<string, SourceFunctionInfo[]>;
179179
}
180180

@@ -201,7 +201,7 @@ export interface TestOption {
201201
includes: string[];
202202
excludes: string[];
203203
testFiles?: string[];
204-
testNamePattern?: string;
204+
testNamePattern: string | null;
205205
collectCoverage: boolean;
206206
onlyFailures: boolean;
207207

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import path from "node:path";
2+
3+
const __dirname = path.dirname(new URL(import.meta.url).pathname);
4+
5+
/**
6+
* @type {import("../../../config.d.ts").Config}
7+
*/
8+
export default {
9+
include: [__dirname],
10+
imports(runtime) {
11+
return {
12+
env: {
13+
log: (msg) => {
14+
runtime.framework.log(runtime.exports.__getString(msg));
15+
},
16+
},
17+
};
18+
},
19+
temp: path.join(__dirname, "tmp"),
20+
output: path.join(__dirname, "tmp"),
21+
mode: [],
22+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { test, expect } from "../../../assembly";
2+
3+
test("failure on test", () => {
4+
expect(1 + 1).equal(3);
5+
});
6+
7+
test("success on test", () => {
8+
expect(1 + 1).equal(2);
9+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
code analysis: OK
2+
compile test files: OK
3+
instrument: OK
4+
execute test files: OK
5+
6+
test case: 1/2 (success/total)
7+
8+
Error Message:
9+
failure on test:
10+
tests/e2e/on-failure-only/index.test.ts:4:2 value: 2 expect: = 3
11+
12+
code analysis: OK
13+
compile test files: OK
14+
instrument: OK
15+
execute test files: OK
16+
17+
test case: 0/1 (success/total)
18+
19+
Error Message:
20+
failure on test:
21+
tests/e2e/on-failure-only/index.test.ts:4:2 value: 2 expect: = 3
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "assemblyscript/std/assembly.json",
3+
"include": ["./**/*.ts"]
4+
}

tests/e2e/run.js

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,49 +28,81 @@ function isEnabled(name) {
2828
return enabledTests.includes(name);
2929
}
3030

31-
function runEndToEndTest(name, flags, handle) {
32-
if (isEnabled(name)) {
31+
async function runEndToEndTest(name, flags, handle) {
32+
if (!isEnabled(name)) {
33+
return;
34+
}
35+
return new Promise((resolve) => {
3336
console.log(`Running e2e test: ${name}`);
3437
exec(`node ./bin/as-test.js --config tests/e2e/${name}/as-test.config.js ${flags}`, (error, stdout, stderr) => {
35-
// standard check
36-
const expectStdOut = readFileSync(`tests/e2e/${name}/stdout.txt`, "utf-8");
37-
if (expectStdOut !== stdout) {
38-
console.log(`========= STDOUT ${name} =========`);
39-
console.log(getDiff(expectStdOut, stdout));
40-
console.log(`========= STDERR ${name} =========`);
41-
console.log(stderr);
42-
process.exit(1);
43-
}
44-
// customize check
4538
handle(error, stdout, stderr);
39+
resolve();
4640
});
41+
});
42+
}
43+
44+
function checkOutput(name, stdout, stderr) {
45+
const expectStdOut = readFileSync(`tests/e2e/${name}/stdout.txt`, "utf-8");
46+
if (expectStdOut !== stdout) {
47+
console.log(`========= STDOUT ${name} =========`);
48+
console.log(getDiff(expectStdOut, stdout));
49+
console.log(`========= STDERR ${name} =========`);
50+
console.log(stderr);
51+
process.exit(1);
4752
}
4853
}
4954

5055
runEndToEndTest("assertFailed", "", (error, stdout, stderr) => {
56+
checkOutput("assertFailed", stdout, stderr);
5157
assert(error.code === 1);
5258
});
5359

5460
runEndToEndTest("compilationFailed", "", (error, stdout, stderr) => {
61+
checkOutput("compilationFailed", stdout, stderr);
5562
assert(error.code === 2);
5663
});
5764

58-
runEndToEndTest("isolated-cli", "--isolated false", (error, stdout, stderr) => {});
59-
runEndToEndTest("isolated-false", "", (error, stdout, stderr) => {});
60-
runEndToEndTest("isolated-true", "", (error, stdout, stderr) => {});
65+
runEndToEndTest("isolated-cli", "--isolated false", (error, stdout, stderr) => {
66+
checkOutput("isolated-cli", stdout, stderr);
67+
});
68+
runEndToEndTest("isolated-false", "", (error, stdout, stderr) => {
69+
checkOutput("isolated-false", stdout, stderr);
70+
});
71+
runEndToEndTest("isolated-true", "", (error, stdout, stderr) => {
72+
checkOutput("isolated-true", stdout, stderr);
73+
});
74+
75+
(async () => {
76+
let mergedStdout = "";
77+
let mergedStderr = "";
78+
await runEndToEndTest("on-failure-only", "", (error, stdout, stderr) => {
79+
mergedStdout += stdout;
80+
mergedStderr += stderr;
81+
});
82+
mergedStdout += "\n";
83+
mergedStderr += "\n";
84+
await runEndToEndTest("on-failure-only", "--onlyFailures", (error, stdout, stderr) => {
85+
mergedStdout += stdout;
86+
mergedStderr += stderr;
87+
});
88+
checkOutput("on-failure-only", mergedStdout, mergedStderr);
89+
})();
6190

6291
runEndToEndTest("printLogInFailedInfo", "", (error, stdout, stderr) => {
92+
checkOutput("printLogInFailedInfo", stdout, stderr);
6393
assert(error.code === 1);
6494
});
6595

6696
runEndToEndTest("setup-teardown", "", (error, stdout, stderr) => {
97+
checkOutput("setup-teardown", stdout, stderr);
6798
assert(error.code === 1);
6899
});
69100

70101
runEndToEndTest(
71102
"testFiles",
72103
"--testFiles tests/e2e/testFiles/succeed_0.test.ts tests/e2e/testFiles/succeed_1.test.ts",
73104
(error, stdout, stderr) => {
105+
checkOutput("testFiles", stdout, stderr);
74106
assert(error === null);
75107
}
76108
);

0 commit comments

Comments
 (0)