Skip to content

Commit 2095474

Browse files
authored
fix: proper handling of situations where AS files are not invalid (#62)
1 parent a53faaf commit 2095474

File tree

14 files changed

+125
-43
lines changed

14 files changed

+125
-43
lines changed

bin/cli.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ const configPath = resolve(".", options.config);
3232
if (!fs.pathExistsSync(configPath)) {
3333
console.error(chalk.redBright("Miss config file") + "\n");
3434
console.error(program.helpInformation());
35-
exit(-1);
35+
exit(3);
3636
}
3737
const config = (await import(pathToFileURL(configPath))).default;
3838

3939
const includes = config.include;
4040
if (includes === undefined) {
4141
console.error(chalk.redBright("Miss include in config file") + "\n");
42-
exit(-1);
42+
exit(3);
4343
}
4444
const excludes = config.exclude || [];
4545
validateArgument(includes, excludes);
@@ -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(255);
86+
exit(returnCode);
8787
}
8888
})
8989
.catch((e) => {

docs/.vitepress/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export default defineConfig({
3434
{ text: "Matchers", link: "/api-documents/matchers" },
3535
{ text: "Mock Function", link: "/api-documents/mock-function" },
3636
{ text: "Report", link: "/api-documents/coverage-report" },
37+
{ text: "Return Code", link: "/api-documents/return-code.md" },
3738
],
3839
},
3940
{

docs/api-documents/return-code.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## Return Code
2+
3+
The platform indicates different error situations by returning different exit codes.
4+
5+
- exit code 1 indicates a test failure.
6+
- exit code 2 indicates that the input AS file cannot be compiled.
7+
- exit code 3 or higher indicates an error in the configuration file.

docs/release-note.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# Release Note
22

3+
## 1.3.1
4+
5+
🚀 Highlight Features
6+
7+
- Defined the meanings of the return values in different situations.
8+
- `0` means success.
9+
- `1` means test failed.
10+
- `2` means invalid AS file.
11+
- `>2` means configuration error.
12+
13+
🚀 Improvements
14+
15+
- Proper handling of situations where AS files are not invalid.
16+
317
## 1.3.0
418

519
🚀 Highlight Features
@@ -13,7 +27,7 @@
1327
- Expose the framework's `log` function in the configuration file, and the logs redirected to this function will be appended to the final test report.
1428
- Support test crashes and provide good call stack information.
1529

16-
Improvements
30+
🚀 Improvements
1731

1832
- Code coverage calculation.
1933
- Skip type definitions.

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.error(stderr.toString());
32-
throw error;
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.error(stderr.toString());
98-
throw error;
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+
const stderr = createMemoryStream();
16+
const { error } = await compiler.compile(ascArgv.concat("--noColors"), { stderr });
17+
if (error) {
18+
throw new CompilationError(stderr.toString());
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import path from "node:path";
2+
3+
const __dirname = path.dirname(new URL(import.meta.url).pathname);
4+
5+
export default {
6+
include: [__dirname],
7+
imports(runtime) {
8+
return {
9+
env: {
10+
log: (msg) => {
11+
runtime.framework.log(runtime.exports.__getString(msg));
12+
},
13+
},
14+
};
15+
},
16+
temp: path.join(__dirname, "tmp"),
17+
output: path.join(__dirname, "tmp"),
18+
mode: [],
19+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { test, expect } from "../../../assembly";
2+
3+
test("assert on test", () => {
4+
f = 1;
5+
});

0 commit comments

Comments
 (0)