From c021d9177c5f9d21051817a34c835e612b7a619d Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Thu, 10 Nov 2022 23:36:02 -0800 Subject: [PATCH] [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 Co-authored-by: Christopher Fujino --- .../flutter_tools/lib/src/commands/test.dart | 22 +++++-- .../commands.shard/hermetic/test_test.dart | 57 +++++++++++++++++++ 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/test.dart b/packages/flutter_tools/lib/src/commands/test.dart index 69240bbaa069..d9148d8345df 100644 --- a/packages/flutter_tools/lib/src/commands/test.dart +++ b/packages/flutter_tools/lib/src/commands/test.dart @@ -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'; @@ -201,12 +199,12 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts { ) ..addOption('reporter', abbr: 'r', - defaultsTo: 'compact', help: 'Set how to print test results.', - allowed: ['compact', 'expanded', 'json'], + allowed: ['compact', 'expanded', 'github', 'json'], allowedHelp: { - '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', }, ) @@ -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 verifyThenRunCommand(String? commandPath) { _testFiles = argResults!.rest.map(globals.fs.path.absolute).toList(); @@ -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, 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 22b737aa9307..d4f3919403eb 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/test_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/test_test.dart @@ -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'; @@ -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 commandRunner = createTestCommandRunner(testCommand); + + await commandRunner.run(const [ + 'test', + '--no-pub', + ]); + + expect( + testRunner.lastReporterOption, + 'github', + ); + }, overrides: { + FileSystem: () => fs, + ProcessManager: () => FakeProcessManager.any(), + Platform: () => FakePlatform( + environment: { + 'GITHUB_ACTIONS': 'true', + }, + ), + DeviceManager: () => _FakeDeviceManager([ + 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 commandRunner = createTestCommandRunner(testCommand); + + await commandRunner.run(const [ + 'test', + '--no-pub', + ]); + + expect( + testRunner.lastReporterOption, + 'compact', + ); + }, overrides: { + FileSystem: () => fs, + ProcessManager: () => FakeProcessManager.any(), + Platform: () => FakePlatform( + environment: {} + ), + DeviceManager: () => _FakeDeviceManager([ + FakeDevice('ephemeral', 'ephemeral', type: PlatformType.android), + ]), + }); + testUsingContext('Integration tests given flavor', () async { final FakeFlutterTestRunner testRunner = FakeFlutterTestRunner(0); @@ -789,6 +844,7 @@ class FakeFlutterTestRunner implements FlutterTestRunner { Duration? leastRunTime; bool? lastEnableObservatoryValue; late DebuggingOptions lastDebuggingOptionsValue; + String? lastReporterOption; @override Future runTests( @@ -824,6 +880,7 @@ class FakeFlutterTestRunner implements FlutterTestRunner { }) async { lastEnableObservatoryValue = enableObservatory; lastDebuggingOptionsValue = debuggingOptions; + lastReporterOption = reporter; if (leastRunTime != null) { await Future.delayed(leastRunTime!);