Skip to content

Commit

Permalink
Test runner debug mode (#268)
Browse files Browse the repository at this point in the history
* Refactored test runner and logger into classes that accept configuration
* Added debugging feature to test logger
  • Loading branch information
lfportal authored Jun 6, 2019
1 parent d17dcd1 commit bf061f9
Show file tree
Hide file tree
Showing 5 changed files with 482 additions and 457 deletions.
29 changes: 15 additions & 14 deletions cli/src/commands/test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Command, flags } from "@oclif/command";
import { safeParse } from "../common/safe-parse";
import { runTest, TestFilter } from "../test-utils/test-runner";
import { TestFilter } from "../test-utils/common";
import { TestRunner } from "../test-utils/test-runner";

const ARG_API = "spot_contract";

Expand All @@ -27,6 +28,7 @@ export default class Test extends Command {

static flags = {
help: flags.help({ char: "h" }),
debug: flags.boolean(),
url: flags.string({
required: true,
char: "u",
Expand All @@ -44,23 +46,22 @@ export default class Test extends Command {

async run() {
const { args, flags } = this.parse(Test);
const { url: baseUrl, stateUrl: baseStateUrl, testFilter } = flags;
const { url: baseUrl, stateUrl: baseStateUrl, testFilter, debug } = flags;
const { definition } = safeParse.call(this, args[ARG_API]);

const resolvedBaseStateUrl = baseStateUrl
? baseStateUrl
: `${baseUrl}/state`;

const filter = testFilter ? parseTestFilter(testFilter) : undefined;

const allPassed = await runTest(
definition,
resolvedBaseStateUrl,
const testRunnerConfig = {
baseStateUrl: baseStateUrl ? baseStateUrl : `${baseUrl}/state`,
baseUrl,
filter
);
debugMode: debug
};
const testConfig = {
testFilter: testFilter ? parseTestFilter(testFilter) : undefined
};

const testRunner = new TestRunner(testRunnerConfig);
const passed = await testRunner.test(definition, testConfig);

if (!allPassed) {
if (!passed) {
this.exit(1);
}
}
Expand Down
8 changes: 8 additions & 0 deletions cli/src/test-utils/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface TestConfig {
testFilter?: TestFilter;
}

export interface TestFilter {
endpoint: string;
test?: string;
}
72 changes: 44 additions & 28 deletions cli/src/test-utils/test-logger.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,54 @@
import chalk from "chalk";

export const TestLogger = {
error,
log,
success,
warn
};

// tslint:disable:no-console
function error(message: string, opts?: LoggerOpts) {
console.log(chalk.red(transformMessage(message, opts)));
}
export class TestLogger {
/** Prepares an object for printing */
static formatObject(obj: any): string {
return JSON.stringify(obj, undefined, 2);
}

function log(message: string, opts?: LoggerOpts) {
console.log(chalk.dim.white(transformMessage(message, opts)));
}
private readonly debugMode: boolean;

function success(message: string, opts?: LoggerOpts) {
console.log(chalk.green(transformMessage(message, opts)));
}
constructor(opts?: LoggerOpts) {
this.debugMode = opts ? !!opts.debugMode : false;
}

function warn(message: string, opts?: LoggerOpts) {
console.log(chalk.yellow(transformMessage(message, opts)));
}
// tslint:enable:no-console

function transformMessage(message: string, customOpts?: LoggerOpts) {
const opts = {
indent: customOpts ? customOpts.indent || 0 : 0
};
const indents = "\t".repeat(opts.indent);
return indents + message.replace(/\n/g, `\n${indents}`);
// tslint:disable:no-console
debug(message: string, opts?: LogOpts): void {
if (this.debugMode) {
console.log(chalk.magenta(this.transformMessage(message, opts)));
}
}

log(message: string, opts?: LogOpts): void {
console.log(chalk.dim.white(this.transformMessage(message, opts)));
}

success(message: string, opts?: LogOpts): void {
console.log(chalk.green(this.transformMessage(message, opts)));
}

warn(message: string, opts?: LogOpts): void {
console.log(chalk.yellow(this.transformMessage(message, opts)));
}

error(message: string, opts?: LogOpts): void {
console.log(chalk.red(this.transformMessage(message, opts)));
}
// tslint:enable:no-console

private transformMessage(message: string, customOpts?: LogOpts): string {
const opts = {
indent: customOpts ? customOpts.indent || 0 : 0
};
const indents = "\t".repeat(opts.indent);
return indents + message.replace(/\n/g, `\n${indents}`);
}
}

interface LoggerOpts {
debugMode?: boolean;
}

interface LogOpts {
indent?: number; // number of tabs
}
42 changes: 24 additions & 18 deletions cli/src/test-utils/test-runner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ import { cleanse } from "../../../lib/src/cleansers/cleanser";
import { ContractDefinition } from "../../../lib/src/models/definitions";
import { parse } from "../../../lib/src/parsers/parser";
import { verify } from "../../../lib/src/verifiers/verifier";
import { runTest } from "./test-runner";
import { TestRunner } from "./test-runner";

describe("test runner", () => {
const stateUrl = "http://localhost:9988/state";
const baseStateUrl = "http://localhost:9988/state";
const baseUrl = "http://localhost:9988";
const testRunnerConfig = {
baseStateUrl,
baseUrl,
debugMode: true
};

const testRunner = new TestRunner(testRunnerConfig);

afterEach(() => {
nock.cleanAll();
Expand All @@ -30,7 +37,7 @@ describe("test runner", () => {
.post("/state/teardown")
.reply(200);

const result = await runTest(contract, stateUrl, baseUrl);
const result = await testRunner.test(contract);
expect(scope.isDone()).toBe(true);
expect(result).toBe(true);
});
Expand All @@ -53,7 +60,7 @@ describe("test runner", () => {
.post("/state/teardown")
.reply(200);

const result = await runTest(contract, stateUrl, baseUrl);
const result = await testRunner.test(contract);
expect(scope.isDone()).toBe(true);
expect(result).toBe(true);
});
Expand Down Expand Up @@ -82,7 +89,7 @@ describe("test runner", () => {
.post("/state/teardown")
.reply(200);

const result = await runTest(contract, stateUrl, baseUrl);
const result = await testRunner.test(contract);
expect(scope.isDone()).toBe(true);
expect(result).toBe(true);
});
Expand Down Expand Up @@ -112,7 +119,7 @@ describe("test runner", () => {
.post("/state/teardown")
.reply(200);

const result = await runTest(contract, stateUrl, baseUrl);
const result = await testRunner.test(contract);
expect(scopeA.isDone()).toBe(true);
expect(scopeB.isDone()).toBe(true);
expect(result).toBe(true);
Expand Down Expand Up @@ -143,9 +150,8 @@ describe("test runner", () => {
.post("/state/teardown")
.reply(200);

const result = await runTest(contract, stateUrl, baseUrl, {
endpoint: "CreateCompany",
test: "badRequestTest"
const result = await testRunner.test(contract, {
testFilter: { endpoint: "CreateCompany", test: "badRequestTest" }
});

expect(scopeA.isDone()).toBe(false);
Expand All @@ -172,7 +178,7 @@ describe("test runner", () => {
.post("/state/teardown")
.reply(200);

const result = await runTest(contract, stateUrl, baseUrl);
const result = await testRunner.test(contract);
expect(initializeScope.isDone()).toBe(true);
expect(tearDownScope.isDone()).toBe(true);
expect(setupScope.isDone()).toBe(false);
Expand All @@ -192,7 +198,7 @@ describe("test runner", () => {
.post("/state/teardown")
.reply(200);

const result = await runTest(contract, stateUrl, baseUrl);
const result = await testRunner.test(contract);
expect(scope.isDone()).toBe(true);
expect(result).toBe(false);
});
Expand All @@ -215,7 +221,7 @@ describe("test runner", () => {
.post("/state/teardown")
.reply(400);

const result = await runTest(contract, stateUrl, baseUrl);
const result = await testRunner.test(contract);
expect(scope.isDone()).toBe(true);
expect(result).toBe(false);
});
Expand All @@ -238,7 +244,7 @@ describe("test runner", () => {
.post("/state/teardown")
.reply(200);

const result = await runTest(contract, stateUrl, baseUrl);
const result = await testRunner.test(contract);
expect(scope.isDone()).toBe(true);
expect(result).toBe(false);
});
Expand All @@ -260,7 +266,7 @@ describe("test runner", () => {
.post("/state/teardown")
.reply(200);

const result = await runTest(contract, stateUrl, baseUrl);
const result = await testRunner.test(contract);
expect(scope.isDone()).toBe(true);
expect(result).toBe(false);
});
Expand All @@ -282,7 +288,7 @@ describe("test runner", () => {
.post("/state/teardown")
.reply(200);

const result = await runTest(contract, stateUrl, baseUrl);
const result = await testRunner.test(contract);
expect(scope.isDone()).toBe(true);
expect(result).toBe(false);
});
Expand All @@ -305,7 +311,7 @@ describe("test runner", () => {
.post("/state/teardown")
.reply(200);

const result = await runTest(contract, stateUrl, baseUrl);
const result = await testRunner.test(contract);
expect(scope.isDone()).toBe(true);
expect(result).toBe(false);
});
Expand All @@ -329,7 +335,7 @@ describe("test runner", () => {
.post("/state/teardown")
.reply(200);

const result = await runTest(contract, stateUrl, baseUrl);
const result = await testRunner.test(contract);
expect(scope.isDone()).toBe(true);
expect(result).toBe(true);
});
Expand All @@ -352,7 +358,7 @@ describe("test runner", () => {
.post("/state/teardown")
.reply(200);

const result = await runTest(contract, stateUrl, baseUrl);
const result = await testRunner.test(contract);
expect(scope.isDone()).toBe(true);
expect(result).toBe(true);
});
Expand Down
Loading

0 comments on commit bf061f9

Please sign in to comment.