-
Notifications
You must be signed in to change notification settings - Fork 55
Add --fail-under flag for minimum coverage threshold #2075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
liamappelbe
merged 9 commits into
dart-lang:main
from
vvs-personalstash:feature/fail_under
Apr 29, 2025
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cb3e43a
Add --fail-under flag for minimum coverage threshold
vvs-personalstash a9ab888
Add CHANGELOG and update Pubspec
vvs-personalstash 6fbe100
Removed Precision and Add BranchHits in Coverage % + Cleanup
vvs-personalstash 2a0c480
Add Integration tests for --fail-under flag
vvs-personalstash 86a8eb9
Minor Fix
vvs-personalstash 3a1eba8
Modify test_with_coverage_test to also test for branch hits in covera…
vvs-personalstash 18bd752
Add Tests for Branch Coverage in percentage+ CleanUp
vvs-personalstash 8b547bf
cleanup+reformat
vvs-personalstash 81d32f4
cleanup
vvs-personalstash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,55 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'hitmap.dart'; | ||
|
||
/// Calculates the coverage percentage from a hitmap. | ||
/// | ||
/// [hitmap] is the map of file paths to hit maps. | ||
/// | ||
/// Returns a [CoverageResult] containing the coverage percentage and line | ||
/// counts. | ||
CoverageResult calculateCoveragePercentage(Map<String, HitMap> hitmap) { | ||
var totalLines = 0; | ||
var coveredLines = 0; | ||
for (final entry in hitmap.entries) { | ||
var coveredBranches = 0; | ||
vvs-personalstash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
final lineHits = entry.value.lineHits; | ||
liamappelbe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
final branchHits = entry.value.branchHits; | ||
totalLines += lineHits.length; | ||
if (branchHits != null) { | ||
coveredBranches += branchHits.values.where((v) => v > 0).length; | ||
totalLines += branchHits.length; | ||
} | ||
coveredLines += | ||
lineHits.values.where((v) => v > 0).length + coveredBranches; | ||
} | ||
final coveragePercentage = | ||
totalLines > 0 ? coveredLines * 100 / totalLines : 0.0; | ||
|
||
return CoverageResult( | ||
percentage: coveragePercentage, | ||
coveredLines: coveredLines, | ||
totalLines: totalLines, | ||
); | ||
} | ||
|
||
/// The result of a coverage calculation. | ||
class CoverageResult { | ||
/// Creates a new [CoverageResult]. | ||
const CoverageResult({ | ||
required this.percentage, | ||
required this.coveredLines, | ||
required this.totalLines, | ||
}); | ||
|
||
/// The coverage percentage. | ||
final double percentage; | ||
|
||
/// The number of covered lines. | ||
final int coveredLines; | ||
|
||
/// The total number of lines. | ||
final int totalLines; | ||
} |
This file contains hidden or 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 hidden or 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,75 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:coverage/src/coverage_percentage.dart'; | ||
import 'package:coverage/src/hitmap.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('calculateCoveragePercentage', () { | ||
test('calculates correct percentage', () { | ||
final hitmap = { | ||
'file1.dart': HitMap({ | ||
1: 1, // covered | ||
2: 1, // covered | ||
3: 0, // not covered | ||
4: 1, // covered | ||
}), | ||
'file2.dart': HitMap({ | ||
1: 1, // covered | ||
2: 0, // not covered | ||
3: 0, // not covered | ||
}), | ||
}; | ||
|
||
final result = calculateCoveragePercentage(hitmap); | ||
|
||
// 4 covered lines out of 7 total lines = 57.14% | ||
expect(result.percentage.toStringAsFixed(2), equals('57.14')); | ||
expect(result.coveredLines, equals(4)); | ||
expect(result.totalLines, equals(7)); | ||
}); | ||
test('handles empty hitmap', () { | ||
final hitmap = <String, HitMap>{}; | ||
|
||
final result = calculateCoveragePercentage(hitmap); | ||
|
||
expect(result.percentage, equals(0)); | ||
expect(result.coveredLines, equals(0)); | ||
expect(result.totalLines, equals(0)); | ||
}); | ||
|
||
test('handles hitmap with no covered lines', () { | ||
final hitmap = { | ||
'file1.dart': HitMap({ | ||
1: 0, // not covered | ||
2: 0, // not covered | ||
3: 0, // not covered | ||
}), | ||
}; | ||
|
||
final result = calculateCoveragePercentage(hitmap); | ||
|
||
expect(result.percentage, equals(0)); | ||
expect(result.coveredLines, equals(0)); | ||
expect(result.totalLines, equals(3)); | ||
}); | ||
|
||
test('handles hitmap with all lines covered', () { | ||
final hitmap = { | ||
'file1.dart': HitMap({ | ||
1: 1, // covered | ||
2: 1, // covered | ||
3: 1, // covered | ||
}), | ||
}; | ||
|
||
final result = calculateCoveragePercentage(hitmap); | ||
|
||
expect(result.percentage, equals(100)); | ||
expect(result.coveredLines, equals(3)); | ||
expect(result.totalLines, equals(3)); | ||
}); | ||
vvs-personalstash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
} |
This file contains hidden or 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
22 changes: 22 additions & 0 deletions
22
pkgs/coverage/test/test_with_coverage_package/test/evaluate_test.dart
This file contains hidden or 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,22 @@ | ||
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import '../lib/validate_lib.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('evaluateScore', () { | ||
test('returns Invalid for negative score', () { | ||
expect(evaluateScore(-10), equals('Invalid')); | ||
}); | ||
|
||
test('returns Fail for score < 50', () { | ||
expect(evaluateScore(30), equals('Fail')); | ||
}); | ||
|
||
test('returns Excellent for score 85', () { | ||
expect(evaluateScore(85), equals('Excellent')); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.