Skip to content

Commit

Permalink
refactor(testament): avoid circular deps
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Sep 6, 2021
1 parent 8b582bc commit 459faa4
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 44 deletions.
11 changes: 1 addition & 10 deletions packages/testament/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@
"doc": "typedoc --excludePrivate --out doc --theme ../../tools/doc/typedoc-theme src/index.ts",
"pub": "yarn build:release && yarn publish --access public"
},
"dependencies": {
"@thi.ng/api": "^7.2.0",
"@thi.ng/args": "^1.1.1",
"@thi.ng/bench": "^2.1.6",
"@thi.ng/checks": "^2.9.11"
},
"devDependencies": {
"@thi.ng/testament": "^0.0.1"
},
"keywords": [
"typescript"
],
Expand Down Expand Up @@ -76,4 +67,4 @@
"status": "alpha",
"year": 2021
}
}
}
28 changes: 21 additions & 7 deletions packages/testament/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ConsoleLogger, Fn0, Fn2, ILogger } from "@thi.ng/api";
export type Fn0<A> = () => A;
export type Fn<A, B> = (a: A) => B;
export type Fn2<A, B, C> = (a: A, b: B) => C;

export type VoidFn = Fn0<void>;

export type Timestamp = number | bigint;

export interface TestOpts {
logger: ILogger;
Expand All @@ -7,15 +13,15 @@ export interface TestOpts {
}

export interface GroupOpts extends TestOpts {
beforeEach: Fn0<void>;
afterEach: Fn0<void>;
beforeEach: VoidFn;
afterEach: VoidFn;
stop: boolean;
exit: boolean;
}

export interface TestCtx {
done: Fn0<void>;
setTimeout: Fn2<Fn0<void>, number, any>;
done: VoidFn;
setTimeout: Fn2<VoidFn, number, any>;
}

export interface TestResult {
Expand All @@ -24,6 +30,14 @@ export interface TestResult {
error?: Error;
}

export let LOGGER = new ConsoleLogger("testament");
export interface ILogger {
level: number;

fine(...args: any[]): void;
debug(...args: any[]): void;
info(...args: any[]): void;
warn(...args: any[]): void;
severe(...args: any[]): void;
}

export let TIMEOUT = 5000;
export let TIMEOUT = 1000;
28 changes: 6 additions & 22 deletions packages/testament/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,17 @@
import { flag, parse } from "@thi.ng/args";
import { isString } from "@thi.ng/checks";
import { readdirSync, statSync } from "fs";
import { resolve } from "path";
import { isString } from "./utils";

interface TestamentArgs {
csv: boolean;
}
// interface TestamentArgs {
// csv: boolean;
// }

(async () => {
const res = parse<TestamentArgs>(
{ csv: flag({ desc: "Output CSV" }) },
process.argv,
{
start: 2,
usageOpts: {
prefix: `
@thi.ng/testament test runner
Usage: testament [opts] DIR1 DIR2...
Options:`,
},
}
);

if (!res) return;
const dirs = process.argv.slice(2);

// const cwd = process.argv[1];

for (let dir of res.rest) {
for (let dir of dirs) {
for (let src of files(resolve(dir), ".ts")) {
try {
await import(src);
Expand Down
6 changes: 4 additions & 2 deletions packages/testament/src/group.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Fn } from "@thi.ng/api";
import { GroupOpts, LOGGER, TestCtx, TestResult } from "./api";
import type { Fn, GroupOpts, TestCtx, TestResult } from "./api";
import { LOGGER } from "./logger";
import { test } from "./test";

export const group = async (
Expand All @@ -14,7 +14,9 @@ export const group = async (
};
let results: TestResult[] = [];
try {
logger.info("----------");
logger.info(title);
logger.info("----------");
for (let k in tests) {
beforeEach && beforeEach();
const res = await test(k, tests[k], opts);
Expand Down
36 changes: 36 additions & 0 deletions packages/testament/src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Logger {
id: string;
level: number;

constructor(id = "testament", level = 0) {
this.id = id;
this.level = level;
}

fine(...args: any[]): void {
this.level <= 0 && this.log("FINE", args);
}

debug(...args: any[]): void {
this.level <= 1 && this.log("DEBUG", args);
}

info(...args: any[]): void {
this.level <= 2 && this.log("INFO", args);
}

warn(...args: any[]): void {
this.level <= 3 && this.log("WARN", args);
}

severe(...args: any[]): void {
this.level <= 4 && this.log("SEVERE", args);
}

protected log(_: string, args: any[]) {
// console.log(`[${level}] ${this.id}:`, ...args);
console.log(...args);
}
}

export let LOGGER = new Logger();
6 changes: 3 additions & 3 deletions packages/testament/src/test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Fn } from "@thi.ng/api";
import { now, timeDiff, Timestamp } from "@thi.ng/bench";
import { LOGGER, TestCtx, TestOpts, TestResult, TIMEOUT } from "./api";
import { Fn, TestCtx, TestOpts, TestResult, TIMEOUT, Timestamp } from "./api";
import { LOGGER } from "./logger";
import { now, timeDiff } from "./utils";

export const test = async (
title: string,
Expand Down
43 changes: 43 additions & 0 deletions packages/testament/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type { Fn0, Fn2, Timestamp } from "./api";

/**
* @remarks
* Copied from thi.ng/checks to avoid circular deps
*
* @param x
*
* @internal
*/
export const isString = (x: any): x is string => typeof x === "string";

/**
* If available, returns wrapper for `process.hrtime.bigint()` else
* falls back to `Date.now()`. In all cases, returns a nanosec-scale
* timestamp, either as `bigint` or `number`.
*
* @remarks
* Copied from thi.ng/bench to avoid circular deps
*/
export const now: Fn0<Timestamp> =
typeof BigInt !== "undefined"
? typeof process !== "undefined" &&
typeof process.hrtime !== "undefined" &&
typeof process.hrtime.bigint === "function"
? () => process.hrtime.bigint()
: () => BigInt(Date.now() * 1e6)
: () => Date.now() * 1e6;

/**
* Returns the difference in milliseconds between 2 given
* {@link Timestamp}s.
*
* @remarks
* Copied from thi.ng/bench to avoid circular deps
*
* @param a
* @param b
*/
export const timeDiff: Fn2<Timestamp, Timestamp, number> = (a, b) =>
(typeof BigInt !== "undefined"
? Number(<bigint>b - <bigint>a)
: <number>b - <number>a) * 1e-6;

0 comments on commit 459faa4

Please sign in to comment.