Skip to content

Commit 49e2096

Browse files
authored
feat: compare coverage against given threshold (#7)
1 parent 405ce41 commit 49e2096

File tree

9 files changed

+196
-13
lines changed

9 files changed

+196
-13
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ jobs:
2222
- uses: actions/checkout@v2
2323
- uses: ./
2424
with:
25-
path: "./fixtures/lcov.info"
25+
path: "./fixtures/lcov.100.info"

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ inputs:
55
description: 'lcov path'
66
required: false
77
default: './coverage/lcov.info'
8+
min_coverage:
9+
description: 'minimum coverage percentage allowed'
10+
required: false
11+
default: 100
812
runs:
913
using: 'node12'
1014
main: 'dist/index.js'

dist/index.js

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.

fixtures/lcov.95.info

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
SF:/Users/felix/Development/github.com/felangel/bloc/packages/bloc/lib/src/bloc.dart
2+
DA:21,2
3+
DA:22,1
4+
DA:26,3
5+
DA:37,1
6+
DA:39,2
7+
DA:41,1
8+
DA:42,2
9+
DA:44,1
10+
DA:66,1
11+
DA:70,2
12+
DA:105,1
13+
DA:109,1
14+
DA:135,1
15+
DA:162,1
16+
DA:166,2
17+
DA:170,1
18+
DA:172,1
19+
DA:197,1
20+
DA:201,1
21+
DA:212,1
22+
DA:213,3
23+
DA:214,3
24+
DA:215,1
25+
DA:223,1
26+
DA:225,1
27+
DA:227,1
28+
DA:228,2
29+
DA:229,1
30+
DA:230,2
31+
DA:231,3
32+
DA:232,2
33+
DA:233,1
34+
DA:239,1
35+
DA:240,1
36+
DA:241,4
37+
DA:243,1
38+
DA:244,2
39+
DA:246,1
40+
DA:248,1
41+
DA:250,1
42+
LF:40
43+
LH:40
44+
end_of_record
45+
SF:/Users/felix/Development/github.com/felangel/bloc/packages/bloc/lib/src/bloc_observer.dart
46+
DA:14,1
47+
DA:20,0
48+
DA:27,0
49+
DA:36,0
50+
DA:43,0
51+
DA:51,0
52+
LF:6
53+
LH:1
54+
end_of_record
55+
SF:/Users/felix/Development/github.com/felangel/bloc/packages/bloc/lib/src/change.dart
56+
DA:10,1
57+
DA:18,1
58+
DA:21,1
59+
DA:22,3
60+
DA:23,3
61+
DA:24,3
62+
DA:26,1
63+
DA:27,5
64+
DA:29,1
65+
DA:31,3
66+
LF:10
67+
LH:10
68+
end_of_record
69+
SF:/Users/felix/Development/github.com/felangel/bloc/packages/bloc/lib/src/cubit.dart
70+
DA:16,1
71+
DA:27,1
72+
DA:29,3
73+
DA:30,1
74+
DA:54,1
75+
DA:56,2
76+
DA:60,2
77+
DA:62,2
78+
DA:79,1
79+
DA:82,2
80+
DA:83,2
81+
DA:84,3
82+
DA:85,3
83+
DA:86,1
84+
DA:87,3
85+
DA:88,1
86+
DA:92,1
87+
DA:93,1
88+
DA:115,1
89+
DA:118,2
90+
DA:138,1
91+
DA:142,2
92+
DA:143,1
93+
DA:144,1
94+
DA:145,1
95+
DA:152,1
96+
DA:159,2
97+
DA:160,3
98+
DA:170,1
99+
DA:175,1
100+
DA:178,2
101+
DA:179,2
102+
LF:32
103+
LH:32
104+
end_of_record
105+
SF:/Users/felix/Development/github.com/felangel/bloc/packages/bloc/lib/src/transition.dart
106+
DA:14,1
107+
DA:18,1
108+
DA:23,1
109+
DA:26,1
110+
DA:27,3
111+
DA:28,3
112+
DA:29,3
113+
DA:30,3
114+
DA:32,1
115+
DA:34,8
116+
DA:37,1
117+
DA:39,4
118+
LF:12
119+
LH:12
120+
end_of_record

fixtures/lcov.error.info

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this should not parse

index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,24 @@ const parse = require("lcov-parse");
33

44
function run() {
55
const lcovPath = core.getInput("path");
6+
const minCoverage = core.getInput("min_coverage");
67

7-
parse(lcovPath, function (err, data) {
8-
if (err) {
8+
parse(lcovPath, function (_, data) {
9+
if (typeof data === "undefined") {
910
core.setFailed("parsing error!");
11+
return;
1012
}
1113
let totalFinds = 0;
1214
let totalHits = 0;
1315
data.forEach(element => {
1416
totalHits += element['lines']['hit'];
1517
totalFinds += element['lines']['found'];
1618
});
17-
core.debug((totalHits / totalFinds) * 100);
19+
const coverage = (totalHits / totalFinds) * 100;
20+
const isValidBuild = coverage >= minCoverage;
21+
if (!isValidBuild) {
22+
core.setFailed(`Coverage ${coverage} is below the minimum ${minCoverage} expected`);
23+
}
1824
});
1925
}
2026

index.test.js

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,58 @@
11
const process = require("process");
22
const cp = require("child_process");
33
const path = require("path");
4+
const { fail } = require("assert");
45

5-
test("test runs", () => {
6-
const lcovPath = "./fixtures/lcov.info";
6+
test("invalid LCOV format throws an error", () => {
7+
const lcovPath = "./fixtures/lcov.error.info";
78
process.env["INPUT_PATH"] = lcovPath;
89
const ip = path.join(__dirname, "index.js");
9-
const actual = cp.execSync(`node ${ip}`, { env: process.env }).toString();
10-
const expected = '::debug::100\n';
11-
expect(actual).toEqual(expected);
10+
try {
11+
cp.execSync(`node ${ip}`, { env: process.env }).toString();
12+
fail('this code should fail');
13+
} catch (err) {
14+
expect(err).toBeDefined();
15+
}
1216
});
17+
18+
test("completes when the coverage is 100 and min_coverage is not provided", () => {
19+
const lcovPath = "./fixtures/lcov.100.info";
20+
process.env["INPUT_PATH"] = lcovPath;
21+
const ip = path.join(__dirname, "index.js");
22+
cp.execSync(`node ${ip}`, { env: process.env }).toString();
23+
});
24+
25+
test("fails when the coverage is not 100 and min_coverage is not provided", () => {
26+
const lcovPath = "./fixtures/lcov.95.info";
27+
process.env["INPUT_PATH"] = lcovPath;
28+
const ip = path.join(__dirname, "index.js");
29+
try {
30+
cp.execSync(`node ${ip}`, { env: process.env }).toString();
31+
fail('this code should fail');
32+
} catch (err) {
33+
expect(err).toBeDefined();
34+
}
35+
});
36+
37+
test("completes when the coverage is above the given min_threshold", () => {
38+
const lcovPath = "./fixtures/lcov.95.info";
39+
const minCoverage = 80;
40+
process.env["INPUT_PATH"] = lcovPath;
41+
process.env["INPUT_MIN_COVERAGE"] = minCoverage;
42+
const ip = path.join(__dirname, "index.js");
43+
cp.execSync(`node ${ip}`, { env: process.env }).toString();
44+
});
45+
46+
test("fails when the coverage is below the given min_threshold", () => {
47+
const lcovPath = "./fixtures/lcov.95.info";
48+
const minCoverage = 98;
49+
process.env["INPUT_PATH"] = lcovPath;
50+
process.env["INPUT_MIN_COVERAGE"] = minCoverage;
51+
const ip = path.join(__dirname, "index.js");
52+
try {
53+
cp.execSync(`node ${ip}`, { env: process.env }).toString();
54+
fail('this code should fail');
55+
} catch (err) {
56+
expect(err).toBeDefined();
57+
}
58+
});

0 commit comments

Comments
 (0)