-
Notifications
You must be signed in to change notification settings - Fork 0
/
getTests.js
99 lines (90 loc) · 2.52 KB
/
getTests.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
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict';
const argv = require('yargs').argv;
const inquirer = require('inquirer');
const fs = require('fs');
const path = require('path');
const localDir = process.cwd();
if (argv.v || argv.version) {
const pkg = require('./package.json');
console.log(pkg.name, pkg.version);
process.exit();
}
const params = {
verbose: argv.verbose,
outfile: argv.o || argv.outfile,
tests: []
};
const resolveBundles = (testPaths, prefix = localDir) => {
const out = [];
testPaths.forEach(testPath => {
let test;
const name = path.parse(testPath).name;
const absoluteTestPath = path.isAbsolute(testPath)? testPath : path.join(prefix, testPath);
try {
test = require(absoluteTestPath);
} catch (err) {
if (err.message === `Cannot find module '${absoluteTestPath}'`) {
console.log(`Cannot find test '${absoluteTestPath}'`);
process.exit();
}
throw({ err, name });
}
if (typeof test === 'function') {
out.push({
name: path.parse(testPath).name,
test
});
} else if (Array.isArray(test)) {
out.push(...resolveBundles(test, path.parse(absoluteTestPath).dir));
} else {
throw({ err: new Error('Invalid test!'), name });
}
});
return out;
};
module.exports = (tests, options) => new Promise((resolve, reject) => {
if (tests && tests.length) {
if (options) Object.assign(params, options);
console.log(params, options);
try {
params.tests.push(...resolveBundles(tests));
} catch(err) {
return reject(err);
}
return resolve(params);
}
if (argv._.length) {
try {
params.tests.push(...resolveBundles(argv._));
} catch(err) {
return reject({ err });
}
return resolve(params);
}
const folderTests = fs.readdirSync(localDir).filter(file => file.slice(0, 2) === 't-' && path.extname(file) === '.js');
if (!folderTests.length) {
return reject({ err: 'No tests found in this folder;\nnote that all tests must be .js files and begin with "t-" prefix to be detected.' });
}
if (argv.a || argv.all) {
try {
params.tests.push(...resolveBundles(folderTests));
} catch(err) {
return reject(err);
}
return resolve(params);
}
inquirer.prompt({
type: 'list',
name: 'tests',
message: 'Select the test to execute: ',
choices: folderTests,
default: folderTests[0]
}).then((result) => {
try {
params.tests.push(...resolveBundles([result.tests]));
} catch(err) {
return reject(err);
}
resolve(params);
});
});