Skip to content

Commit f24e576

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

16 files changed

+392
-226
lines changed

Diff for: .eslintrc.js

-1
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",

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ node_modules/
22
coverage/
33
.eslintcache
44
.DS_Store
5+
.idea
6+
badges/

Diff for: action.yml

+27
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,33 @@ 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
37+
38+
badge_path:
39+
description: Output badge path.
40+
default: build
41+
required: false
42+
badge_style:
43+
description: 'Badges style: flat, classic.'
44+
default: classic
45+
required: false
46+
badge_label:
47+
description: The left label of the badge, usually static.
48+
default: coverage
49+
required: false
2350
runs:
2451
using: node16
2552
main: dist/main.js

Diff for: dist/main.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@rollup/plugin-json": "^5.0.1",
3535
"@rollup/plugin-node-resolve": "^15.0.1",
3636
"babel-jest": "^29.3.1",
37+
"badgen": "^3.2.3",
3738
"core-js": "3.27.2",
3839
"eslint": "^8.32.0",
3940
"eslint-config-airbnb": "19.0.4",

Diff for: src/badge.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { percentage } from "./lcov";
2+
import { badgen } from "badgen";
3+
import path from "path";
4+
import fs from "fs";
5+
6+
function badge(option, pct) {
7+
const { label = "coverage", style = "classic" } = option || {};
8+
const colorData = {
9+
"#49c31a": [100],
10+
"#97c40f": [99.99, 90],
11+
"#a0a127": [89.99, 80],
12+
"#cba317": [79.99, 60],
13+
"#ce0000": [59.99, 0],
14+
};
15+
const color = Object.keys(colorData).find(
16+
(value, idx) =>
17+
(colorData[value].length === 1 && pct >= colorData[value][0]) ||
18+
(colorData[value].length === 2 &&
19+
pct <= colorData[value][0] &&
20+
pct >= colorData[value][1]),
21+
);
22+
const badgenArgs = {
23+
style,
24+
label,
25+
status: `${pct < 0 ? "Unknown" : `${pct}%`}`,
26+
color: (color || "#e5e5e5").replace(/^#/, ""),
27+
};
28+
return badgen(badgenArgs);
29+
}
30+
31+
export const createBadges = (badgePath, toCheck, options) => {
32+
const dirName = path.resolve(badgePath);
33+
if (!fs.existsSync(dirName)) {
34+
fs.mkdirSync(dirName);
35+
} else if (!fs.statSync(dirName).isDirectory()) {
36+
throw new Error("badge path is not a directory");
37+
}
38+
for (const lcovObj of toCheck) {
39+
const coverage = percentage(lcovObj.lcov);
40+
const svgStr = badge(options, coverage.toFixed(2));
41+
const fileName = path.join(dirName, `${lcovObj.packageName}.svg`);
42+
console.log("writing badge", fileName);
43+
try {
44+
fs.writeFileSync(fileName, svgStr);
45+
} catch (err) {
46+
console.error("Error writing badge", fileName, err.toString());
47+
}
48+
}
49+
};

Diff for: src/check.js

+19
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+
};

Diff for: src/cli.js

+46-14
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,64 @@
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";
7+
import { createBadges } from "./badge";
68

9+
const addPackageName = (x) => ({
10+
...x,
11+
...{ packageName: x.dir.split("/").slice(-2)[0] },
12+
});
713
const main = async () => {
814
const file = process.argv[2];
915
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-
}
2016

17+
const prefix = `${path.dirname(path.dirname(path.resolve(file)))}/`;
2118
const options = {
2219
repository: "example/foo",
2320
commit: "f9d42291812ed03bb197e48050ac38ac6befe4e5",
2421
prefix,
2522
head: "feat/test",
2623
base: "master",
24+
maxLines: "10",
2725
};
2826

29-
console.log(diff(lcov, before, options));
27+
if (fs.statSync(file).isDirectory()) {
28+
const lcovArrayForMonorepo = (
29+
await getLcovArray(file, "lcov.info")
30+
).map(addPackageName);
31+
console.log(
32+
diffForMonorepo(
33+
lcovArrayForMonorepo,
34+
await getLcovArray(file, "lcov-base"),
35+
options,
36+
),
37+
);
38+
createBadges("./badges", lcovArrayForMonorepo, {});
39+
console.log(checkCoverage(90, lcovArrayForMonorepo));
40+
} else {
41+
const lcov = await readLcov(file);
42+
console.log(
43+
diff(lcov, beforeFile && (await readLcov(beforeFile)), options),
44+
);
45+
createBadges(
46+
"./build",
47+
{
48+
packageName: "root",
49+
lcov,
50+
},
51+
{},
52+
);
53+
console.log(
54+
checkCoverage(90, [
55+
{
56+
packageName: "root",
57+
lcov,
58+
},
59+
]),
60+
);
61+
}
3062
};
3163

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

0 commit comments

Comments
 (0)