Skip to content

Commit

Permalink
Support failures-only and silent reporters in flutter test (#148739)
Browse files Browse the repository at this point in the history
Fixes #148738.

Also add some tests for this part of the `flutter test` CLI.
  • Loading branch information
gnprice authored Jun 3, 2024
1 parent 691a18d commit 5e448f4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/flutter_tools/lib/src/commands/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,14 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
..addOption('reporter',
abbr: 'r',
help: 'Set how to print test results. If unset, value will default to either compact or expanded.',
allowed: <String>['compact', 'expanded', 'github', 'json'],
allowed: <String>['compact', 'expanded', 'failures-only', 'github', 'json', 'silent'],
allowedHelp: <String, String>{
'compact': 'A single line that updates dynamically (The default reporter).',
'compact': 'A single line, updated continuously (the default).',
'expanded': 'A separate line for each update. May be preferred when logging to a file or in continuous integration.',
'failures-only': 'A separate line for failing tests, with no output for passing tests.',
'github': 'A custom reporter for GitHub Actions (the default reporter when running on GitHub Actions).',
'json': 'A machine-readable format. See: https://dart.dev/go/test-docs/json_reporter.md',
'silent': 'A reporter with no output. May be useful when only the exit code is meaningful.'
},
)
..addOption('file-reporter',
Expand Down
57 changes: 57 additions & 0 deletions packages/flutter_tools/test/commands.shard/hermetic/test_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,63 @@ dev_dependencies:
});
});

group('--reporter/-r', () {
String? passedReporter(List<String> args) {
final int i = args.indexOf('-r');
if (i < 0) {
expect(args, isNot(contains('--reporter')));
expect(args, isNot(contains(matches(RegExp(r'^(-r|--reporter=)')))));
return null;
} else {
return args[i+1];
}
}

Future<void> expectPassesReporter(String value) async {
final FakePackageTest fakePackageTest = FakePackageTest();
final TestCommand testCommand = TestCommand(testWrapper: fakePackageTest);
final CommandRunner<void> commandRunner = createTestCommandRunner(testCommand);

await commandRunner.run(<String>['test', '--no-pub', '-r', value]);
expect(passedReporter(fakePackageTest.lastArgs!), equals(value));

await commandRunner.run(<String>['test', '--no-pub', '-r$value']);
expect(passedReporter(fakePackageTest.lastArgs!), equals(value));

await commandRunner.run(<String>['test', '--no-pub', '--reporter', value]);
expect(passedReporter(fakePackageTest.lastArgs!), equals(value));

await commandRunner.run(<String>['test', '--no-pub', '--reporter=$value']);
expect(passedReporter(fakePackageTest.lastArgs!), equals(value));
}

testUsingContext('accepts valid values and passes them through', () async {
await expectPassesReporter('compact');
await expectPassesReporter('expanded');
await expectPassesReporter('failures-only');
await expectPassesReporter('github');
await expectPassesReporter('json');
await expectPassesReporter('silent');
}, overrides: <Type, Generator>{
FileSystem: () => fs,
ProcessManager: () => FakeProcessManager.any(),
Cache: () => Cache.test(processManager: FakeProcessManager.any()),
});

testUsingContext('by default, passes no reporter', () async {
final FakePackageTest fakePackageTest = FakePackageTest();
final TestCommand testCommand = TestCommand(testWrapper: fakePackageTest);
final CommandRunner<void> commandRunner = createTestCommandRunner(testCommand);

await commandRunner.run(<String>['test', '--no-pub']);
expect(passedReporter(fakePackageTest.lastArgs!), isNull);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
ProcessManager: () => FakeProcessManager.any(),
Cache: () => Cache.test(processManager: FakeProcessManager.any()),
});
});

testUsingContext('Supports coverage and machine', () async {
final FakePackageTest fakePackageTest = FakePackageTest();

Expand Down

0 comments on commit 5e448f4

Please sign in to comment.