Skip to content

Commit

Permalink
fix: ignore node_modules directories, code refactor, check min-coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Andres Adjimann committed Aug 17, 2023
1 parent d7bf334 commit e20f472
Show file tree
Hide file tree
Showing 13 changed files with 282 additions and 227 deletions.
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ module.exports = {
"generator-star-spacing": ["error", { before: false, after: true }],
"jsx-quotes": ["error", "prefer-double"],
"max-depth": ["error", 10],
"newline-before-return": "error",
"no-alert": "error",
"no-confusing-arrow": ["error", { allowParens: false }],
"no-constant-condition": "error",
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules/
coverage/
.eslintcache
.DS_Store
.idea
14 changes: 14 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ inputs:
app-name:
description: App name to display on comment
required: false
max_lines:
description: Maximum numbers of line print
required: false
default: "15"
min_coverage:
description: Minimum coverage percentage allowed
required: false
default: "100"
exclude:
description: list of files you would like to exclude from min_coverage check
required: false
exclude_root:
description: exclude the root project coverage from min_coverage check
required: false
runs:
using: node16
main: dist/main.js
4 changes: 2 additions & 2 deletions dist/main.js

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions src/check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { percentage } from "./lcov";

export const checkCoverage = (minCoverage, toCheck) => {
let accum = 0;
for (const lcovObj of toCheck) {
const coverage = percentage(lcovObj.lcov);
const isValidBuild = coverage >= minCoverage;
if (!isValidBuild) {
return { isValidBuild, coverage, name: lcovObj.packageName };
}
accum += coverage;
}

return {
isValidBuild: true,
coverage: toCheck.length === 0 ? 0 : accum / toCheck.length,
name: "average",
};
};
45 changes: 30 additions & 15 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
import process from "process";
import { promises as fs } from "fs";
import fs from "fs";
import path from "path";
import { parse } from "./lcov";
import { diff } from "./comment";
import { diff, diffForMonorepo } from "./comment";
import { getLcovArray, readLcov } from "./monorepo";
import { checkCoverage } from "./check";

const main = async () => {
const file = process.argv[2];
const beforeFile = process.argv[3];
const prefix = `${path.dirname(path.dirname(path.resolve(file)))}/`;

const content = await fs.readFile(file, "utf-8");
const lcov = await parse(content);

let before;
if (beforeFile) {
const contentBefore = await fs.readFile(beforeFile, "utf-8");
before = await parse(contentBefore);
}

const prefix = `${path.dirname(path.dirname(path.resolve(file)))}/`;
const options = {
repository: "example/foo",
commit: "f9d42291812ed03bb197e48050ac38ac6befe4e5",
prefix,
head: "feat/test",
base: "master",
maxLines: "10",
};

console.log(diff(lcov, before, options));
if (fs.statSync(file).isDirectory()) {
const lcovArrayForMonorepo = await getLcovArray(file, "lcov.info");
console.log(
diffForMonorepo(
lcovArrayForMonorepo,
await getLcovArray(file, "lcov-base"),
options,
),
);
console.log(checkCoverage(90, lcovArrayForMonorepo));
} else {
const lcov = await readLcov(file);
console.log(
diff(lcov, beforeFile && (await readLcov(beforeFile)), options),
);
console.log(
checkCoverage(90, [
{
packageName: file,
lcov,
},
]),
);
}
};

main().catch((err) => {
Expand Down
161 changes: 71 additions & 90 deletions src/comment.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { details, summary, b, fragment, table, tbody, tr, th } from "./html";
import { details, summary, b, fragment, table, tbody, tr, th, p } from "./html";
import { percentage } from "./lcov";
import { tabulate } from "./tabulate";

Expand All @@ -9,10 +9,19 @@ import { tabulate } from "./tabulate";
*/
const renderEmoji = (pdiff) => {
if (pdiff.toFixed(2) < 0) return "❌";

return "✅";
};

const renderArrow = (pdiff) => {
if (pdiff < 0) {
return "▾";
}
if (pdiff > 0) {
return "▴";
}
return "";
};

/**
* Compares two arrays of objects and returns with unique lines update
* @param {Array} otherArray
Expand All @@ -25,6 +34,50 @@ const comparer = (otherArray) => (current) =>
other.lines.hit === current.lines.hit,
).length === 0;

const renderLcov = (lcov, base, appTitle, options) => {
const maxLines = options.maxLines || 15;
const pbefore = base ? percentage(base) : 0;
const pafter = base ? percentage(lcov) : 0;
const pdiff = pafter - pbefore;
const plus = pdiff > 0 ? "+" : "";

let report = lcov;
if (base) {
const onlyInLcov = lcov.filter(comparer(base));
const onlyInBefore = base.filter(comparer(lcov));
report = onlyInBefore.concat(onlyInLcov);
}

const h = th(percentage(lcov).toFixed(2), "%");

const row = [];
if (appTitle) {
row.push(th(appTitle));
}
row.push(h);
if (base) {
const arrow = renderArrow(pdiff);
row.push(
th(
renderEmoji(pdiff),
" ",
arrow,
" ",
plus,
pdiff.toFixed(2),
"%",
),
);
}
return [
table(tbody(tr(...row))),
report.length > maxLines
? p("Coverage Report too long to display")
: details(summary("Coverage Report"), tabulate(report, options)),
"<br/>",
].join("");
};

/**
* Github comment for monorepo
* @param {Array<{packageName, lcovPath}>} lcovArrayForMonorepo
Expand All @@ -36,60 +89,23 @@ const commentForMonorepo = (
lcovBaseArrayForMonorepo,
options,
) => {
const { base } = options;
const html = lcovArrayForMonorepo.map((lcovObj) => {
const baseLcov = lcovBaseArrayForMonorepo.find(
(el) => el.packageName === lcovObj.packageName,
);

const pbefore = baseLcov ? percentage(baseLcov.lcov) : 0;
const pafter = baseLcov ? percentage(lcovObj.lcov) : 0;
const pdiff = pafter - pbefore;
const plus = pdiff > 0 ? "+" : "";

let arrow = "";
if (pdiff < 0) {
arrow = "▾";
} else if (pdiff > 0) {
arrow = "▴";
}

const pdiffHtml = baseLcov
? th(
renderEmoji(pdiff),
" ",
arrow,
" ",
plus,
pdiff.toFixed(2),
"%",
)
: "";
let report = lcovObj.lcov;

if (baseLcov) {
const onlyInLcov = lcovObj.lcov.filter(comparer(baseLcov));
const onlyInBefore = baseLcov.filter(comparer(lcovObj.lcov));
report = onlyInBefore.concat(onlyInLcov);
}

return `${table(
tbody(
tr(
th(lcovObj.packageName),
th(percentage(lcovObj.lcov).toFixed(2), "%"),
pdiffHtml,
),
),
)} \n\n ${details(
summary("Coverage Report"),
tabulate(report, options),
)} <br/>`;
});
const body = lcovArrayForMonorepo
.map((lcovObj) => {
const baseLcov = lcovBaseArrayForMonorepo.find(
(el) => el.packageName === lcovObj.packageName,
);
return renderLcov(
lcovObj.lcov,
baseLcov && baseLcov.lcov,
lcovObj.packageName,
options,
);
})
.join("");

const { base } = options;
const title = `Coverage after merging into ${b(base)} <p></p>`;

return fragment(title, html.join(""));
return fragment(title, body);
};

/**
Expand All @@ -99,43 +115,8 @@ const commentForMonorepo = (
*/
const comment = (lcov, before, options) => {
const { appName, base } = options;
const pbefore = before ? percentage(before) : 0;
const pafter = before ? percentage(lcov) : 0;
const pdiff = pafter - pbefore;
const plus = pdiff > 0 ? "+" : "";

let arrow = "";
if (pdiff < 0) {
arrow = "▾";
} else if (pdiff > 0) {
arrow = "▴";
}

const pdiffHtml = before
? th(renderEmoji(pdiff), " ", arrow, " ", plus, pdiff.toFixed(2), "%")
: "";

let report = lcov;

if (before) {
const onlyInLcov = lcov.filter(comparer(before));
const onlyInBefore = before.filter(comparer(lcov));
report = onlyInBefore.concat(onlyInLcov);
}

const title = `Coverage after merging into ${b(base)} <p></p>`;
const header = appName
? tbody(
tr(th(appName), th(percentage(lcov).toFixed(2), "%"), pdiffHtml),
)
: tbody(tr(th(percentage(lcov).toFixed(2), "%"), pdiffHtml));

return fragment(
title,
table(header),
"\n\n",
details(summary("Coverage Report"), tabulate(report, options)),
);
return fragment(title, renderLcov(lcov, before, appName, options));
};

/**
Expand Down
1 change: 1 addition & 0 deletions src/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const b = tag("b");
export const table = tag("table");
export const tbody = tag("tbody");
export const a = tag("a");
export const p = tag("p");
export const span = tag("span");

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

0 comments on commit e20f472

Please sign in to comment.