|
| 1 | +#!/usr/bin/env ts-node |
| 2 | + |
| 3 | +import fs from "fs"; |
| 4 | +import path from "path"; |
| 5 | + |
| 6 | +interface CoverageData { |
| 7 | + lines: string; |
| 8 | + functions: string; |
| 9 | + branches: string; |
| 10 | + statements: string; |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Parses coverage from lcov.info file |
| 15 | + */ |
| 16 | +function parseLcovCoverage(lcovPath: string): CoverageData { |
| 17 | + const content = fs.readFileSync(lcovPath, "utf8"); |
| 18 | + |
| 19 | + let linesFound = 0; |
| 20 | + let linesHit = 0; |
| 21 | + let functionsFound = 0; |
| 22 | + let functionsHit = 0; |
| 23 | + let branchesFound = 0; |
| 24 | + let branchesHit = 0; |
| 25 | + |
| 26 | + const lines = content.split("\n"); |
| 27 | + for (const line of lines) { |
| 28 | + if (line.startsWith("LF:")) { |
| 29 | + linesFound += parseInt(line.substring(3), 10); |
| 30 | + } else if (line.startsWith("LH:")) { |
| 31 | + linesHit += parseInt(line.substring(3), 10); |
| 32 | + } else if (line.startsWith("BRF:")) { |
| 33 | + branchesFound += parseInt(line.substring(4), 10); |
| 34 | + } else if (line.startsWith("BRH:")) { |
| 35 | + branchesHit += parseInt(line.substring(4), 10); |
| 36 | + } else if (line.startsWith("FNF:")) { |
| 37 | + functionsFound += parseInt(line.substring(4), 10); |
| 38 | + } else if (line.startsWith("FNH:")) { |
| 39 | + functionsHit += parseInt(line.substring(4), 10); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return { |
| 44 | + lines: linesFound > 0 ? (linesHit / linesFound * 100).toFixed(2) : "0", |
| 45 | + functions: functionsFound > 0 ? (functionsHit / functionsFound * 100).toFixed(2) : "0", |
| 46 | + branches: branchesFound > 0 ? (branchesHit / branchesFound * 100).toFixed(2) : "0", |
| 47 | + statements: linesFound > 0 ? (linesHit / linesFound * 100).toFixed(2) : "0" |
| 48 | + }; |
| 49 | +} |
| 50 | + |
| 51 | +// Main |
| 52 | +const lcovPath = path.join(__dirname, "..", "coverage", "lcov.info"); |
| 53 | + |
| 54 | +// Check if custom baseline path provided (for CI to compare against main) |
| 55 | +const baselineArg = process.argv.find(arg => arg.startsWith("--baseline=")); |
| 56 | +const baselinePath = baselineArg |
| 57 | + ? baselineArg.split("=")[1] |
| 58 | + : path.join(__dirname, "..", "coverage-baseline.json"); |
| 59 | + |
| 60 | +// Check if we're updating baseline |
| 61 | +const isUpdatingBaseline = process.argv.includes("--update-baseline"); |
| 62 | + |
| 63 | +if (!fs.existsSync(lcovPath)) { |
| 64 | + console.error("❌ Coverage file not found. Run: npm run coverage"); |
| 65 | + process.exit(1); |
| 66 | +} |
| 67 | + |
| 68 | +const current = parseLcovCoverage(lcovPath); |
| 69 | + |
| 70 | +// If updating baseline, save and exit |
| 71 | +if (isUpdatingBaseline) { |
| 72 | + fs.writeFileSync(baselinePath, JSON.stringify(current, null, 2)); |
| 73 | + console.log("\n✅ Coverage baseline updated:"); |
| 74 | + console.log(` Lines: ${current.lines}%`); |
| 75 | + console.log(` Functions: ${current.functions}%`); |
| 76 | + console.log(` Branches: ${current.branches}%`); |
| 77 | + console.log(` Statements: ${current.statements}%\n`); |
| 78 | + process.exit(0); |
| 79 | +} |
| 80 | + |
| 81 | +// Load baseline |
| 82 | +let baseline: CoverageData = {lines: "0", functions: "0", branches: "0", statements: "0"}; |
| 83 | +if (fs.existsSync(baselinePath)) { |
| 84 | + baseline = JSON.parse(fs.readFileSync(baselinePath, "utf8")) as CoverageData; |
| 85 | +} |
| 86 | + |
| 87 | +// Display comparison |
| 88 | +console.log("\n📊 Coverage Comparison:"); |
| 89 | +console.log("─".repeat(50)); |
| 90 | +console.log(`Lines: ${baseline.lines}% → ${current.lines}%`); |
| 91 | +console.log(`Functions: ${baseline.functions}% → ${current.functions}%`); |
| 92 | +console.log(`Branches: ${baseline.branches}% → ${current.branches}%`); |
| 93 | +console.log(`Statements: ${baseline.statements}% → ${current.statements}%`); |
| 94 | +console.log("─".repeat(50)); |
| 95 | + |
| 96 | +// Check for drops |
| 97 | +const drops: string[] = []; |
| 98 | +if (parseFloat(current.lines) < parseFloat(baseline.lines)) { |
| 99 | + drops.push(`Lines dropped: ${baseline.lines}% → ${current.lines}%`); |
| 100 | +} |
| 101 | +if (parseFloat(current.functions) < parseFloat(baseline.functions)) { |
| 102 | + drops.push(`Functions dropped: ${baseline.functions}% → ${current.functions}%`); |
| 103 | +} |
| 104 | +if (parseFloat(current.branches) < parseFloat(baseline.branches)) { |
| 105 | + drops.push(`Branches dropped: ${baseline.branches}% → ${current.branches}%`); |
| 106 | +} |
| 107 | +if (parseFloat(current.statements) < parseFloat(baseline.statements)) { |
| 108 | + drops.push(`Statements dropped: ${baseline.statements}% → ${current.statements}%`); |
| 109 | +} |
| 110 | + |
| 111 | +if (drops.length > 0) { |
| 112 | + console.log("\n❌ Coverage decreased:\n"); |
| 113 | + drops.forEach((drop: string) => console.log(` • ${drop}`)); |
| 114 | + console.log("\n💡 Please add tests to maintain or improve coverage.\n"); |
| 115 | + process.exit(1); |
| 116 | +} |
| 117 | + |
| 118 | +console.log("\n✅ Coverage maintained or improved!\n"); |
| 119 | +process.exit(0); |
0 commit comments