Skip to content

Commit

Permalink
[flutter_tools] support github reporter (#115137)
Browse files Browse the repository at this point in the history
* [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]>
  • Loading branch information
jonahwilliams and christopherfujino authored Nov 11, 2022
1 parent 83cda7e commit c021d91
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
22 changes: 16 additions & 6 deletions packages/flutter_tools/lib/src/commands/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.



import 'dart:math' as math;

import 'package:meta/meta.dart';
Expand Down Expand Up @@ -201,12 +199,12 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
)
..addOption('reporter',
abbr: 'r',
defaultsTo: 'compact',
help: 'Set how to print test results.',
allowed: <String>['compact', 'expanded', 'json'],
allowed: <String>['compact', 'expanded', 'github', 'json'],
allowedHelp: <String, String>{
'compact': 'A single line that updates dynamically.',
'compact': 'A single line that updates dynamically (The default reporter).',
'expanded': 'A separate line for each update. May be preferred when logging to a file or in continuous integration.',
'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',
},
)
Expand Down Expand Up @@ -256,6 +254,18 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
@override
String get category => FlutterCommandCategory.project;

// Lookup the default reporter if one was not specified.
String _getReporter() {
final String? reporter = stringArgDeprecated('reporter');
if (reporter != null) {
return reporter;
}
if (globals.platform.environment['GITHUB_ACTIONS']?.toLowerCase() == 'true') {
return 'github';
}
return 'compact';
}

@override
Future<FlutterCommandResult> verifyThenRunCommand(String? commandPath) {
_testFiles = argResults!.rest.map<String>(globals.fs.path.absolute).toList();
Expand Down Expand Up @@ -463,7 +473,7 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
flutterProject: flutterProject,
web: stringArgDeprecated('platform') == 'chrome',
randomSeed: stringArgDeprecated('test-randomize-ordering-seed'),
reporter: stringArgDeprecated('reporter'),
reporter: _getReporter(),
timeout: stringArgDeprecated('timeout'),
runSkipped: boolArgDeprecated('run-skipped'),
shardIndex: shardIndex,
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 @@ -10,6 +10,7 @@ import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/common.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/test.dart';
import 'package:flutter_tools/src/device.dart';
Expand Down Expand Up @@ -660,6 +661,60 @@ dev_dependencies:
]),
});

testUsingContext('Tests on github actions default to github reporter', () async {
final FakeFlutterTestRunner testRunner = FakeFlutterTestRunner(0);

final TestCommand testCommand = TestCommand(testRunner: testRunner);
final CommandRunner<void> commandRunner = createTestCommandRunner(testCommand);

await commandRunner.run(const <String>[
'test',
'--no-pub',
]);

expect(
testRunner.lastReporterOption,
'github',
);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
ProcessManager: () => FakeProcessManager.any(),
Platform: () => FakePlatform(
environment: <String, String>{
'GITHUB_ACTIONS': 'true',
},
),
DeviceManager: () => _FakeDeviceManager(<Device>[
FakeDevice('ephemeral', 'ephemeral', type: PlatformType.android),
]),
});

testUsingContext('Tests default to compact reporter if not specified and not on Github actions', () async {
final FakeFlutterTestRunner testRunner = FakeFlutterTestRunner(0);

final TestCommand testCommand = TestCommand(testRunner: testRunner);
final CommandRunner<void> commandRunner = createTestCommandRunner(testCommand);

await commandRunner.run(const <String>[
'test',
'--no-pub',
]);

expect(
testRunner.lastReporterOption,
'compact',
);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
ProcessManager: () => FakeProcessManager.any(),
Platform: () => FakePlatform(
environment: <String, String>{}
),
DeviceManager: () => _FakeDeviceManager(<Device>[
FakeDevice('ephemeral', 'ephemeral', type: PlatformType.android),
]),
});

testUsingContext('Integration tests given flavor', () async {
final FakeFlutterTestRunner testRunner = FakeFlutterTestRunner(0);

Expand Down Expand Up @@ -789,6 +844,7 @@ class FakeFlutterTestRunner implements FlutterTestRunner {
Duration? leastRunTime;
bool? lastEnableObservatoryValue;
late DebuggingOptions lastDebuggingOptionsValue;
String? lastReporterOption;

@override
Future<int> runTests(
Expand Down Expand Up @@ -824,6 +880,7 @@ class FakeFlutterTestRunner implements FlutterTestRunner {
}) async {
lastEnableObservatoryValue = enableObservatory;
lastDebuggingOptionsValue = debuggingOptions;
lastReporterOption = reporter;

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

0 comments on commit c021d91

Please sign in to comment.