-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTestExecutor.ts
152 lines (119 loc) · 4.46 KB
/
TestExecutor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import {ParallelIterator,IIteratorController} from "steroidslibrary/helpers/ParallelIterator"
import {AsyncIterator} from "steroidslibrary/helpers/AsyncIterator"
declare function require(module:string):any;
declare let console:any;
declare let process:any;
declare let v8debug:any;
function Splash(){
let splashString =
`
\x1b[31m ______ _ __
\x1b[31m / __/ /____ _______ (_)__/ /__
\x1b[31m _\ \/ __/ -_) __/ _ \/ / _ (_-<
\x1b[31m/___/\__/\__/_/ \___/_/\_,_/___/
`;
console.log(splashString);
console.log("\x1b[32m", `Executing steroids unit test cases`,"\x1b[0m","\n\n");
}
class SteroidsTestEngine {
private _basePath;
constructor (basePath:string){
this._basePath = basePath;
}
private getUnitTestFiles(): string[]{
let lastArgv = process.argv[process.argv.length -1];
if (lastArgv.includes(".tests.ts")){
let modifiedArgv = lastArgv.replace(/\\/g,"/").replace("/src/","/dist/").replace(".tests.ts",".tests.js");
return [modifiedArgv];
}else {
const fs = require('fs');
const path = require('path');
let filesToReturn = [];
function walkDir(currentPath) {
let files = fs.readdirSync(currentPath);
for (let i in files) {
var curFile = path.join(currentPath, files[i]);
if (fs.statSync(curFile).isFile() && curFile.endsWith(".tests.js")) {
filesToReturn.push(curFile);
} else if (fs.statSync(curFile).isDirectory()) {
walkDir(curFile);
}
}
};
walkDir(this._basePath);
return filesToReturn;
}
}
private printResults(results:string){
}
start(): Promise<any>{
const child_process = require('child_process');
return new Promise<any>((resolve,reject)=>{
let fileList = this.getUnitTestFiles();
let iterator = new AsyncIterator (fileList,{results:[]});
iterator.onComplete((results:any)=>{
resolve(results.results);
});
iterator.onCompleteOne((success:boolean, result:any)=>{
this.printResults("");
})
iterator.logic((element,inObj, ctrl)=>{
try {
let moduleObj = require(element);
moduleObj._handler
.then((result)=>{
inObj.results.push({validFormat:true, result:result});
ctrl(element);
})
.catch((e)=>{
inObj.results.push({validFormat:false, result:e});
ctrl(e);
})
}catch (e){
inObj.results.push({validFormat:false, result:e});
ctrl(e);
}
});
iterator.start();
});
}
}
export class TestExecutor {
public static execute(basePath:string){
Splash();
let executor = new SteroidsTestEngine(basePath);
executor.start().then((results)=>{
let passCount = 0;
let failCount = 0;
for (let i=0;i<results.length;i++){
let r = results[i];
if (r.validFormat){
let scenarios = r.result.scenarios;
for (let j=0;j<scenarios.length;j++){
let scenario = scenarios[j];
for (let k=0;k<scenario.then.length;k++){
let t = scenario.then[k];
if (t.exception)
failCount++;
else
passCount++;
}
}
}else {
failCount++;
}
}
console.log ("\x1b[32m","Number of success unit test cases : " + passCount, "\x1b[0m");
if (failCount > 0)
console.log ("\x1b[31m", "Number of failed unit test cases : " + failCount, "\x1b[0m");
if (failCount > 0){
process.exit(3);
}
else{
process.exit(0);
}
}).catch((err)=>{
console.log ("Execution error : " , err);
});
}
}