forked from romeovs/lcov-reporter-action
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ignore node_modules directories, code refactor, check min-coverage
- Loading branch information
Andres Adjimann
committed
Aug 28, 2023
1 parent
d7bf334
commit 3670acb
Showing
16 changed files
with
391 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ node_modules/ | |
coverage/ | ||
.eslintcache | ||
.DS_Store | ||
.idea | ||
badges/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import path from "path"; | ||
import fs from "fs"; | ||
import { badgen } from "badgen"; | ||
import { percentage } from "./lcov"; | ||
|
||
const badge = (option, pct) => { | ||
const { label = "coverage", style = "classic" } = option || {}; | ||
const colorData = { | ||
"49c31a": [100], | ||
"97c40f": [99.99, 90], | ||
a0a127: [89.99, 80], | ||
cba317: [79.99, 60], | ||
ce0000: [59.99, 0], | ||
}; | ||
const color = Object.keys(colorData).find( | ||
(value) => | ||
(colorData[value].length === 1 && pct >= colorData[value][0]) || | ||
(colorData[value].length === 2 && | ||
pct <= colorData[value][0] && | ||
pct >= colorData[value][1]), | ||
); | ||
const badgenArgs = { | ||
style, | ||
label, | ||
status: `${pct < 0 ? "Unknown" : `${pct}%`}`, | ||
color: color || "e5e5e5", | ||
}; | ||
return badgen(badgenArgs); | ||
}; | ||
|
||
export const createBadges = (badgePath, toCheck, options) => { | ||
const dirName = path.resolve(badgePath); | ||
if (!fs.existsSync(dirName)) { | ||
fs.mkdirSync(dirName); | ||
} else if (!fs.statSync(dirName).isDirectory()) { | ||
throw new Error("badge path is not a directory"); | ||
} | ||
for (const lcovObj of toCheck) { | ||
const coverage = percentage(lcovObj.lcov); | ||
const svgStr = badge(options, coverage.toFixed(2)); | ||
const fileName = path.join(dirName, `${lcovObj.packageName}.svg`); | ||
console.log("writing badge", fileName); | ||
try { | ||
fs.writeFileSync(fileName, svgStr); | ||
} catch (err) { | ||
console.error("Error writing badge", fileName, err.toString()); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.