diff --git a/packages/flutter_tools/lib/src/commands/format.dart b/packages/flutter_tools/lib/src/commands/format.dart index 69f4ef4988ae..07b0077cf798 100644 --- a/packages/flutter_tools/lib/src/commands/format.dart +++ b/packages/flutter_tools/lib/src/commands/format.dart @@ -32,6 +32,17 @@ class FormatCommand extends FlutterCommand { @override String get invocation => '${runner?.executableName} $name '; + @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 runCommand() async { final String dartBinary = globals.artifacts!.getHostArtifact(HostArtifact.engineDartBinary).path; diff --git a/packages/flutter_tools/lib/src/runner/flutter_command.dart b/packages/flutter_tools/lib/src/runner/flutter_command.dart index 9d7ba798a0b3..e63a940c4101 100644 --- a/packages/flutter_tools/lib/src/runner/flutter_command.dart +++ b/packages/flutter_tools/lib/src/runner/flutter_command.dart @@ -1269,14 +1269,17 @@ abstract class FlutterCommand extends Command { ); } + @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); } } diff --git a/packages/flutter_tools/test/commands.shard/permeable/format_test.dart b/packages/flutter_tools/test/commands.shard/permeable/format_test.dart index 8180d2f7b962..f289440eb9a0 100644 --- a/packages/flutter_tools/test/commands.shard/permeable/format_test.dart +++ b/packages/flutter_tools/test/commands.shard/permeable/format_test.dart @@ -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 runner = createTestCommandRunner(command); + await runner.run(['format', srcFile.path]); + expect( + logger.warningText, + contains('The "format" command is deprecated and will be removed in a future version of Flutter'), + ); + }, overrides: { + 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: { - Stdio: () => mockStdio, + Logger: () => logger, }); testUsingContext('dry-run', () async { @@ -61,6 +78,8 @@ void main() { final String shouldNotFormatted = srcFile.readAsStringSync(); expect(shouldNotFormatted, nonFormatted); + }, overrides: { + Logger: () => logger, }); testUsingContext('dry-run with -n', () async {