|
| 1 | +// oxlint-disable max-depth |
| 2 | +import { styleText } from 'node:util' |
| 3 | +import type { Report } from '../program.js' |
| 4 | +import type { CliArguments } from '../arguments.js' |
| 5 | + |
| 6 | +// Re-indent because tabs in the terminal tend to be bigger than usual |
| 7 | +function indent(line?: string): string { |
| 8 | + return (line || '').replace(/^\t+/, (tabs) => ' '.repeat(tabs.length * 4)) |
| 9 | +} |
| 10 | + |
| 11 | +export function print({ report, context }: Report, params: CliArguments) { |
| 12 | + if (report.min_line_coverage.ok) { |
| 13 | + console.log(`${styleText(['bold', 'green'], 'Success')}: total line coverage is ${(report.min_line_coverage.actual * 100).toFixed(2)}%`) |
| 14 | + } else { |
| 15 | + console.error( |
| 16 | + `${styleText(['bold', 'red'], 'Failed')}: line coverage is ${(report.min_line_coverage.actual * 100).toFixed( |
| 17 | + 2, |
| 18 | + )}% which is lower than the threshold of ${report.min_line_coverage.expected}`, |
| 19 | + ) |
| 20 | + } |
| 21 | + |
| 22 | + if (report.min_file_line_coverage.expected !== undefined) { |
| 23 | + let { expected, actual, ok } = report.min_file_line_coverage |
| 24 | + if (ok) { |
| 25 | + console.log(`${styleText(['bold', 'green'], 'Success')}: all files pass minimum line coverage of ${expected * 100}%`) |
| 26 | + } else { |
| 27 | + let num_files_failed = context.coverage.coverage_per_stylesheet.filter((sheet) => sheet.line_coverage_ratio < expected!).length |
| 28 | + console.error( |
| 29 | + `${styleText(['bold', 'red'], 'Failed')}: ${num_files_failed} files do not meet the minimum line coverage of ${ |
| 30 | + expected * 100 |
| 31 | + }% (minimum coverage was ${(actual * 100).toFixed(2)}%)`, |
| 32 | + ) |
| 33 | + if (params['show-uncovered'] === 'none') { |
| 34 | + console.log(` Hint: set --show-uncovered=violations to see which files didn't pass`) |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + // Show un-covered chunks |
| 40 | + if (params['show-uncovered'] !== 'none') { |
| 41 | + const NUM_LEADING_LINES = 3 |
| 42 | + const NUM_TRAILING_LINES = NUM_LEADING_LINES |
| 43 | + let terminal_width = process.stdout.columns || 80 |
| 44 | + let line_number = (num: number, covered: boolean = true) => `${num.toString().padStart(5, ' ')} ${covered ? '│' : '━'} ` |
| 45 | + let min_file_line_coverage = report.min_file_line_coverage.expected |
| 46 | + |
| 47 | + for (let sheet of context.coverage.coverage_per_stylesheet.sort((a, b) => a.line_coverage_ratio - b.line_coverage_ratio)) { |
| 48 | + if ( |
| 49 | + (sheet.line_coverage_ratio !== 1 && params['show-uncovered'] === 'all') || |
| 50 | + (min_file_line_coverage !== undefined && |
| 51 | + min_file_line_coverage !== 0 && |
| 52 | + sheet.line_coverage_ratio < min_file_line_coverage && |
| 53 | + params['show-uncovered'] === 'violations') |
| 54 | + ) { |
| 55 | + console.log() |
| 56 | + console.log(styleText('dim', '─'.repeat(terminal_width))) |
| 57 | + console.log(sheet.url) |
| 58 | + console.log(`Coverage: ${(sheet.line_coverage_ratio * 100).toFixed(2)}%, ${sheet.covered_lines}/${sheet.total_lines} lines covered`) |
| 59 | + |
| 60 | + if (min_file_line_coverage && min_file_line_coverage !== 0 && sheet.line_coverage_ratio < min_file_line_coverage) { |
| 61 | + let lines_to_cover = min_file_line_coverage * sheet.total_lines - sheet.covered_lines |
| 62 | + console.log(`Tip: cover ${Math.ceil(lines_to_cover)} more lines to meet the file threshold of ${min_file_line_coverage * 100}%`) |
| 63 | + } |
| 64 | + console.log(styleText('dim', '─'.repeat(terminal_width))) |
| 65 | + |
| 66 | + let lines = sheet.text.split('\n') |
| 67 | + let line_coverage = sheet.line_coverage |
| 68 | + |
| 69 | + for (let i = 0; i < lines.length; i++) { |
| 70 | + if (line_coverage[i] === 1) continue |
| 71 | + |
| 72 | + // Rewind cursor N lines to render N previous lines |
| 73 | + for (let j = i - NUM_LEADING_LINES; j < i; j++) { |
| 74 | + // Make sure that we don't try to start before line 0 |
| 75 | + if (j >= 0) { |
| 76 | + console.log(styleText('dim', line_number(j)), styleText('dim', indent(lines[j]))) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Render uncovered lines while increasing cursor until reaching next covered block |
| 81 | + while (line_coverage[i] === 0) { |
| 82 | + console.log(styleText('red', line_number(i, false)), indent(lines[i])) |
| 83 | + i++ |
| 84 | + } |
| 85 | + |
| 86 | + // Forward cursor N lines to render N trailing lines |
| 87 | + for (let end = i + NUM_TRAILING_LINES; i < end && i < lines.length; i++) { |
| 88 | + console.log(styleText('dim', line_number(i)), styleText('dim', indent(lines[i]))) |
| 89 | + } |
| 90 | + |
| 91 | + // Show empty line between blocks |
| 92 | + console.log() |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments