forked from scality/cloudserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tester.js
77 lines (59 loc) · 1.62 KB
/
tester.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
import fs from 'fs';
import cp from 'child_process';
import process from 'process';
import path from 'path';
const rootPath = path.join(__dirname, 'tests', 'functional');
function log(message) {
return process.stdout.write(`${message}\n`);
}
function installDependency(dir) {
log(`installing dependencies ${dir}`);
try {
fs.statSync(path.join(dir, 'package.json'));
} catch (e) {
return log('package.json file not found, skip installation');
}
try {
cp.execSync('npm install', {
stdio: 'inherit',
cwd: dir,
});
} catch (e) {
log('`npm install` fail');
return false;
}
return true;
}
function runTest(dir, fileName) {
const testFile = path.join(dir, fileName);
const test = cp.spawnSync('mocha', ['-t', '40000', testFile], {
stdio: 'inherit',
cwd: dir,
});
return !test.status;
}
function testing(dir) {
let masterConfig;
log(`testing ${dir}`);
try {
masterConfig = require(path.join(rootPath, dir, 'master.json'));
} catch (err) {
return log('master.json file not found, skipping this directory');
}
return masterConfig.tests.files.reduce((prev, file) => {
const cwd = path.join(rootPath, dir);
if (!installDependency(cwd)) {
return false;
}
return prev && runTest(cwd, file);
}, true);
}
function main() {
log(`reading dirs in ${rootPath}`);
const dirs = fs.readdirSync(rootPath);
return dirs.reduce((prev, dir) => prev && testing(dir), true);
}
if (main()) {
process.exit(0);
}
process.exit(1);