route CLI command usage information through the logger instead of using print (#161533)

Fixes https://github.com/flutter/flutter/issues/158532


<details>

<summary> Pre-launch checklist </summary> 


- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

</details>


<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
This commit is contained in:
Andrew Kolos
2025-01-16 13:53:58 -08:00
committed by GitHub
parent cbe269808b
commit 5a3541c40c
6 changed files with 30 additions and 60 deletions

View File

@@ -25,10 +25,6 @@ import '../features.dart';
import '../runner/flutter_command.dart';
import '../runner/flutter_command_runner.dart';
/// just the function signature of the [print] function.
/// The Object arg may be null.
typedef PrintFn = void Function(Object);
class CustomDevicesCommand extends FlutterCommand {
factory CustomDevicesCommand({
required CustomDevicesConfig customDevicesConfig,
@@ -62,7 +58,6 @@ class CustomDevicesCommand extends FlutterCommand {
required FileSystem fileSystem,
required Logger logger,
required FeatureFlags featureFlags,
PrintFn usagePrintFn = print,
}) {
return CustomDevicesCommand._common(
customDevicesConfig: customDevicesConfig,
@@ -73,7 +68,6 @@ class CustomDevicesCommand extends FlutterCommand {
fileSystem: fileSystem,
logger: logger,
featureFlags: featureFlags,
usagePrintFn: usagePrintFn,
);
}
@@ -86,10 +80,8 @@ class CustomDevicesCommand extends FlutterCommand {
required FileSystem fileSystem,
required Logger logger,
required FeatureFlags featureFlags,
PrintFn usagePrintFn = print,
}) : _customDevicesConfig = customDevicesConfig,
_featureFlags = featureFlags,
_usagePrintFn = usagePrintFn {
_featureFlags = featureFlags {
addSubcommand(
CustomDevicesListCommand(
customDevicesConfig: customDevicesConfig,
@@ -129,7 +121,6 @@ class CustomDevicesCommand extends FlutterCommand {
final CustomDevicesConfig _customDevicesConfig;
final FeatureFlags _featureFlags;
final PrintFn _usagePrintFn;
@override
String get description {
@@ -163,11 +154,6 @@ Requires the custom devices feature to be enabled. You can enable it using "flut
Future<FlutterCommandResult> runCommand() async {
return FlutterCommandResult.success();
}
@override
void printUsage() {
_usagePrintFn(usage);
}
}
/// This class is meant to provide some commonly used utility functions

View File

@@ -22,11 +22,8 @@ import '../project.dart';
import '../reporting/reporting.dart';
import '../runner/flutter_command.dart';
/// The function signature of the [print] function.
typedef PrintFn = void Function(Object?);
class PackagesCommand extends FlutterCommand {
PackagesCommand({PrintFn usagePrintFn = print}) : _usagePrintFn = usagePrintFn {
PackagesCommand() {
addSubcommand(
PackagesGetCommand('get', "Get the current package's dependencies.", PubContext.pubGet),
);
@@ -87,8 +84,6 @@ class PackagesCommand extends FlutterCommand {
addSubcommand(PackagesPassthroughCommand());
}
final PrintFn _usagePrintFn;
@override
final String name = 'pub';
@@ -103,9 +98,6 @@ class PackagesCommand extends FlutterCommand {
@override
Future<FlutterCommandResult> runCommand() async => FlutterCommandResult.fail();
@override
void printUsage() => _usagePrintFn(usage);
}
class PackagesTestCommand extends FlutterCommand {

View File

@@ -1575,6 +1575,11 @@ abstract class FlutterCommand extends Command<void> {
);
}
@override
void printUsage() {
globals.logger.printStatus(usage);
}
@visibleForOverriding
String get deprecationWarning {
return '${globals.logger.terminal.warningMark} The "$name" command is '

View File

@@ -293,13 +293,11 @@ CustomDevicesCommand createCustomDevicesCommand({
FileSystem? fileSystem,
ProcessManager? processManager,
Logger? logger,
PrintFn? usagePrintFn,
bool featureEnabled = false,
}) {
platform ??= FakePlatform();
processManager ??= FakeProcessManager.any();
fileSystem ??= MemoryFileSystem.test();
usagePrintFn ??= print;
logger ??= BufferLogger.test();
return CustomDevicesCommand.test(
@@ -328,7 +326,6 @@ CustomDevicesCommand createCustomDevicesCommand({
processManager: processManager,
fileSystem: fileSystem,
logger: logger,
usagePrintFn: usagePrintFn,
);
}
@@ -341,7 +338,6 @@ CommandRunner<void> createCustomDevicesCommandRunner({
FileSystem? fileSystem,
ProcessManager? processManager,
Logger? logger,
PrintFn? usagePrintFn,
bool featureEnabled = false,
}) {
platform ??= FakePlatform();
@@ -356,7 +352,6 @@ CommandRunner<void> createCustomDevicesCommandRunner({
fileSystem: fileSystem,
processManager: processManager,
logger: logger,
usagePrintFn: usagePrintFn,
featureEnabled: featureEnabled,
),
);
@@ -408,7 +403,6 @@ void main() {
final CommandRunner<void> runner = createCustomDevicesCommandRunner(
logger: logger,
usagePrintFn: (Object o) => logger.printStatus(o.toString()),
featureEnabled: true,
);
await expectLater(runner.run(const <String>['custom-devices', '--help']), completes);
@@ -424,7 +418,6 @@ void main() {
final CommandRunner<void> runner = createCustomDevicesCommandRunner(
logger: logger,
usagePrintFn: (Object o) => logger.printStatus(o.toString()),
featureEnabled: true,
);

View File

@@ -589,31 +589,25 @@ void main() {
},
);
testUsingContext('flutter drive --help explains how to use the command', () async {
final DriveCommand command = DriveCommand(
fileSystem: fileSystem,
logger: logger,
platform: platform,
signals: signals,
);
testUsingContext(
'flutter drive --help explains how to use the command',
() async {
final DriveCommand command = DriveCommand(
fileSystem: fileSystem,
logger: logger,
platform: platform,
signals: signals,
);
// TODO(matanlurey): https://github.com/flutter/flutter/issues/158532.
final StringBuffer printOutput = StringBuffer();
final Zone capturePrintZone = Zone.current.fork(
specification: ZoneSpecification(
print: (_, _, _, String line) {
printOutput.writeln(line);
},
),
);
await capturePrintZone.run(() async {
await createTestCommandRunner(command).run(<String>['drive', '--help']);
});
expect(
printOutput.toString(),
stringContainsInOrder(<String>['flutter drive', '--target', '--driver']),
);
});
expect(
logger.statusText,
stringContainsInOrder(<String>['flutter drive', '--target', '--driver']),
);
},
overrides: <Type, Generator>{Logger: () => logger},
);
}
class ThrowingScreenshotDevice extends ScreenshotDevice {

View File

@@ -5,6 +5,7 @@
import 'package:args/command_runner.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/packages.dart';
import 'package:flutter_tools/src/dart/pub.dart';
@@ -33,6 +34,7 @@ const String minimalV2EmbeddingManifest = r'''
void main() {
late FileSystem fileSystem;
late FakePub pub;
late BufferLogger logger;
// TODO(matanlurey): Remove after `flutter_gen` is removed.
// See https://github.com/flutter/flutter/issues/102983 for details.
@@ -47,6 +49,7 @@ void main() {
Cache.disableLocking();
fileSystem = MemoryFileSystem.test();
pub = FakePub(fileSystem);
logger = BufferLogger.test();
});
tearDown(() {
@@ -54,21 +57,18 @@ void main() {
});
testUsingContext('pub shows help', () async {
Object? usage;
final PackagesCommand command = PackagesCommand(
usagePrintFn: (Object? object) => usage = object,
);
final PackagesCommand command = PackagesCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['pub']);
expect(
usage,
logger.statusText,
allOf(
contains('Commands for managing Flutter packages.'),
contains('Usage: flutter pub <subcommand> [arguments]'),
),
);
});
}, overrides: <Type, Generator>{Logger: () => logger});
testUsingContext(
'pub get usage values are resilient to missing package config files before running "pub get"',