Skip to content

Commit 0a2dcdf

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

File tree

16 files changed

+394
-226
lines changed

16 files changed

+394
-226
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: 2 additions & 0 deletions
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/

action.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,32 @@ 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+
badge_path:
38+
description: Output badge path.
39+
default: build
40+
required: false
41+
badge_style:
42+
description: 'Badges style: flat, classic.'
43+
default: classic
44+
required: false
45+
badge_label:
46+
description: The left label of the badge, usually static.
47+
default: coverage
48+
required: false
2349
runs:
2450
using: node16
2551
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.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"dependencies": {
2424
"@actions/core": "^1.10.0",
2525
"@actions/github": "^4.0.0",
26+
"badgen": "^3.2.3",
2627
"lcov-parse": "^1.0.0"
2728
},
2829
"devDependencies": {

src/badge.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import path from "path";
2+
import fs from "fs";
3+
import { badgen } from "badgen";
4+
import { percentage } from "./lcov";
5+
6+
const 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) =>
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",
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+
};

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: 46 additions & 14 deletions
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)