forked from HaveAGitGat/Tdarr_Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runTests.js
77 lines (68 loc) · 2.46 KB
/
runTests.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
/* eslint no-console: 0 */ // --> OFF
const fs = require('fs');
const chalk = require('chalk');
const childProcess = require('child_process');
const filenames = fs.readdirSync(`${process.cwd()}/Community`).reverse();
const errorsEncountered = [];
const run = async () => {
const pluginsToRun = [];
for (let i = 0; i < filenames.length; i += 1) {
const filename = filenames[i];
const pluginPath = `${process.cwd()}/Community/${filename}`;
const text = fs.readFileSync(pluginPath);
const pluginTestpath = `${__dirname}/Community/${filename}`;
if (!text.includes('// tdarrSkipTest') && !fs.existsSync(pluginTestpath)) {
console.log(chalk.red(`${filename} does not have a test but should do.`));
process.exit(1);
} else if (!text.includes('// tdarrSkipTest') && fs.existsSync(pluginTestpath)) {
pluginsToRun.push({
filename,
pluginTestpath,
});
} else if (text.includes('// tdarrSkipTest') && fs.existsSync(pluginTestpath)) {
console.log(chalk.red(`${filename} should have // tdarrSkipTest removed`));
process.exit(1);
} else if (text.includes('// tdarrSkipTest') && !fs.existsSync(pluginTestpath)) {
console.log(chalk.yellow(`${filename} skipping tests`));
}
}
let pluginsFinished = 0;
for (let i = 0; i < pluginsToRun.length; i += 1) {
const { filename } = pluginsToRun[i];
const { pluginTestpath } = pluginsToRun[i];
console.log(chalk.white(`${filename} running test`));
const output = {};
childProcess.exec(`node "${pluginTestpath}"`, (err, stdout, stderr) => {
if (err) {
output.err = err;
}
output.stdout = stdout;
output.stderr = stderr;
// eslint-disable-next-line no-loop-func
}).on('exit', async (code) => {
if (code !== 0) {
await new Promise((resolve2) => setTimeout(resolve2, 1000));
errorsEncountered.push({
id: filenames[i],
...output,
});
}
pluginsFinished += 1;
if (pluginsFinished === pluginsToRun.length) {
if (errorsEncountered.length > 0) {
errorsEncountered.forEach((plugin) => {
console.log(plugin.id);
console.log(chalk.red(plugin.err));
console.log(plugin.stdout);
console.log(chalk.red(plugin.stderr));
});
process.exit(1);
} else {
console.log(chalk.green('No errors encountered!'));
process.exit(0);
}
}
});
}
};
run();