Skip to content

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions pkgs/coverage/bin/format_coverage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Environment {
required this.sdkRoot,
required this.verbose,
required this.workers,
required this.includeUncovered,
});

String? baseDirectory;
Expand All @@ -49,6 +50,7 @@ class Environment {
String? sdkRoot;
bool verbose;
int workers;
bool includeUncovered;
}

Future<void> main(List<String> arguments) async {
Expand Down Expand Up @@ -97,13 +99,15 @@ Future<void> main(List<String> arguments) async {
reportOn: env.reportOn,
ignoreGlobs: ignoreGlobs,
reportFuncs: env.prettyPrintFunc,
reportBranches: env.prettyPrintBranch);
reportBranches: env.prettyPrintBranch,
includeUncovered: (String path) => env.includeUncovered);
} else {
assert(env.lcov);
output = hitmap.formatLcov(resolver,
reportOn: env.reportOn,
ignoreGlobs: ignoreGlobs,
basePath: env.baseDirectory);
basePath: env.baseDirectory,
includeUncovered: (String path) => env.includeUncovered);
}

final outputSink =
Expand Down Expand Up @@ -196,6 +200,8 @@ Environment parseArgs(List<String> arguments, CoverageOptions defaultOptions) {
help: 'check for coverage ignore comments.'
' Not supported in web coverage.',
)
..addFlag('include-uncovered',
Copy link
Contributor

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 the includeUncovered function passed to the formatter can check if the path matches any of the globs.

Copy link
Author

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.

negatable: false, help: 'Include uncovered Dart files in the output')
..addMultiOption(
'ignore-files',
defaultsTo: [],
Expand Down Expand Up @@ -308,6 +314,9 @@ Environment parseArgs(List<String> arguments, CoverageOptions defaultOptions) {
final checkIgnore = args['check-ignore'] as bool;
final ignoredGlobs = args['ignore-files'] as List<String>;
final verbose = args['verbose'] as bool;
final includeUncovered = args['include-uncovered'] as bool;
print('includeUncovered: $includeUncovered');

return Environment(
baseDirectory: baseDirectory,
bazel: bazel,
Expand All @@ -325,7 +334,8 @@ Environment parseArgs(List<String> arguments, CoverageOptions defaultOptions) {
ignoreFiles: ignoredGlobs,
sdkRoot: sdkRoot,
verbose: verbose,
workers: workers);
workers: workers,
includeUncovered: includeUncovered);
}

/// Given an absolute path absPath, this function returns a [List] of files
Expand Down
155 changes: 155 additions & 0 deletions pkgs/coverage/lib/src/formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand All @@ -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,
Expand Down Expand Up @@ -193,8 +258,98 @@ extension FileHitMapsFormatter on Map<String, HitMap> {
}
}

if (includeUncovered != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicated code with the stuff on line 136

Copy link
Author

Choose a reason for hiding this comment

The 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 = ' ';
Expand Down
1 change: 1 addition & 0 deletions pkgs/coverage/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies:
source_maps: ^0.10.10
stack_trace: ^1.10.0
vm_service: '>=12.0.0 <16.0.0'
yaml: ^3.1.3

dev_dependencies:
benchmark_harness: ^2.2.0
Expand Down