-
Notifications
You must be signed in to change notification settings - Fork 595
/
test_all.js
90 lines (81 loc) · 2.59 KB
/
test_all.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
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
const chalk = require("chalk");
const semver = require("semver");
const examplesFolder = path.join(__dirname, "src");
function getAllExamples(pathToCheck) {
const directoriesToTest = [];
for (const fd of fs.readdirSync(pathToCheck)) {
const absPath = path.join(pathToCheck, fd);
if (fs.existsSync(path.join(absPath, "package.json"))) {
directoriesToTest.push(absPath);
continue;
}
if (fs.statSync(absPath).isDirectory()) {
directoriesToTest.push(...getAllExamples(absPath));
}
}
return directoriesToTest;
}
const passed = [];
const failedInstalls = [];
const noTest = [];
const failedTests = [];
for (directoryToTest of getAllExamples(examplesFolder)) {
console.log(chalk.green(`testing: ${directoryToTest}`));
const pkgJson = require(path.join(directoryToTest, "package.json"));
if (pkgJson.engines && pkgJson.engines.node) {
const currentNodeVersion = process.versions.node;
const range = pkgJson.engines.node;
const engineOk = semver.satisfies(currentNodeVersion, range);
if (!engineOk) {
console.warn(
chalk.yellow(
`${directoryToTest} require Node.js ${range}, current is ${currentNodeVersion}, skipping`
)
);
continue;
}
}
try {
const stdout = execSync("npm install", { cwd: directoryToTest });
console.log(stdout.toString());
} catch (err) {
console.log(err);
failedInstalls.push(directoryToTest);
continue;
}
let testCommand;
if ("scripts" in pkgJson && "start" in pkgJson.scripts) {
testCommand = "npm start";
} else if ("scripts" in pkgJson && "test" in pkgJson.scripts) {
testCommand = "npm test";
} else if ("main" in pkgJson) {
testCommand = `node ${pkgJson.main}`
} else {
noTest.push(directoryToTest);
continue;
}
try {
const stdout = execSync(testCommand, { cwd: directoryToTest });
console.log(stdout.toString());
passed.push(directoryToTest);
} catch (err) {
console.log(err);
failedTests.push(directoryToTest);
}
}
passed.map((dir) => console.log(chalk.green(`passed: ${dir}`)));
if (noTest.length > 0) {
console.warn(chalk.yellow("no test found:"));
noTest.map((dir) => console.warn(chalk.yellow(` ${dir}`)));
}
if (failedInstalls.length > 0) {
console.error(chalk.red("failed to install:"));
failedInstalls.map((dir) => console.warn(chalk.red(` ${dir}`)));
}
if (failedTests.length > 0) {
console.error(chalk.red("failed tests:"));
failedTests.map((dir) => console.warn(chalk.red(` ${dir}`)));
}