[flutter_tools] add deprecation message for "flutter format" (#116145)

This commit is contained in:
Christopher Fujino 2022-11-30 14:00:01 -08:00 committed by GitHub
parent a29796e339
commit 2ef2cc89e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 11 deletions

View File

@ -32,6 +32,17 @@ class FormatCommand extends FlutterCommand {
@override
String get invocation => '${runner?.executableName} $name <one or more paths>';
@override
final bool deprecated = true;
@override
String get deprecationWarning {
return '${globals.logger.terminal.warningMark} The "format" command is '
'deprecated and will be removed in a future version of Flutter. '
'Please use the "dart format" sub-command instead, which takes all '
'of the same command-line arguments as "flutter format".\n';
}
@override
Future<FlutterCommandResult> runCommand() async {
final String dartBinary = globals.artifacts!.getHostArtifact(HostArtifact.engineDartBinary).path;

View File

@ -1269,14 +1269,17 @@ abstract class FlutterCommand extends Command<void> {
);
}
@visibleForOverriding
String get deprecationWarning {
return '${globals.logger.terminal.warningMark} The "$name" command is '
'deprecated and will be removed in a future version of Flutter. '
'See https://flutter.dev/docs/development/tools/sdk/releases '
'for previous releases of Flutter.\n';
}
void _printDeprecationWarning() {
if (deprecated) {
globals.printWarning(
'${globals.logger.terminal.warningMark} The "$name" command is deprecated and '
'will be removed in a future version of Flutter. '
'See https://flutter.dev/docs/development/tools/sdk/releases '
'for previous releases of Flutter.\n',
);
globals.printWarning(deprecationWarning);
}
}

View File

@ -4,31 +4,48 @@
import 'package:args/command_runner.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/format.dart';
import 'package:flutter_tools/src/globals.dart' as globals;
import '../../src/common.dart';
import '../../src/context.dart';
import '../../src/fakes.dart';
import '../../src/test_flutter_command_runner.dart';
void main() {
group('format', () {
late Directory tempDir;
late FakeStdio mockStdio;
late BufferLogger logger;
setUp(() {
Cache.disableLocking();
tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_tools_format_test.');
mockStdio = FakeStdio();
logger = BufferLogger.test();
});
tearDown(() {
tryToDelete(tempDir);
});
testUsingContext('shows deprecation warning', () async {
final String projectPath = await createProject(tempDir);
final File srcFile = globals.fs.file(globals.fs.path.join(projectPath, 'lib', 'main.dart'));
final String original = srcFile.readAsStringSync();
srcFile.writeAsStringSync(original);
final FormatCommand command = FormatCommand(verboseHelp: false);
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['format', srcFile.path]);
expect(
logger.warningText,
contains('The "format" command is deprecated and will be removed in a future version of Flutter'),
);
}, overrides: <Type, Generator>{
Logger: () => logger,
});
testUsingContext('a file', () async {
final String projectPath = await createProject(tempDir);
@ -43,7 +60,7 @@ void main() {
final String formatted = srcFile.readAsStringSync();
expect(formatted, original);
}, overrides: <Type, Generator>{
Stdio: () => mockStdio,
Logger: () => logger,
});
testUsingContext('dry-run', () async {
@ -61,6 +78,8 @@ void main() {
final String shouldNotFormatted = srcFile.readAsStringSync();
expect(shouldNotFormatted, nonFormatted);
}, overrides: <Type, Generator>{
Logger: () => logger,
});
testUsingContext('dry-run with -n', () async {