Skip to content

Commit

Permalink
Updated total coverage calculation to include branch coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
bibipkins committed Mar 2, 2023
1 parent 5bae2c1 commit cb1fece
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 14 deletions.
20 changes: 13 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ const formatResultMarkdown = (result) => {
};
exports.formatResultMarkdown = formatResultMarkdown;
const formatCoverageMarkdown = (coverage, min) => {
const { linesCovered, linesTotal, lineCoverage, branchesTotal, branchesCovered, success } = coverage;
const { totalCoverage, linesCovered, linesTotal, branchesTotal, branchesCovered, success } = coverage;
const title = `${min ? (0, common_1.getStatusIcon)(success) : '📝'} Coverage`;
const info = `**${lineCoverage}%**`;
const info = `**${totalCoverage}%**`;
const status = min ? `- ${getStatusText(success)} with ${min}% threshold` : '';
const lines = `📏 ${linesCovered} / ${linesTotal} lines covered`;
const branches = `🌿 ${branchesCovered} / ${branchesTotal} branches covered`;
Expand Down Expand Up @@ -290,7 +290,9 @@ const common_1 = __nccwpck_require__(9023);
const parseCobertura = (filePath, threshold) => __awaiter(void 0, void 0, void 0, function* () { return (0, common_1.parseCoverage)(filePath, threshold, parseSummary, parseModules); });
const parseSummary = (file) => {
const summary = file.coverage['$'];
const totalCoverage = (0, common_1.calculateCoverage)(summary['lines-covered'] + summary['branches-covered'], summary['lines-valid'] + summary['branches-valid']);
return {
totalCoverage,
linesTotal: Number(summary['lines-valid']),
linesCovered: Number(summary['lines-covered']),
lineCoverage: (0, common_1.calculateCoverage)(summary['lines-covered'], summary['lines-valid']),
Expand Down Expand Up @@ -330,6 +332,7 @@ const parseFiles = (classes) => {
const fileNames = [...new Set(classes.map(c => String(c['$'].filename)))];
return fileNames.map(file => ({
name: file,
totalCoverage: 0,
linesTotal: 0,
linesCovered: 0,
lineCoverage: 0,
Expand Down Expand Up @@ -366,12 +369,12 @@ const calculateCoverage = (covered, total) => {
};
exports.calculateCoverage = calculateCoverage;
const createCoverageModule = (name, threshold, files) => {
const linesTotal = files.reduce((summ, file) => summ + file.linesTotal, 0);
const linesCovered = files.reduce((summ, file) => summ + file.linesCovered, 0);
const coverage = (0, exports.calculateCoverage)(linesCovered, linesTotal);
const total = files.reduce((summ, file) => summ + file.linesTotal + file.branchesTotal, 0);
const covered = files.reduce((summ, file) => summ + file.linesCovered + file.linesCovered, 0);
const coverage = (0, exports.calculateCoverage)(covered, total);
const success = !threshold || coverage >= threshold;
const updatedFiles = files
.map(file => (Object.assign(Object.assign({}, file), { lineCoverage: (0, exports.calculateCoverage)(file.linesCovered, file.linesTotal), branchCoverage: (0, exports.calculateCoverage)(file.branchesCovered, file.branchesTotal) })))
.map(file => (Object.assign(Object.assign({}, file), { totalCoverage: (0, exports.calculateCoverage)(file.linesCovered + file.branchesCovered, file.linesTotal + file.branchesTotal), lineCoverage: (0, exports.calculateCoverage)(file.linesCovered, file.linesTotal), branchCoverage: (0, exports.calculateCoverage)(file.branchesCovered, file.branchesTotal) })))
.sort((a, b) => a.name.localeCompare(b.name));
return { name, coverage, success, files: updatedFiles };
};
Expand All @@ -383,7 +386,7 @@ const parseCoverage = (filePath, threshold, parseSummary, parseModules) => __awa
}
const summary = parseSummary(file);
const modules = parseModules(file, threshold);
const success = !threshold || summary.lineCoverage >= threshold;
const success = !threshold || summary.totalCoverage >= threshold;
return Object.assign(Object.assign({ success }, summary), { modules });
});
exports.parseCoverage = parseCoverage;
Expand All @@ -410,7 +413,9 @@ const common_1 = __nccwpck_require__(9023);
const parseOpencover = (filePath, threshold) => __awaiter(void 0, void 0, void 0, function* () { return (0, common_1.parseCoverage)(filePath, threshold, parseSummary, parseModules); });
const parseSummary = (file) => {
const summary = file.CoverageSession.Summary[0]['$'];
const totalCoverage = (0, common_1.calculateCoverage)(summary.visitedSequencePoints + summary.visitedBranchPoints, summary.numSequencePoints + summary.numBranchPoints);
return {
totalCoverage,
linesTotal: Number(summary.numSequencePoints),
linesCovered: Number(summary.visitedSequencePoints),
lineCoverage: (0, common_1.calculateCoverage)(summary.visitedSequencePoints, summary.numSequencePoints),
Expand Down Expand Up @@ -455,6 +460,7 @@ const parseFiles = (moduleName, module) => {
return ({
id: String(file['$'].uid),
name: (_a = String(file['$'].fullPath).split(`${moduleName}\\`).slice(-1).pop()) !== null && _a !== void 0 ? _a : '',
totalCoverage: 0,
linesTotal: 0,
linesCovered: 0,
lineCoverage: 0,
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/data/ICoverageData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default interface ICoverageData {
totalCoverage: number;
linesTotal: number;
linesCovered: number;
lineCoverage: number;
Expand Down
4 changes: 2 additions & 2 deletions src/formatting/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ export const formatResultMarkdown = (result: IResult): string => {
};

export const formatCoverageMarkdown = (coverage: ICoverage, min: number): string => {
const { linesCovered, linesTotal, lineCoverage, branchesTotal, branchesCovered, success } = coverage;
const { totalCoverage, linesCovered, linesTotal, branchesTotal, branchesCovered, success } = coverage;

const title = `${min ? getStatusIcon(success) : '📝'} Coverage`;
const info = `**${lineCoverage}%**`;
const info = `**${totalCoverage}%**`;
const status = min ? `- ${getStatusText(success)} with ${min}% threshold` : '';

const lines = `📏 ${linesCovered} / ${linesTotal} lines covered`;
Expand Down
6 changes: 6 additions & 0 deletions src/parsers/cobertura.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ const parseCobertura: CoverageParser = async (filePath: string, threshold: numbe

const parseSummary = (file: any): ICoverageData => {
const summary = file.coverage['$'];
const totalCoverage = calculateCoverage(
summary['lines-covered'] + summary['branches-covered'],
summary['lines-valid'] + summary['branches-valid']
);

return {
totalCoverage,
linesTotal: Number(summary['lines-valid']),
linesCovered: Number(summary['lines-covered']),
lineCoverage: calculateCoverage(summary['lines-covered'], summary['lines-valid']),
Expand Down Expand Up @@ -53,6 +58,7 @@ const parseFiles = (classes: any[]) => {

return fileNames.map(file => ({
name: file,
totalCoverage: 0,
linesTotal: 0,
linesCovered: 0,
lineCoverage: 0,
Expand Down
9 changes: 5 additions & 4 deletions src/parsers/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ export const calculateCoverage = (covered: number, total: number): number => {
};

export const createCoverageModule = (name: string, threshold: number, files: ICoverageFile[]): ICoverageModule => {
const linesTotal = files.reduce((summ, file) => summ + file.linesTotal, 0);
const linesCovered = files.reduce((summ, file) => summ + file.linesCovered, 0);
const coverage = calculateCoverage(linesCovered, linesTotal);
const total = files.reduce((summ, file) => summ + file.linesTotal + file.branchesTotal, 0);
const covered = files.reduce((summ, file) => summ + file.linesCovered + file.branchesCovered, 0);
const coverage = calculateCoverage(covered, total);
const success = !threshold || coverage >= threshold;

const updatedFiles = files
.map(file => ({
...file,
totalCoverage: calculateCoverage(file.linesCovered + file.branchesCovered, file.linesTotal + file.branchesTotal),
lineCoverage: calculateCoverage(file.linesCovered, file.linesTotal),
branchCoverage: calculateCoverage(file.branchesCovered, file.branchesTotal)
}))
Expand All @@ -36,7 +37,7 @@ export const parseCoverage = async (

const summary = parseSummary(file);
const modules = parseModules(file, threshold);
const success = !threshold || summary.lineCoverage >= threshold;
const success = !threshold || summary.totalCoverage >= threshold;

return { success, ...summary, modules };
};
6 changes: 6 additions & 0 deletions src/parsers/opencover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ const parseOpencover: CoverageParser = async (filePath: string, threshold: numbe

const parseSummary = (file: any): ICoverageData => {
const summary = file.CoverageSession.Summary[0]['$'];
const totalCoverage = calculateCoverage(
summary.visitedSequencePoints + summary.visitedBranchPoints,
summary.numSequencePoints + summary.numBranchPoints
);

return {
totalCoverage,
linesTotal: Number(summary.numSequencePoints),
linesCovered: Number(summary.visitedSequencePoints),
lineCoverage: calculateCoverage(summary.visitedSequencePoints, summary.numSequencePoints),
Expand Down Expand Up @@ -55,6 +60,7 @@ const parseFiles = (moduleName: string, module: any) => {
return fileData.map(file => ({
id: String(file['$'].uid),
name: String(file['$'].fullPath).split(`${moduleName}\\`).slice(-1).pop() ?? '',
totalCoverage: 0,
linesTotal: 0,
linesCovered: 0,
lineCoverage: 0,
Expand Down

0 comments on commit cb1fece

Please sign in to comment.