-
Notifications
You must be signed in to change notification settings - Fork 18
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
ci: add code coverage checking script #58
Merged
huaweigu
merged 2 commits into
circlefin:master
from
ashutosh-ukey:add-code-coverage-script
Feb 3, 2025
+106
−3
Merged
Changes from 1 commit
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 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 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 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 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 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,103 @@ | ||
const fs = require('fs'); | ||
|
||
const COVERAGE_TABLE_HEADER = "| File | % Lines | % Statements | % Branches | % Funcs |"; | ||
const COVERAGE_TABLE_HEADER_SEPARATOR = "|--------------------------------------------------------------------------------|--------------------|--------------------|------------------|------------------|"; | ||
const COVERAGE_TABLE_TOTAL_ROW_NAME = 'Total'; | ||
const COVERAGE_TABLE_COLUMN_DELIM = '|'; | ||
|
||
// Matches expressions like "12.25%" | ||
const COVERAGE_TABLE_COVERAGE_PECENTAGE_REGEXP = /[\d\.]+%/; | ||
|
||
const MIN_REQUIRED_LINE_COVERAGE_PERCENTAGE = 90; | ||
const MIN_REQUIRED_STATEMENT_COVERAGE_PERCENTAGE = 90; | ||
const MIN_REQUIRED_BRANCH_COVERAGE_PERCENTAGE = 90; | ||
const MIN_REQUIRED_FUNCTION_COVERAGE_PERCENTAGE = 90; | ||
|
||
const NUM_COLUMNS = COVERAGE_TABLE_HEADER.split(COVERAGE_TABLE_COLUMN_DELIM).length - 2; | ||
|
||
function parsePercentage(rawCoveragePercentText) { | ||
const numericDecimalText = COVERAGE_TABLE_COVERAGE_PECENTAGE_REGEXP.exec(rawCoveragePercentText)[0].slice(0, -1); | ||
return parseFloat(numericDecimalText); | ||
} | ||
|
||
function parseCoverageTableRow(rawRowText) { | ||
let rowParts = rawRowText.split(COVERAGE_TABLE_COLUMN_DELIM); | ||
if (rowParts.length - 2 != NUM_COLUMNS) { | ||
return null | ||
} | ||
|
||
rowParts = rowParts.slice(1, -1); | ||
return { | ||
fileName: rowParts[0].trim(), | ||
lineCoveragePercent: parsePercentage(rowParts[1]), | ||
statementCoveragePercent: parsePercentage(rowParts[2]), | ||
branchCoveragePercent: parsePercentage(rowParts[3]), | ||
functionCoveragePercent: parsePercentage(rowParts[4]), | ||
} | ||
} | ||
|
||
function getFormattedCoverageTableRowsTest(coverageTableRows) { | ||
return COVERAGE_TABLE_HEADER + '\n' | ||
+ COVERAGE_TABLE_HEADER_SEPARATOR + '\n' | ||
+ coverageTableRows.join('\n') + '\n'; | ||
} | ||
|
||
(async function main() { | ||
const coverateReportFileName = process.argv[2]; | ||
const coverageReportRawText = fs.readFileSync(coverateReportFileName, "utf8"); | ||
|
||
let coverageTableBodyRaw = ""; | ||
try { | ||
coverageTableBodyRaw = coverageReportRawText.split(COVERAGE_TABLE_HEADER)[1]; | ||
} catch (error) { | ||
console.error("Unexpected coverage report format"); | ||
console.error(error); | ||
process.exit(1); | ||
} | ||
|
||
const belowThresholdFiles = []; | ||
const aboveThresholdFiles = []; | ||
let totalCoverageRow = ""; | ||
const coverageTableRows = coverageTableBodyRaw.split("\n").slice(3); | ||
|
||
for (const coverageTableRowRaw of coverageTableRows) { | ||
const coverageRow = parseCoverageTableRow(coverageTableRowRaw); | ||
if (!coverageRow) { | ||
continue; | ||
} | ||
|
||
// Check minimum required coverage percentages | ||
if (coverageRow.fileName == COVERAGE_TABLE_TOTAL_ROW_NAME) { | ||
totalCoverageRow = coverageTableRowRaw; | ||
} else if (coverageRow.lineCoveragePercent < MIN_REQUIRED_LINE_COVERAGE_PERCENTAGE || | ||
coverageRow.statementCoveragePercent < MIN_REQUIRED_STATEMENT_COVERAGE_PERCENTAGE || | ||
coverageRow.branchCoveragePercent < MIN_REQUIRED_BRANCH_COVERAGE_PERCENTAGE || | ||
coverageRow.functionCoveragePercent < MIN_REQUIRED_FUNCTION_COVERAGE_PERCENTAGE) { | ||
|
||
belowThresholdFiles.push(coverageTableRowRaw); | ||
} else { | ||
aboveThresholdFiles.push(coverageTableRowRaw); | ||
} | ||
} | ||
|
||
// Print coverage breakdown details | ||
console.log("Total coverage: "); | ||
console.log(getFormattedCoverageTableRowsTest([totalCoverageRow])); | ||
|
||
if (belowThresholdFiles.length > 0) { | ||
console.log("Found files below coverage threshold: "); | ||
console.log(getFormattedCoverageTableRowsTest(belowThresholdFiles)); | ||
} else { | ||
console.log("All source code files meet minimum coverage requirements."); | ||
} | ||
if (aboveThresholdFiles.length > 0) { | ||
console.log("Files above coverage threshold: "); | ||
console.log(getFormattedCoverageTableRowsTest(aboveThresholdFiles)); | ||
} | ||
|
||
// Fail if any files found below the minimum coverage threshold | ||
if (belowThresholdFiles.length > 0) { | ||
// TODO: uncomment line once source code coverages have been bumped up | ||
// process.exit(2); | ||
} | ||
})(); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing new line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we please fix this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually you can fix this when the threshold is enforced