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.
test: more testeable code, multiple comments
- Loading branch information
Andres Adjimann
committed
Sep 11, 2023
1 parent
0a2dcdf
commit 9d3a7b4
Showing
7 changed files
with
278 additions
and
233 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
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
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,175 @@ | ||
import { renderCommentArray, renderCommentLine } from "./comment"; | ||
import { getLcovArray, readLcov } from "./monorepo"; | ||
import { checkCoverage } from "./check"; | ||
import { badge } from "./badge"; | ||
import path from "path"; | ||
import { percentage } from "./lcov"; | ||
|
||
async function comments( | ||
rootLcov, | ||
baseFile, | ||
monorepoBasePath, | ||
lcovArrayForMonorepo, | ||
options, | ||
addPackageName, | ||
upsert, | ||
) { | ||
// Comments | ||
if (lcovArrayForMonorepo.length > 0) { | ||
const lcovBaseArrayForMonorepo = ( | ||
await getLcovArray(monorepoBasePath, "lcov-base") | ||
).map(addPackageName); | ||
if (options.multipleComment) { | ||
for (const lcovObj of lcovArrayForMonorepo) { | ||
const baseLcov = lcovBaseArrayForMonorepo.find( | ||
(el) => el.packageName === lcovObj.packageName, | ||
); | ||
const body = renderCommentLine( | ||
lcovObj.lcov, | ||
baseLcov && baseLcov.lcov, | ||
lcovObj.packageName, | ||
options, | ||
); | ||
await upsert(`m-${lcovObj.packageName}`, body); | ||
} | ||
} else { | ||
const body = renderCommentArray( | ||
lcovArrayForMonorepo, | ||
lcovBaseArrayForMonorepo, | ||
options, | ||
); | ||
await upsert("single-comment", body); | ||
} | ||
} | ||
if (rootLcov) { | ||
const body = renderCommentLine( | ||
rootLcov, | ||
baseFile && (await readLcov(baseFile)), | ||
"root", | ||
options, | ||
); | ||
await upsert(`root-comment`, body); | ||
} | ||
} | ||
|
||
async function badges( | ||
rootLcov, | ||
lcovArrayForMonorepo, | ||
options, | ||
mkDir, | ||
writeBadge, | ||
) { | ||
const badgeOptions = { | ||
label: options.badgeLabel, | ||
style: options.badgeStyle, | ||
}; | ||
const toBadge = lcovArrayForMonorepo; | ||
if (rootLcov) { | ||
toBadge.push({ | ||
packageName: "root_package", | ||
lcov: rootLcov, | ||
}); | ||
} | ||
const dirName = path.resolve(options.badgePath); | ||
mkDir(dirName); | ||
for (const lcovObj of toBadge) { | ||
const coverage = percentage(lcovObj.lcov); | ||
const svgStr = badge(badgeOptions, coverage.toFixed(2)); | ||
const fileName = path.join(dirName, `${lcovObj.packageName}.svg`); | ||
console.log("writing badge", fileName); | ||
try { | ||
writeBadge(fileName, svgStr); | ||
} catch (err) { | ||
console.error("Error writing badge", fileName, err.toString()); | ||
} | ||
} | ||
} | ||
|
||
async function checks( | ||
rootLcov, | ||
lcovArrayForMonorepo, | ||
options, | ||
setFailed, | ||
info, | ||
) { | ||
const excludedFiles = (options.excluded || "") | ||
.split(" ") | ||
.map((x) => x.trim()) | ||
.filter((x) => x.length > 0); | ||
const toCheck = lcovArrayForMonorepo.filter( | ||
(x) => !excludedFiles.some((y) => x.packageName === y), | ||
); | ||
if (rootLcov && !options.excludeRoot) { | ||
toCheck.unshift({ | ||
packageName: "root_package", | ||
lcov: rootLcov, | ||
}); | ||
} | ||
const { isValidBuild, coverage, name } = checkCoverage( | ||
options.minCoverage, | ||
toCheck, | ||
); | ||
if (!isValidBuild) { | ||
setFailed( | ||
`${coverage.toFixed(2)}% for ${name} is less than min_coverage ${ | ||
options.minCoverage | ||
}.`, | ||
); | ||
} else { | ||
info( | ||
`Coverage: ${coverage.toFixed( | ||
2, | ||
)}% is greater than or equal to min_coverage ${ | ||
options.minCoverage | ||
}.`, | ||
); | ||
} | ||
} | ||
|
||
export const codeCoverageAssistant = async ({ | ||
monorepoBasePath, | ||
lcovFile, | ||
baseFile, | ||
options, | ||
upsert, | ||
mkDir, | ||
writeBadge, | ||
getPackageName, | ||
setFailed, | ||
info, | ||
}) => { | ||
const addPackageName = (x) => ({ | ||
...x, | ||
...{ packageName: getPackageName(x.dir) }, | ||
}); | ||
const lcovArrayForMonorepo = ( | ||
monorepoBasePath | ||
? await getLcovArray(monorepoBasePath, "lcov.info") | ||
: [] | ||
).map(addPackageName); | ||
// Always process root file if exists. | ||
const rootLcov = lcovFile && (await readLcov(lcovFile)); | ||
if (options.maxLines > 0) { | ||
await comments( | ||
rootLcov, | ||
baseFile, | ||
monorepoBasePath, | ||
lcovArrayForMonorepo, | ||
options, | ||
addPackageName, | ||
upsert, | ||
); | ||
} | ||
if (options.badgePath) { | ||
await badges( | ||
rootLcov, | ||
lcovArrayForMonorepo, | ||
options, | ||
mkDir, | ||
writeBadge, | ||
); | ||
} | ||
if (options.minCoverage) { | ||
await checks(rootLcov, lcovArrayForMonorepo, options, setFailed, info); | ||
} | ||
}; |
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.