Skip to content

Commit

Permalink
fix: code coverage calculation improvements (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejchm authored May 12, 2022
1 parent 31be36d commit a1dde4d
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 145 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ jobs:

- name: generate code coverage
timeout-minutes: 4
run: flutter test --dart-define=IS_CI=true --coverage --coverage-path=coverage/main.lcov.info
run: |
flutter test --dart-define=IS_CI=true --coverage --coverage-path=coverage/main.lcov.info
lcov --remove coverage/main.lcov.info 'lib/*/*.g.dart' 'lib/generated_assets/*.dart' 'lib/*/*.gen.dart' 'lib/generated_plugin_registrant.dart'
- uses: stefanzweifel/git-auto-commit-action@v4
with:
Expand Down
17 changes: 8 additions & 9 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,19 @@ jobs:

- name: unit tests
timeout-minutes: 4
run: flutter test --dart-define=IS_CI=true --coverage --coverage-path=coverage/lcov.info
run: |
flutter test --dart-define=IS_CI=true --coverage --coverage-path=coverage/lcov.info
lcov --remove coverage/lcov.info 'lib/*/*.g.dart' 'lib/generated_assets/*.dart' 'lib/*/*.gen.dart' 'lib/generated_plugin_registrant.dart'
- name: read coverage percentage
run: |
echo "BASE_COVERAGE=$(lcov --summary coverage/main.lcov.info | sed -n '3p' | sed -r 's/(.+): (.+)(\%.+)/\2/' | cat)" >> $GITHUB_ENV
echo "THIS_COVERAGE=$(lcov --summary coverage/lcov.info | sed -n '3p' | sed -r 's/(.+): (.+)(\%.+)/\2/' | cat)" >> $GITHUB_ENV
echo $BASE_COVERAGE
echo "BASE_COVERAGE=$(dart coverage/get_coverage_threshold.dart main_branch_percentage)" >> $GITHUB_ENV
echo "THIS_COVERAGE=$(dart coverage/get_coverage_threshold.dart pr_branch_percentage)" >> $GITHUB_ENV
- name: Comment PR
uses: thollander/actions-comment-pull-request@v1
with:
comment_includes: '## Code Coverage'
message: |
## Code Coverage
<table><tbody>
Expand All @@ -67,11 +69,8 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Check coverage percentage
uses: VeryGoodOpenSource/very_good_coverage@v1
with:
path: './coverage/lcov.info'
min_coverage: ${{ env.BASE_COVERAGE }}
exclude: 'lib/*/*.g.dart lib/generated_assets/*.dart lib/*/*.gen.dart lib/generated_plugin_registrant.dart'
run: |
dart coverage/get_coverage_threshold.dart check_percentage
- uses: actions/upload-artifact@v2
name: upload screenshot test failures
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,6 @@ android/.idea

# TEST COVERAGE
coverage/**/*
!coverage/**/*.dart
test/coverage_helper_test.dart
!coverage/main.lcov.info
51 changes: 51 additions & 0 deletions coverage/get_coverage_threshold.dart
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;
}
Loading

0 comments on commit a1dde4d

Please sign in to comment.