|
| 1 | +import { expect, test, describe, beforeEach } from 'vitest'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { getCoverageJsonReport, exportedForTesting } from '../src/parseJson'; |
| 4 | +import { spyCore } from './setup'; |
| 5 | +import type { Options } from '../src/types'; |
| 6 | + |
| 7 | +const { isValidCoverageContent, collapseRanges, getMissing, getTotalCoverage } = |
| 8 | + exportedForTesting; |
| 9 | + |
| 10 | +type MissingArg = Parameters<typeof getMissing>[0]; |
| 11 | +type TotalsArg = Parameters<typeof getTotalCoverage>[0]; |
| 12 | + |
| 13 | +const dataPath = path.resolve(__dirname, '..', 'data'); |
| 14 | + |
| 15 | +const baseOptions: Options = { |
| 16 | + token: 'token_123', |
| 17 | + repository: 'MishaKav/pytest-coverage-comment', |
| 18 | + commit: 'abc123', |
| 19 | + prefix: '', |
| 20 | + pathPrefix: '', |
| 21 | + covFile: '', |
| 22 | + covXmlFile: '', |
| 23 | + covJsonFile: '', |
| 24 | + xmlFile: '', |
| 25 | + title: 'Coverage Report', |
| 26 | + badgeTitle: 'Coverage', |
| 27 | + hideBadge: false, |
| 28 | + hideReport: false, |
| 29 | + createNewComment: false, |
| 30 | + hideComment: false, |
| 31 | + hideEmoji: false, |
| 32 | + xmlSkipCovered: false, |
| 33 | + reportOnlyChangedFiles: false, |
| 34 | + removeLinkFromBadge: false, |
| 35 | + removeLinksToFiles: false, |
| 36 | + removeLinksToLines: false, |
| 37 | + textInsteadBadge: false, |
| 38 | + defaultBranch: 'main', |
| 39 | + xmlTitle: '', |
| 40 | + multipleFiles: [], |
| 41 | + repoUrl: 'https://github.com/MishaKav/pytest-coverage-comment', |
| 42 | +}; |
| 43 | + |
| 44 | +describe('getCoverageJsonReport', () => { |
| 45 | + beforeEach(() => { |
| 46 | + spyCore.error.mockClear(); |
| 47 | + spyCore.warning.mockClear(); |
| 48 | + spyCore.info.mockClear(); |
| 49 | + }); |
| 50 | + |
| 51 | + test('should return null for empty covJsonFile', () => { |
| 52 | + const result = getCoverageJsonReport(baseOptions); |
| 53 | + expect(result).toBeNull(); |
| 54 | + }); |
| 55 | + |
| 56 | + test('should return null for non-existent file', () => { |
| 57 | + const options = { ...baseOptions, covJsonFile: '/nonexistent/coverage.json' }; |
| 58 | + const result = getCoverageJsonReport(options); |
| 59 | + expect(result).toBeNull(); |
| 60 | + }); |
| 61 | + |
| 62 | + test('should warn and return null for malformed json', () => { |
| 63 | + const covJsonFile = path.join(dataPath, 'coverage_1.xml'); // not json |
| 64 | + const options = { ...baseOptions, covJsonFile }; |
| 65 | + const result = getCoverageJsonReport(options); |
| 66 | + |
| 67 | + expect(result).toBeNull(); |
| 68 | + expect(spyCore.warning).toHaveBeenCalled(); |
| 69 | + }); |
| 70 | + |
| 71 | + test('should parse coverage_1.json (no branches) successfully', () => { |
| 72 | + const covJsonFile = path.join(dataPath, 'coverage_1.json'); |
| 73 | + const options = { ...baseOptions, covJsonFile }; |
| 74 | + const result = getCoverageJsonReport(options); |
| 75 | + |
| 76 | + expect(result).not.toBeNull(); |
| 77 | + expect(result!.html).toContain('img.shields.io/badge'); |
| 78 | + expect(result!.coverage).not.toBeNull(); |
| 79 | + expect(result!.coverage!.name).toBe('TOTAL'); |
| 80 | + expect(result!.coverage!.cover).toBe('88%'); |
| 81 | + // foo.py is 80% with missing lines 5 and 12 |
| 82 | + expect(result!.html).toContain('<td>80%</td>'); |
| 83 | + expect(result!.html).toContain('>5</a>'); |
| 84 | + expect(result!.html).toContain('>12</a>'); |
| 85 | + // no branch columns for a statement-only report |
| 86 | + expect(result!.html).not.toContain('<th>Branch</th>'); |
| 87 | + }); |
| 88 | + |
| 89 | + test('should parse coverage_2.json with branch coverage', () => { |
| 90 | + const covJsonFile = path.join(dataPath, 'coverage_2.json'); |
| 91 | + const options = { ...baseOptions, covJsonFile }; |
| 92 | + const result = getCoverageJsonReport(options); |
| 93 | + |
| 94 | + expect(result).not.toBeNull(); |
| 95 | + expect(result!.coverage!.cover).toBe('82%'); |
| 96 | + expect(result!.coverage!.branch).toBe('10'); |
| 97 | + expect(result!.coverage!.brpart).toBe('3'); |
| 98 | + expect(result!.html).toContain('<th>Branch</th><th>BrPart</th>'); |
| 99 | + // branch arcs from getMissing are wired into the missing column |
| 100 | + expect(result!.html).toContain('>2->exit</a>'); |
| 101 | + }); |
| 102 | + |
| 103 | + test('should skip covered files when xmlSkipCovered is true', () => { |
| 104 | + const covJsonFile = path.join(dataPath, 'coverage_1.json'); |
| 105 | + const options = { ...baseOptions, covJsonFile, xmlSkipCovered: true }; |
| 106 | + const result = getCoverageJsonReport(options); |
| 107 | + |
| 108 | + expect(result).not.toBeNull(); |
| 109 | + // bar.py is 100% covered and should be excluded from the table |
| 110 | + expect(result!.html).not.toContain('bar.py'); |
| 111 | + expect(result!.html).toContain('foo.py'); |
| 112 | + }); |
| 113 | +}); |
| 114 | + |
| 115 | +describe('parseJson internals', () => { |
| 116 | + test('isValidCoverageContent', () => { |
| 117 | + expect(isValidCoverageContent(null)).toBe(false); |
| 118 | + expect(isValidCoverageContent({ files: {} } as never)).toBe(false); |
| 119 | + expect(isValidCoverageContent({ files: {}, totals: {} } as never)).toBe( |
| 120 | + true, |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | + test('collapseRanges groups consecutive lines', () => { |
| 125 | + expect(collapseRanges([4, 10, 11, 12])).toEqual([ |
| 126 | + { sort: 4, text: '4' }, |
| 127 | + { sort: 10, text: '10-12' }, |
| 128 | + ]); |
| 129 | + expect(collapseRanges([])).toEqual([]); |
| 130 | + }); |
| 131 | + |
| 132 | + test('getMissing renders lines, exit arcs, and normal arcs sorted by line', () => { |
| 133 | + const asArg = (file: { |
| 134 | + missing_lines: number[]; |
| 135 | + missing_branches: number[][]; |
| 136 | + }): MissingArg => file as unknown as MissingArg; |
| 137 | + |
| 138 | + expect( |
| 139 | + getMissing(asArg({ missing_lines: [5, 14], missing_branches: [[2, 5]] })), |
| 140 | + ).toEqual(['5', '14']); |
| 141 | + expect( |
| 142 | + getMissing(asArg({ missing_lines: [], missing_branches: [[2, -1]] })), |
| 143 | + ).toEqual(['2->exit']); |
| 144 | + expect( |
| 145 | + getMissing(asArg({ missing_lines: [], missing_branches: [[2, 4]] })), |
| 146 | + ).toEqual(['2->4']); |
| 147 | + expect( |
| 148 | + getMissing(asArg({ missing_lines: [6], missing_branches: [[2, 4]] })), |
| 149 | + ).toEqual(['2->4', '6']); |
| 150 | + }); |
| 151 | + |
| 152 | + test('getTotalCoverage maps totals to a TotalLine', () => { |
| 153 | + expect( |
| 154 | + getTotalCoverage({ |
| 155 | + num_statements: 16, |
| 156 | + missing_lines: 2, |
| 157 | + percent_covered: 87.5, |
| 158 | + } as TotalsArg), |
| 159 | + ).toEqual({ name: 'TOTAL', stmts: 16, miss: 2, cover: '88%' }); |
| 160 | + |
| 161 | + expect( |
| 162 | + getTotalCoverage({ |
| 163 | + num_statements: 18, |
| 164 | + missing_lines: 2, |
| 165 | + percent_covered: 82.14, |
| 166 | + num_branches: 10, |
| 167 | + missing_branches: 3, |
| 168 | + } as TotalsArg), |
| 169 | + ).toEqual({ |
| 170 | + name: 'TOTAL', |
| 171 | + stmts: 18, |
| 172 | + miss: 2, |
| 173 | + cover: '82%', |
| 174 | + branch: '10', |
| 175 | + brpart: '3', |
| 176 | + }); |
| 177 | + }); |
| 178 | + |
| 179 | + test('getTotalCoverage returns null for a non-finite percentage', () => { |
| 180 | + spyCore.warning.mockClear(); |
| 181 | + expect( |
| 182 | + getTotalCoverage({ |
| 183 | + num_statements: 0, |
| 184 | + missing_lines: 0, |
| 185 | + percent_covered: NaN, |
| 186 | + } as TotalsArg), |
| 187 | + ).toBeNull(); |
| 188 | + expect(spyCore.warning).toHaveBeenCalled(); |
| 189 | + }); |
| 190 | +}); |
0 commit comments