Skip to content

Commit e20f472

Browse files
author
Andres Adjimann
committed
fix: ignore node_modules directories, code refactor, check min-coverage
1 parent d7bf334 commit e20f472

13 files changed

+282
-227
lines changed

.eslintrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ module.exports = {
1717
"generator-star-spacing": ["error", { before: false, after: true }],
1818
"jsx-quotes": ["error", "prefer-double"],
1919
"max-depth": ["error", 10],
20-
"newline-before-return": "error",
2120
"no-alert": "error",
2221
"no-confusing-arrow": ["error", { allowParens: false }],
2322
"no-constant-condition": "error",

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules/
22
coverage/
33
.eslintcache
44
.DS_Store
5+
.idea

action.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ inputs:
2020
app-name:
2121
description: App name to display on comment
2222
required: false
23+
max_lines:
24+
description: Maximum numbers of line print
25+
required: false
26+
default: "15"
27+
min_coverage:
28+
description: Minimum coverage percentage allowed
29+
required: false
30+
default: "100"
31+
exclude:
32+
description: list of files you would like to exclude from min_coverage check
33+
required: false
34+
exclude_root:
35+
description: exclude the root project coverage from min_coverage check
36+
required: false
2337
runs:
2438
using: node16
2539
main: dist/main.js

dist/main.js

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

src/check.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { percentage } from "./lcov";
2+
3+
export const checkCoverage = (minCoverage, toCheck) => {
4+
let accum = 0;
5+
for (const lcovObj of toCheck) {
6+
const coverage = percentage(lcovObj.lcov);
7+
const isValidBuild = coverage >= minCoverage;
8+
if (!isValidBuild) {
9+
return { isValidBuild, coverage, name: lcovObj.packageName };
10+
}
11+
accum += coverage;
12+
}
13+
14+
return {
15+
isValidBuild: true,
16+
coverage: toCheck.length === 0 ? 0 : accum / toCheck.length,
17+
name: "average",
18+
};
19+
};

src/cli.js

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,47 @@
11
import process from "process";
2-
import { promises as fs } from "fs";
2+
import fs from "fs";
33
import path from "path";
4-
import { parse } from "./lcov";
5-
import { diff } from "./comment";
4+
import { diff, diffForMonorepo } from "./comment";
5+
import { getLcovArray, readLcov } from "./monorepo";
6+
import { checkCoverage } from "./check";
67

78
const main = async () => {
89
const file = process.argv[2];
910
const beforeFile = process.argv[3];
10-
const prefix = `${path.dirname(path.dirname(path.resolve(file)))}/`;
11-
12-
const content = await fs.readFile(file, "utf-8");
13-
const lcov = await parse(content);
14-
15-
let before;
16-
if (beforeFile) {
17-
const contentBefore = await fs.readFile(beforeFile, "utf-8");
18-
before = await parse(contentBefore);
19-
}
2011

12+
const prefix = `${path.dirname(path.dirname(path.resolve(file)))}/`;
2113
const options = {
2214
repository: "example/foo",
2315
commit: "f9d42291812ed03bb197e48050ac38ac6befe4e5",
2416
prefix,
2517
head: "feat/test",
2618
base: "master",
19+
maxLines: "10",
2720
};
28-
29-
console.log(diff(lcov, before, options));
21+
if (fs.statSync(file).isDirectory()) {
22+
const lcovArrayForMonorepo = await getLcovArray(file, "lcov.info");
23+
console.log(
24+
diffForMonorepo(
25+
lcovArrayForMonorepo,
26+
await getLcovArray(file, "lcov-base"),
27+
options,
28+
),
29+
);
30+
console.log(checkCoverage(90, lcovArrayForMonorepo));
31+
} else {
32+
const lcov = await readLcov(file);
33+
console.log(
34+
diff(lcov, beforeFile && (await readLcov(beforeFile)), options),
35+
);
36+
console.log(
37+
checkCoverage(90, [
38+
{
39+
packageName: file,
40+
lcov,
41+
},
42+
]),
43+
);
44+
}
3045
};
3146

3247
main().catch((err) => {

src/comment.js

Lines changed: 71 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { details, summary, b, fragment, table, tbody, tr, th } from "./html";
1+
import { details, summary, b, fragment, table, tbody, tr, th, p } from "./html";
22
import { percentage } from "./lcov";
33
import { tabulate } from "./tabulate";
44

@@ -9,10 +9,19 @@ import { tabulate } from "./tabulate";
99
*/
1010
const renderEmoji = (pdiff) => {
1111
if (pdiff.toFixed(2) < 0) return "❌";
12-
1312
return "✅";
1413
};
1514

15+
const renderArrow = (pdiff) => {
16+
if (pdiff < 0) {
17+
return "▾";
18+
}
19+
if (pdiff > 0) {
20+
return "▴";
21+
}
22+
return "";
23+
};
24+
1625
/**
1726
* Compares two arrays of objects and returns with unique lines update
1827
* @param {Array} otherArray
@@ -25,6 +34,50 @@ const comparer = (otherArray) => (current) =>
2534
other.lines.hit === current.lines.hit,
2635
).length === 0;
2736

37+
const renderLcov = (lcov, base, appTitle, options) => {
38+
const maxLines = options.maxLines || 15;
39+
const pbefore = base ? percentage(base) : 0;
40+
const pafter = base ? percentage(lcov) : 0;
41+
const pdiff = pafter - pbefore;
42+
const plus = pdiff > 0 ? "+" : "";
43+
44+
let report = lcov;
45+
if (base) {
46+
const onlyInLcov = lcov.filter(comparer(base));
47+
const onlyInBefore = base.filter(comparer(lcov));
48+
report = onlyInBefore.concat(onlyInLcov);
49+
}
50+
51+
const h = th(percentage(lcov).toFixed(2), "%");
52+
53+
const row = [];
54+
if (appTitle) {
55+
row.push(th(appTitle));
56+
}
57+
row.push(h);
58+
if (base) {
59+
const arrow = renderArrow(pdiff);
60+
row.push(
61+
th(
62+
renderEmoji(pdiff),
63+
" ",
64+
arrow,
65+
" ",
66+
plus,
67+
pdiff.toFixed(2),
68+
"%",
69+
),
70+
);
71+
}
72+
return [
73+
table(tbody(tr(...row))),
74+
report.length > maxLines
75+
? p("Coverage Report too long to display")
76+
: details(summary("Coverage Report"), tabulate(report, options)),
77+
"<br/>",
78+
].join("");
79+
};
80+
2881
/**
2982
* Github comment for monorepo
3083
* @param {Array<{packageName, lcovPath}>} lcovArrayForMonorepo
@@ -36,60 +89,23 @@ const commentForMonorepo = (
3689
lcovBaseArrayForMonorepo,
3790
options,
3891
) => {
39-
const { base } = options;
40-
const html = lcovArrayForMonorepo.map((lcovObj) => {
41-
const baseLcov = lcovBaseArrayForMonorepo.find(
42-
(el) => el.packageName === lcovObj.packageName,
43-
);
44-
45-
const pbefore = baseLcov ? percentage(baseLcov.lcov) : 0;
46-
const pafter = baseLcov ? percentage(lcovObj.lcov) : 0;
47-
const pdiff = pafter - pbefore;
48-
const plus = pdiff > 0 ? "+" : "";
49-
50-
let arrow = "";
51-
if (pdiff < 0) {
52-
arrow = "▾";
53-
} else if (pdiff > 0) {
54-
arrow = "▴";
55-
}
56-
57-
const pdiffHtml = baseLcov
58-
? th(
59-
renderEmoji(pdiff),
60-
" ",
61-
arrow,
62-
" ",
63-
plus,
64-
pdiff.toFixed(2),
65-
"%",
66-
)
67-
: "";
68-
let report = lcovObj.lcov;
69-
70-
if (baseLcov) {
71-
const onlyInLcov = lcovObj.lcov.filter(comparer(baseLcov));
72-
const onlyInBefore = baseLcov.filter(comparer(lcovObj.lcov));
73-
report = onlyInBefore.concat(onlyInLcov);
74-
}
75-
76-
return `${table(
77-
tbody(
78-
tr(
79-
th(lcovObj.packageName),
80-
th(percentage(lcovObj.lcov).toFixed(2), "%"),
81-
pdiffHtml,
82-
),
83-
),
84-
)} \n\n ${details(
85-
summary("Coverage Report"),
86-
tabulate(report, options),
87-
)} <br/>`;
88-
});
92+
const body = lcovArrayForMonorepo
93+
.map((lcovObj) => {
94+
const baseLcov = lcovBaseArrayForMonorepo.find(
95+
(el) => el.packageName === lcovObj.packageName,
96+
);
97+
return renderLcov(
98+
lcovObj.lcov,
99+
baseLcov && baseLcov.lcov,
100+
lcovObj.packageName,
101+
options,
102+
);
103+
})
104+
.join("");
89105

106+
const { base } = options;
90107
const title = `Coverage after merging into ${b(base)} <p></p>`;
91-
92-
return fragment(title, html.join(""));
108+
return fragment(title, body);
93109
};
94110

95111
/**
@@ -99,43 +115,8 @@ const commentForMonorepo = (
99115
*/
100116
const comment = (lcov, before, options) => {
101117
const { appName, base } = options;
102-
const pbefore = before ? percentage(before) : 0;
103-
const pafter = before ? percentage(lcov) : 0;
104-
const pdiff = pafter - pbefore;
105-
const plus = pdiff > 0 ? "+" : "";
106-
107-
let arrow = "";
108-
if (pdiff < 0) {
109-
arrow = "▾";
110-
} else if (pdiff > 0) {
111-
arrow = "▴";
112-
}
113-
114-
const pdiffHtml = before
115-
? th(renderEmoji(pdiff), " ", arrow, " ", plus, pdiff.toFixed(2), "%")
116-
: "";
117-
118-
let report = lcov;
119-
120-
if (before) {
121-
const onlyInLcov = lcov.filter(comparer(before));
122-
const onlyInBefore = before.filter(comparer(lcov));
123-
report = onlyInBefore.concat(onlyInLcov);
124-
}
125-
126118
const title = `Coverage after merging into ${b(base)} <p></p>`;
127-
const header = appName
128-
? tbody(
129-
tr(th(appName), th(percentage(lcov).toFixed(2), "%"), pdiffHtml),
130-
)
131-
: tbody(tr(th(percentage(lcov).toFixed(2), "%"), pdiffHtml));
132-
133-
return fragment(
134-
title,
135-
table(header),
136-
"\n\n",
137-
details(summary("Coverage Report"), tabulate(report, options)),
138-
);
119+
return fragment(title, renderLcov(lcov, before, appName, options));
139120
};
140121

141122
/**

src/html.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const b = tag("b");
2323
export const table = tag("table");
2424
export const tbody = tag("tbody");
2525
export const a = tag("a");
26+
export const p = tag("p");
2627
export const span = tag("span");
2728

2829
export const fragment = (...children) => children.join("");

0 commit comments

Comments
 (0)