forked from plotly/plotly.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshard_jasmine_tests.js
94 lines (77 loc) · 2.2 KB
/
shard_jasmine_tests.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
var fs = require('fs');
var path = require('path');
var falafel = require('falafel');
var glob = require('glob');
var minimist = require('minimist');
var pathToJasmineTests = require('./util/constants').pathToJasmineTests;
var isJasmineTestIt = require('./util/common').isJasmineTestIt;
var argv = minimist(process.argv.slice(2), {
string: ['tag', 'limit'],
alias: {
tag: ['t'],
limit: ['l'],
},
default: {
limit: 20
}
});
var tag = argv.tag;
var limit = argv.limit;
glob(path.join(pathToJasmineTests, '*.js'), function(err, files) {
if(err) throw err;
var file2cnt = {};
files.forEach(function(file) {
var code = fs.readFileSync(file, 'utf-8');
var bn = path.basename(file);
falafel(code, function(node) {
if(isJasmineTestIt(node, tag)) {
if(file2cnt[bn]) {
file2cnt[bn]++;
} else {
file2cnt[bn] = 1;
}
}
});
});
var ranking = Object.keys(file2cnt);
var runs = [];
// if 'it' count in file greater than threshold,
// run only this file separately,
// don't try to shard within file
Object.keys(file2cnt).forEach(function(f) {
if(file2cnt[f] > limit) {
runs.push(f);
ranking.splice(ranking.indexOf(f), 1);
}
});
// sort ranking in decreasing order
ranking.sort(function(a, b) { return file2cnt[b] - file2cnt[a]; });
var runi;
var cnt;
function newRun() {
var r0 = ranking[0];
runi = [r0];
cnt = file2cnt[r0];
ranking.shift();
}
function concat() {
runs.push(runi.join(','));
}
// try to match files with many tests with files not-that-many,
// by matching first rank with one or multiple trailing ranks.
newRun();
while(ranking.length) {
var rn = ranking[ranking.length - 1];
if((cnt + file2cnt[rn]) > limit) {
concat();
newRun();
} else {
runi.push(rn);
cnt += file2cnt[rn];
ranking.pop();
}
}
concat();
// print result to stdout
console.log(runs.join('\n'));
});