Skip to content

Commit

Permalink
release: v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jul 17, 2024
1 parent 208aebe commit 13f1338
Show file tree
Hide file tree
Showing 17 changed files with 237 additions and 111 deletions.
6 changes: 1 addition & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,4 @@ v0.1.10 - Feat: support node, deno, and bun
v0.2.0 - Fix mock -> mockFn artifacts
v0.2.1 - Remove accidental logging

v0.3.0 - Pass metadata through terminal
- Support for multiple files
- Better reporting
- Timing for suites
- Terminal utilities
v0.3.0 - Pass metadata through terminal - Support for multiple files - Better reporting - Timing for suites - Terminal utilities
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ as-test init

Note: You can use either `ast` or `as-test` in the terminal.

Next, create a test file
Next, take a look at the generated test file

`assembly/__tests__/example.spec.ts`

Expand All @@ -56,7 +56,7 @@ afterAll(() => {

// Mock/override the function console.log
mockFn<void>("console.log", (data: string): void => {
console.log("[MOCKED]: " + data + "\\n");
console.log("[MOCKED]: " + data + "\n");
});

describe("Should sleep", () => {
Expand Down Expand Up @@ -130,17 +130,13 @@ function sleep(ms: i64): void {
Build and run it using as-test

```bash
as-test test
npm run test
```

<h6>

## Running

```bash
npm run test
```

To add `as-test` to your CI/CD workflow, check out [The provided example](https://github.com/JairusSW/as-test/blob/main/.github/workflows/nodejs.yml)

If you use this project in your codebase, consider dropping a [⭐ HERE](https://github.com/JairusSW/as-test). I would really appreciate it!
Expand Down
14 changes: 6 additions & 8 deletions as-test.config.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
{
"input": [
"./assembly/__tests__/*.spec.ts"
],
"input": ["./assembly/__tests__/*.spec.ts"],
"outDir": "./build",
"logs": "./logs",
"config": "none",
"plugins": {
"coverage": true
},
"buildOptions": {
"args": ["--transform json-as/transform"],
"target": "wasi"
"args": [],
"target": "bindings"
},
"runOptions": {
"runtime": {
"name": "wasmtime",
"run": "wasmtime <file>"
"name": "node",
"run": "node ./tests/<name>.run.js"
}
}
}
}
2 changes: 1 addition & 1 deletion assembly/util/term.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ export function writeRaw(data: string): void {
} else {
process_stdout_write(data);
}
}
}
2 changes: 1 addition & 1 deletion bin/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function buildFile(command) {
}
function getBuildArgs(config) {
let buildArgs = "";
buildArgs += " --transform as-test/transform";
buildArgs += " --transform as-test/transform --transform json-as/transform";
if (config.config && config.config !== "none") {
buildArgs += " --config " + config.config;
}
Expand Down
63 changes: 48 additions & 15 deletions bin/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import * as path from "path";
import { createInterface } from "readline";
import { loadConfig } from "./util.js";
const rl = createInterface({
input: process.stdin,
output: process.stdout,
});
const TARGETS = ["wasi", "bindings"];
export async function init(args) {
const rl = createInterface({
input: process.stdin,
output: process.stdout,
});
console.log(chalk.bold("as-test init v0.3.0") + "\n");
console.log(chalk.dim("[1/3]") + " select a target [wasi/bindings]");
const target = await ask(chalk.dim(" -> "));
const target = await ask(chalk.dim(" -> "), rl);
if (!TARGETS.includes(target)) {
console.log("Invalid target " + target + ". Exiting.");
process.exit(0);
}
process.stdout.write(`\u001B[1A`);
process.stdout.write("\x1B[2K");
process.stdout.write("\x1B[0G");
console.log("\n" + chalk.dim("[2/3]") + " attempting to create the following files. Continue? [y/n]\n");
console.log("\n" +
chalk.dim("[2/3]") +
" attempting to create the following files. Continue? [y/n]\n");
console.log(chalk.dim(` ├── 📂 assembly/
│ └── 📂 __tests__/
│ └── 🧪 example.spec.ts
Expand All @@ -29,7 +31,7 @@ export async function init(args) {
│ └── 📃 as-test.run.js
├── ⚙️ as-test.config.json
└── ⚙️ package.json\n`));
const cont = (await ask(chalk.dim(" -> "))).toLowerCase().trim();
const cont = (await ask(chalk.dim(" -> "), rl)).toLowerCase().trim();
if (cont == "n" || cont == "no") {
console.log("Exiting.");
process.exit(0);
Expand All @@ -43,7 +45,7 @@ export async function init(args) {
else if (target == "bindings" && config.buildOptions.target != "bindings") {
config.buildOptions.target = "bindings";
config.runOptions.runtime.name = "node";
config.runOptions.runtime.run = "node ./tests/as-test.run.js";
config.runOptions.runtime.run = "node ./tests/<name>.run.js";
}
writeFile("./as-test.config.json", JSON.stringify(config, null, 2));
writeFile("./assembly/__tests__/example.spec.ts", `import {
Expand Down Expand Up @@ -139,13 +141,23 @@ function sleep(ms: i64): void {
}`);
writeDir("./build/");
writeDir("./logs/");
writeFile("./tests/as-test.run.js", ``);
if (target == "bindings") {
writeFile("./tests/example.run.js", `import { readFileSync } from "fs";
import { instantiate } from "../build/example.spec.js";
const binary = readFileSync("./build/example.spec.wasm");
const module = new WebAssembly.Module(binary);
const exports = instantiate(module, {});`);
}
const PKG_PATH = path.join(process.cwd(), "./package.json");
if (!hasDep(PKG_PATH, "assemblyscript")) {
console.log(chalk.dim("AssemblyScript is not included in dependencies.\nInstall it with " +
(process.env.npm_config_user_agent == "yarn"
? process.env.npm_config_user_agent + " add assemblyscript"
: process.env.npm_config_user_agent + " install assemblyscript")));
}
const pkg = JSON.parse(existsSync(PKG_PATH) ? readFileSync(PKG_PATH).toString() : "{}");
if (!pkg["devDependencies"])
pkg["devDependencies"] = {};
if (!pkg["devDependencies"]["as-test"])
pkg["devDependencies"]["as-test"] = "^0.3.0";
if (!pkg["scripts"])
pkg["scripts"] = {};
if (pkg.scripts["test"])
Expand All @@ -157,12 +169,19 @@ function sleep(ms: i64): void {
else {
pkg.scripts["test"] = "as-test test";
}
if (!pkg["devDependencies"])
pkg["devDependencies"] = {};
if (!pkg["devDependencies"]["as-test"])
pkg["devDependencies"]["as-test"] = "^0.3.0";
if (target == "bindings") {
pkg["type"] = "module";
}
writeFileSync(PKG_PATH, JSON.stringify(pkg, null, 2));
process.exit(0);
}
function ask(question) {
function ask(question, face) {
return new Promise((res, _) => {
rl.question(question, (answer) => {
face.question(question, (answer) => {
res(answer);
});
});
Expand All @@ -181,3 +200,17 @@ function writeDir(pth) {
return;
mkdirSync(fmtPath);
}
function hasDep(PKG_PATH, dep) {
const pkg = JSON.parse(existsSync(PKG_PATH) ? readFileSync(PKG_PATH).toString() : "{}");
if (existsSync(path.join(process.cwd(), "./node_modules/", dep)))
return true;
if (pkg.dependencies &&
!Object.keys(pkg.dependencies).includes(dep) &&
pkg.devDependencies &&
!Object.keys(pkg.devDependencies).includes(dep) &&
pkg.peerDependencies &&
!Object.keys(pkg.peerDependencies).includes(dep)) {
return false;
}
return true;
}
23 changes: 17 additions & 6 deletions bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,24 @@ export async function run() {
for (let i = 0; i < inputFiles.length; i++) {
const file = inputFiles[i];
const outFile = path.join(config.outDir, file.slice(file.lastIndexOf("/") + 1).replace(".ts", ".wasm"));
let cmd = config.runOptions.runtime.run
.replace(command, execPath)
.replace("<file>", outFile);
let cmd = config.runOptions.runtime.run.replace(command, execPath);
if (config.buildOptions.target == "bindings") {
cmd = config.runOptions.runtime.run
.replace(command, execPath)
.replace("<file>", outFile.replace(".wasm", ".js"));
cmd = config.runOptions.runtime.run.replace(command, execPath);
if (cmd.includes("<name>")) {
cmd = cmd.replace("<name>", file
.slice(file.lastIndexOf("/") + 1)
.replace(".ts", "")
.replace(".spec", ""));
}
else {
cmd = cmd.replace("<file>", outFile
.replace("build", "tests")
.replace(".spec", "")
.replace(".wasm", ".run.js"));
}
}
else {
cmd = cmd.replace("<file>", outFile);
}
const report = JSON.parse(await (() => {
return new Promise((res, _) => {
Expand Down
3 changes: 3 additions & 0 deletions bin/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export class Config {
this.outDir = "./build";
this.logs = "./logs";
this.config = "none";
this.plugins = {
coverage: true,
};
this.buildOptions = new BuildOptions();
this.runOptions = new RunOptions();
}
Expand Down
2 changes: 1 addition & 1 deletion cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function buildFile(command: string): void {
function getBuildArgs(config: Config): string {
let buildArgs = "";

buildArgs += " --transform as-test/transform";
buildArgs += " --transform as-test/transform --transform json-as/transform";

if (config.config && config.config !== "none") {
buildArgs += " --config " + config.config;
Expand Down
Loading

0 comments on commit 13f1338

Please sign in to comment.