|
| 1 | +#!/usr/bin/env node |
| 2 | +/* |
| 3 | +Simple coverage enforcement script. |
| 4 | +- Reads a JSON summary from stdin or a file (istanbul/nyc, jest --coverage, etc.) |
| 5 | +- Enforces global >= 90% |
| 6 | +- Enforces core modules >= 95% (by path includes /src/core or /core/) |
| 7 | +- Enforces integrations/adapters >= 85% (by path includes /adapters or /integrations/) |
| 8 | +- Enforces 100% for files matched as hot paths or error/security (by filename hints) |
| 9 | +Exit non-zero on failure with a readable summary. |
| 10 | +*/ |
| 11 | + |
| 12 | +const fs = require('fs'); |
| 13 | + |
| 14 | +function parseArgs() { |
| 15 | + const args = process.argv.slice(2); |
| 16 | + const params = { file: null }; |
| 17 | + for (let i = 0; i < args.length; i++) { |
| 18 | + if (args[i] === '--file' && args[i + 1]) { |
| 19 | + params.file = args[i + 1]; |
| 20 | + i++; |
| 21 | + } |
| 22 | + } |
| 23 | + return params; |
| 24 | +} |
| 25 | + |
| 26 | +function loadSummary(file) { |
| 27 | + const input = file ? fs.readFileSync(file, 'utf8') : fs.readFileSync(0, 'utf8'); |
| 28 | + return JSON.parse(input); |
| 29 | +} |
| 30 | + |
| 31 | +function pct(n) { return Math.round(n * 10000) / 100; } |
| 32 | + |
| 33 | +function classifyFile(path) { |
| 34 | + const p = path.toLowerCase(); |
| 35 | + if (p.includes('/core/') || p.includes('/src/core/')) return 'core'; |
| 36 | + if (p.includes('/adapters/') || p.includes('/integrations/')) return 'integration'; |
| 37 | + return 'other'; |
| 38 | +} |
| 39 | + |
| 40 | +function isCritical(path) { |
| 41 | + const p = path.toLowerCase(); |
| 42 | + return ( |
| 43 | + p.includes('hot') || |
| 44 | + p.includes('critical') || |
| 45 | + p.includes('auth') || |
| 46 | + p.includes('security') || |
| 47 | + p.includes('error') || |
| 48 | + p.includes('exception') |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +function check(summary) { |
| 53 | + const metrics = summary.total || summary; |
| 54 | + const globalLines = metrics.lines.pct || metrics.lines.covered / metrics.lines.total * 100; |
| 55 | + const globalPass = globalLines >= 90; |
| 56 | + |
| 57 | + const failures = []; |
| 58 | + if (!globalPass) failures.push(`Global lines coverage ${pct(globalLines)}% < 90%`); |
| 59 | + |
| 60 | + // Per-file checks if available |
| 61 | + if (summary && typeof summary === 'object') { |
| 62 | + for (const [file, m] of Object.entries(summary)) { |
| 63 | + if (file === 'total' || !m || !m.lines) continue; |
| 64 | + const filePct = m.lines.pct || (m.lines.covered / m.lines.total * 100); |
| 65 | + const cls = classifyFile(file); |
| 66 | + if (isCritical(file) && filePct < 100) { |
| 67 | + failures.push(`Critical path not fully covered: ${file} ${pct(filePct)}% < 100%`); |
| 68 | + continue; |
| 69 | + } |
| 70 | + if (cls === 'core' && filePct < 95) { |
| 71 | + failures.push(`Core module below 95%: ${file} ${pct(filePct)}%`); |
| 72 | + } else if (cls === 'integration' && filePct < 85) { |
| 73 | + failures.push(`Integration below 85%: ${file} ${pct(filePct)}%`); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return failures; |
| 79 | +} |
| 80 | + |
| 81 | +function main() { |
| 82 | + try { |
| 83 | + const { file } = parseArgs(); |
| 84 | + const summary = loadSummary(file); |
| 85 | + const failures = check(summary); |
| 86 | + if (failures.length) { |
| 87 | + console.error('Coverage enforcement failed:\n- ' + failures.join('\n- ')); |
| 88 | + process.exit(1); |
| 89 | + } |
| 90 | + console.log('Coverage enforcement passed.'); |
| 91 | + } catch (e) { |
| 92 | + console.error('Error running coverage enforcement:', e.message); |
| 93 | + process.exit(2); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +if (require.main === module) { |
| 98 | + main(); |
| 99 | +} |
0 commit comments