-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
73 additions
and
6 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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
.DS_Store | ||
.coverage | ||
lcov.info | ||
staging/ | ||
coverage/ | ||
.env |
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,66 @@ | ||
/** | ||
* Calculates and checks the branch coverage of a Deno project. | ||
* | ||
* @remarks | ||
* This script runs the tests with coverage enabled and then analyzes the coverage results. | ||
* If the branch coverage is below a specified threshold, it exits with an error. | ||
* | ||
* @param THRESHOLD - The minimum branch coverage threshold (in percentage) required for the project. | ||
*/ | ||
|
||
// Minimum branch coverage threshold (in percentage) | ||
const THRESHOLD = 90; | ||
|
||
const getCommandOutput = async (command: Deno.Command) => { | ||
const { code, stdout, stderr } = await command.output(); | ||
const error = new TextDecoder().decode(stderr); | ||
if (code || error) { | ||
console.error(error); | ||
Deno.exit(1); | ||
} | ||
const output = new TextDecoder().decode(stdout); | ||
return output; | ||
}; | ||
|
||
// Run tests with coverage enabled | ||
const testCmd = new Deno.Command(Deno.execPath(), { | ||
args: [ | ||
"test", | ||
"--coverage", | ||
], | ||
}); | ||
|
||
// Check that the command ran successfully | ||
await getCommandOutput(testCmd); | ||
|
||
// Calculate branch coverage | ||
const covCmd = new Deno.Command(Deno.execPath(), { | ||
args: [ | ||
"coverage", | ||
"./coverage", | ||
], | ||
}); | ||
|
||
// Check that the command ran successfully | ||
const coverageReport = await getCommandOutput(covCmd); | ||
|
||
// Remove ANSI escape codes | ||
const words = coverageReport.replaceAll("\x1b", " ").split(" "); | ||
|
||
// Remove all non-numbers | ||
const floats = words.filter((word) => !isNaN(parseFloat(word))); | ||
|
||
// Pick the second last number | ||
const branchTotal = parseFloat(floats.at(-2) || ""); | ||
|
||
if (!branchTotal) { | ||
console.error("Could not retrieve branch coverage"); | ||
Deno.exit(1); | ||
} | ||
|
||
if (branchTotal > THRESHOLD) { | ||
console.log(`Branch coverage is good: ${branchTotal}`); | ||
} else { | ||
console.log(`Branch coverage is bad: ${branchTotal}`); | ||
Deno.exit(1); | ||
} |