as-test 0.1.4 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/{nodejs.yml → as-test.yml} +10 -9
- package/.prettierrc +3 -0
- package/CHANGELOG.md +4 -1
- package/README.md +18 -65
- package/as-test.config.json +22 -0
- package/asconfig.json +30 -33
- package/assembly/__tests__/example.spec.ts +70 -0
- package/assembly/coverage.ts +15 -15
- package/assembly/index.ts +195 -132
- package/assembly/reporters/tap.ts +30 -0
- package/assembly/src/expectation.ts +302 -277
- package/assembly/src/group.ts +33 -33
- package/assembly/src/node.ts +7 -7
- package/assembly/test.ts +43 -46
- package/assembly/tsconfig.json +96 -98
- package/assembly/util.ts +90 -87
- package/bin/build.js +143 -0
- package/bin/index.js +188 -0
- package/bin/init.js +44 -0
- package/bin/package.json +3 -0
- package/bin/run.js +64 -0
- package/bin/types.js +41 -0
- package/bin/util.js +23 -0
- package/cli/build.ts +154 -0
- package/cli/index.ts +201 -0
- package/cli/init.ts +47 -0
- package/cli/run.ts +68 -0
- package/cli/tsconfig.json +8 -0
- package/cli/types.ts +34 -0
- package/cli/util.ts +30 -0
- package/index.ts +1 -1
- package/jest.test.js +44 -5
- package/package.json +18 -15
- package/tests/test.tap +14 -0
- package/transform/lib/index.js +391 -397
- package/transform/package.json +4 -4
- package/transform/src/index.ts +474 -506
- package/transform/tsconfig.json +2 -2
- package/assembly/example.spec.ts +0 -5
- package/src/cli.ts +0 -0
package/bin/util.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export function formatTime(ms) {
|
|
2
|
+
if (ms < 0) {
|
|
3
|
+
throw new Error("Time should be a non-negative number.");
|
|
4
|
+
}
|
|
5
|
+
// Convert milliseconds to microseconds
|
|
6
|
+
const us = ms * 1000;
|
|
7
|
+
const units = [
|
|
8
|
+
{ name: "μs", divisor: 1 },
|
|
9
|
+
{ name: "ms", divisor: 1000 },
|
|
10
|
+
{ name: "s", divisor: 1000 * 1000 },
|
|
11
|
+
{ name: "m", divisor: 60 * 1000 * 1000 },
|
|
12
|
+
{ name: "h", divisor: 60 * 60 * 1000 * 1000 },
|
|
13
|
+
{ name: "d", divisor: 24 * 60 * 60 * 1000 * 1000 },
|
|
14
|
+
];
|
|
15
|
+
for (let i = units.length - 1; i >= 0; i--) {
|
|
16
|
+
const unit = units[i];
|
|
17
|
+
if (us >= unit.divisor) {
|
|
18
|
+
const value = Math.round((us / unit.divisor) * 1000) / 1000;
|
|
19
|
+
return `${value}${unit.name}`;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return `${us}us`;
|
|
23
|
+
}
|
package/cli/build.ts
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "fs";
|
|
2
|
+
import { Config } from "./types.js";
|
|
3
|
+
import { glob } from "glob";
|
|
4
|
+
import chalk from "chalk";
|
|
5
|
+
import { exec } from "child_process";
|
|
6
|
+
import { formatTime } from "./util.js";
|
|
7
|
+
import * as path from "path";
|
|
8
|
+
|
|
9
|
+
export async function build(args: string[]) {
|
|
10
|
+
const CONFIG_PATH = path.join(process.cwd(), "./as-test.config.json");
|
|
11
|
+
let config: Config;
|
|
12
|
+
if (!existsSync(CONFIG_PATH)) {
|
|
13
|
+
console.log(
|
|
14
|
+
chalk.bgMagentaBright(" WARN ") +
|
|
15
|
+
chalk.dim(":") +
|
|
16
|
+
" Could not locate config file in the current directory! Continuing with default config." +
|
|
17
|
+
"\n",
|
|
18
|
+
);
|
|
19
|
+
config = new Config();
|
|
20
|
+
} else {
|
|
21
|
+
config = Object.assign(
|
|
22
|
+
new Config(),
|
|
23
|
+
JSON.parse(readFileSync(CONFIG_PATH).toString()),
|
|
24
|
+
) as Config;
|
|
25
|
+
console.log(chalk.dim("Loading config from: " + CONFIG_PATH) + "\n");
|
|
26
|
+
}
|
|
27
|
+
const ASCONFIG_PATH = path.join(process.cwd(), config.config);
|
|
28
|
+
if (!existsSync(ASCONFIG_PATH)) {
|
|
29
|
+
console.log(
|
|
30
|
+
chalk.bgMagentaBright(" WARN ") +
|
|
31
|
+
chalk.dim(":") +
|
|
32
|
+
' Could not locate asconfig.json file! If you do not want to provide a config, set "config": "none". Continuing with default config.' +
|
|
33
|
+
"\n",
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
const pkg = JSON.parse(readFileSync("./package.json").toString()) as {
|
|
37
|
+
dependencies: string[] | null;
|
|
38
|
+
devDependencies: string[] | null;
|
|
39
|
+
peerDependencies: string[] | null;
|
|
40
|
+
};
|
|
41
|
+
let buildCommands: string[] = [];
|
|
42
|
+
|
|
43
|
+
if (config.buildOptions.wasi) {
|
|
44
|
+
if (!existsSync("./node_modules/@assemblyscript/wasi-shim/asconfig.json")) {
|
|
45
|
+
console.log(
|
|
46
|
+
chalk.bgRed(" ERROR ") +
|
|
47
|
+
chalk.dim(":") +
|
|
48
|
+
" " +
|
|
49
|
+
"could not find @assemblyscript/wasi-shim! Add it to your dependencies to run with WASI!",
|
|
50
|
+
);
|
|
51
|
+
process.exit(1);
|
|
52
|
+
}
|
|
53
|
+
if (
|
|
54
|
+
(pkg.dependencies &&
|
|
55
|
+
!Object.keys(pkg.dependencies).includes("@assemblyscript/wasi-shim")) ||
|
|
56
|
+
(pkg.devDependencies &&
|
|
57
|
+
!Object.keys(pkg.devDependencies).includes(
|
|
58
|
+
"@assemblyscript/wasi-shim",
|
|
59
|
+
)) ||
|
|
60
|
+
(pkg.peerDependencies &&
|
|
61
|
+
!Object.keys(pkg.peerDependencies).includes(
|
|
62
|
+
"@assemblyscript/wasi-shim",
|
|
63
|
+
))
|
|
64
|
+
) {
|
|
65
|
+
if (
|
|
66
|
+
existsSync("./node_modules/@assemblyscript/wasi-shim/asconfig.json")
|
|
67
|
+
) {
|
|
68
|
+
console.log(
|
|
69
|
+
chalk.bold.bgMagentaBright(" WARN ") +
|
|
70
|
+
chalk.dim(": @assemblyscript/wasi-shim") +
|
|
71
|
+
" is not included in project dependencies!",
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let packageManagerCommand = "npx";
|
|
78
|
+
if (
|
|
79
|
+
process.env.npm_config_user_agent &&
|
|
80
|
+
process.env.npm_config_user_agent.includes("pnpm")
|
|
81
|
+
) {
|
|
82
|
+
packageManagerCommand = "pnpx";
|
|
83
|
+
} else if (
|
|
84
|
+
process.env.npm_config_user_agent &&
|
|
85
|
+
process.env.npm_config_user_agent.includes("yarn")
|
|
86
|
+
) {
|
|
87
|
+
packageManagerCommand = "yarn run";
|
|
88
|
+
} else if (
|
|
89
|
+
process.env.npm_config_user_agent &&
|
|
90
|
+
process.env.npm_config_user_agent.includes("bun")
|
|
91
|
+
) {
|
|
92
|
+
packageManagerCommand = "bunx";
|
|
93
|
+
}
|
|
94
|
+
console.log("");
|
|
95
|
+
|
|
96
|
+
const inputFiles = await glob(config.input);
|
|
97
|
+
for (const file of inputFiles) {
|
|
98
|
+
console.log(chalk.dim("Including " + file));
|
|
99
|
+
let command = `${packageManagerCommand} asc ${file}${args.length ? " " + args.join(" ") : ""}`;
|
|
100
|
+
if (config.buildOptions.wasi) {
|
|
101
|
+
command +=
|
|
102
|
+
" --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json";
|
|
103
|
+
}
|
|
104
|
+
if (config.config !== "none") {
|
|
105
|
+
command += " --config " + config.config;
|
|
106
|
+
}
|
|
107
|
+
const outFile =
|
|
108
|
+
config.outDir +
|
|
109
|
+
"/" +
|
|
110
|
+
file.slice(file.lastIndexOf("/") + 1).replace(".ts", ".wasm");
|
|
111
|
+
if (config.outDir) {
|
|
112
|
+
command += " -o " + outFile;
|
|
113
|
+
}
|
|
114
|
+
if (config.coverage.enabled) {
|
|
115
|
+
console.log(chalk.dim("Enabling coverage"));
|
|
116
|
+
command += " --use COVERAGE_USE=1 --transform as-test/transform";
|
|
117
|
+
if (config.coverage.show) command += " --use COVERAGE_SHOW=1";
|
|
118
|
+
}
|
|
119
|
+
if (config.buildOptions.args) {
|
|
120
|
+
command += " " + config.buildOptions.args.join(" ");
|
|
121
|
+
}
|
|
122
|
+
buildCommands.push(command);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const build = (command: string) => {
|
|
126
|
+
return new Promise<void>((resolve, _) => {
|
|
127
|
+
console.log(chalk.dim("Building: " + command));
|
|
128
|
+
exec(command, (err, stdout, stderr) => {
|
|
129
|
+
if (config.buildOptions.verbose) {
|
|
130
|
+
process.stdout.write(stdout);
|
|
131
|
+
}
|
|
132
|
+
if (err) {
|
|
133
|
+
process.stderr.write(stderr + "\n");
|
|
134
|
+
process.exit(1);
|
|
135
|
+
}
|
|
136
|
+
resolve();
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
if (config.buildOptions.parallel) {
|
|
142
|
+
console.log(chalk.dim("Building sources in parallel..."));
|
|
143
|
+
const start = performance.now();
|
|
144
|
+
let builders: Promise<void>[] = [];
|
|
145
|
+
for (const command of buildCommands) {
|
|
146
|
+
builders.push(build(command));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
await Promise.all(builders);
|
|
150
|
+
console.log(
|
|
151
|
+
chalk.dim("Compiled in " + formatTime(performance.now() - start)) + "\n",
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
}
|
package/cli/index.ts
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import chalk from "chalk";
|
|
4
|
+
import { build } from "./build.js";
|
|
5
|
+
import { run } from "./run.js";
|
|
6
|
+
import { init } from "./init.js";
|
|
7
|
+
|
|
8
|
+
const _args = process.argv.slice(2);
|
|
9
|
+
const flags: string[] = [];
|
|
10
|
+
const args: string[] = [];
|
|
11
|
+
|
|
12
|
+
const COMMANDS: string[] = ["run", "build", "test", "init"];
|
|
13
|
+
|
|
14
|
+
const version = "0.1.6";
|
|
15
|
+
|
|
16
|
+
for (const arg of _args) {
|
|
17
|
+
if (arg.startsWith("-")) flags.push(arg);
|
|
18
|
+
else args.push(arg);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (!args.length) {
|
|
22
|
+
if (flags.includes("--tap")) {
|
|
23
|
+
console.log(`TAP version 13
|
|
24
|
+
# timing test
|
|
25
|
+
ok 1 should be strictly equal
|
|
26
|
+
not ok 2 should be strictly equal
|
|
27
|
+
---
|
|
28
|
+
operator: equal
|
|
29
|
+
expected: 100
|
|
30
|
+
actual: 107
|
|
31
|
+
...
|
|
32
|
+
|
|
33
|
+
1..2
|
|
34
|
+
# tests 2
|
|
35
|
+
# pass 1
|
|
36
|
+
# fail 1`);
|
|
37
|
+
} else if (flags.includes("--version") || flags.includes("-v")) {
|
|
38
|
+
console.log("as-test" + " " + version.toString());
|
|
39
|
+
} else {
|
|
40
|
+
console.log(
|
|
41
|
+
chalk.bold.blueBright("as-test") +
|
|
42
|
+
" is a testing framework for AssemblyScript. " +
|
|
43
|
+
chalk.dim("(v" + version + ")") +
|
|
44
|
+
"\n",
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
console.log(
|
|
48
|
+
chalk.bold("Usage: as-test") +
|
|
49
|
+
" " +
|
|
50
|
+
chalk.dim("<command>") +
|
|
51
|
+
" " +
|
|
52
|
+
chalk.bold.blueBright("[...flags]") +
|
|
53
|
+
" " +
|
|
54
|
+
chalk.bold("[...args]") +
|
|
55
|
+
" " +
|
|
56
|
+
chalk.dim("(alias: ast)") +
|
|
57
|
+
"\n",
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
console.log(chalk.bold("Commands:"));
|
|
61
|
+
console.log(
|
|
62
|
+
" " +
|
|
63
|
+
chalk.bold.blueBright("run") +
|
|
64
|
+
" " +
|
|
65
|
+
chalk.dim("<my-test.spec.ts>") +
|
|
66
|
+
" " +
|
|
67
|
+
"Run unit tests with selected runtime",
|
|
68
|
+
);
|
|
69
|
+
console.log(
|
|
70
|
+
" " +
|
|
71
|
+
chalk.bold.blueBright("build") +
|
|
72
|
+
" " +
|
|
73
|
+
chalk.dim("<my-test.spec.ts>") +
|
|
74
|
+
" " +
|
|
75
|
+
"Build unit tests and compile",
|
|
76
|
+
);
|
|
77
|
+
console.log(
|
|
78
|
+
" " +
|
|
79
|
+
chalk.bold.blueBright("test") +
|
|
80
|
+
" " +
|
|
81
|
+
chalk.dim("<my-test.spec.ts>") +
|
|
82
|
+
" " +
|
|
83
|
+
"Build and run unit tests with selected runtime" +
|
|
84
|
+
"\n",
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
console.log(
|
|
88
|
+
" " +
|
|
89
|
+
chalk.bold.magentaBright("init") +
|
|
90
|
+
" " +
|
|
91
|
+
chalk.strikethrough.dim("") +
|
|
92
|
+
" " +
|
|
93
|
+
"Initialize an empty testing template",
|
|
94
|
+
);
|
|
95
|
+
console.log(
|
|
96
|
+
" " +
|
|
97
|
+
chalk.strikethrough.bold.magentaBright("config") +
|
|
98
|
+
" " +
|
|
99
|
+
chalk.strikethrough.dim("as-test.config.json") +
|
|
100
|
+
" " +
|
|
101
|
+
"Specify the configuration file",
|
|
102
|
+
);
|
|
103
|
+
console.log(
|
|
104
|
+
" " +
|
|
105
|
+
chalk.strikethrough.bold.magentaBright("reporter") +
|
|
106
|
+
" " +
|
|
107
|
+
chalk.strikethrough.dim("<tap>") +
|
|
108
|
+
" " +
|
|
109
|
+
"Specify the test reporter to use",
|
|
110
|
+
);
|
|
111
|
+
console.log(
|
|
112
|
+
" " +
|
|
113
|
+
chalk.strikethrough.bold.magentaBright("use") +
|
|
114
|
+
" " +
|
|
115
|
+
chalk.strikethrough.dim("wasmtime") +
|
|
116
|
+
" " +
|
|
117
|
+
"Specify the runtime to use" +
|
|
118
|
+
"\n",
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
console.log(chalk.bold("Flags:"));
|
|
122
|
+
|
|
123
|
+
console.log(
|
|
124
|
+
" " +
|
|
125
|
+
chalk.strikethrough.dim("run") +
|
|
126
|
+
" " +
|
|
127
|
+
chalk.strikethrough.bold.blue("--coverage") +
|
|
128
|
+
" " +
|
|
129
|
+
"Use code coverage",
|
|
130
|
+
);
|
|
131
|
+
console.log(
|
|
132
|
+
" " +
|
|
133
|
+
chalk.strikethrough.dim("run") +
|
|
134
|
+
" " +
|
|
135
|
+
chalk.strikethrough.bold.blue("--snapshot") +
|
|
136
|
+
" " +
|
|
137
|
+
"Take a snapshot of the tests",
|
|
138
|
+
);
|
|
139
|
+
console.log(
|
|
140
|
+
" " +
|
|
141
|
+
chalk.strikethrough.dim("use") +
|
|
142
|
+
" " +
|
|
143
|
+
chalk.strikethrough.bold.blue("--list") +
|
|
144
|
+
" " +
|
|
145
|
+
"List supported runtimes",
|
|
146
|
+
);
|
|
147
|
+
console.log(
|
|
148
|
+
" " +
|
|
149
|
+
chalk.strikethrough.dim("reporter") +
|
|
150
|
+
" " +
|
|
151
|
+
chalk.strikethrough.bold.blue("--list") +
|
|
152
|
+
" " +
|
|
153
|
+
"List supported reporters",
|
|
154
|
+
);
|
|
155
|
+
console.log(
|
|
156
|
+
" " +
|
|
157
|
+
chalk.strikethrough.dim("<command>") +
|
|
158
|
+
" " +
|
|
159
|
+
chalk.strikethrough.bold.blue("--help") +
|
|
160
|
+
" " +
|
|
161
|
+
"Print info about command" +
|
|
162
|
+
"\n",
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
console.log(
|
|
166
|
+
chalk.dim(
|
|
167
|
+
"If your using this, consider dropping a star, it would help a lot!",
|
|
168
|
+
) + "\n",
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
console.log(
|
|
172
|
+
"View the repo: " +
|
|
173
|
+
chalk.magenta("https://github.com/JairusSW/as-test"),
|
|
174
|
+
);
|
|
175
|
+
console.log(
|
|
176
|
+
"View the docs: " +
|
|
177
|
+
chalk.strikethrough.blue("https://docs.jairus.dev/as-test"),
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
} else if (COMMANDS.includes(args[0]!)) {
|
|
181
|
+
const command = args.shift();
|
|
182
|
+
if (command === "build") {
|
|
183
|
+
build(args);
|
|
184
|
+
} else if (command === "run") {
|
|
185
|
+
run();
|
|
186
|
+
} else if (command === "test") {
|
|
187
|
+
build(args).then(() => {
|
|
188
|
+
run();
|
|
189
|
+
});
|
|
190
|
+
} else if (command === "init") {
|
|
191
|
+
init(args);
|
|
192
|
+
}
|
|
193
|
+
} else {
|
|
194
|
+
console.log(
|
|
195
|
+
chalk.bgRed(" ERROR ") +
|
|
196
|
+
chalk.dim(":") +
|
|
197
|
+
" " +
|
|
198
|
+
chalk.bold("Unknown command: ") +
|
|
199
|
+
args[0],
|
|
200
|
+
);
|
|
201
|
+
}
|
package/cli/init.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
import { existsSync, writeFileSync } from "fs";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
import { createInterface } from "readline";
|
|
5
|
+
import { Config } from "./types.js";
|
|
6
|
+
|
|
7
|
+
export function init(args: string[]): void {
|
|
8
|
+
console.log(
|
|
9
|
+
chalk.bold("This command will make sure that the following files exist") +
|
|
10
|
+
"\n",
|
|
11
|
+
);
|
|
12
|
+
console.log(
|
|
13
|
+
" " +
|
|
14
|
+
chalk.bold.blueBright("./as-test.config.json") +
|
|
15
|
+
chalk.dim(" - The core config file for as-test") +
|
|
16
|
+
"\n",
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
console.log(
|
|
20
|
+
"This command will attempt to update files to match the correct configuration.\n",
|
|
21
|
+
);
|
|
22
|
+
console.log("Do you want to proceed? [Y/n] ");
|
|
23
|
+
createInterface({
|
|
24
|
+
input: process.stdin,
|
|
25
|
+
output: process.stdout,
|
|
26
|
+
}).question("", (answer) => {
|
|
27
|
+
if (answer.toLowerCase() === "y") {
|
|
28
|
+
initialize();
|
|
29
|
+
} else {
|
|
30
|
+
console.log("Exiting...");
|
|
31
|
+
process.exit(0);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function initialize(): void {
|
|
37
|
+
const CONFIG_PATH = path.join(process.cwd(), "./as-test.config.json");
|
|
38
|
+
if (existsSync(CONFIG_PATH)) {
|
|
39
|
+
console.log("Found ./as-test.config.json. Updating...");
|
|
40
|
+
process.exit(0);
|
|
41
|
+
} else {
|
|
42
|
+
console.log("Wrote ./as-test.config.json");
|
|
43
|
+
writeFileSync(CONFIG_PATH, JSON.stringify(new Config(), null, 2));
|
|
44
|
+
console.log(JSON.stringify(new Config(), null, 2));
|
|
45
|
+
process.exit(0);
|
|
46
|
+
}
|
|
47
|
+
}
|
package/cli/run.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { existsSync, readFileSync, readdirSync } from "fs";
|
|
2
|
+
import { Config } from "./types.js";
|
|
3
|
+
import chalk from "chalk";
|
|
4
|
+
import { exec } from "child_process";
|
|
5
|
+
import { glob } from "glob";
|
|
6
|
+
|
|
7
|
+
const installScripts = new Map<string, string>([
|
|
8
|
+
["wasmtime", "curl https://wasmtime.dev/install.sh -sSf | bash"],
|
|
9
|
+
["wasmer", "curl https://get.wasmer.io -sSfL | sh"],
|
|
10
|
+
]);
|
|
11
|
+
export async function run() {
|
|
12
|
+
const config = Object.assign(
|
|
13
|
+
new Config(),
|
|
14
|
+
JSON.parse(readFileSync("./as-test.config.json").toString()),
|
|
15
|
+
) as Config;
|
|
16
|
+
const inputFiles = await glob(config.input);
|
|
17
|
+
|
|
18
|
+
console.log(
|
|
19
|
+
chalk.dim("Running tests using " + config.runOptions.runtime.name + ""),
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
let execPath = "";
|
|
23
|
+
const PATH = process.env["PATH"]?.split(":")!;
|
|
24
|
+
for (const bin of PATH) {
|
|
25
|
+
if (bin.startsWith("/mnt/")) continue; // WSL
|
|
26
|
+
if (!existsSync(bin)) continue;
|
|
27
|
+
for (const file of readdirSync(bin)) {
|
|
28
|
+
if (
|
|
29
|
+
file == config.runOptions.runtime.name ||
|
|
30
|
+
file == config.runOptions.runtime.name + ".exe"
|
|
31
|
+
) {
|
|
32
|
+
execPath = bin + "/" + file;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (!execPath) {
|
|
38
|
+
console.log(
|
|
39
|
+
chalk.bgRed(" ERROR ") +
|
|
40
|
+
chalk.dim(":") +
|
|
41
|
+
" could not locate " +
|
|
42
|
+
config.runOptions.runtime.name +
|
|
43
|
+
" in your PATH variable. Either set it, or install it" +
|
|
44
|
+
(config.runOptions.runtime.name
|
|
45
|
+
? "using " +
|
|
46
|
+
chalk.dim(installScripts.get(config.runOptions.runtime.name))
|
|
47
|
+
: "."),
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
for (const file of inputFiles) {
|
|
51
|
+
const outFile =
|
|
52
|
+
config.outDir +
|
|
53
|
+
"/" +
|
|
54
|
+
file.slice(file.lastIndexOf("/") + 1).replace(".ts", ".wasm");
|
|
55
|
+
exec(
|
|
56
|
+
config.runOptions.runtime.run
|
|
57
|
+
.replace(config.runOptions.runtime.name, execPath)
|
|
58
|
+
.replace("<file>", outFile),
|
|
59
|
+
(err, stdout, stderr) => {
|
|
60
|
+
process.stdout.write(stdout);
|
|
61
|
+
process.stderr.write(stderr);
|
|
62
|
+
if (err) {
|
|
63
|
+
process.exit(err.code);
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
}
|
package/cli/types.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export class Config {
|
|
2
|
+
input: string[] = ["./assembly/__tests__/*.spec.ts"];
|
|
3
|
+
outDir: string = "./build";
|
|
4
|
+
config: string = "./asconfig.json";
|
|
5
|
+
suites: Suite[] = [];
|
|
6
|
+
coverage: Coverage = new Coverage();
|
|
7
|
+
buildOptions: BuildOptions = new BuildOptions();
|
|
8
|
+
runOptions: RunOptions = new RunOptions();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class Suite {
|
|
12
|
+
name: string = "";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class Coverage {
|
|
16
|
+
enabled: boolean = false;
|
|
17
|
+
show: boolean = false;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class BuildOptions {
|
|
21
|
+
args: string[] = [];
|
|
22
|
+
wasi: boolean = true;
|
|
23
|
+
parallel: boolean = true;
|
|
24
|
+
verbose: boolean = true;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class RunOptions {
|
|
28
|
+
runtime: Runtime = new Runtime();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class Runtime {
|
|
32
|
+
name: string = "wasmtime";
|
|
33
|
+
run: string = "wasmtime <file>";
|
|
34
|
+
}
|
package/cli/util.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export function formatTime(ms: number): string {
|
|
2
|
+
if (ms < 0) {
|
|
3
|
+
throw new Error("Time should be a non-negative number.");
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
// Convert milliseconds to microseconds
|
|
7
|
+
const us = ms * 1000;
|
|
8
|
+
|
|
9
|
+
const units: {
|
|
10
|
+
name: string;
|
|
11
|
+
divisor: number;
|
|
12
|
+
}[] = [
|
|
13
|
+
{ name: "μs", divisor: 1 },
|
|
14
|
+
{ name: "ms", divisor: 1000 },
|
|
15
|
+
{ name: "s", divisor: 1000 * 1000 },
|
|
16
|
+
{ name: "m", divisor: 60 * 1000 * 1000 },
|
|
17
|
+
{ name: "h", divisor: 60 * 60 * 1000 * 1000 },
|
|
18
|
+
{ name: "d", divisor: 24 * 60 * 60 * 1000 * 1000 },
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
for (let i = units.length - 1; i >= 0; i--) {
|
|
22
|
+
const unit = units[i]!;
|
|
23
|
+
if (us >= unit.divisor) {
|
|
24
|
+
const value = Math.round((us / unit.divisor) * 1000) / 1000;
|
|
25
|
+
return `${value}${unit.name}`;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return `${us}us`;
|
|
30
|
+
}
|
package/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from "./assembly/index";
|
|
1
|
+
export * from "./assembly/index";
|
package/jest.test.js
CHANGED
|
@@ -1,5 +1,44 @@
|
|
|
1
|
-
describe("
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
});
|
|
1
|
+
describe("Math operations", () => {
|
|
2
|
+
// Setup before each test in this group (optional)
|
|
3
|
+
beforeEach(() => {
|
|
4
|
+
log("Initializing test...");
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
// Teardown after each test in this group (optional)
|
|
8
|
+
afterEach(() => {
|
|
9
|
+
log("Cleaning up after test...");
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test("Addition", () => {
|
|
13
|
+
expect(1 + 2).toBe(3);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("Comparison", () => {
|
|
17
|
+
expect(5).toBeGreaterThan(3);
|
|
18
|
+
expect(2).toBeLessThan(4);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("Type checking", () => {
|
|
22
|
+
expect("hello").toBeString();
|
|
23
|
+
expect(true).toBeBoolean();
|
|
24
|
+
expect(10.5).toBeNumber();
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
let myArray = [];
|
|
29
|
+
|
|
30
|
+
describe("Array manipulation", () => {
|
|
31
|
+
beforeAll(() => {
|
|
32
|
+
myArray = [1, 2, 3];
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("Array length", () => {
|
|
36
|
+
expect(myArray).toHaveLength(3);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test("Array inclusion", () => {
|
|
40
|
+
expect(myArray).toContain(2);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
function foo() {}
|
package/package.json
CHANGED
|
@@ -1,37 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "as-test",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"description": "Testing framework for AssemblyScript. Compatible with WASI or Bindings ",
|
|
5
5
|
"types": "assembly/index.ts",
|
|
6
6
|
"author": "Jairus Tanaka",
|
|
7
7
|
"contributors": [],
|
|
8
8
|
"license": "MIT",
|
|
9
9
|
"scripts": {
|
|
10
|
-
"test": "
|
|
11
|
-
"
|
|
12
|
-
"
|
|
10
|
+
"test": "node ./bin/index.js run",
|
|
11
|
+
"test:tap": "node ./bin/index.js --tap",
|
|
12
|
+
"pretest": "node ./bin/index.js build",
|
|
13
13
|
"build:transform": "tsc -p ./transform",
|
|
14
|
-
"
|
|
14
|
+
"build:cli": "tsc -p cli",
|
|
15
|
+
"prettier": "prettier -w .",
|
|
16
|
+
"prepublish": "npm run build:cli && npm run build:transform && npm run test"
|
|
15
17
|
},
|
|
16
18
|
"devDependencies": {
|
|
17
|
-
"as-test": "./",
|
|
18
19
|
"@assemblyscript/wasi-shim": "^0.1.0",
|
|
19
|
-
"@types/
|
|
20
|
+
"@types/node": "^20.14.9",
|
|
21
|
+
"as-test": "./",
|
|
20
22
|
"assemblyscript": "^0.27.28",
|
|
21
23
|
"assemblyscript-prettier": "^3.0.1",
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"visitor-as": "^0.11.4",
|
|
25
|
-
"json-as": "^0.9.8"
|
|
24
|
+
"typescript": "^5.5.3",
|
|
25
|
+
"visitor-as": "^0.11.4"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"as-console": "^
|
|
28
|
+
"as-console": "^7.0.0",
|
|
29
29
|
"as-rainbow": "^0.1.0",
|
|
30
|
-
"as-string-sink": "^0.5.3",
|
|
31
30
|
"as-variant": "^0.4.1",
|
|
32
31
|
"chalk": "^5.3.0",
|
|
33
|
-
"
|
|
34
|
-
"
|
|
32
|
+
"glob": "^10.4.2",
|
|
33
|
+
"jest": "^29.7.0"
|
|
35
34
|
},
|
|
36
35
|
"overrides": {
|
|
37
36
|
"assemblyscript": "$assemblyscript",
|
|
@@ -55,5 +54,9 @@
|
|
|
55
54
|
"type": "module",
|
|
56
55
|
"publishConfig": {
|
|
57
56
|
"@JairusSW:registry": "https://npm.pkg.github.com"
|
|
57
|
+
},
|
|
58
|
+
"bin": {
|
|
59
|
+
"as-test": "./bin/index.js",
|
|
60
|
+
"ast": "./bin/index.js"
|
|
58
61
|
}
|
|
59
62
|
}
|