Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
[flutter_tools] add deprecation message for "flutter format" (#116145)
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherfujino committed Nov 30, 2022
1 parent a29796e commit 2ef2cc8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
11 changes: 11 additions & 0 deletions packages/flutter_tools/lib/src/commands/format.dart
Expand Up @@ -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;
Expand Down
15 changes: 9 additions & 6 deletions packages/flutter_tools/lib/src/runner/flutter_command.dart
Expand Up @@ -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);
}
}

Expand Down
Expand Up @@ -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);

Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 2ef2cc8

Please sign in to comment.