Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add coverage #290

Merged
merged 8 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ jobs:
run: |
pnpm install --no-frozen-lockfile
pnpm ci-test

- shell: bash
run: |
pnpm run check-coverage
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ docs/
node_modules/

**/tsconfig.tsbuildinfo
coverage/*
.vscode/*
51 changes: 51 additions & 0 deletions check-coverage.ts
d-roak marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as fs from "node:fs";
import path from "node:path";
const lcovPath = path.resolve("./coverage/lcov.info");
const threshold = 20; // Set your desired threshold

function parseCoverage(lcovFilePath: string): number {
if (!fs.existsSync(lcovFilePath)) {
throw new Error(`File not found: ${lcovFilePath}`);
}

const lcovData = fs.readFileSync(lcovFilePath, "utf-8");
const totalLinesMatch = lcovData.match(/LF:(\d+)/g);
const coveredLinesMatch = lcovData.match(/LH:(\d+)/g);

if (!totalLinesMatch || !coveredLinesMatch) {
throw new Error("Coverage data is missing or invalid in lcov.info");
}

const totalLines = totalLinesMatch
.map((line) => Number.parseInt(line.split(":")[1], 10))
.reduce((sum, value) => sum + value, 0);

const coveredLines = coveredLinesMatch
.map((line) => Number.parseInt(line.split(":")[1], 10))
.reduce((sum, value) => sum + value, 0);

return (coveredLines / totalLines) * 100;
}

try {
const coveragePercentage = parseCoverage(lcovPath);
console.log(`Total Coverage: ${coveragePercentage.toFixed(2)}%`);

if (coveragePercentage < threshold) {
console.error(
`Coverage (${coveragePercentage.toFixed(
2,
)}%) is below the threshold (${threshold}%).`,
);
process.exit(1); // Exit with an error code if threshold is not met
} else {
console.log(
`Coverage (${coveragePercentage.toFixed(
2,
)}%) meets the threshold (${threshold}%).`,
);
}
} catch (error) {
console.error(`Error: ${(error as Error).message}`);
process.exit(1); // Exit with an error code for any script failure
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
"release": "release-it",
"test": "vitest",
"ci-test": "vitest --exclude **/node.test.ts",
"watch": "pnpm --parallel --filter '@ts-drp/*' watch"
"watch": "pnpm --parallel --filter '@ts-drp/*' watch",
"check-coverage": "npx ts-node check-coverage.ts"
d-roak marked this conversation as resolved.
Show resolved Hide resolved
},
"devDependencies": {
"@biomejs/biome": "^1.8.3",
"@release-it-plugins/workspaces": "^4.2.0",
"@types/node": "^22.5.4",
"@vitest/coverage-v8": "2.1.8",
"assemblyscript": "^0.27.29",
"release-it": "^17.6.0",
"ts-proto": "^2.2.4",
Expand Down
Loading
Loading