-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
87 lines (72 loc) · 2.5 KB
/
index.js
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
/**
* Copyright 2017 aixigo AG
* Released under the MIT license.
* https://github.com/aixigo/arestocats/blob/master/LICENSE
*/
const path = require( 'path' );
const fs = require( 'fs' );
const minimist = require( 'minimist' );
const createState = require( './src/services/state' );
const createRestService = require( './src/services/rest-service' );
const createCli = require( './src/services/cli' );
const { print } = require( './src/util/general-helpers' );
module.exports = { main };
if( require.main === module ) {
main();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
function main() {
const options = minimist( process.argv.slice( 2 ) );
const state = createState();
const scenarios = collectScenarios( options._, options.src || 'scenarios' );
const { context = {} } = options;
if( options.service ) {
const restService = createRestService( state, { scenarios, ...options.service } );
restService.start( context );
}
if( options.cli || !options.service ) {
const cli = createCli( state, {
scenarios,
...options.cli,
reporters: options.cli ? ( options.cli.reporters || 'stdout' ).split( ',' ) : [ 'stdout' ]
} );
cli.run( context )
.then( success => {
if( !options.service ) {
// If test execution fails, then node will return with 1.
// If test execution is successful but some tests failed, 2 is returned.
process.exit( success ? 0 : 2 );
}
} );
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
function collectScenarios( args, src ) {
let entries = [];
try {
entries = ( args && args.length ) ? args : fs.readdirSync( src );
}
catch( e ) {
print( `The folder "${src}" could not be found. Exiting.` );
process.exit( 1 );
}
return entries
.map( entry => path.resolve( process.cwd(), `${src}/${entry}` ) )
.filter( entry => {
try {
require.resolve( entry );
return true;
}
catch( e ) {
// folder without index.js, probably a shared library of test items
return false;
}
} )
.map( entry => {
if( !fs.existsSync( entry ) ) {
print( `The scenario "${entry}" could not be found. Exiting.` );
process.exit( 1 );
}
return entry;
} );
}