-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtestMain.js
126 lines (121 loc) · 3.92 KB
/
testMain.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
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
/**
* Runs embedded app's test cases.
*
* (C) 2023 Tekmonks. All rights reserved.
*/
const fs = require("fs");
const path = require("path");
const { spawn } = require('child_process');
global.CONSTANTS = require(__dirname + "/lib/constants.js");
const { initiateReport, updateReport } = require(`${CONSTANTS.LIBDIR}/report`);
let asbProcess;
let testResult;
async function runTestsAsync(argv, report) {
const testCasesDir = path.resolve(argv[0]);
const allfiles = fs.readdirSync(testCasesDir);
for (const fileEntry of allfiles) if (fileEntry.toLowerCase().startsWith("test") &&
fileEntry.toLowerCase().endsWith(".js") && (fileEntry != path.basename(__filename))) {
const testModule = require(`${testCasesDir}/${fileEntry}`);
if (testModule.runTestsAsync)
try {
let resultLog;
let result;
let startTime = Date.now();
testResult = await testModule.runTestsAsync(argv.slice(1));
if (testResult == 'skipped') {
resultLog = `Testcase ${fileEntry} skipped.\n\n`;
result = 'skipped';
message = 'skipped'
}
else if (testResult === true) {
resultLog = `Testcase ${fileEntry} succeeded.\n\n`;
result = 'passed';
message = 'success'
}
else {
resultLog = `Testcase ${fileEntry} failed with error false\n\n`;
result = 'failed';
message = testResult.message;
}
LOG[testResult ? "info" : "error"](resultLog);
updateReport(report, fileEntry, result, message, startTime, Date.now());
} catch (err) {
const error = `Testcase ${fileEntry} failed with error ${err}\n\n`;
LOG.error(error);
LOG.info(error);
}
else {
const errorMsg = `Skipping ${fileEntry} as it is not a proper test case module.\n\n`;
LOG.warn(errorMsg); LOG.info(errorMsg);
}
}
}
function setupServerEnvironmentForTesting() {
const conf = require(`${CONSTANTS.CONFDIR}/server.json`);
/* Init - Server bootup */
console.log("creating server for testing ..");
/* Init the logs */
console.log("Initializing the logs.");
require(CONSTANTS.LIBDIR + "/log.js").initGlobalLoggerSync(`${CONSTANTS.LOGDIR}/${conf.logfile}`);
LOG.overrideConsole();
/* Warn if in debug mode */
if (conf.debug_mode) {
LOG.info("**** Server is in debug mode, expect severe performance degradation.\n");
}
asbProcess = spawn('node', [CONSTANTS.ROOTDIR + "/asb.js"], {
stdio: ['inherit'] // This will capture the stdout and stderr
});
return new Promise((resolve, reject) => {
asbProcess.on('error', (err) => {
LOG.error(`Failed to start server: ${err}`);
reject(err);
});
asbProcess.on('exit', (code) => {
if (code !== 0) {
reject(new Error(`Server process exited with code ${code}`));
}
});
asbProcess.stdout.on('data', (data) => {
const message = data.toString();
LOG.info(`stdout: ${message}`);
if (message.includes('Running...')) { // Adjust this condition based on the actual readiness message
resolve();
}
if (message.includes('Error')) {
asbProcess.kill('SIGTERM');
testResult = message;
reject();
}
});
});
}
async function main(argv) {
if (!argv[0]) {
console.error(`Usage: ${__filename} [app tests folder path] [...other params]`);
process.exit(1);
}
try {
await setupServerEnvironmentForTesting(); // init the server environment only when it's ready
let report = await initiateReport();
await runTestsAsync(argv, report); // run the tests
} catch (error) {
LOG.error(`Error during setup or testing: ${error.message}`);
process.exit(1);
}
shouldExit = true; // exit
}
const exit = _ => {
if (asbProcess) {
asbProcess.on('exit', () => process.exit(0)); // Exit the parent process when the child exits
asbProcess.kill('SIGTERM'); // Send SIGTERM to allow the child process to clean up
} else {
process.exit(0); // Exit immediately if there's no child process
}
}
let shouldExit = false;
if (require.main === module) {
main(process.argv.slice(2)); setInterval(_ => {
if (shouldExit)
exit();
}, 100);
}