Skip to content

Commit c7c7d94

Browse files
authored
Merge pull request anuraag016#6 from anuraag016/users/sanjsin/improve-delta-experience
Improving the test coverage reduction experience by a tolerance level.
2 parents dcf7dba + 1561ece commit c7c7d94

File tree

3 files changed

+69
-20
lines changed

3 files changed

+69
-20
lines changed

dist/index.js

+32-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,17 @@ 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+
messageToPost = `Current PR reduces the test coverage percentage by ${delta} for some tests`;
2069+
yield githubClient.issues.createComment({
2070+
repo: repoName,
2071+
owner: repoOwner,
2072+
body: messageToPost,
2073+
issue_number: prNumber
2074+
});
2075+
throw Error(messageToPost);
2076+
}
20662077
}
20672078
catch (error) {
20682079
core.setFailed(error);
@@ -6711,11 +6722,11 @@ class DiffChecker {
67116722
}
67126723
}
67136724
}
6714-
getCoverageDetails(diffOnly, currentDirectory, delta) {
6725+
getCoverageDetails(diffOnly, currentDirectory) {
67156726
const keys = Object.keys(this.diffCoverageReport);
67166727
const returnStrings = [];
67176728
for (const key of keys) {
6718-
if (this.compareCoverageValues(this.diffCoverageReport[key], delta) !== 0) {
6729+
if (this.compareCoverageValues(this.diffCoverageReport[key]) !== 0) {
67196730
returnStrings.push(this.createDiffLine(key.replace(currentDirectory, ''), this.diffCoverageReport[key]));
67206731
}
67216732
else {
@@ -6726,6 +6737,23 @@ class DiffChecker {
67266737
}
67276738
return returnStrings;
67286739
}
6740+
checkIfTestCoverageFallsBelowDelta(delta) {
6741+
const keys = Object.keys(this.diffCoverageReport);
6742+
for (const key of keys) {
6743+
const diffCoverageData = this.diffCoverageReport[key];
6744+
const keys = Object.keys(diffCoverageData);
6745+
for (const key of keys) {
6746+
if (diffCoverageData[key].oldPct !== diffCoverageData[key].newPct) {
6747+
const oldValue = Number(diffCoverageData[key].oldPct);
6748+
const newValue = Number(diffCoverageData[key].newPct);
6749+
if (oldValue - newValue > delta) {
6750+
return true;
6751+
}
6752+
}
6753+
}
6754+
}
6755+
return false;
6756+
}
67296757
createDiffLine(name, diffFileCoverageData) {
67306758
if (!diffFileCoverageData.branches.oldPct) {
67316759
return `**${name}** | **${diffFileCoverageData.statements.newPct}** | **${diffFileCoverageData.branches.newPct}** | **${diffFileCoverageData.functions.newPct}** | **${diffFileCoverageData.lines.newPct}**`;
@@ -6735,15 +6763,10 @@ class DiffChecker {
67356763
}
67366764
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}**`;
67376765
}
6738-
compareCoverageValues(diffCoverageData, delta) {
6766+
compareCoverageValues(diffCoverageData) {
67396767
const keys = Object.keys(diffCoverageData);
67406768
for (const key of keys) {
67416769
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-
}
67476770
return 1;
67486771
}
67496772
}

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

+13-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,18 @@ 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+
messageToPost = `Current PR reduces the test coverage percentage by ${delta} for some tests`
61+
await githubClient.issues.createComment({
62+
repo: repoName,
63+
owner: repoOwner,
64+
body: messageToPost,
65+
issue_number: prNumber
66+
})
67+
throw Error(messageToPost)
68+
}
5869
} catch (error) {
5970
core.setFailed(error)
6071
}

0 commit comments

Comments
 (0)