Skip to content

Commit c021d91

Browse files
[flutter_tools] support github reporter (#115137)
* [flutter_tools] support github reporter * Update packages/flutter_tools/lib/src/commands/test.dart Co-authored-by: Christopher Fujino <[email protected]> Co-authored-by: Christopher Fujino <[email protected]>
1 parent 83cda7e commit c021d91

File tree

2 files changed

+73
-6
lines changed

2 files changed

+73
-6
lines changed

packages/flutter_tools/lib/src/commands/test.dart

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
6-
75
import 'dart:math' as math;
86

97
import 'package:meta/meta.dart';
@@ -201,12 +199,12 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
201199
)
202200
..addOption('reporter',
203201
abbr: 'r',
204-
defaultsTo: 'compact',
205202
help: 'Set how to print test results.',
206-
allowed: <String>['compact', 'expanded', 'json'],
203+
allowed: <String>['compact', 'expanded', 'github', 'json'],
207204
allowedHelp: <String, String>{
208-
'compact': 'A single line that updates dynamically.',
205+
'compact': 'A single line that updates dynamically (The default reporter).',
209206
'expanded': 'A separate line for each update. May be preferred when logging to a file or in continuous integration.',
207+
'github': 'A custom reporter for GitHub Actions (the default reporter when running on GitHub Actions).',
210208
'json': 'A machine-readable format. See: https://dart.dev/go/test-docs/json_reporter.md',
211209
},
212210
)
@@ -256,6 +254,18 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
256254
@override
257255
String get category => FlutterCommandCategory.project;
258256

257+
// Lookup the default reporter if one was not specified.
258+
String _getReporter() {
259+
final String? reporter = stringArgDeprecated('reporter');
260+
if (reporter != null) {
261+
return reporter;
262+
}
263+
if (globals.platform.environment['GITHUB_ACTIONS']?.toLowerCase() == 'true') {
264+
return 'github';
265+
}
266+
return 'compact';
267+
}
268+
259269
@override
260270
Future<FlutterCommandResult> verifyThenRunCommand(String? commandPath) {
261271
_testFiles = argResults!.rest.map<String>(globals.fs.path.absolute).toList();
@@ -463,7 +473,7 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
463473
flutterProject: flutterProject,
464474
web: stringArgDeprecated('platform') == 'chrome',
465475
randomSeed: stringArgDeprecated('test-randomize-ordering-seed'),
466-
reporter: stringArgDeprecated('reporter'),
476+
reporter: _getReporter(),
467477
timeout: stringArgDeprecated('timeout'),
468478
runSkipped: boolArgDeprecated('run-skipped'),
469479
shardIndex: shardIndex,

packages/flutter_tools/test/commands.shard/hermetic/test_test.dart

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:file/memory.dart';
1010
import 'package:flutter_tools/src/base/common.dart';
1111
import 'package:flutter_tools/src/base/file_system.dart';
1212
import 'package:flutter_tools/src/base/logger.dart';
13+
import 'package:flutter_tools/src/base/platform.dart';
1314
import 'package:flutter_tools/src/cache.dart';
1415
import 'package:flutter_tools/src/commands/test.dart';
1516
import 'package:flutter_tools/src/device.dart';
@@ -660,6 +661,60 @@ dev_dependencies:
660661
]),
661662
});
662663

664+
testUsingContext('Tests on github actions default to github reporter', () async {
665+
final FakeFlutterTestRunner testRunner = FakeFlutterTestRunner(0);
666+
667+
final TestCommand testCommand = TestCommand(testRunner: testRunner);
668+
final CommandRunner<void> commandRunner = createTestCommandRunner(testCommand);
669+
670+
await commandRunner.run(const <String>[
671+
'test',
672+
'--no-pub',
673+
]);
674+
675+
expect(
676+
testRunner.lastReporterOption,
677+
'github',
678+
);
679+
}, overrides: <Type, Generator>{
680+
FileSystem: () => fs,
681+
ProcessManager: () => FakeProcessManager.any(),
682+
Platform: () => FakePlatform(
683+
environment: <String, String>{
684+
'GITHUB_ACTIONS': 'true',
685+
},
686+
),
687+
DeviceManager: () => _FakeDeviceManager(<Device>[
688+
FakeDevice('ephemeral', 'ephemeral', type: PlatformType.android),
689+
]),
690+
});
691+
692+
testUsingContext('Tests default to compact reporter if not specified and not on Github actions', () async {
693+
final FakeFlutterTestRunner testRunner = FakeFlutterTestRunner(0);
694+
695+
final TestCommand testCommand = TestCommand(testRunner: testRunner);
696+
final CommandRunner<void> commandRunner = createTestCommandRunner(testCommand);
697+
698+
await commandRunner.run(const <String>[
699+
'test',
700+
'--no-pub',
701+
]);
702+
703+
expect(
704+
testRunner.lastReporterOption,
705+
'compact',
706+
);
707+
}, overrides: <Type, Generator>{
708+
FileSystem: () => fs,
709+
ProcessManager: () => FakeProcessManager.any(),
710+
Platform: () => FakePlatform(
711+
environment: <String, String>{}
712+
),
713+
DeviceManager: () => _FakeDeviceManager(<Device>[
714+
FakeDevice('ephemeral', 'ephemeral', type: PlatformType.android),
715+
]),
716+
});
717+
663718
testUsingContext('Integration tests given flavor', () async {
664719
final FakeFlutterTestRunner testRunner = FakeFlutterTestRunner(0);
665720

@@ -789,6 +844,7 @@ class FakeFlutterTestRunner implements FlutterTestRunner {
789844
Duration? leastRunTime;
790845
bool? lastEnableObservatoryValue;
791846
late DebuggingOptions lastDebuggingOptionsValue;
847+
String? lastReporterOption;
792848

793849
@override
794850
Future<int> runTests(
@@ -824,6 +880,7 @@ class FakeFlutterTestRunner implements FlutterTestRunner {
824880
}) async {
825881
lastEnableObservatoryValue = enableObservatory;
826882
lastDebuggingOptionsValue = debuggingOptions;
883+
lastReporterOption = reporter;
827884

828885
if (leastRunTime != null) {
829886
await Future<void>.delayed(leastRunTime!);

0 commit comments

Comments
 (0)