-
Notifications
You must be signed in to change notification settings - Fork 50
Added a tool to find uncovered files (#529) #2088
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
Open
Victowolf
wants to merge
1
commit into
dart-lang:main
Choose a base branch
from
Victowolf:fix-issue-529
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,10 @@ | |
// 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 'dart:io'; | ||
import 'package:glob/glob.dart'; | ||
import 'package:path/path.dart' as p; | ||
import 'package:yaml/yaml.dart'; | ||
|
||
import 'hitmap.dart'; | ||
import 'resolver.dart'; | ||
|
@@ -78,11 +80,13 @@ extension FileHitMapsFormatter on Map<String, HitMap> { | |
String? basePath, | ||
List<String>? reportOn, | ||
Set<Glob>? ignoreGlobs, | ||
bool Function(String path)? includeUncovered, | ||
}) { | ||
final pathFilter = _getPathFilter( | ||
reportOn: reportOn, | ||
ignoreGlobs: ignoreGlobs, | ||
); | ||
|
||
final buf = StringBuffer(); | ||
for (final entry in entries) { | ||
final v = entry.value; | ||
|
@@ -129,6 +133,65 @@ extension FileHitMapsFormatter on Map<String, HitMap> { | |
buf.write('end_of_record\n'); | ||
} | ||
|
||
if (includeUncovered != null) { | ||
// Step 1: Identify all Dart files | ||
final allFiles = _findAllDartFiles(reportOn: reportOn); | ||
print('detected files: $allFiles'); | ||
|
||
// Step 2: Identify covered files | ||
final coveredFiles = Map.fromEntries(entries | ||
.where((entry) => entry.value.lineHits.values.any((hit) => hit > 0))); | ||
|
||
// check if the file is covered or no. | ||
final packageName = getPackageName(); | ||
|
||
final uncoveredFiles = <String>[]; | ||
for (final file in allFiles) { | ||
final pkgUri = toPackageUri(file, packageName); | ||
if (!coveredFiles.containsKey(pkgUri)) { | ||
uncoveredFiles.add(file); | ||
} | ||
} | ||
|
||
print('Uncovered Dart files:'); | ||
for (final file in uncoveredFiles) { | ||
print(file); | ||
} | ||
|
||
//formatlcov for including uncovered. | ||
final uncoveredBuf = StringBuffer(); | ||
|
||
for (final file in uncoveredFiles) { | ||
if (!pathFilter(file)) continue; | ||
|
||
final lines = File(file).readAsLinesSync(); | ||
var displayPath = file; | ||
if (basePath != null) { | ||
displayPath = p.relative(file, from: basePath); | ||
} | ||
displayPath = | ||
displayPath.replaceAll('\\', '/'); // For Windows compatibility | ||
|
||
uncoveredBuf.writeln('SF:$displayPath'); | ||
|
||
var lineNumber = 1; | ||
var realLines = 0; | ||
for (final line in lines) { | ||
final trimmed = line.trim(); | ||
if (trimmed.isNotEmpty && !trimmed.startsWith('//')) { | ||
uncoveredBuf.writeln('DA:$lineNumber,0'); | ||
realLines++; | ||
} | ||
lineNumber++; | ||
} | ||
|
||
uncoveredBuf.writeln('LF:$realLines'); | ||
uncoveredBuf.writeln('LH:0'); | ||
uncoveredBuf.writeln('end_of_record'); | ||
} | ||
|
||
buf.write(uncoveredBuf.toString()); | ||
} | ||
return buf.toString(); | ||
} | ||
|
||
|
@@ -141,9 +204,11 @@ extension FileHitMapsFormatter on Map<String, HitMap> { | |
Resolver resolver, | ||
Loader loader, { | ||
List<String>? reportOn, | ||
String? basePath, | ||
Set<Glob>? ignoreGlobs, | ||
bool reportFuncs = false, | ||
bool reportBranches = false, | ||
bool Function(String path)? includeUncovered, | ||
}) async { | ||
final pathFilter = _getPathFilter( | ||
reportOn: reportOn, | ||
|
@@ -193,8 +258,98 @@ extension FileHitMapsFormatter on Map<String, HitMap> { | |
} | ||
} | ||
|
||
if (includeUncovered != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicated code with the stuff on line 136 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops!! 😅 |
||
// Step 1: Identify all Dart files | ||
final allFiles = _findAllDartFiles(reportOn: reportOn); | ||
print('detected files: $allFiles'); | ||
|
||
// Step 2: Identify covered files | ||
final coveredFiles = Map.fromEntries(entries | ||
.where((entry) => entry.value.lineHits.values.any((hit) => hit > 0))); | ||
|
||
// check if the file is covered or no. | ||
final packageName = getPackageName(); | ||
|
||
final uncoveredFiles = <String>[]; | ||
for (final file in allFiles) { | ||
final pkgUri = toPackageUri(file, packageName); | ||
if (!coveredFiles.containsKey(pkgUri)) { | ||
uncoveredFiles.add(file); | ||
} | ||
} | ||
|
||
print('Uncovered Dart files:'); | ||
for (final file in uncoveredFiles) { | ||
print(file); | ||
} | ||
|
||
//formatlcov for including uncovered. | ||
final uncoveredBuf = StringBuffer(); | ||
|
||
for (final file in uncoveredFiles) { | ||
if (!pathFilter(file)) continue; | ||
|
||
final lines = File(file).readAsLinesSync(); | ||
var displayPath = file; | ||
if (basePath != null) { | ||
displayPath = p.relative(file, from: basePath); | ||
} | ||
displayPath = | ||
displayPath.replaceAll('\\', '/'); // For Windows compatibility | ||
|
||
uncoveredBuf.writeln('SF:$displayPath'); | ||
|
||
var lineNumber = 1; | ||
var realLines = 0; | ||
for (final line in lines) { | ||
final trimmed = line.trim(); | ||
if (trimmed.isNotEmpty && !trimmed.startsWith('//')) { | ||
uncoveredBuf.writeln('DA:$lineNumber,0'); | ||
realLines++; | ||
} | ||
lineNumber++; | ||
} | ||
|
||
uncoveredBuf.writeln('LF:$realLines'); | ||
uncoveredBuf.writeln('LH:0'); | ||
uncoveredBuf.writeln('end_of_record'); | ||
} | ||
|
||
buf.write(uncoveredBuf.toString()); | ||
} | ||
|
||
return buf.toString(); | ||
} | ||
|
||
List<String> _findAllDartFiles({List<String>? reportOn}) { | ||
final files = <String>[]; | ||
final roots = reportOn ?? ['lib/src']; | ||
for (final root in roots) { | ||
final dir = Directory(root); | ||
if (!dir.existsSync()) continue; | ||
final dartFiles = dir | ||
.listSync(recursive: true) | ||
.whereType<File>() | ||
.where((file) => file.path.endsWith('.dart')); | ||
files.addAll(dartFiles.map((f) => f.path)); | ||
} | ||
return files; | ||
} | ||
|
||
String getPackageName() { | ||
final pubspecFile = File('pubspec.yaml'); | ||
final doc = loadYaml(pubspecFile.readAsStringSync()); | ||
return doc['name'] as String; | ||
} | ||
|
||
String toPackageUri(String filePath, String packageName) { | ||
if (filePath.startsWith('lib${Platform.pathSeparator}')) { | ||
final relativePath = p.relative(filePath, from: 'lib'); | ||
return 'package:$packageName/$relativePath'.replaceAll('\\', '/'); | ||
} | ||
return filePath.replaceAll( | ||
'\\', '/'); // fallback (or handle `src/`, etc. if needed) | ||
} | ||
} | ||
|
||
const _prefix = ' '; | ||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be a bool flag. It should be a multi-option that accepts glob patterns, like
ignore-files
. Then theincludeUncovered
function passed to the formatter can check if thepath
matches any of the globs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok ...got it...
I will change that.