Skip to content

Commit 5cf1d27

Browse files
authored
feat: Adding report-on flag to the dart test command (#1348)
1 parent 1b893e1 commit 5cf1d27

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

lib/src/cli/dart_cli.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class Dart {
110110
void Function(String)? stdout,
111111
void Function(String)? stderr,
112112
GeneratorBuilder buildGenerator = MasonGenerator.fromBundle,
113+
String? reportOn,
113114
}) async {
114115
return TestCLIRunner.test(
115116
logger: logger,
@@ -126,6 +127,7 @@ class Dart {
126127
arguments: arguments,
127128
stdout: stdout,
128129
stderr: stderr,
130+
reportOn: reportOn,
129131
buildGenerator: buildGenerator,
130132
);
131133
}

lib/src/cli/test_cli_runner.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class TestCLIRunner {
7878
void Function(String)? stdout,
7979
void Function(String)? stderr,
8080
GeneratorBuilder buildGenerator = MasonGenerator.fromBundle,
81+
String? reportOn,
8182
@visibleForTesting VeryGoodTestRunner? overrideTestRunner,
8283
}) async {
8384
final initialCwd = cwd;
@@ -182,7 +183,7 @@ class TestCLIRunner {
182183

183184
final output = hitmap.formatLcov(
184185
resolver,
185-
reportOn: ['lib'],
186+
reportOn: [reportOn ?? 'lib'],
186187
basePath: cwd,
187188
);
188189

lib/src/commands/dart/commands/dart_test_command.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class DartTestOptions {
2121
required this.optimizePerformance,
2222
required this.forceAnsi,
2323
required this.rest,
24+
required this.reportOn,
2425
});
2526

2627
/// Parses [ArgResults] into a [DartTestOptions] instance.
@@ -40,6 +41,7 @@ class DartTestOptions {
4041
: randomOrderingSeed;
4142
final optimizePerformance = argResults['optimization'] as bool;
4243
final forceAnsi = argResults['force-ansi'] as bool?;
44+
final reportOn = argResults['report-on'] as String?;
4345
final rest = argResults.rest;
4446

4547
return DartTestOptions._(
@@ -52,6 +54,7 @@ class DartTestOptions {
5254
randomSeed: randomSeed,
5355
optimizePerformance: optimizePerformance,
5456
forceAnsi: forceAnsi,
57+
reportOn: reportOn,
5558
rest: rest,
5659
);
5760
}
@@ -84,6 +87,9 @@ class DartTestOptions {
8487
/// default behavior based on stdout and stderr.
8588
final bool? forceAnsi;
8689

90+
/// An optional file path to report coverage information to.
91+
final String? reportOn;
92+
8793
/// The remaining arguments passed to the `dart test` command.
8894
final List<String> rest;
8995
}
@@ -106,6 +112,7 @@ typedef DartTestCommandCall =
106112
List<String>? arguments,
107113
void Function(String)? stdout,
108114
void Function(String)? stderr,
115+
String? reportOn,
109116
});
110117

111118
/// {@template dart_test_command}
@@ -176,6 +183,13 @@ class DartTestCommand extends Command<int> {
176183
'Whether to force ansi output. If not specified, '
177184
'it will maintain the default behavior based on stdout and stderr.',
178185
negatable: false,
186+
)
187+
..addOption(
188+
'report-on',
189+
help:
190+
'An optional file path to report coverage information to. '
191+
'This should be a path relative to the current working directory.',
192+
valueHelp: 'lib/',
179193
);
180194
}
181195

@@ -234,6 +248,7 @@ This command should be run from the root of your Dart project.''');
234248
...['-j', options.concurrency],
235249
...options.rest,
236250
],
251+
reportOn: options.reportOn,
237252
);
238253
if (results.any((code) => code != ExitCode.success.code)) {
239254
return ExitCode.unavailable.code;

test/src/commands/dart/commands/dart_test_test.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const expectedTestUsage = [
3737
' --min-coverage Whether to enforce a minimum coverage percentage.\n'
3838
' --test-randomize-ordering-seed The seed to randomize the execution order of test cases within test files.\n'
3939
' --force-ansi Whether to force ansi output. If not specified, it will maintain the default behavior based on stdout and stderr.\n'
40+
' --report-on=<lib/> An optional file path to report coverage information to. This should be a path relative to the current working directory.\n'
4041
'\n'
4142
'Run "very_good help" to see global options.',
4243
];
@@ -58,6 +59,7 @@ abstract class DartTestCommandCall {
5859
void Function(String)? stdout,
5960
void Function(String)? stderr,
6061
bool? forceAnsi,
62+
String? reportOn,
6163
});
6264
}
6365

@@ -97,6 +99,7 @@ void main() {
9799
stdout: any(named: 'stdout'),
98100
stderr: any(named: 'stderr'),
99101
forceAnsi: any(named: 'forceAnsi'),
102+
reportOn: any(named: 'reportOn'),
100103
),
101104
).thenAnswer((_) async => [0]);
102105
when<dynamic>(() => argResults['concurrency']).thenReturn(concurrency);
@@ -367,6 +370,28 @@ void main() {
367370
},
368371
);
369372

373+
test(
374+
'reports on a different directory when --report-on is supplied',
375+
() async {
376+
when<dynamic>(() => argResults['min-coverage']).thenReturn('0');
377+
when<dynamic>(() => argResults['report-on']).thenReturn('routes');
378+
final result = await testCommand.run();
379+
expect(result, equals(ExitCode.success.code));
380+
verify(
381+
() => dartTest(
382+
optimizePerformance: true,
383+
collectCoverage: true,
384+
arguments: defaultArguments,
385+
minCoverage: 0,
386+
logger: logger,
387+
stdout: logger.write,
388+
stderr: logger.err,
389+
reportOn: 'routes',
390+
),
391+
).called(1);
392+
},
393+
);
394+
370395
test('fails when coverage not met', () async {
371396
when<dynamic>(() => argResults['coverage']).thenReturn(true);
372397
when<dynamic>(() => argResults['min-coverage']).thenReturn('100');

0 commit comments

Comments
 (0)