-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: code coverage calculation improvements (#164)
- Loading branch information
1 parent
31be36d
commit a1dde4d
Showing
5 changed files
with
198 additions
and
145 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// ignore_for_file: avoid_print | ||
import 'dart:io'; | ||
|
||
const mainLcov = 'coverage/main.lcov.info'; | ||
const prLcov = 'coverage/lcov.info'; | ||
|
||
Future<void> main(List<String> args) async { | ||
switch (args[0]) { | ||
case 'main_branch_percentage': | ||
print((await _calculateCoveragePercentage(mainLcov)).toStringAsFixed(2)); | ||
return; | ||
case 'pr_branch_percentage': | ||
print((await _calculateCoveragePercentage(prLcov)).toStringAsFixed(2)); | ||
return; | ||
case 'check_percentage': | ||
default: | ||
await _checkCoveragePercentage(); | ||
return; | ||
} | ||
} | ||
|
||
Future<void> _checkCoveragePercentage() async { | ||
final mainPercentage = await _calculateCoveragePercentage(mainLcov); | ||
|
||
final prPercentage = await _calculateCoveragePercentage(prLcov); | ||
print( | ||
"\n\nmain branch' test coverage percentage: ${mainPercentage.toStringAsFixed(2)}\n" | ||
"pr branch' test coverage percentage: ${prPercentage.toStringAsFixed(2)}\n\n", | ||
); | ||
if (prPercentage < mainPercentage) { | ||
throw '\n\n!!ERROR!!\n\nThe test coverage percentage in this pull request is lower than the one on the target branch.\n\n'; | ||
} | ||
} | ||
|
||
Future<double> _calculateCoveragePercentage(String lcovFile) async { | ||
final lines = await File(lcovFile).readAsLines(); | ||
final coverage = lines.fold<List<int>>([0, 0], (data, line) { | ||
var testedLines = data[0]; | ||
var totalLines = data[1]; | ||
if (line.startsWith('DA')) { | ||
totalLines++; | ||
if (!line.endsWith('0')) { | ||
testedLines++; | ||
} | ||
} | ||
return [testedLines, totalLines]; | ||
}); | ||
final testedLines = coverage[0]; | ||
final totalLines = coverage[1]; | ||
return testedLines / totalLines * 100; | ||
} |
Oops, something went wrong.