Skip to content

Commit

Permalink
refactor: streamline argument parsing in coverage scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhruv-Maradiya committed Jan 9, 2025
1 parent 781b2d7 commit c3cbd55
Show file tree
Hide file tree
Showing 8 changed files with 6 additions and 147 deletions.
10 changes: 3 additions & 7 deletions pkgs/coverage/bin/collect_coverage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,12 @@ Options _parseArgs(List<String> arguments, CoverageOptions defaultOptions) {
'the provided package path are considered')
..addFlag('wait-paused',
abbr: 'w',
defaultsTo: defaultOptions.waitPaused,
defaultsTo: false,
help: 'wait for all isolates to be paused before collecting coverage')
..addFlag('resume-isolates',
abbr: 'r',
defaultsTo: defaultOptions.resumeIsolates,
help: 'resume all isolates on exit')
abbr: 'r', defaultsTo: false, help: 'resume all isolates on exit')
..addFlag('include-dart',
abbr: 'd',
defaultsTo: defaultOptions.includeDart,
help: 'include "dart:" libraries')
abbr: 'd', defaultsTo: false, help: 'include "dart:" libraries')
..addFlag('function-coverage',
abbr: 'f',
defaultsTo: defaultOptions.functionCoverage,
Expand Down
10 changes: 2 additions & 8 deletions pkgs/coverage/bin/format_coverage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ Environment parseArgs(List<String> arguments, CoverageOptions defaultOptions) {
'sdk-root',
abbr: 's',
help: 'path to the SDK root',
defaultsTo: defaultOptions.sdkRoot,
)
..addOption(
'packages',
Expand All @@ -166,28 +165,24 @@ Environment parseArgs(List<String> arguments, CoverageOptions defaultOptions) {
help: 'number of workers',
)
..addOption('bazel-workspace',
defaultsTo: defaultOptions.bazelWorkspace,
help: 'Bazel workspace directory')
defaultsTo: '', help: 'Bazel workspace directory')
..addOption('base-directory',
abbr: 'b',
defaultsTo: defaultOptions.baseDirectory,
help: 'the base directory relative to which source paths are output')
..addFlag('bazel',
defaultsTo: defaultOptions.bazel,
help: 'use Bazel-style path resolution')
defaultsTo: false, help: 'use Bazel-style path resolution')
..addFlag('pretty-print',
abbr: 'r',
defaultsTo: defaultOptions.prettyPrint,
negatable: false,
help: 'convert line coverage data to pretty print format')
..addFlag('pretty-print-func',
abbr: 'f',
defaultsTo: defaultOptions.prettyPrintFunc,
negatable: false,
help: 'convert function coverage data to pretty print format')
..addFlag('pretty-print-branch',
negatable: false,
defaultsTo: defaultOptions.prettyPrintBranch,
help: 'convert branch coverage data to pretty print format')
..addFlag('lcov',
abbr: 'l',
Expand All @@ -202,7 +197,6 @@ Environment parseArgs(List<String> arguments, CoverageOptions defaultOptions) {
..addFlag(
'check-ignore',
abbr: 'c',
defaultsTo: defaultOptions.checkIgnore,
negatable: false,
help: 'check for coverage ignore comments.'
' Not supported in web coverage.',
Expand Down
38 changes: 0 additions & 38 deletions pkgs/coverage/lib/src/coverage_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,16 @@ class CoverageOptions {
required this.output,
this.connectTimeout,
required this.scopeOutput,
required this.waitPaused,
required this.resumeIsolates,
required this.includeDart,
required this.functionCoverage,
required this.branchCoverage,
this.sdkRoot,
required this.packagePath,
this.input,
this.reportOn,
required this.workers,
required this.bazelWorkspace,
this.baseDirectory,
required this.bazel,
required this.prettyPrint,
required this.prettyPrintFunc,
required this.prettyPrintBranch,
required this.lcov,
required this.checkIgnore,
required this.ignoreFiles,
this.packageName,
required this.testScript,
Expand All @@ -38,37 +30,23 @@ class CoverageOptions {
defaultOptions.connectTimeout,
scopeOutput: options.optionalStringList('scope_output') ??
defaultOptions.scopeOutput,
waitPaused:
options.optionalBool('wait_paused') ?? defaultOptions.waitPaused,
resumeIsolates: options.optionalBool('resume_isolates') ??
defaultOptions.resumeIsolates,
includeDart:
options.optionalBool('include_dart') ?? defaultOptions.includeDart,
functionCoverage: options.optionalBool('function_coverage') ??
defaultOptions.functionCoverage,
branchCoverage: options.optionalBool('branch_coverage') ??
defaultOptions.branchCoverage,
sdkRoot: options.optionalString('sdk_root') ?? defaultOptions.sdkRoot,
packagePath:
options.optionalString('package') ?? defaultOptions.packagePath,
input: options.optionalString('in') ?? defaultOptions.input,
reportOn:
options.optionalStringList('report_on') ?? defaultOptions.reportOn,
workers: options.optionalInt('workers') ?? defaultOptions.workers,
bazelWorkspace: options.optionalString('bazel_workspace') ??
defaultOptions.bazelWorkspace,
baseDirectory: options.optionalString('base_directory') ??
defaultOptions.baseDirectory,
bazel: options.optionalBool('bazel') ?? defaultOptions.bazel,
prettyPrint:
options.optionalBool('pretty_print') ?? defaultOptions.prettyPrint,
prettyPrintFunc: options.optionalBool('pretty_print_func') ??
defaultOptions.prettyPrintFunc,
prettyPrintBranch: options.optionalBool('pretty_print_branch') ??
defaultOptions.prettyPrintBranch,
lcov: options.optionalBool('lcov') ?? defaultOptions.lcov,
checkIgnore:
options.optionalBool('check_ignore') ?? defaultOptions.checkIgnore,
ignoreFiles: options.optionalStringList('ignore_files') ??
defaultOptions.ignoreFiles,
packageName:
Expand All @@ -81,24 +59,16 @@ class CoverageOptions {
final String output;
final int? connectTimeout;
final List<String> scopeOutput;
final bool waitPaused;
final bool resumeIsolates;
final bool includeDart;
final bool functionCoverage;
final bool branchCoverage;
final String? sdkRoot;
final String packagePath;
final String? input;
final List<String>? reportOn;
final int workers;
final String bazelWorkspace;
final String? baseDirectory;
final bool bazel;
final bool prettyPrint;
final bool prettyPrintFunc;
final bool prettyPrintBranch;
final bool lcov;
final bool checkIgnore;
final List<String> ignoreFiles;
final String? packageName;
final String testScript;
Expand Down Expand Up @@ -136,24 +106,16 @@ class CoverageOptionsProvider {
output: 'stdout',
connectTimeout: null,
scopeOutput: [],
waitPaused: false,
resumeIsolates: false,
includeDart: false,
functionCoverage: false,
branchCoverage: false,
sdkRoot: null,
packagePath: '.',
input: null,
reportOn: null,
workers: 1,
bazelWorkspace: '',
baseDirectory: '.',
bazel: false,
prettyPrint: false,
prettyPrintFunc: false,
prettyPrintBranch: false,
lcov: false,
checkIgnore: false,
ignoreFiles: [],
packageName: null,
testScript: 'test',
Expand Down
36 changes: 0 additions & 36 deletions pkgs/coverage/test/collect_coverage_config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ void main() {

expect(collectedCoverage.scopeOutput, defaults.scopeOutput);
expect(collectedCoverage.resumeIsolates, defaults.resumeIsolates);
expect(collectedCoverage.waitPaused, defaults.waitPaused);
expect(collectedCoverage.functionCoverage, defaults.functionCoverage);
expect(collectedCoverage.branchCoverage, defaults.branchCoverage);
expect(collectedCoverage.connectTimeout, defaults.connectTimeout);
expect(collectedCoverage.includeDart, defaults.includeDart);
expect(collectedCoverage.output, defaults.output);
});

Expand All @@ -28,19 +26,13 @@ void main() {
parseArgsFormatCoverage(defaultInputArgs, defaults);

expect(formattedCoverage.baseDirectory, defaults.baseDirectory);
expect(formattedCoverage.bazel, defaults.bazel);
expect(formattedCoverage.bazelWorkspace, defaults.bazelWorkspace);
expect(formattedCoverage.checkIgnore, defaults.checkIgnore);
expect(formattedCoverage.input, 'coverage.json');
expect(formattedCoverage.lcov, defaults.lcov);
expect(formattedCoverage.output, defaults.output);
expect(formattedCoverage.packagePath, defaults.packagePath);
expect(formattedCoverage.prettyPrint, defaults.prettyPrint);
expect(formattedCoverage.prettyPrintFunc, defaults.prettyPrintFunc);
expect(formattedCoverage.prettyPrintBranch, defaults.prettyPrintBranch);
expect(formattedCoverage.reportOn, defaults.reportOn);
expect(formattedCoverage.ignoreFiles, defaults.ignoreFiles);
expect(formattedCoverage.sdkRoot, defaults.sdkRoot);
expect(formattedCoverage.verbose, defaults.verbose);
expect(formattedCoverage.workers, defaults.workers);
});
Expand Down Expand Up @@ -72,28 +64,20 @@ void main() {
// Verify collect coverage yaml values
expect(collectedCoverage.scopeOutput, ['lib', 'src']);
expect(collectedCoverage.resumeIsolates, isFalse);
expect(collectedCoverage.waitPaused, isTrue);
expect(collectedCoverage.functionCoverage, isTrue);
expect(collectedCoverage.branchCoverage, isFalse);
expect(collectedCoverage.connectTimeout, 30);
expect(collectedCoverage.includeDart, isFalse);
expect(collectedCoverage.output, 'coverage');

// Verify format coverage yaml values
expect(formattedCoverage.baseDirectory, '.');
expect(formattedCoverage.bazel, isFalse);
expect(formattedCoverage.bazelWorkspace, '');
expect(formattedCoverage.checkIgnore, isFalse);
expect(formattedCoverage.input, 'coverage.json');
expect(formattedCoverage.lcov, isTrue);
expect(formattedCoverage.output, 'coverage');
expect(formattedCoverage.packagePath, '.');
expect(formattedCoverage.prettyPrint, isFalse);
expect(formattedCoverage.prettyPrintFunc, isFalse);
expect(formattedCoverage.prettyPrintBranch, isFalse);
expect(formattedCoverage.reportOn, ['lib', 'bin']);
expect(formattedCoverage.ignoreFiles, ['test']);
expect(formattedCoverage.sdkRoot, '.');
expect(formattedCoverage.verbose, isTrue);
expect(formattedCoverage.workers, 2);

Expand Down Expand Up @@ -123,9 +107,7 @@ void main() {
// Verify collect coverage yaml values
expect(collectedCoverage.output, 'custom_coverage.json');
expect(collectedCoverage.scopeOutput, ['lib', 'test']);
expect(collectedCoverage.resumeIsolates, isTrue);
expect(collectedCoverage.functionCoverage, isFalse);
expect(collectedCoverage.includeDart, isTrue);
expect(collectedCoverage.connectTimeout, 20);

// Verify format coverage yaml values
Expand All @@ -135,7 +117,6 @@ void main() {
expect(formattedCoverage.ignoreFiles, ['example']);
expect(formattedCoverage.reportOn, ['lib']);
expect(formattedCoverage.prettyPrint, isTrue);
expect(formattedCoverage.prettyPrintFunc, isFalse);

// Verify test with coverage yaml values
expect(testCoverage.packageName, 'Custom Dart Package');
Expand All @@ -155,20 +136,15 @@ void main() {

// Verify collect coverage yaml values
expect(collectedCoverage.scopeOutput, ['lib', 'tools']);
expect(collectedCoverage.includeDart, isFalse);
expect(collectedCoverage.branchCoverage, isFalse);
expect(collectedCoverage.waitPaused, isFalse);
expect(collectedCoverage.connectTimeout, 15);
expect(collectedCoverage.functionCoverage, isTrue);

// Verify format coverage yaml values
expect(formattedCoverage.bazel, isTrue);
expect(formattedCoverage.checkIgnore, isTrue);
expect(formattedCoverage.input, 'custom_coverage.json');
expect(formattedCoverage.output, 'custom_lcov.info');
expect(formattedCoverage.packagePath, '.');
expect(formattedCoverage.reportOn, ['src', 'scripts']);
expect(formattedCoverage.sdkRoot, './dart-sdk');

// Verify test with coverage yaml values
expect(testCoverage.packageName, 'coverage');
Expand All @@ -188,8 +164,6 @@ void main() {
'--out=coverage.json',
'--scope-output=lib',
'--connect-timeout=10',
'--resume-isolates',
'--no-wait-paused',
'--no-function-coverage',
'--branch-coverage',
], configuredOptions);
Expand All @@ -211,8 +185,6 @@ void main() {
// Verify collect coverage command line args
expect(collectedCoverage.output, 'coverage.json');
expect(collectedCoverage.scopeOutput, ['lib']);
expect(collectedCoverage.resumeIsolates, isTrue);
expect(collectedCoverage.waitPaused, isFalse);
expect(collectedCoverage.functionCoverage, isFalse);
expect(collectedCoverage.branchCoverage, isTrue);
expect(collectedCoverage.connectTimeout, 10);
Expand Down Expand Up @@ -245,28 +217,20 @@ void main() {
// Verify collect coverage defaults
expect(collectedCoverage.scopeOutput, isEmpty);
expect(collectedCoverage.resumeIsolates, isFalse);
expect(collectedCoverage.waitPaused, isFalse);
expect(collectedCoverage.functionCoverage, isFalse);
expect(collectedCoverage.branchCoverage, isFalse);
expect(collectedCoverage.connectTimeout, isNull);
expect(collectedCoverage.includeDart, isFalse);
expect(collectedCoverage.output, 'stdout');

// Verify format coverage defaults
expect(formattedCoverage.baseDirectory, '.');
expect(formattedCoverage.bazel, isFalse);
expect(formattedCoverage.bazelWorkspace, '');
expect(formattedCoverage.checkIgnore, isFalse);
expect(formattedCoverage.input, 'coverage.json');
expect(formattedCoverage.lcov, isFalse);
expect(formattedCoverage.output, 'stdout');
expect(formattedCoverage.packagePath, '.');
expect(formattedCoverage.prettyPrint, isFalse);
expect(formattedCoverage.prettyPrintFunc, isFalse);
expect(formattedCoverage.prettyPrintBranch, isFalse);
expect(formattedCoverage.reportOn, isNull);
expect(formattedCoverage.ignoreFiles, isEmpty);
expect(formattedCoverage.sdkRoot, isNull);
expect(formattedCoverage.verbose, isFalse);
expect(formattedCoverage.workers, 1);

Expand Down
9 changes: 0 additions & 9 deletions pkgs/coverage/test/test_coverage_options/all_field.yaml
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
out: "coverage"
connect-timeout: 30
scope-output: ["lib", "src"]
resume-isolates: false
include-dart: false
function-coverage: true
branch-coverage: false
wait-paused: true
base-directory: "."
bazel: false
bazel-workspace: null
in: "coverage.json"
package: "."
package-name: "My Dart Package"
report-on: ["lib", "bin"]
ignore-files: ["test"]
sdk-root: "."
workers: 2
check-ignore: false
verbose: true
lcov: true
pretty-print: false
pretty-print-func: false
pretty-print-branch: false
test: "test"
4 changes: 0 additions & 4 deletions pkgs/coverage/test/test_coverage_options/partial_fields1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ out: 'custom_coverage.json'
scope-output:
- 'lib'
- 'test'
resume-isolates: true
function-coverage: false
include-dart: true
connect-timeout: 20
lcov: false
verbose: false
Expand All @@ -14,5 +11,4 @@ ignore-files:
report-on:
- 'lib'
pretty-print: true
pretty-print-func: false
package-name: 'Custom Dart Package'
5 changes: 0 additions & 5 deletions pkgs/coverage/test/test_coverage_options/partial_fields2.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
scope-output:
- 'lib'
- 'tools'
include-dart: false
branch-coverage: false
wait-paused: false
connect-timeout: 15

bazel: true
check-ignore: true
in: 'custom_coverage.json'
out: 'custom_lcov.info'
package: '.'
report-on:
- 'src'
- 'scripts'
sdk-root: './dart-sdk'

test: 'custom_test'
function-coverage: true
Loading

0 comments on commit c3cbd55

Please sign in to comment.