-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrun-all-tests.js
More file actions
120 lines (102 loc) · 3.98 KB
/
Copy pathrun-all-tests.js
File metadata and controls
120 lines (102 loc) · 3.98 KB
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
/**
* Module depenencies
*/
var fs = require('fs');
var path = require('path');
var util = require('util');
var _ = require('@sailshq/lodash');
var async = require('async');
require('json5/lib/require');//<<replace's nodejs' require to support requiring of .json5 files
module.exports = function (mpPath, beforeRunningAnyTests, eachTestSuite, done){
// Use provided machinepack path
var mainPath = path.resolve(mpPath);
// var mainPath = path.resolve(mpPath, 'index.js');
var packageJsonPath = path.resolve(mpPath, 'package.json');
// Determine suitable path to tests
// (we try `tests/` and `test/`)
var testsPath;
testsPath = path.resolve(mpPath, 'tests');
if (!fs.existsSync(testsPath)) {
testsPath = path.resolve(mpPath, 'test')
}//fi
// Load pack
var Pack = require(mainPath);
// Encode machinepack path on disk on the Pack object
// so it can be used in `run-test-suite`
Pack._meta = {path: mainPath};
// TODO: load opts from somewhere, normalize them, then pass them to `beforeRunningAnyTests` fn in driver.
var opts = {};
beforeRunningAnyTests(opts, function readyToGo(err){
if (err) return done(err);
var machineIdentities;
try {
machineIdentities = require(packageJsonPath).machinepack.machines;
}
catch (e) {
return done(new Error(util.format('Encountered error loading or parsing pack\'s package.json file (located at `%s`). Details:\n',packageJsonPath, e)));
}
var missingSuites = [];
async.map(machineIdentities, function (machineIdentity, next_machineSuite){
eachTestSuite(machineIdentity, function (onTestFn, informSuiteFinished){
// Load machine tests, supporting .json and .json5 files
var pathToTestSuiteModule = path.resolve(testsPath, machineIdentity);
var testSuite;
try {
testSuite = require(pathToTestSuiteModule + '.json');
} catch (e_cannotFindJsonFile) {
// TODO: (-> low priority)
// instead of checking for syntax error, do the inverse,
// checking that if this is a MODULE_NOT_FOUND error. if it's not
// MODULE_NOT_FOUND error, we should bail out by calling next_machineSuite
// with an error (and I think that should work gracefully)
if (e_cannotFindJsonFile.toString().indexOf('SyntaxError') > -1) {
throw e_cannotFindJsonFile;
}
try {
testSuite = require(pathToTestSuiteModule + '.json5');
}
catch (err_cannotFindJson5File) {
// if this is a MODULE_NOT_FOUND error, then the file doesn't exist.
// so we can skip this particular test
if (err_cannotFindJson5File.code === 'MODULE_NOT_FOUND') {
missingSuites.push(machineIdentity);
// TODO: consider adding a special case that drivers can use
// to optionally provide special handling for skipped tests
// Call informational callback, if provided
if (_.isFunction(informSuiteFinished)) {
informSuiteFinished();
}
return next_machineSuite();
}
}
}
// And run them
require('./run-test-suite')(
Pack,
testSuite,
function eachTestCase (testCase, runTest){
onTestFn(testCase, runTest);
},
function afterwards (err, finalTestResults) {
if (err) {
// TODO: better error msg which includes info about the test suite
console.error('Internal error while running tests:',err);
next_machineSuite();
return;
}
// Call informational callback, if provided
if (_.isFunction(informSuiteFinished)) {
informSuiteFinished();
}
next_machineSuite();
}
);
});
}, function (err) {
if (err) {
return done(err, missingSuites);
}
return done(null, missingSuites);
});
});
};