Skip to content

Commit

Permalink
refactor: changed test cases to align with updated coverage options
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhruv-Maradiya committed Jan 16, 2025
1 parent 4a1d57f commit c610504
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 165 deletions.
89 changes: 17 additions & 72 deletions pkgs/coverage/test/collect_coverage_config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import 'package:test/test.dart';
import 'test_util.dart';

void main() {
final defaultInputArgs = <String>['--in=coverage.json'];

group('defaults when no yaml or command-line args provided', () {
// Setup
final defaults = CoverageOptionsProvider.defaultOptions;
Expand All @@ -14,35 +12,25 @@ void main() {
final collectedCoverage = parseArgsCollectCoverage([], defaults);

expect(collectedCoverage.scopeOutput, defaults.scopeOutput);
expect(collectedCoverage.resumeIsolates, defaults.resumeIsolates);
expect(collectedCoverage.functionCoverage, defaults.functionCoverage);
expect(collectedCoverage.branchCoverage, defaults.branchCoverage);
expect(collectedCoverage.connectTimeout, defaults.connectTimeout);
expect(collectedCoverage.output, defaults.output);
expect(collectedCoverage.output, 'stdout');
});

test('format coverage', () {
final formattedCoverage =
parseArgsFormatCoverage(defaultInputArgs, defaults);
parseArgsFormatCoverage([], defaults);

expect(formattedCoverage.baseDirectory, defaults.baseDirectory);
expect(formattedCoverage.input, 'coverage.json');
expect(formattedCoverage.lcov, defaults.lcov);
expect(formattedCoverage.output, defaults.output);
expect(formattedCoverage.output, 'stdout');
expect(formattedCoverage.packagePath, defaults.packagePath);
expect(formattedCoverage.prettyPrint, defaults.prettyPrint);
expect(formattedCoverage.reportOn, defaults.reportOn);
expect(formattedCoverage.ignoreFiles, defaults.ignoreFiles);
expect(formattedCoverage.verbose, defaults.verbose);
expect(formattedCoverage.workers, defaults.workers);
});

test('test with coverage', () async {
final testCoverage = await parseArgsTestWithCoverage([], defaults);

expect(testCoverage.packagePath, getPackageDir(defaults.packagePath));
expect(testCoverage.packageName, 'coverage');
expect(testCoverage.output, defaults.output);
expect(testCoverage.output, 'coverage');
expect(testCoverage.testScript, defaults.testScript);
expect(testCoverage.functionCoverage, defaults.functionCoverage);
expect(testCoverage.branchCoverage, defaults.branchCoverage);
Expand All @@ -58,28 +46,18 @@ void main() {
// Parse arguments with empty command line args
final collectedCoverage = parseArgsCollectCoverage([], configuredOptions);
final formattedCoverage =
parseArgsFormatCoverage(defaultInputArgs, configuredOptions);
parseArgsFormatCoverage([], configuredOptions);
final testCoverage = await parseArgsTestWithCoverage([], configuredOptions);

// Verify collect coverage yaml values
expect(collectedCoverage.scopeOutput, ['lib', 'src']);
expect(collectedCoverage.resumeIsolates, isFalse);
expect(collectedCoverage.functionCoverage, isTrue);
expect(collectedCoverage.branchCoverage, isFalse);
expect(collectedCoverage.connectTimeout, 30);
expect(collectedCoverage.output, 'coverage');
expect(collectedCoverage.output, 'coverage/coverage.json');

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

// Verify test with coverage yaml values
expect(testCoverage.packagePath, getPackageDir('.'));
Expand All @@ -99,26 +77,14 @@ void main() {

// Parse arguments with empty command line args
final collectedCoverage = parseArgsCollectCoverage([], configuredOptions);
final formattedCoverage =
parseArgsFormatCoverage(defaultInputArgs, configuredOptions);
final testCoverage =
await parseArgsTestWithCoverage([], configuredOptions);
final formattedCoverage = parseArgsFormatCoverage([], configuredOptions);

// Verify collect coverage yaml values
expect(collectedCoverage.output, 'custom_coverage.json');
expect(collectedCoverage.output, 'custom_coverage/coverage.json');
expect(collectedCoverage.scopeOutput, ['lib', 'test']);
expect(collectedCoverage.functionCoverage, isFalse);
expect(collectedCoverage.connectTimeout, 20);

// Verify format coverage yaml values
expect(formattedCoverage.lcov, isFalse);
expect(formattedCoverage.verbose, isFalse);
expect(formattedCoverage.baseDirectory, 'src');
expect(formattedCoverage.ignoreFiles, ['example']);
expect(formattedCoverage.reportOn, ['lib']);
expect(formattedCoverage.prettyPrint, isTrue);

// Verify test with coverage yaml values
expect(formattedCoverage.output, 'custom_coverage/lcov.info');
expect(testCoverage.packageName, 'Custom Dart Package');
expect(testCoverage.scopeOutput, ['lib', 'test']);
});
Expand All @@ -137,18 +103,16 @@ void main() {
// Verify collect coverage yaml values
expect(collectedCoverage.scopeOutput, ['lib', 'tools']);
expect(collectedCoverage.branchCoverage, isFalse);
expect(collectedCoverage.connectTimeout, 15);
expect(collectedCoverage.functionCoverage, isTrue);
expect(collectedCoverage.output, 'custom_lcov/coverage.json');

// Verify format coverage yaml values
expect(formattedCoverage.input, 'custom_coverage.json');
expect(formattedCoverage.output, 'custom_lcov.info');
expect(formattedCoverage.output, 'custom_lcov/lcov.info');
expect(formattedCoverage.packagePath, '.');
expect(formattedCoverage.reportOn, ['src', 'scripts']);

// Verify test with coverage yaml values
expect(testCoverage.packageName, 'coverage');
expect(testCoverage.output, 'custom_lcov.info');
expect(testCoverage.output, 'custom_lcov');
expect(testCoverage.testScript, 'custom_test');
expect(testCoverage.functionCoverage, isTrue);
});
Expand All @@ -163,17 +127,12 @@ void main() {
final collectedCoverage = parseArgsCollectCoverage([
'--out=coverage.json',
'--scope-output=lib',
'--connect-timeout=10',
'--no-function-coverage',
'--branch-coverage',
], configuredOptions);
final formattedCoverage = parseArgsFormatCoverage([
'--in=data.json',
'--out=out_test.info',
'--report-on=src',
'--report-on=src2',
'--ignore-files=bin',
'--workers=4',
'--package=code_builder',
], configuredOptions);
final testCoverage = await parseArgsTestWithCoverage([
'--package-name=test',
Expand All @@ -187,14 +146,10 @@ void main() {
expect(collectedCoverage.scopeOutput, ['lib']);
expect(collectedCoverage.functionCoverage, isFalse);
expect(collectedCoverage.branchCoverage, isTrue);
expect(collectedCoverage.connectTimeout, 10);

// Verify format coverage command line args
expect(formattedCoverage.input, 'data.json');
expect(formattedCoverage.output, 'out_test.info');
expect(formattedCoverage.reportOn, ['src', 'src2']);
expect(formattedCoverage.ignoreFiles, ['bin']);
expect(formattedCoverage.workers, 4);
expect(formattedCoverage.packagePath, 'code_builder');

// Verify test with coverage command line args
expect(testCoverage.packageName, 'test');
Expand All @@ -211,33 +166,23 @@ void main() {
// Parse arguments with empty command line args
final collectedCoverage = parseArgsCollectCoverage([], configuredOptions);
final formattedCoverage =
parseArgsFormatCoverage(defaultInputArgs, configuredOptions);
parseArgsFormatCoverage([], configuredOptions);
final testCoverage = await parseArgsTestWithCoverage([], configuredOptions);

// Verify collect coverage defaults
expect(collectedCoverage.scopeOutput, isEmpty);
expect(collectedCoverage.resumeIsolates, isFalse);
expect(collectedCoverage.functionCoverage, isFalse);
expect(collectedCoverage.branchCoverage, isFalse);
expect(collectedCoverage.connectTimeout, isNull);
expect(collectedCoverage.output, 'stdout');

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

// Verify test with coverage defaults
expect(testCoverage.packagePath, getPackageDir('.'));
expect(testCoverage.packageName, 'coverage');
expect(testCoverage.output, 'stdout');
expect(testCoverage.output, 'coverage');
expect(testCoverage.testScript, 'test');
expect(testCoverage.functionCoverage, isFalse);
expect(testCoverage.branchCoverage, isFalse);
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,16 +1,7 @@
out: "coverage"
connect-timeout: 30
scope-output: ["lib", "src"]
function-coverage: true
branch-coverage: false
base-directory: "."
in: "coverage.json"
package: "."
package-name: "My Dart Package"
report-on: ["lib", "bin"]
ignore-files: ["test"]
workers: 2
verbose: true
lcov: true
pretty-print: false
test: "test"
11 changes: 1 addition & 10 deletions pkgs/coverage/test/test_coverage_options/partial_fields1.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
out: 'custom_coverage.json'
out: 'custom_coverage'
scope-output:
- 'lib'
- 'test'
connect-timeout: 20
lcov: false
verbose: false
base-directory: 'src'
ignore-files:
- 'example'
report-on:
- 'lib'
pretty-print: true
package-name: 'Custom Dart Package'
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@ scope-output:
- 'lib'
- 'tools'
branch-coverage: false
connect-timeout: 15

in: 'custom_coverage.json'
out: 'custom_lcov.info'
out: 'custom_lcov'
package: '.'
report-on:
- 'src'
- 'scripts'

test: 'custom_test'
function-coverage: true
87 changes: 21 additions & 66 deletions pkgs/coverage/test/test_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,89 +96,56 @@ extension ListTestExtension on List {
CoverageOptions parseArgsCollectCoverage(
List<String> arguments, CoverageOptions defaultOptions) {
final parser = ArgParser()
..addOption('host', abbr: 'H')
..addOption('port', abbr: 'p')
..addOption('uri', abbr: 'u')
..addOption('out', abbr: 'o', defaultsTo: defaultOptions.output)
..addOption('connect-timeout',
abbr: 't', defaultsTo: defaultOptions.connectTimeout?.toString())
..addOption('out', abbr: 'o')
..addMultiOption('scope-output', defaultsTo: defaultOptions.scopeOutput)
..addFlag('resume-isolates',
abbr: 'r', defaultsTo: defaultOptions.resumeIsolates)
..addFlag('function-coverage',
abbr: 'f', defaultsTo: defaultOptions.functionCoverage)
..addFlag('branch-coverage',
abbr: 'b', defaultsTo: defaultOptions.branchCoverage)
..addFlag('help', abbr: 'h', negatable: false);
abbr: 'b', defaultsTo: defaultOptions.branchCoverage);

final args = parser.parse(arguments);

String out;
final outPath = args['out'] as String?;
if (outPath == null && defaultOptions.output == null) {
out = 'stdout';
} else {
out = outPath ?? '${defaultOptions.output}/coverage.json';
}

return CoverageOptions(
output: args['out'] as String,
connectTimeout: args['connect-timeout'] == null
? defaultOptions.connectTimeout
: int.parse(args['connect-timeout'] as String),
output: out,
scopeOutput: args['scope-output'] as List<String>,
resumeIsolates: args['resume-isolates'] as bool,
functionCoverage: args['function-coverage'] as bool,
branchCoverage: args['branch-coverage'] as bool,
baseDirectory: defaultOptions.baseDirectory,
ignoreFiles: defaultOptions.ignoreFiles,
input: defaultOptions.input,
lcov: defaultOptions.lcov,
packagePath: defaultOptions.packagePath,
packageName: defaultOptions.packageName,
prettyPrint: defaultOptions.prettyPrint,
reportOn: defaultOptions.reportOn,
testScript: defaultOptions.testScript,
verbose: defaultOptions.verbose,
workers: defaultOptions.workers,
);
}

CoverageOptions parseArgsFormatCoverage(
List<String> arguments, CoverageOptions defaultOptions) {
final parser = ArgParser()
..addOption('packages')
..addOption('package', defaultsTo: defaultOptions.packagePath)
..addOption('in', abbr: 'i', defaultsTo: defaultOptions.input)
..addOption('out', abbr: 'o', defaultsTo: defaultOptions.output)
..addMultiOption('report-on', defaultsTo: defaultOptions.reportOn)
..addOption('workers',
abbr: 'j', defaultsTo: defaultOptions.workers.toString())
..addOption('base-directory',
abbr: 'b', defaultsTo: defaultOptions.baseDirectory)
..addFlag('pretty-print',
abbr: 'r', defaultsTo: defaultOptions.prettyPrint, negatable: false)
..addFlag('lcov',
abbr: 'l', defaultsTo: defaultOptions.lcov, negatable: false)
..addFlag('verbose',
abbr: 'v', defaultsTo: defaultOptions.verbose, negatable: false)
..addMultiOption('ignore-files', defaultsTo: defaultOptions.ignoreFiles)
..addFlag('help', abbr: 'h', negatable: false);
..addOption('out', abbr: 'o');

final args = parser.parse(arguments);

if (args['in'] == null) throw ArgumentError('Missing required argument: in');
String out;
final outPath = args['out'] as String?;
if (outPath == null && defaultOptions.output == null) {
out = 'stdout';
} else {
out = outPath ?? '${defaultOptions.output}/lcov.info';
}

return CoverageOptions(
baseDirectory: args['base-directory'] as String?,
input: args['in'] as String,
lcov: args['lcov'] as bool,
output: args['out'] as String,
output: out,
packagePath: args['package'] as String,
prettyPrint: args['lcov'] as bool ? false : args['pretty-print'] as bool,
reportOn: (args['report-on'] as List<String>).isNotEmpty
? args['report-on'] as List<String>
: null,
ignoreFiles: args['ignore-files'] as List<String>,
verbose: args['verbose'] as bool,
workers: int.parse(args['workers'] as String),
branchCoverage: defaultOptions.branchCoverage,
functionCoverage: defaultOptions.functionCoverage,
connectTimeout: defaultOptions.connectTimeout,
packageName: defaultOptions.packageName,
resumeIsolates: defaultOptions.resumeIsolates,
scopeOutput: defaultOptions.scopeOutput,
testScript: defaultOptions.testScript,
);
Expand All @@ -195,7 +162,6 @@ Future<CoverageOptions> parseArgsTestWithCoverage(
'package-name',
defaultsTo: defaultOptions.packageName,
)
..addOption('port')
..addOption(
'out',
defaultsTo: defaultOptions.output,
Expand All @@ -212,8 +178,7 @@ Future<CoverageOptions> parseArgsTestWithCoverage(
abbr: 'b',
defaultsTo: defaultOptions.branchCoverage,
)
..addMultiOption('scope-output', defaultsTo: defaultOptions.scopeOutput)
..addFlag('help', abbr: 'h', negatable: false);
..addMultiOption('scope-output', defaultsTo: defaultOptions.scopeOutput);

final args = parser.parse(arguments);

Expand All @@ -236,16 +201,6 @@ Future<CoverageOptions> parseArgsTestWithCoverage(
functionCoverage: args['function-coverage'] as bool,
branchCoverage: args['branch-coverage'] as bool,
scopeOutput: args['scope-output'] as List<String>,
baseDirectory: defaultOptions.baseDirectory,
connectTimeout: defaultOptions.connectTimeout,
ignoreFiles: defaultOptions.ignoreFiles,
input: defaultOptions.input,
lcov: defaultOptions.lcov,
prettyPrint: defaultOptions.prettyPrint,
reportOn: defaultOptions.reportOn,
resumeIsolates: defaultOptions.resumeIsolates,
verbose: defaultOptions.verbose,
workers: defaultOptions.workers,
);
}

Expand Down

0 comments on commit c610504

Please sign in to comment.