Skip to content

Commit c7eaf05

Browse files
author
Sanjeev Singh
committed
Improving the experience of failure.
1 parent dcf7dba commit c7eaf05

File tree

3 files changed

+55
-20
lines changed

3 files changed

+55
-20
lines changed

dist/index.js

+25-9
Original file line numberDiff line numberDiff line change
@@ -2047,7 +2047,7 @@ function run() {
20472047
.trim();
20482048
const diffChecker = new DiffChecker_1.DiffChecker(codeCoverageNew, codeCoverageOld);
20492049
let messageToPost = `Code coverage diff between base branch:${branchNameBase} and head branch: ${branchNameHead} \n`;
2050-
const coverageDetails = diffChecker.getCoverageDetails(!fullCoverage, `${currentDirectory}/`, delta);
2050+
const coverageDetails = diffChecker.getCoverageDetails(!fullCoverage, `${currentDirectory}/`);
20512051
if (coverageDetails.length === 0) {
20522052
messageToPost =
20532053
'No changes to code coverage between the base branch and the head branch';
@@ -2063,6 +2063,10 @@ function run() {
20632063
body: messageToPost,
20642064
issue_number: prNumber
20652065
});
2066+
// check if the test coverage is falling below delta/tolerance.
2067+
if (diffChecker.checkIfTestCoverageFallsBelowDelta(delta)) {
2068+
throw Error(`Current PR reduces the test percentage by ${delta}`);
2069+
}
20662070
}
20672071
catch (error) {
20682072
core.setFailed(error);
@@ -6711,11 +6715,11 @@ class DiffChecker {
67116715
}
67126716
}
67136717
}
6714-
getCoverageDetails(diffOnly, currentDirectory, delta) {
6718+
getCoverageDetails(diffOnly, currentDirectory) {
67156719
const keys = Object.keys(this.diffCoverageReport);
67166720
const returnStrings = [];
67176721
for (const key of keys) {
6718-
if (this.compareCoverageValues(this.diffCoverageReport[key], delta) !== 0) {
6722+
if (this.compareCoverageValues(this.diffCoverageReport[key]) !== 0) {
67196723
returnStrings.push(this.createDiffLine(key.replace(currentDirectory, ''), this.diffCoverageReport[key]));
67206724
}
67216725
else {
@@ -6726,6 +6730,23 @@ class DiffChecker {
67266730
}
67276731
return returnStrings;
67286732
}
6733+
checkIfTestCoverageFallsBelowDelta(delta) {
6734+
const keys = Object.keys(this.diffCoverageReport);
6735+
for (const key of keys) {
6736+
const diffCoverageData = this.diffCoverageReport[key];
6737+
const keys = Object.keys(diffCoverageData);
6738+
for (const key of keys) {
6739+
if (diffCoverageData[key].oldPct !== diffCoverageData[key].newPct) {
6740+
const oldValue = Number(diffCoverageData[key].oldPct);
6741+
const newValue = Number(diffCoverageData[key].newPct);
6742+
if (oldValue - newValue > delta) {
6743+
return true;
6744+
}
6745+
}
6746+
}
6747+
}
6748+
return false;
6749+
}
67296750
createDiffLine(name, diffFileCoverageData) {
67306751
if (!diffFileCoverageData.branches.oldPct) {
67316752
return `**${name}** | **${diffFileCoverageData.statements.newPct}** | **${diffFileCoverageData.branches.newPct}** | **${diffFileCoverageData.functions.newPct}** | **${diffFileCoverageData.lines.newPct}**`;
@@ -6735,15 +6756,10 @@ class DiffChecker {
67356756
}
67366757
return `${name} | ~~${diffFileCoverageData.statements.oldPct}~~ **${diffFileCoverageData.statements.newPct}** | ~~${diffFileCoverageData.branches.oldPct}~~ **${diffFileCoverageData.branches.newPct}** | ~~${diffFileCoverageData.functions.oldPct}~~ **${diffFileCoverageData.functions.newPct}** | ~~${diffFileCoverageData.lines.oldPct}~~ **${diffFileCoverageData.lines.newPct}**`;
67376758
}
6738-
compareCoverageValues(diffCoverageData, delta) {
6759+
compareCoverageValues(diffCoverageData) {
67396760
const keys = Object.keys(diffCoverageData);
67406761
for (const key of keys) {
67416762
if (diffCoverageData[key].oldPct !== diffCoverageData[key].newPct) {
6742-
const oldValue = Number(diffCoverageData[key].oldPct);
6743-
const newValue = Number(diffCoverageData[key].newPct);
6744-
if (oldValue - newValue > delta) {
6745-
throw Error(`Current PR reduces the test percentage by ${delta}`);
6746-
}
67476763
return 1;
67486764
}
67496765
}

src/DiffChecker.ts

+24-9
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ export class DiffChecker {
6060
}
6161
}
6262

63-
getCoverageDetails(diffOnly: boolean, currentDirectory: string, delta: number): string[] {
63+
getCoverageDetails(diffOnly: boolean, currentDirectory: string): string[] {
6464
const keys = Object.keys(this.diffCoverageReport)
6565
const returnStrings: string[] = []
6666
for (const key of keys) {
67-
if (this.compareCoverageValues(this.diffCoverageReport[key], delta) !== 0) {
67+
if (this.compareCoverageValues(this.diffCoverageReport[key]) !== 0) {
6868
returnStrings.push(
6969
this.createDiffLine(
7070
key.replace(currentDirectory, ''),
@@ -86,6 +86,27 @@ export class DiffChecker {
8686
return returnStrings
8787
}
8888

89+
checkIfTestCoverageFallsBelowDelta(delta: number): boolean {
90+
const keys = Object.keys(this.diffCoverageReport)
91+
for (const key of keys) {
92+
const diffCoverageData = this.diffCoverageReport[key]
93+
const keys: ('lines' | 'statements' | 'branches' | 'functions')[] = <
94+
('lines' | 'statements' | 'branches' | 'functions')[]
95+
>Object.keys(diffCoverageData)
96+
for (const key of keys) {
97+
if (diffCoverageData[key].oldPct !== diffCoverageData[key].newPct) {
98+
const oldValue: number = Number(diffCoverageData[key].oldPct)
99+
const newValue: number = Number(diffCoverageData[key].newPct)
100+
if (oldValue - newValue > delta) {
101+
return true
102+
}
103+
}
104+
}
105+
}
106+
107+
return false
108+
}
109+
89110
private createDiffLine(
90111
name: string,
91112
diffFileCoverageData: DiffFileCoverageData
@@ -99,19 +120,13 @@ export class DiffChecker {
99120
}
100121

101122
private compareCoverageValues(
102-
diffCoverageData: DiffFileCoverageData,
103-
delta: number
123+
diffCoverageData: DiffFileCoverageData
104124
): number {
105125
const keys: ('lines' | 'statements' | 'branches' | 'functions')[] = <
106126
('lines' | 'statements' | 'branches' | 'functions')[]
107127
>Object.keys(diffCoverageData)
108128
for (const key of keys) {
109129
if (diffCoverageData[key].oldPct !== diffCoverageData[key].newPct) {
110-
const oldValue: number = Number(diffCoverageData[key].oldPct)
111-
const newValue: number = Number(diffCoverageData[key].newPct)
112-
if (oldValue - newValue > delta) {
113-
throw Error(`Current PR reduces the test percentage by ${delta}`)
114-
}
115130
return 1
116131
}
117132
}

src/main.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ async function run(): Promise<void> {
3838
let messageToPost = `Code coverage diff between base branch:${branchNameBase} and head branch: ${branchNameHead} \n`
3939
const coverageDetails = diffChecker.getCoverageDetails(
4040
!fullCoverage,
41-
`${currentDirectory}/`,
42-
delta,
41+
`${currentDirectory}/`
4342
)
4443
if (coverageDetails.length === 0) {
4544
messageToPost =
@@ -55,6 +54,11 @@ async function run(): Promise<void> {
5554
body: messageToPost,
5655
issue_number: prNumber
5756
})
57+
58+
// check if the test coverage is falling below delta/tolerance.
59+
if (diffChecker.checkIfTestCoverageFallsBelowDelta(delta)) {
60+
throw Error(`Current PR reduces the test percentage by ${delta}`)
61+
}
5862
} catch (error) {
5963
core.setFailed(error)
6064
}

0 commit comments

Comments
 (0)