Skip to content

Commit 11ddb71

Browse files
committed
fix
1 parent 651fec0 commit 11ddb71

File tree

6 files changed

+51
-44
lines changed

6 files changed

+51
-44
lines changed

bin/cli.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ const testOption = {
8080
};
8181

8282
start_unit_test(testOption)
83-
.then((success) => {
84-
if (!success) {
83+
.then((returnCode) => {
84+
if (returnCode !== 0) {
8585
console.error(chalk.redBright("Test Failed") + "\n");
86-
exit(1);
86+
exit(returnCode);
8787
}
8888
})
8989
.catch((e) => {

src/core/compile.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { main } from "assemblyscript/asc";
21
import { join, relative } from "node:path";
32
import { findRoot } from "../utils/pathResolver.js";
3+
import { ascMain } from "../utils/ascWrapper.js";
44

55
export async function compile(testCodePaths: string[], outputFolder: string, compileFlags: string): Promise<string[]> {
66
const wasm: string[] = [];
@@ -25,12 +25,7 @@ export async function compile(testCodePaths: string[], outputFolder: string, com
2525
const argv = compileFlags.split(" ");
2626
ascArgv = ascArgv.concat(argv);
2727
}
28-
const { error, stderr } = await main(ascArgv);
29-
if (error) {
30-
// eslint-disable-next-line @typescript-eslint/no-base-to-string
31-
console.log(stderr.toString());
32-
process.exit(2);
33-
}
28+
await ascMain(ascArgv);
3429
};
3530

3631
// Here, for-await is more efficient and less memory cost than Promise.all()

src/core/precompile.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
*/
44

55
import ignore from "ignore";
6-
import { main } from "assemblyscript/asc";
76
import { join, relative, resolve } from "node:path";
87
import { getIncludeFiles } from "../utils/pathResolver.js";
98
import { SourceFunctionInfo, UnittestPackage } from "../interface.js";
109
import { projectRoot } from "../utils/projectRoot.js";
1110
import assert from "node:assert";
11+
import { ascMain } from "../utils/ascWrapper.js";
1212

1313
// eslint-disable-next-line sonarjs/cognitive-complexity
1414
export async function precompile(
@@ -91,12 +91,7 @@ async function transform(transformFunction: string, codePath: string, flags: str
9191
const argv = flags.split(" ");
9292
ascArgv = ascArgv.concat(argv);
9393
}
94-
const { error, stderr } = await main(ascArgv);
95-
if (error) {
96-
// eslint-disable-next-line @typescript-eslint/no-base-to-string
97-
console.log(stderr.toString());
98-
process.exit(2);
99-
}
94+
await ascMain(ascArgv);
10095
collectCallback();
10196
}
10297

src/index.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { execWasmBinaries } from "./core/execute.js";
88
import { generateReport, reportConfig } from "./generator/index.js";
99
import { TestOption } from "./interface.js";
1010
import { join } from "node:path";
11+
import { CompilationError } from "./utils/ascWrapper.js";
1112

1213
const { readFileSync, emptydirSync } = pkg;
1314

@@ -30,10 +31,7 @@ export function validateArgument(includes: unknown, excludes: unknown) {
3031
}
3132
}
3233

33-
/**
34-
* main function of unit-test, will throw Exception in most condition except job carsh
35-
*/
36-
export async function start_unit_test(options: TestOption): Promise<boolean> {
34+
async function startUniTestImpl(options: TestOption): Promise<number> {
3735
const failurePath = join(options.outputFolder, "failures.json");
3836
let failedTestCases: string[] = [];
3937
if (options.onlyFailures) {
@@ -85,6 +83,18 @@ export async function start_unit_test(options: TestOption): Promise<boolean> {
8583
reportConfig.errorLimit = options.errorLimit || reportConfig.errorLimit;
8684
generateReport(options.mode, options.outputFolder, fileCoverageInfo);
8785
}
86+
return executedResult.fail === 0 ? 0 : 1;
87+
}
8888

89-
return executedResult.fail === 0;
89+
export async function start_unit_test(options: TestOption): Promise<number> {
90+
try {
91+
return await startUniTestImpl(options);
92+
} catch (error) {
93+
if (error instanceof CompilationError) {
94+
console.log(error.message);
95+
return 2;
96+
}
97+
// unknown exception.
98+
throw error;
99+
}
90100
}

src/utils/ascWrapper.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { createMemoryStream, main } from "assemblyscript/asc";
2+
3+
export const compiler = {
4+
compile: main,
5+
};
6+
7+
export class CompilationError extends Error {
8+
constructor(errorMessage: string) {
9+
super(errorMessage);
10+
this.name = "CompilationError";
11+
}
12+
}
13+
14+
export async function ascMain(ascArgv: string[]) {
15+
let stderr = createMemoryStream();
16+
const { error } = await compiler.compile(ascArgv, { stderr });
17+
if (error) {
18+
throw new CompilationError(stderr.toString());
19+
}
20+
}

tests/ts/test/core/throwError.test.ts

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,27 @@
11
// eslint-disable-next-line n/no-extraneous-import
22
import { jest } from "@jest/globals";
3-
4-
jest.unstable_mockModule("assemblyscript/asc", () => ({
5-
main: jest.fn(() => {
6-
return {
7-
error: new Error("mock asc.main() error"),
8-
stderr: "mock asc.main() error",
9-
};
10-
}),
11-
}));
12-
13-
const { precompile } = await import("../../../../src/core/precompile.js");
14-
const { compile } = await import("../../../../src/core/compile.js");
15-
16-
let exitSpy: any | null = null;
3+
import { precompile } from "../../../../src/core/precompile.js";
4+
import { compile } from "../../../../src/core/compile.js";
5+
import { compiler } from "../../../../src/utils/ascWrapper.js";
176

187
beforeEach(() => {
19-
exitSpy = jest.spyOn(process, "exit").mockImplementation(() => {
20-
throw new Error("process.exit");
8+
jest.spyOn(compiler, "compile").mockImplementation(async () => {
9+
throw new Error("mock asc.main() error");
2110
});
2211
});
2312

2413
afterEach(() => {
25-
exitSpy!.mockRestore();
14+
jest.clearAllMocks();
2615
});
2716

2817
test("transform error", async () => {
2918
await expect(async () => {
30-
await precompile(["tests/ts/fixture/transformFunction.ts"], [], ["non-exist.ts"], undefined, [], true, "");
31-
}).rejects.toThrow("process.exit");
32-
expect(exitSpy).toHaveBeenCalled();
19+
await precompile(["tests/ts/fixture/transformFunction.ts"], [], undefined, undefined, [], true, "");
20+
}).rejects.toThrow("mock asc.main() error");
3321
});
3422

3523
test("compile error", async () => {
3624
await expect(async () => {
3725
await compile(["non-exist.ts"], "mockFolder", "");
38-
}).rejects.toThrow("process.exit");
39-
expect(exitSpy).toHaveBeenCalled();
26+
}).rejects.toThrow("mock asc.main() error");
4027
});

0 commit comments

Comments
 (0)