Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
266 lines (241 loc) · 8.25 KB
/
Copy pathtest.js
File metadata and controls
266 lines (241 loc) · 8.25 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* @classification UNCLASSIFIED
*
* @module scripts.test
*
* @copyright Copyright (C) 2018, Lockheed Martin Corporation
*
* @license Apache-2.0
*
* @owner Connor Doyle
*
* @author Josh Kaplan
* @author Leah De Laurell
*
* @description This file executes the MBEE test suite with Mocha.
*/
// Node modules
const fs = require('fs');
const path = require('path');
// NPM modules
const Mocha = require('mocha');
require('@babel/register')(); // Transpile react tests to javascript
// require('@babel/polyfill'); // Transpile async await for javascript
require('core-js/stable');
require('regenerator-runtime/runtime');
// require(path.join(M.root, 'test', '7xx_ui_tests', 'setup.js'));
// If the application is run directly from node, notify the user and fail
if (module.parent == null) {
// eslint-disable-next-line no-console
console.log('\nError: please use mbee to run this script by using the '
+ 'following command. \n\nnode mbee test\n');
process.exit(-1);
}
/**
* @description Runs the collection of test suites script with Mocha.
* Any command line accepted by Mocha is valid.
*
* @param {string} _args - Options from the user for how to run the tests.
*
* @returns {Promise} Returns a promise which allows plugin testing to be synchronous
* upon startup. When used purely as a script, the function will terminate with a
* process.exit().
*/
function test(_args) {
// Don't print the header if specified; this is usually only used for server startup
if (!_args.includes('--no-header')) printHeader();
M.log.verbose(`Running tests with DB strategy: ${M.config.db.strategy}`);
// Default timeout changed to 5000
// Add default timeout if not provided
if (!_args.includes('--timeout')) {
_args.push('--timeout');
_args.push('5000');
}
// Add default slow speed if not provided
if (!_args.includes('--slow')) {
_args.push('--slow');
_args.push('50');
}
// Add default grep command to define which tests to run
if (!_args.includes('--grep') && !_args.includes('--all')) {
_args.push('--grep');
_args.push('^[1-57]');
}
// Test everything if --all was specified
if (_args.includes('--all')) {
if (M.env.toLowerCase() === 'production' && !_args.includes('--force')) {
// Throw an error if the server is running as production
M.log.error('\nWARNING! You are attempting to run tests on a production database.\n\n'
+ 'This operation could ERASE PRODUCTION DATA PERMANENTLY.\n'
+ 'If you would still like to perform this action, use the\n'
+ 'optional parameter --force\n\n'
+ 'The following command is recommended:'
+ 'node mbee test --grep "[^[1-8]]"\n');
process.exit(-1);
}
else if (_args.includes('--grep')) {
// Throw an error if --grep and --all are used together
M.log.error('Cannot use arguments --grep and --all together');
process.exit(-1);
}
const removeInd = _args.indexOf('--all');
_args.splice(removeInd, 1);
_args.push('--grep');
_args.push('^[0-9]');
}
// Remove --force from args
if (_args.includes('--force')) {
const removeInd = _args.indexOf('--force');
_args.splice(removeInd, 1);
}
// Remove --suppress-console
if (_args.includes('--suppress-console')) {
const removeInd = _args.indexOf('--suppress-console');
_args.splice(removeInd, 1);
}
// Remove --no-header
if (_args.includes('--no-header')) {
const removeInd = _args.indexOf('--no-header');
_args.splice(removeInd, 1);
}
// Handle the plugin / plugins options
let plugin;
let plugins;
let pluginName;
const pluginNames = Object.keys(M.config.server.plugins.plugins);
if (_args.includes('--plugins')) {
// Set flag
plugins = true;
const ind = _args.indexOf('--plugins');
// Remove the plugin arguments
_args.splice(ind, 1);
// Remove the grep command
const grepInd = _args.indexOf('--grep');
_args.splice(grepInd, 2);
}
else if (_args.includes('--plugin')) {
// Set flag
plugin = true;
const ind = _args.indexOf('--plugin');
try {
pluginName = _args[ind + 1];
}
catch (error) {
throw new M.DataFormatError('No plugin name provided');
}
if (!pluginNames.includes(pluginName)) {
throw new M.DataFormatError(`Plugin [${pluginName}] is not specified in the config`);
}
// Remove the plugin arguments
_args.splice(ind, 2);
// Remove the grep command
const grepInd = _args.indexOf('--grep');
_args.splice(grepInd, 2);
}
// Allocate options variable for mocha
const opts = {};
// Loop through _args array and load the opts object
for (let i = 0; i < _args.length; i += 2) {
// Check the arg starts with '--'
if (RegExp(/^(--)/).test(_args[i])) {
// The arg started with '--', remove '--' and load the arg in to the opts
// object as a key with the following arg as the value
opts[_args[i].replace('--', '')] = _args[i + 1];
}
else {
// The arg did NOT start with '--', log the error and exit the process
M.log.error(`invalid argument (${_args[i]})`);
process.exit(-1);
}
}
// Create mocha object with options
const mocha = new Mocha(opts);
// If testing all plugins, add all plugin test directories
if (plugins) {
M.log.info('Running tests on all installed plugins.');
pluginNames.forEach((name) => {
const testDir = `${M.root}/plugins/${name}/test`;
if (fs.existsSync(testDir)) {
// Call the mochaWalk function to load in all of the test files
mochaWalk(testDir, mocha);
}
else {
M.log.info(`Plugin "${name}" does not have a test directory.`);
}
});
}
else {
// Set the test directory
let testDir = `${M.root}/test`;
if (plugin) {
testDir = `${M.root}/plugins/${pluginName}/test`;
if (!fs.existsSync(testDir)) {
M.log.warn(`Plugin "${pluginName}" does not have a test directory.`);
process.exit(0);
}
}
// Call the mochaWalk function to load in all of the test files
mochaWalk(testDir, mocha);
}
return new Promise((resolve) => {
// Run the tests.
mocha.run((error) => {
// Check for failures
if (error) {
// mocha did not pass all test, exit with error code -1
process.exit(-1);
}
else if (plugin) {
resolve();
}
else {
// mocha passed all tests, exit with error code 0
process.exit(0);
}
});
});
}
/**
* @description Prints the MBEE test framework header.
*/
function printHeader() {
const Y = '\u001b[33m';
const W = '\u001b[39m';
const G = '\u001b[37m';
const date = new Date(Date.now()).toUTCString();
const title = '\u001b[1mModel-Based Engineering Environment Test Framework\u001b[0m';
console.log('_'.repeat(65)); // eslint-disable-line no-console
console.log(` ${G},-.${W} `); // eslint-disable-line no-console
console.log(` ${G}\\_/${W} \t ${title}`); // eslint-disable-line no-console
console.log(`${Y}{|||)${W}${G}<${W} `); // eslint-disable-line no-console
console.log(` ${G}/ \\${W} \t Version: ${M.version}`); // eslint-disable-line no-console
console.log(` ${G}\`-'${W} \t Date: ${date}`); // eslint-disable-line no-console
console.log('_'.repeat(65)); // eslint-disable-line no-console
console.log(''); // eslint-disable-line no-console
}
/**
* @description The mocha walk function is responsible for loading .js files into mocha
* for use during tests.
*
* @param {string} dir - The directory to read.
* @param {object} mochaObj - The mocha object storing all the paths to test.
*/
function mochaWalk(dir, mochaObj) {
// Read the current directory and use a callback to filter the results
fs.readdirSync(dir).filter((file) => {
// Check if the file is actually a directory
if (fs.lstatSync(path.join(dir, file)).isDirectory()) {
// The file is actually a directory, call mochaWalk recursively with the
// full path of the directory
mochaWalk(path.join(dir, file), mochaObj);
}
// Return true to the filter if the file is a javascript file
return file.substr(-3) === '.js';
})
// Loop through the resulting array of files
.forEach(function(file) {
// Add the full path and filename to mocha
mochaObj.addFile(path.join(dir, file));
});
}
module.exports = test;