From 5e448f4ce57723ac0792ae822ebac69df3188ba1 Mon Sep 17 00:00:00 2001 From: Greg Price Date: Mon, 3 Jun 2024 12:44:39 -0700 Subject: [PATCH] Support failures-only and silent reporters in `flutter test` (#148739) Fixes #148738. Also add some tests for this part of the `flutter test` CLI. --- .../flutter_tools/lib/src/commands/test.dart | 6 +- .../commands.shard/hermetic/test_test.dart | 57 +++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/test.dart b/packages/flutter_tools/lib/src/commands/test.dart index ba505dfa5e10..cc95de420f71 100644 --- a/packages/flutter_tools/lib/src/commands/test.dart +++ b/packages/flutter_tools/lib/src/commands/test.dart @@ -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: ['compact', 'expanded', 'github', 'json'], + allowed: ['compact', 'expanded', 'failures-only', 'github', 'json', 'silent'], allowedHelp: { - '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', diff --git a/packages/flutter_tools/test/commands.shard/hermetic/test_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/test_test.dart index 7ddef36ef623..d3d955d98ed6 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/test_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/test_test.dart @@ -283,6 +283,63 @@ dev_dependencies: }); }); + group('--reporter/-r', () { + String? passedReporter(List 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 expectPassesReporter(String value) async { + final FakePackageTest fakePackageTest = FakePackageTest(); + final TestCommand testCommand = TestCommand(testWrapper: fakePackageTest); + final CommandRunner commandRunner = createTestCommandRunner(testCommand); + + await commandRunner.run(['test', '--no-pub', '-r', value]); + expect(passedReporter(fakePackageTest.lastArgs!), equals(value)); + + await commandRunner.run(['test', '--no-pub', '-r$value']); + expect(passedReporter(fakePackageTest.lastArgs!), equals(value)); + + await commandRunner.run(['test', '--no-pub', '--reporter', value]); + expect(passedReporter(fakePackageTest.lastArgs!), equals(value)); + + await commandRunner.run(['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: { + 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 commandRunner = createTestCommandRunner(testCommand); + + await commandRunner.run(['test', '--no-pub']); + expect(passedReporter(fakePackageTest.lastArgs!), isNull); + }, overrides: { + FileSystem: () => fs, + ProcessManager: () => FakeProcessManager.any(), + Cache: () => Cache.test(processManager: FakeProcessManager.any()), + }); + }); + testUsingContext('Supports coverage and machine', () async { final FakePackageTest fakePackageTest = FakePackageTest();