-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinfraRunner.ts
128 lines (104 loc) · 4.17 KB
/
infraRunner.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
/**
* Main access point runner
* Can still be consumed as a class
*/
import {Argv} from "yargs";
import {CucumberRunner, ICucumberRunOptions} from "./src/runHelpers/cucumberRunner";
const path = require('path');
const projectPath = path.resolve(__dirname);
const moment = require('moment');
/**
* Runner for automation commands
*/
class AutomationInfrastructureRunner {
/**
* Run a command from arguments
*/
public runFromArgs(): Promise<{result: boolean, error?: Error}> {
const pendingPromise = this.getPendingPromise<{result: boolean, error?: Error}>();
require('yargs')
.command(
'cucumber', 'Run the infrastructure cucumber scenarios',
args => this.parseRunCucumberArgs(args),
args => this.doRunCucumber(args as ICucumberRunOptions).then(pendingPromise.finishedOK).catch(pendingPromise.finishedError)
)
.demandCommand(1, 1)
.parse();
return pendingPromise.result;
}
/**
* Perform an infrastructure automation cucumber run
* @param options The run options
*/
public async doRunCucumber(options: ICucumberRunOptions): Promise<{result: boolean, error?: Error}> {
const runner = new CucumberRunner(options);
const result = await runner.run();
return result;
}
/**
* Parses arguments for standard infrastructure cucumber run
* @param commandArgs The process command args
*/
private parseRunCucumberArgs(commandArgs: Argv): Argv {
return commandArgs
.option('workPath', {
string: true,
describe: 'Work directory',
default: path.resolve(projectPath, 'temp', moment().format('DDMMYY_HHmm'))
})
.option('cucumberLibPath', {
string: true,
describe: 'The relative cucumber library path (containing features and support files)',
default: path.resolve(projectPath, 'src', 'cucumber'),
hidden: true
})
.options('configFile', {
describe: 'Config override files',
alias: 'configFiles',
array: true
})
.option('configParam', {
alias: 'configParams',
describe: 'Specific config overrides',
array: true,
coerce: arg => arg.map(item => {
const [paramPath, paramValue] = item.split('=');
return {paramPath, paramValue};
})
})
.group(['configParam', 'configFile'], 'Configuration')
.option('tags', {
string: true,
describe: 'Tag expression to run (not @skip will be added by default)',
default: '@debug'
})
.option('passwordFilesPath', {
string: true,
describe: 'Passwords file locations',
default: path.resolve(projectPath, 'passwords'),
hidden: true
});
}
/**
* Returns a pending promise with exposed resolve and reject methods
*/
private getPendingPromise<T>(): {finishedOK: (result: T) => void, finishedError: (e: Error) => void, result: Promise<T>} {
let finishedOK: (T) => any = (res: T) => {throw new Error(`Called before initiation. Result = ${res}`); };
let finishedError: (e: Error) => void = (e: Error) => {throw new Error(`Called before initiation. Error = ${e.message}`); };
const result = new Promise((resolve, reject) => [finishedOK, finishedError] = [resolve, reject]) as Promise<T>;
return {result, finishedError, finishedOK};
}
}
if (require.main === module) {
const infraRunner = new AutomationInfrastructureRunner();
infraRunner.runFromArgs()
.then(runResult => {
if (runResult.result === false) throw runResult.error || new Error('Run ended with result = false');
console.log('Run ended');
})
.catch(e => {
console.log(`Error running process: ${e.stack}`);
process.exit(1);
});
}
export {AutomationInfrastructureRunner, ICucumberRunOptions};