-
-
Notifications
You must be signed in to change notification settings - Fork 74
feat: add coverage.py JSON report support (pytest-json-coverage-path) #286
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| import { expect, test, describe, beforeEach } from 'vitest'; | ||
| import * as path from 'path'; | ||
| import { getCoverageJsonReport, exportedForTesting } from '../src/parseJson'; | ||
| import { spyCore } from './setup'; | ||
| import type { Options } from '../src/types'; | ||
|
|
||
| const { isValidCoverageContent, collapseRanges, getMissing, getTotalCoverage } = | ||
| exportedForTesting; | ||
|
|
||
| type MissingArg = Parameters<typeof getMissing>[0]; | ||
| type TotalsArg = Parameters<typeof getTotalCoverage>[0]; | ||
|
|
||
| const dataPath = path.resolve(__dirname, '..', 'data'); | ||
|
|
||
| const baseOptions: Options = { | ||
| token: 'token_123', | ||
| repository: 'MishaKav/pytest-coverage-comment', | ||
| commit: 'abc123', | ||
| prefix: '', | ||
| pathPrefix: '', | ||
| covFile: '', | ||
| covXmlFile: '', | ||
| covJsonFile: '', | ||
| xmlFile: '', | ||
| title: 'Coverage Report', | ||
| badgeTitle: 'Coverage', | ||
| hideBadge: false, | ||
| hideReport: false, | ||
| createNewComment: false, | ||
| hideComment: false, | ||
| hideEmoji: false, | ||
| xmlSkipCovered: false, | ||
| reportOnlyChangedFiles: false, | ||
| removeLinkFromBadge: false, | ||
| removeLinksToFiles: false, | ||
| removeLinksToLines: false, | ||
| textInsteadBadge: false, | ||
| defaultBranch: 'main', | ||
| xmlTitle: '', | ||
| multipleFiles: [], | ||
| repoUrl: 'https://github.com/MishaKav/pytest-coverage-comment', | ||
| }; | ||
|
|
||
| describe('getCoverageJsonReport', () => { | ||
| beforeEach(() => { | ||
| spyCore.error.mockClear(); | ||
| spyCore.warning.mockClear(); | ||
| spyCore.info.mockClear(); | ||
| }); | ||
|
|
||
| test('should return null for empty covJsonFile', () => { | ||
| const result = getCoverageJsonReport(baseOptions); | ||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| test('should return null for non-existent file', () => { | ||
| const options = { ...baseOptions, covJsonFile: '/nonexistent/coverage.json' }; | ||
| const result = getCoverageJsonReport(options); | ||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| test('should warn and return null for malformed json', () => { | ||
| const covJsonFile = path.join(dataPath, 'coverage_1.xml'); // not json | ||
| const options = { ...baseOptions, covJsonFile }; | ||
| const result = getCoverageJsonReport(options); | ||
|
|
||
| expect(result).toBeNull(); | ||
| expect(spyCore.warning).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('should parse coverage_1.json (no branches) successfully', () => { | ||
| const covJsonFile = path.join(dataPath, 'coverage_1.json'); | ||
| const options = { ...baseOptions, covJsonFile }; | ||
| const result = getCoverageJsonReport(options); | ||
|
|
||
| expect(result).not.toBeNull(); | ||
| expect(result!.html).toContain('img.shields.io/badge'); | ||
| expect(result!.coverage).not.toBeNull(); | ||
| expect(result!.coverage!.name).toBe('TOTAL'); | ||
| expect(result!.coverage!.cover).toBe('88%'); | ||
| // foo.py is 80% with missing lines 5 and 12 | ||
| expect(result!.html).toContain('<td>80%</td>'); | ||
| expect(result!.html).toContain('>5</a>'); | ||
| expect(result!.html).toContain('>12</a>'); | ||
| // no branch columns for a statement-only report | ||
| expect(result!.html).not.toContain('<th>Branch</th>'); | ||
| }); | ||
|
|
||
| test('should parse coverage_2.json with branch coverage', () => { | ||
| const covJsonFile = path.join(dataPath, 'coverage_2.json'); | ||
| const options = { ...baseOptions, covJsonFile }; | ||
| const result = getCoverageJsonReport(options); | ||
|
|
||
| expect(result).not.toBeNull(); | ||
| expect(result!.coverage!.cover).toBe('82%'); | ||
| expect(result!.coverage!.branch).toBe('10'); | ||
| expect(result!.coverage!.brpart).toBe('3'); | ||
| expect(result!.html).toContain('<th>Branch</th><th>BrPart</th>'); | ||
| // branch arcs from getMissing are wired into the missing column | ||
| expect(result!.html).toContain('>2->exit</a>'); | ||
| }); | ||
|
|
||
| test('should skip covered files when xmlSkipCovered is true', () => { | ||
| const covJsonFile = path.join(dataPath, 'coverage_1.json'); | ||
| const options = { ...baseOptions, covJsonFile, xmlSkipCovered: true }; | ||
| const result = getCoverageJsonReport(options); | ||
|
|
||
| expect(result).not.toBeNull(); | ||
| // bar.py is 100% covered and should be excluded from the table | ||
| expect(result!.html).not.toContain('bar.py'); | ||
| expect(result!.html).toContain('foo.py'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('parseJson internals', () => { | ||
| test('isValidCoverageContent', () => { | ||
| expect(isValidCoverageContent(null)).toBe(false); | ||
| expect(isValidCoverageContent({ files: {} } as never)).toBe(false); | ||
| expect(isValidCoverageContent({ files: {}, totals: {} } as never)).toBe( | ||
| true, | ||
| ); | ||
| }); | ||
|
|
||
| test('collapseRanges groups consecutive lines', () => { | ||
| expect(collapseRanges([4, 10, 11, 12])).toEqual([ | ||
| { sort: 4, text: '4' }, | ||
| { sort: 10, text: '10-12' }, | ||
| ]); | ||
| expect(collapseRanges([])).toEqual([]); | ||
| }); | ||
|
|
||
| test('getMissing renders lines, exit arcs, and normal arcs sorted by line', () => { | ||
| const asArg = (file: { | ||
| missing_lines: number[]; | ||
| missing_branches: number[][]; | ||
| }): MissingArg => file as unknown as MissingArg; | ||
|
|
||
| expect( | ||
| getMissing(asArg({ missing_lines: [5, 14], missing_branches: [[2, 5]] })), | ||
| ).toEqual(['5', '14']); | ||
| expect( | ||
| getMissing(asArg({ missing_lines: [], missing_branches: [[2, -1]] })), | ||
| ).toEqual(['2->exit']); | ||
| expect( | ||
| getMissing(asArg({ missing_lines: [], missing_branches: [[2, 4]] })), | ||
| ).toEqual(['2->4']); | ||
| expect( | ||
| getMissing(asArg({ missing_lines: [6], missing_branches: [[2, 4]] })), | ||
| ).toEqual(['2->4', '6']); | ||
| }); | ||
|
|
||
| test('getTotalCoverage maps totals to a TotalLine', () => { | ||
| expect( | ||
| getTotalCoverage({ | ||
| num_statements: 16, | ||
| missing_lines: 2, | ||
| percent_covered: 87.5, | ||
| } as TotalsArg), | ||
| ).toEqual({ name: 'TOTAL', stmts: 16, miss: 2, cover: '88%' }); | ||
|
|
||
| expect( | ||
| getTotalCoverage({ | ||
| num_statements: 18, | ||
| missing_lines: 2, | ||
| percent_covered: 82.14, | ||
| num_branches: 10, | ||
| missing_branches: 3, | ||
| } as TotalsArg), | ||
| ).toEqual({ | ||
| name: 'TOTAL', | ||
| stmts: 18, | ||
| miss: 2, | ||
| cover: '82%', | ||
| branch: '10', | ||
| brpart: '3', | ||
| }); | ||
| }); | ||
|
|
||
| test('getTotalCoverage returns null for a non-finite percentage', () => { | ||
| spyCore.warning.mockClear(); | ||
| expect( | ||
| getTotalCoverage({ | ||
| num_statements: 0, | ||
| missing_lines: 0, | ||
| percent_covered: NaN, | ||
| } as TotalsArg), | ||
| ).toBeNull(); | ||
| expect(spyCore.warning).toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| { | ||
| "meta": { | ||
| "format": 3, | ||
| "version": "7.15.0", | ||
| "branch_coverage": false, | ||
| "show_contexts": false | ||
| }, | ||
| "files": { | ||
| "src/foo.py": { | ||
| "executed_lines": [1, 2, 3, 4, 6, 7, 8, 9], | ||
| "summary": { | ||
| "covered_lines": 8, | ||
| "num_statements": 10, | ||
| "percent_covered": 80.0, | ||
| "percent_covered_display": "80", | ||
| "missing_lines": 2, | ||
| "excluded_lines": 0 | ||
| }, | ||
| "missing_lines": [5, 12], | ||
| "excluded_lines": [] | ||
| }, | ||
| "src/bar.py": { | ||
| "executed_lines": [1, 2, 3, 4, 5, 6], | ||
| "summary": { | ||
| "covered_lines": 6, | ||
| "num_statements": 6, | ||
| "percent_covered": 100.0, | ||
| "percent_covered_display": "100", | ||
| "missing_lines": 0, | ||
| "excluded_lines": 0 | ||
| }, | ||
| "missing_lines": [], | ||
| "excluded_lines": [] | ||
| } | ||
| }, | ||
| "totals": { | ||
| "covered_lines": 14, | ||
| "num_statements": 16, | ||
| "percent_covered": 87.5, | ||
| "percent_covered_display": "88", | ||
| "missing_lines": 2, | ||
| "excluded_lines": 0 | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.