From 663aa0b94cea8f29408fcefcba9767db22ff348d Mon Sep 17 00:00:00 2001 From: Jairus Date: Wed, 17 Jul 2024 11:27:08 -0700 Subject: [PATCH] output metadata --- assembly/__tests__/sleep.spec.ts | 10 +- assembly/src/log.ts | 2 +- bin/run.js | 101 ++++++++-------- cli/run.ts | 4 +- reporters/log/index.ts | 197 ------------------------------- test.config.json | 0 6 files changed, 61 insertions(+), 253 deletions(-) delete mode 100644 reporters/log/index.ts delete mode 100644 test.config.json diff --git a/assembly/__tests__/sleep.spec.ts b/assembly/__tests__/sleep.spec.ts index 7f9c21a..55e07dd 100644 --- a/assembly/__tests__/sleep.spec.ts +++ b/assembly/__tests__/sleep.spec.ts @@ -1,4 +1,4 @@ -import { describe, expect, run, test } from ".."; +import { describe, expect, log, run, test } from ".."; import { sleep } from "as-sleep/assembly"; describe("Should sleep", () => { test("1ms", () => { @@ -16,10 +16,12 @@ describe("Should sleep", () => { sleep(1000); expect(Date.now() - start).toBeGreaterOrEqualTo(1000); }); - test("10s", () => { + test("5s", () => { const start = Date.now(); - sleep(10000); - expect(Date.now() - start).toBeGreaterOrEqualTo(10000); + log("Sleeping...") + sleep(5000); + log("Done!") + expect(Date.now() - start).toBeGreaterOrEqualTo(5000); }); }); diff --git a/assembly/src/log.ts b/assembly/src/log.ts index bdb9a11..4d9f061 100644 --- a/assembly/src/log.ts +++ b/assembly/src/log.ts @@ -10,6 +10,6 @@ export class Log { this.text = text; } display(): void { - term.write(" ".repeat(this.depth + 1) + `${rainbow.bgMagentaBright(" LOG ")}${rainbow.dimMk(":")} ${this.text}\n`); + term.write(" ".repeat(this.depth + 1) + `${rainbow.bgBlackBright(" LOG ")}${rainbow.dimMk(":")} ${this.text}\n`); } } \ No newline at end of file diff --git a/bin/run.js b/bin/run.js index daf711d..b1edfa1 100644 --- a/bin/run.js +++ b/bin/run.js @@ -1,9 +1,9 @@ import chalk from "chalk"; import { exec } from "child_process"; import { glob } from "glob"; -import { getExec, loadConfig } from "./util.js"; +import { formatTime, getExec, loadConfig } from "./util.js"; import * as path from "path"; -import { writeFileSync } from "fs"; +import { existsSync, mkdirSync, 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() { @@ -40,7 +40,7 @@ export async function run() { .replace(command, execPath) .replace("", outFile.replace(".wasm", ".js")); } - const report = await (() => { + const report = JSON.parse(await (() => { return new Promise((res, _) => { let stdout = ""; const io = exec(cmd); @@ -53,54 +53,57 @@ export async function run() { res(stdout); }); }); - })(); + })()); reports.push(report); } - 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); + 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) { diff --git a/cli/run.ts b/cli/run.ts index 879c637..d443393 100644 --- a/cli/run.ts +++ b/cli/run.ts @@ -52,7 +52,7 @@ export async function run() { .replace("", outFile.replace(".wasm", ".js")); } - const report = await (() => { + const report = JSON.parse(await (() => { return new Promise((res, _) => { let stdout = ""; const io = exec(cmd); @@ -65,7 +65,7 @@ export async function run() { res(stdout); }); }); - })(); + })()); reports.push(report); } diff --git a/reporters/log/index.ts b/reporters/log/index.ts deleted file mode 100644 index ef6c6da..0000000 --- a/reporters/log/index.ts +++ /dev/null @@ -1,197 +0,0 @@ -import { rainbow } from "as-rainbow"; -import { Report, SuiteReport, TestReport, Time } from "../report"; -import { Verdict } from "../../assembly/index"; -import { diff } from "../../assembly/util/helpers"; -import { JSON } from "json-as"; -import { Result } from "../../plugins/index"; -class LogReporter { - public logs: Report[]; - private depth: string = ""; - - private passedFiles: i32 = 0; - private failedFiles: i32 = 0; - - private passedSuites: i32 = 0; - private failedSuites: i32 = 0; - - private passedTests: i32 = 0; - private failedTests: i32 = 0; - - private failed: SuiteReport[] = []; - - private initialized: boolean = false; - constructor(logs: Report[]) { - this.logs = logs; - } - depthInc(): void { - this.depth += " "; - } - depthDec(): void { - this.depth = this.depth.slice(0, this.depth.length - 2); - } - init(): string { - if (this.initialized) return ""; - this.initialized = true; - let out: string = ""; - out += rainbow.boldMk( - rainbow.blueBright( - ` _____ _____ _____ _____ _____ _____ \n` + - `| _ || __| ___|_ _|| __|| __||_ _|\n` + - `| ||__ ||___| | | | __||__ | | | \n` + - `|__|__||_____| |_| |_____||_____| |_| \n`, - ), - ); - out += rainbow.dimMk( - "\n------------------- v0.3.0 -------------------\n\n", - ); - - // @ts-ignore - if (isDefined(COVERAGE_USE)) { - out += - rainbow.bgBlueBright(" PLUGIN ") + - " " + - rainbow.dimMk("Using Code Coverage") + - "\n\n"; - } - return out; - } - report(): string { - let out: string = ""; - out += this.init(); - for (let i = 0; i < this.logs.length; i++) { - const log = unchecked(this.logs[i]); - if (log.verdict === Verdict.Fail) this.failedFiles++; - else this.passedFiles++; - out += this.reportLog(log); - } - out += this.summarize(); - return out; - } - reportLog(log: Report): string { - // @ts-ignore - let out: string = ""; - - out += `${rainbow.bgCyanBright(" FILE ")} ${rainbow.dimMk(log.file)} ${rainbow.italicMk(log.time.format())}\n\n`; - - for (let i = 0; i < log.groups.length; i++) { - const group = unchecked(log.groups[i]); - if (group.verdict === Verdict.Fail) { - this.failedSuites++; - } else { - this.passedSuites++; - } - out += this.reportSuite(group); - } - - return out; - } - reportSuite(suite: SuiteReport): string { - let out = ""; - this.depthInc(); - if (suite.verdict == Verdict.Ok) { - this.passedSuites++; - out += `${this.depth}${rainbow.bgGreenBright(" PASS ")} ${rainbow.dimMk(suite.description)} ${rainbow.italicMk(suite.time.format())}\n\n`; - } else if (suite.verdict == Verdict.Fail) { - this.failedSuites++; - out += `${this.depth}${rainbow.bgRed(" FAIL ")} ${rainbow.dimMk(suite.description)} ${rainbow.italicMk(suite.time.format())}\n\n`; - } else if (suite.verdict == Verdict.None) { - out += `${this.depth}${rainbow.bgBlackBright(" EMPTY ")} ${rainbow.dimMk(suite.description)} ${rainbow.italicMk("0.00μs")}\n\n`; - } - - for (let i = 0; i < suite.tests.length; i++) { - const _test = unchecked(suite.tests[i]); - if (_test.verdict != Verdict.Ok) { - this.failedTests++; - if (!this.failed.includes(suite)) this.failed.push(suite); - } else { - this.passedTests++; - } - } - - for (let i = 0; i < suite.suites.length; i++) { - const _suite = unchecked(suite.suites[i]); - out += this.reportSuite(_suite); - } - this.depthDec(); - return out; - } - reportTest(test: TestReport): string { - let out: string = ""; - this.depthInc(); - const dif = diff(test.left, test.right); - out += - this.depth + - rainbow.dimMk("(expected) ->") + - " " + - rainbow.boldMk(dif.left) + - "\n"; - out += - this.depth + - rainbow.dimMk("(received) ->") + - " " + - rainbow.boldMk(dif.right) + - "\n\n"; - this.depthDec(); - return out; - } - errors(): string { - let out: string = ""; - if (!this.failed.length) return ""; - out += rainbow.dimMk("----------------- [FAILED] -------------------\n\n"); - for (let i = 0; i < this.failed.length; i++) { - const suite = unchecked(this.failed[i]); - out += `${rainbow.bgRed(" FAIL ")} ${rainbow.dimMk(suite.description)} ${rainbow.italicMk(suite.time.format())}\n\n`; - for (let i = 0; i < suite.tests.length; i++) { - const _test = unchecked(suite.tests[i]); - if (_test.verdict != Verdict.Ok) { - out += this.reportTest(_test); - } - } - } - return out; - } - summarize(): string { - let out: string = ""; - out += this.errors(); - out += rainbow.dimMk("----------------- [RESULTS] ------------------\n\n"); - - const filesResult = new Result( - "Files: ", - this.failedFiles, - this.passedFiles, - ); - out += filesResult.display(); - - const suitesResult = new Result( - "Suites: ", - this.failedSuites, - this.passedSuites, - ); - out += suitesResult.display(); - - const testsResult = new Result( - "Tests: ", - this.failedTests, - this.passedTests, - ); - out += testsResult.display(); - - // @ts-ignore - const time = new Time(); - for (let i = 0; i < this.logs.length; i++) { - const log = unchecked(this.logs[i]); - time.end += log.time.end - log.time.start; - } - - out += `${rainbow.boldMk("Time:")} ${time.format()}`; - - return out; - } -} - -export function report(_logs: string): void { - const logs = JSON.parse(_logs); - const reporter = new LogReporter(logs); - const out = reporter.report(); - console.log(out); -} diff --git a/test.config.json b/test.config.json deleted file mode 100644 index e69de29..0000000