feat(pretask): introduce new pretask functionality for setting up testing environment

This commit is contained in:
Philipp Kunz 2019-11-16 23:40:36 +01:00
parent bdcc94e723
commit 427878763e
4 changed files with 45 additions and 15 deletions

View File

@ -0,0 +1,19 @@
import * as plugins from './tapbundle.plugins';
import { TapTools } from './tapbundle.classes.taptools';
export interface IPreTaskFunction {
(tapTools?: TapTools): Promise<any>;
}
export class PreTask {
public preTaskFunction: IPreTaskFunction;
constructor(preTaskFunctionArg: IPreTaskFunction) {
this.preTaskFunction = preTaskFunctionArg;
}
public async run () {
await this.preTaskFunction(new TapTools(null));
}
}

View File

@ -1,5 +1,6 @@
import * as plugins from './tapbundle.plugins'; import * as plugins from './tapbundle.plugins';
import { IPreTaskFunction, PreTask } from './tapbundle.classes.pretask';
import { TapTest, ITestFunction } from './tapbundle.classes.taptest'; import { TapTest, ITestFunction } from './tapbundle.classes.taptest';
import { TapWrap, ITapWrapFunction } from './tapbundle.classes.tapwrap'; import { TapWrap, ITapWrapFunction } from './tapbundle.classes.tapwrap';
export class Tap { export class Tap {
@ -25,6 +26,7 @@ export class Tap {
} }
}; };
private _tapPreTasks: PreTask[] = [];
private _tapTests: TapTest[] = []; private _tapTests: TapTest[] = [];
private _tapTestsOnly: TapTest[] = []; private _tapTestsOnly: TapTest[] = [];
@ -51,6 +53,10 @@ export class Tap {
return localTest; return localTest;
} }
public preTask (functionArg: IPreTaskFunction) {
this._tapPreTasks.push(new PreTask(functionArg));
}
/** /**
* wraps function * wraps function
*/ */
@ -82,6 +88,7 @@ export class Tap {
// safeguard against empty test array // safeguard against empty test array
if (this._tapTests.length === 0) { if (this._tapTests.length === 0) {
console.log('no tests specified. Ending here!'); console.log('no tests specified. Ending here!');
// TODO: throw proper error
return; return;
} }
@ -93,6 +100,11 @@ export class Tap {
concerningTests = this._tapTests; concerningTests = this._tapTests;
} }
// lets run the pretasks
for (const preTask of this._tapPreTasks) {
await preTask.run();
}
console.log(`1..${concerningTests.length}`); console.log(`1..${concerningTests.length}`);
for (let testKey = 0; testKey < concerningTests.length; testKey++) { for (let testKey = 0; testKey < concerningTests.length; testKey++) {
const currentTest = concerningTests[testKey]; const currentTest = concerningTests[testKey];

View File

@ -9,21 +9,19 @@ import { Deferred } from '@pushrocks/smartpromise';
// interfaces // interfaces
export type TTestStatus = 'success' | 'error' | 'pending' | 'errorAfterSuccess' | 'timeout'; export type TTestStatus = 'success' | 'error' | 'pending' | 'errorAfterSuccess' | 'timeout';
export interface ITestFunction { export type ITestFunction = (tapTools?: TapTools) => Promise<any>;
(tapTools?: TapTools): Promise<any>;
}
export class TapTest { export class TapTest {
description: string; public description: string;
failureAllowed: boolean; public failureAllowed: boolean;
hrtMeasurement: HrtMeasurement; public hrtMeasurement: HrtMeasurement;
parallel: boolean; public parallel: boolean;
status: TTestStatus; public status: TTestStatus;
tapTools: TapTools; public tapTools: TapTools;
testFunction: ITestFunction; public testFunction: ITestFunction;
testKey: number; // the testKey the position in the test qeue. Set upon calling .run() public testKey: number; // the testKey the position in the test qeue. Set upon calling .run()
testDeferred: Deferred<TapTest> = plugins.smartpromise.defer(); public testDeferred: Deferred<TapTest> = plugins.smartpromise.defer();
testPromise: Promise<TapTest> = this.testDeferred.promise; public testPromise: Promise<TapTest> = this.testDeferred.promise;
/** /**
* constructor * constructor
*/ */
@ -39,10 +37,10 @@ export class TapTest {
/** /**
* run the test * run the test
*/ */
async run(testKeyArg: number) { public async run(testKeyArg: number) {
this.hrtMeasurement.start(); this.hrtMeasurement.start();
this.testKey = testKeyArg; this.testKey = testKeyArg;
let testNumber = testKeyArg + 1; const testNumber = testKeyArg + 1;
try { try {
await this.testFunction(this.tapTools); await this.testFunction(this.tapTools);
if (this.status === 'timeout') { if (this.status === 'timeout') {

View File

@ -19,6 +19,7 @@ export class TapWrap {
* run the wrapFunction * run the wrapFunction
*/ */
async run() { async run() {
// TODO: make sure it makes sense what we do here.
await this.wrapFunction(); await this.wrapFunction();
} }
} }