Skip to content

Commit

Permalink
invisible message passing
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jul 17, 2024
1 parent de404a7 commit f36e38c
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 72 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ as-test test

## Running

You can run as-test _anywhere_ that WASI is supported! I've yet to add support for bindings, but all it needs is access to the terminal.

And finally, run it with:

```bash
npm run test
```
Expand Down
10 changes: 5 additions & 5 deletions assembly/__tests__/sleep.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ describe("Should sleep", () => {
sleep(10);
expect(Date.now() - start).toBeGreaterOrEqualTo(10);
});
test("100ms", () => {
const start = Date.now();
sleep(100);
expect(Date.now() - start).toBeGreaterOrEqualTo(100);
});
test("1s", () => {
const start = Date.now();
sleep(1000);
expect(Date.now() - start).toBeGreaterOrEqualTo(1000);
});
test("10s", () => {
const start = Date.now();
sleep(10000);
expect(Date.now() - start).toBeGreaterOrEqualTo(10000);
});
});

run();
14 changes: 12 additions & 2 deletions assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,18 @@ export function run(options: RunOptions = new RunOptions()): void {
}
time.end = performance.now();
fileLn.edit(`${rainbow.bgCyanBright(" FILE ")} ${rainbow.dimMk(FILE)} ${rainbow.dimMk(time.format())}`);
const reportText = "\x1B[8mSTART_READ" + JSON.stringify(entrySuites) + "END_READ\x1B[0m\n";
term.write(reportText).clear();
const reportText = JSON.stringify(entrySuites);
const chunk_size = 48;
let chunks = reportText.length / chunk_size;
let index = 0;
term.write("\x1B[8m\n").clear(); // Hide text (so that the cursor doesn't flash for a moment)
while (chunks--) {
term.write("READ_LINE" + reportText.slice(index, index += chunk_size) + "END_LINE\n").clear(); // Write a line and then clear it, making it invisible
}
if (index < reportText.length) {
term.write("READ_LINE" + reportText.slice(index) + "END_LINE\n").clear();
}
term.write("\x1B[0m\n").clear(); // Un-hide text
}

export class Result {
Expand Down
116 changes: 62 additions & 54 deletions bin/run.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import chalk from "chalk";
import { exec } from "child_process";
import { glob } from "glob";
import { formatTime, getExec, loadConfig } from "./util.js";
import { getExec, loadConfig } from "./util.js";
import * as path from "path";
import { existsSync, mkdirSync, writeFileSync } from "fs";
import { writeFileSync } from "fs";
const CONFIG_PATH = path.join(process.cwd(), "./as-test.config.json");
const ansi = new RegExp("[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))", "g");
export async function run() {
Expand Down Expand Up @@ -40,70 +40,67 @@ export async function run() {
.replace(command, execPath)
.replace("<file>", outFile.replace(".wasm", ".js"));
}
const report = JSON.parse(await (() => {
const report = await (() => {
return new Promise((res, _) => {
let stdout = "";
const io = exec(cmd);
io.stdout.pipe(process.stdout);
io.stderr.pipe(process.stderr);
io.stdout.on("data", (data) => {
stdout += data;
stdout += readData(data);
});
io.stdout.on("close", () => {
res(stdout.slice(stdout.indexOf("START_READ") + 10, stdout.indexOf("END_READ")));
res(stdout);
});
});
})());
})();
reports.push(report);
}
if (config.logs && config.logs != "none") {
if (!existsSync(path.join(process.cwd(), config.logs))) {
mkdirSync(path.join(process.cwd(), config.logs));
}
writeFileSync(path.join(process.cwd(), config.logs, "test.log.json"), JSON.stringify(reports, null, 2));
}
const reporter = new Reporter(reports);
if (reporter.failed.length) {
console.log(chalk.dim("----------------- [FAILED] -------------------\n"));
for (const failed of reporter.failed) {
console.log(`${chalk.bgRed(" FAIL ")} ${chalk.dim(failed.description)}\n`);
for (const test of failed.tests) {
if (test.verdict == "fail") {
console.log(`${chalk.dim("(expected) ->")} ${chalk.bold(test._left.toString())}`);
console.log(`${chalk.dim("(received) ->")} ${chalk.bold(test._right.toString())}\n`);
}
}
}
}
console.log("----------------- [RESULTS] ------------------\n");
process.stdout.write(chalk.bold("Files: "));
if (reporter.failedFiles) {
process.stdout.write(chalk.bold.red(reporter.failedFiles + " failed"));
}
else {
process.stdout.write(chalk.bold.greenBright("0 failed"));
}
process.stdout.write(", " + (reporter.failedFiles + reporter.passedFiles) + " total\n");
process.stdout.write(chalk.bold("Suites: "));
if (reporter.failedSuites) {
process.stdout.write(chalk.bold.red(reporter.failedSuites + " failed"));
}
else {
process.stdout.write(chalk.bold.greenBright("0 failed"));
}
process.stdout.write(", " + (reporter.failedSuites + reporter.passedSuites) + " total\n");
process.stdout.write(chalk.bold("Tests: "));
if (reporter.failedTests) {
process.stdout.write(chalk.bold.red(reporter.failedTests + " failed"));
}
else {
process.stdout.write(chalk.bold.greenBright("0 failed"));
}
process.stdout.write(", " + (reporter.failedTests + reporter.passedTests) + " total\n");
process.stdout.write(chalk.bold("Time: ") + formatTime(reporter.time) + "\n");
if (reporter.failedFiles)
process.exit(1);
process.exit(0);
writeFileSync(path.join(process.cwd(), config.logs, "test.log.json"), reports.join("\n\n"));
// if (config.logs && config.logs != "none") {
// if (!existsSync(path.join(process.cwd(), config.logs))) {
// mkdirSync(path.join(process.cwd(), config.logs));
// }
// writeFileSync(path.join(process.cwd(), config.logs, "test.log.json"), JSON.stringify(reports, null, 2));
// }
// const reporter = new Reporter(reports);
// if (reporter.failed.length) {
// console.log(chalk.dim("----------------- [FAILED] -------------------\n"));
// for (const failed of reporter.failed) {
// console.log(`${chalk.bgRed(" FAIL ")} ${chalk.dim(failed.description)}\n`);
// for (const test of failed.tests) {
// if (test.verdict == "fail") {
// console.log(`${chalk.dim("(expected) ->")} ${chalk.bold(test._left.toString())}`);
// console.log(`${chalk.dim("(received) ->")} ${chalk.bold(test._right.toString())}\n`);
// }
// }
// }
// }
// console.log("----------------- [RESULTS] ------------------\n");
// process.stdout.write(chalk.bold("Files: "));
// if (reporter.failedFiles) {
// process.stdout.write(chalk.bold.red(reporter.failedFiles + " failed"));
// } else {
// process.stdout.write(chalk.bold.greenBright("0 failed"));
// }
// process.stdout.write(", " + (reporter.failedFiles + reporter.passedFiles) + " total\n");
// process.stdout.write(chalk.bold("Suites: "));
// if (reporter.failedSuites) {
// process.stdout.write(chalk.bold.red(reporter.failedSuites + " failed"));
// } else {
// process.stdout.write(chalk.bold.greenBright("0 failed"));
// }
// process.stdout.write(", " + (reporter.failedSuites + reporter.passedSuites) + " total\n");
// process.stdout.write(chalk.bold("Tests: "));
// if (reporter.failedTests) {
// process.stdout.write(chalk.bold.red(reporter.failedTests + " failed"));
// } else {
// process.stdout.write(chalk.bold.greenBright("0 failed"));
// }
// process.stdout.write(", " + (reporter.failedTests + reporter.passedTests) + " total\n");
// process.stdout.write(chalk.bold("Time: ") + formatTime(reporter.time) + "\n");
// if (reporter.failedFiles) process.exit(1);
// process.exit(0);
}
class Reporter {
constructor(reports) {
Expand Down Expand Up @@ -173,3 +170,14 @@ class Reporter {
}
}
}
function readData(data) {
let out = "";
const start = data.indexOf("READ_LINE");
if (start >= 0) {
const slice = data.slice(start + 9);
const end = slice.indexOf("END_LINE");
out += slice.slice(0, end);
out += readData(slice);
}
return out;
}
24 changes: 17 additions & 7 deletions cli/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { glob } from "glob";

import { formatTime, getExec, loadConfig } from "./util.js";
import * as path from "path";
import { appendFileSync, existsSync, mkdirSync, writeFileSync } from "fs";
import { appendFileSync, existsSync, fstat, mkdirSync, writeFileSync } from "fs";

const CONFIG_PATH = path.join(process.cwd(), "./as-test.config.json");

Expand Down Expand Up @@ -52,22 +52,20 @@ export async function run() {
.replace("<file>", outFile.replace(".wasm", ".js"));
}

const report = JSON.parse(await (() => {
const report = await (() => {
return new Promise<string>((res, _) => {
let stdout = "";
const io = exec(cmd);
io.stdout.pipe(process.stdout);
io.stderr.pipe(process.stderr);

io.stdout.on("data", (data: string) => {
stdout += data;
stdout += readData(data);
});

io.stdout.on("close", () => {
res(stdout.slice(stdout.indexOf("START_READ") + 10, stdout.indexOf("END_READ")));
res(stdout);
});
});
})());
})();
reports.push(report);
}

Expand Down Expand Up @@ -188,4 +186,16 @@ class Reporter {
this.passedTests++;
}
}
}

function readData(data: string): string {
let out = "";
const start = data.indexOf("READ_LINE");
if (start >= 0) {
const slice = data.slice(start + 9);
const end = slice.indexOf("END_LINE");
out += slice.slice(0, end);
out += readData(slice);
}
return out;
}

0 comments on commit f36e38c

Please sign in to comment.