-
Notifications
You must be signed in to change notification settings - Fork 27
/
check-coverage
executable file
·53 lines (49 loc) · 1.29 KB
/
check-coverage
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
#!/usr/bin/node
'use strict';
/* eslint-disable no-console */
const fs = require('fs');
const { execFileSync } = require('child_process');
const coverage_file = process.argv[2];
const new_coverage = JSON.parse(
fs.readFileSync(coverage_file, { encoding: 'utf8' }),
);
let prev_coverage_ref;
if (Object.prototype.hasOwnProperty.call(process.env, 'PRE_COMMIT_FROM_REF')) {
prev_coverage_ref = process.env.PRE_COMMIT_FROM_REF;
} else {
prev_coverage_ref = 'HEAD';
}
let prev_coverage_text;
try {
prev_coverage_text = execFileSync(
'git',
['show', `${prev_coverage_ref}:${coverage_file}`],
{ encoding: 'utf8' },
);
} catch (e) {
console.log('Previous coverage is not found');
prev_coverage_text = '{}';
}
const prev_coverage = JSON.parse(prev_coverage_text);
let decreased = false;
Object.keys(new_coverage).forEach((f) => {
if (f === 'total') {
return;
}
Object.keys(new_coverage[f])
.sort()
.forEach((what) => {
let prev_val = 0;
if (f in prev_coverage) {
prev_val = parseFloat(prev_coverage[f][what].pct);
}
const new_val = parseFloat(new_coverage[f][what].pct);
if (new_val < prev_val) {
decreased = true;
console.log(`${f} ${what}: ${prev_val} -> ${new_val}`);
}
});
});
if (decreased) {
process.exit(2);
}