Skip to content

Commit

Permalink
Ci coverage (#5)
Browse files Browse the repository at this point in the history
* ci: Add code coverage script
  • Loading branch information
tomas-zijdemans-vipps authored Dec 18, 2023
1 parent fa1b221 commit 44f1924
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/deno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ on:

jobs:
deno:
runs-on: macos-latest
runs-on: ubuntu-latest

steps:
- name: Setup repo
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Deno
run: brew install deno
run: sudo snap install deno

- name: Run tests
run: deno fmt --check && deno lint && deno test --allow-net --parallel tests/

- name: Check coverage
run: deno run -A scripts/coverage.ts
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
.DS_Store
.coverage
lcov.info
staging/
coverage/
.env
66 changes: 66 additions & 0 deletions scripts/coverage.ts
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);
}

0 comments on commit 44f1924

Please sign in to comment.