Skip to content
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

coverage: Allow specifying coverage flags via a yaml file #1954

Merged
merged 21 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
99afba1
Implemented functionality to allows specifying coverage options and f…
Dhruv-Maradiya Dec 31, 2024
ff390a9
Added test cases to cover YAML configuration options.
Dhruv-Maradiya Dec 31, 2024
9cd8dce
Bump version to 1.11.2 and add support for specifying coverage flags …
Dhruv-Maradiya Dec 31, 2024
949917c
feat: update dependencies and refactor coverage options handling usin…
Dhruv-Maradiya Jan 7, 2025
eebc68f
test: refactor test cases to align with updated coverage options hand…
Dhruv-Maradiya Jan 7, 2025
bb0eb60
test: optimized test cases
Dhruv-Maradiya Jan 7, 2025
781b2d7
fix: set default VM service port to 8181 in test_with_coverage.dart
Dhruv-Maradiya Jan 7, 2025
c3cbd55
refactor: streamline argument parsing in coverage scripts
Dhruv-Maradiya Jan 9, 2025
4a1d57f
removed support for all uncommon flags
Dhruv-Maradiya Jan 16, 2025
c610504
refactor: changed test cases to align with updated coverage options
Dhruv-Maradiya Jan 16, 2025
b1efad0
change: Resolve output and package paths to absolute paths; add upwar…
Dhruv-Maradiya Jan 17, 2025
beac649
refactor: use absolute and normalize instead of canonicalize for path…
Dhruv-Maradiya Jan 20, 2025
8d3ca0c
refactor: made argument parser visible for testing
Dhruv-Maradiya Jan 23, 2025
c6452f7
test: add coverage options file locator tests
Dhruv-Maradiya Jan 23, 2025
fcfca3f
refactor: handling path in test using canonicalize
Dhruv-Maradiya Jan 27, 2025
69858bf
refactor: update coverage output paths to use 'coverage_data' directory
Dhruv-Maradiya Jan 29, 2025
6c85d14
refactor: improve file creation logic for coverage output paths
Dhruv-Maradiya Jan 30, 2025
fc28600
refactor: update coverage output paths to use 'var/coverage_data' dir…
Dhruv-Maradiya Feb 2, 2025
a8a3844
refactor: formatted the code
Dhruv-Maradiya Feb 3, 2025
28c6c7b
Merge branch 'dart-lang:main' into coverage-flags-yaml
Dhruv-Maradiya Feb 4, 2025
e7a315a
fix: make coverage output paths relative on Ubuntu and macOS by remov…
Dhruv-Maradiya Feb 4, 2025
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
2 changes: 1 addition & 1 deletion pkgs/coverage/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 1.12.0
## 1.12.0-wip

- Introduced support for specifying coverage flags through a YAML file.

Expand Down
6 changes: 3 additions & 3 deletions pkgs/coverage/bin/collect_coverage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ Options _parseArgs(List<String> arguments, CoverageOptions defaultOptions) {
IOSink out;
final outPath = args['out'] as String?;
if (outPath == 'stdout' ||
(outPath == null && defaultOptions.output == null)) {
(outPath == null && defaultOptions.outputDirectory == null)) {
out = stdout;
} else {
final outFilePath = p.absolute(
p.normalize(outPath ?? '${defaultOptions.output}/coverage.json'));
final outFilePath = p.canonicalize(
Dhruv-Maradiya marked this conversation as resolved.
Show resolved Hide resolved
outPath ?? '${defaultOptions.outputDirectory}/coverage.json');
final outFile = File(outFilePath)..createSync(recursive: true);
out = outFile.openWrite();
}
Expand Down
12 changes: 6 additions & 6 deletions pkgs/coverage/bin/format_coverage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Environment parseArgs(List<String> arguments, CoverageOptions defaultOptions) {
)
..addOption('package',
help: 'root directory of the package',
defaultsTo: defaultOptions.packagePath)
defaultsTo: defaultOptions.packageDirectory)
..addOption(
'in',
abbr: 'i',
Expand Down Expand Up @@ -239,11 +239,11 @@ Environment parseArgs(List<String> arguments, CoverageOptions defaultOptions) {
fail('Package spec "${args["package"]}" not found, or not a directory.');
}

if (args['in'] == null && defaultOptions.output == null) {
if (args['in'] == null && defaultOptions.outputDirectory == null) {
fail('No input files given.');
}
final inputPath =
args['in'] as String? ?? '${defaultOptions.output}/coverage.json';
final inputPath = args['in'] as String? ??
'${defaultOptions.outputDirectory}/coverage.json';
final input = p.absolute(p.normalize(inputPath));
Dhruv-Maradiya marked this conversation as resolved.
Show resolved Hide resolved
if (!FileSystemEntity.isDirectorySync(input) &&
!FileSystemEntity.isFileSync(input)) {
Expand All @@ -253,11 +253,11 @@ Environment parseArgs(List<String> arguments, CoverageOptions defaultOptions) {
IOSink output;
final outPath = args['out'] as String?;
if (outPath == 'stdout' ||
(outPath == null && defaultOptions.output == null)) {
(outPath == null && defaultOptions.outputDirectory == null)) {
output = stdout;
} else {
final outFilePath = p
.absolute(p.normalize(outPath ?? '${defaultOptions.output}/lcov.info'));
.canonicalize(outPath ?? '${defaultOptions.outputDirectory}/lcov.info');
Dhruv-Maradiya marked this conversation as resolved.
Show resolved Hide resolved
final outfile = File(outFilePath)..createSync(recursive: true);
output = outfile.openWrite();
}
Expand Down
17 changes: 2 additions & 15 deletions pkgs/coverage/bin/test_with_coverage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ ArgParser _createArgParser(CoverageOptions defaultOptions) => ArgParser()
..addOption(
'package',
help: 'Root directory of the package to test.',
defaultsTo: defaultOptions.packagePath,
defaultsTo: defaultOptions.packageDirectory,
)
..addOption(
'package-name',
Expand All @@ -66,7 +66,7 @@ ArgParser _createArgParser(CoverageOptions defaultOptions) => ArgParser()
..addOption('port', help: 'VM service port.', defaultsTo: '8181')
..addOption(
'out',
defaultsTo: defaultOptions.output,
defaultsTo: defaultOptions.outputDirectory,
abbr: 'o',
help: 'Output directory. Defaults to <package-dir>/coverage.',
)
Expand Down Expand Up @@ -174,19 +174,6 @@ ${parser.usage}
Future<void> main(List<String> arguments) async {
final defaultOptions = CoverageOptionsProvider().coverageOptions;
final flags = await _parseArgs(arguments, defaultOptions);

print('Flags: ');
print(' packageDir: ${flags.packageDir}');
print(' packageName: ${flags.packageName}');
print(' outDir: ${flags.outDir}');
print(' port: ${flags.port}');
print(' testScript: ${flags.testScript}');
print(' functionCoverage: ${flags.functionCoverage}');
print(' branchCoverage: ${flags.branchCoverage}');
print(' scopeOutput: ${flags.scopeOutput}');
print(' rest: ${flags.rest}');
print('');

final outJson = path.join(flags.outDir, 'coverage.json');
final outLcov = path.join(flags.outDir, 'lcov.info');

Expand Down
71 changes: 55 additions & 16 deletions pkgs/coverage/lib/src/coverage_options.dart
Original file line number Diff line number Diff line change
@@ -1,40 +1,55 @@
import 'dart:io';
import 'package:cli_config/cli_config.dart';
import 'package:path/path.dart' as path;

class CoverageOptions {
const CoverageOptions({
required this.output,
this.outputDirectory,
required this.scopeOutput,
required this.functionCoverage,
required this.branchCoverage,
required this.packagePath,
required this.packageDirectory,
this.packageName,
required this.testScript,
});

factory CoverageOptions.fromConfig(
Config options, CoverageOptions defaultOptions) {
Config options, CoverageOptions defaultOptions, String optionsFilePath) {
var outputDirectory = options.optionalString('output_directory') ??
defaultOptions.outputDirectory;
var packageDirectory = options.optionalString('package_directory') ??
defaultOptions.packageDirectory;

if (outputDirectory != null && !path.isAbsolute(outputDirectory)) {
outputDirectory = path.canonicalize(
Dhruv-Maradiya marked this conversation as resolved.
Show resolved Hide resolved
path.join(path.dirname(optionsFilePath), outputDirectory));
}
if (!path.isAbsolute(packageDirectory)) {
packageDirectory = path.canonicalize(
path.join(path.dirname(optionsFilePath), packageDirectory));
}

return CoverageOptions(
Dhruv-Maradiya marked this conversation as resolved.
Show resolved Hide resolved
output: options.optionalString('out') ?? defaultOptions.output,
outputDirectory: outputDirectory,
scopeOutput: options.optionalStringList('scope_output') ??
defaultOptions.scopeOutput,
functionCoverage: options.optionalBool('function_coverage') ??
defaultOptions.functionCoverage,
branchCoverage: options.optionalBool('branch_coverage') ??
defaultOptions.branchCoverage,
packagePath:
options.optionalString('package') ?? defaultOptions.packagePath,
packageDirectory: packageDirectory,
packageName:
options.optionalString('package_name') ?? defaultOptions.packageName,
testScript: options.optionalString('test') ?? defaultOptions.testScript,
testScript:
options.optionalString('test_script') ?? defaultOptions.testScript,
);
}

final String? output;
final String? outputDirectory;
final List<String> scopeOutput;
final bool functionCoverage;
final bool branchCoverage;
final String packagePath;
final String packageDirectory;
final String? packageName;
final String testScript;
}
Expand All @@ -43,7 +58,7 @@ class CoverageOptionsProvider {
CoverageOptionsProvider({
String? filePath,
}) {
final file = _getOptionsFile(filePath ?? CoverageOptionsProvider.filePath);
final file = _getOptionsFile(filePath);
final fileContents = file?.readAsStringSync();

final isFileEmpty = fileContents?.isEmpty ?? false;
Expand All @@ -54,24 +69,48 @@ class CoverageOptionsProvider {
fileSourceUri: file?.uri,
);

coverageOptions = CoverageOptions.fromConfig(options, defaultOptions);
coverageOptions =
CoverageOptions.fromConfig(options, defaultOptions, optionsFilePath);
}

late final CoverageOptions coverageOptions;
late final String optionsFilePath;
static const defaultFilePath = 'coverage_options.yaml';

File? _getOptionsFile(String? filePath) {
filePath ??= _findOptionsFilePath();
if (filePath == null) {
throw StateError('Could not find a pubspec.yaml file.');
Dhruv-Maradiya marked this conversation as resolved.
Show resolved Hide resolved
}

static const filePath = 'coverage.yaml';
optionsFilePath = path.canonicalize(filePath);

static File? _getOptionsFile(String filePath) {
final file = File(filePath);
final file = File(optionsFilePath);
return file.existsSync() ? file : null;
}

String? _findOptionsFilePath() {
var currentDir = Directory.current;

while (true) {
final pubSpecFilePath = path.join(currentDir.path, 'pubspec.yaml');
if (File(pubSpecFilePath).existsSync()) {
return path.join(currentDir.path, defaultFilePath);
}
final parentDir = currentDir.parent;
if (parentDir.path == currentDir.path) {
return null;
}
currentDir = parentDir;
}
}

static const defaultOptions = CoverageOptions(
output: null,
outputDirectory: null,
scopeOutput: [],
functionCoverage: false,
branchCoverage: false,
packagePath: '.',
packageDirectory: '.',
packageName: null,
testScript: 'test',
);
Expand Down
2 changes: 1 addition & 1 deletion pkgs/coverage/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: coverage
version: 1.12.0
version: 1.12.0-wip
description: Coverage data manipulation and formatting
repository: https://github.com/dart-lang/tools/tree/main/pkgs/coverage
issue_tracker: https://github.com/dart-lang/tools/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Acoverage
Expand Down
Loading
Loading