Unverified Commit 7e151b46 authored by Christopher Fujino's avatar Christopher Fujino Committed by GitHub

[flutter_tools] throw tool exit on invocation of flutter format (#121646)

[flutter_tools] throw tool exit on invocation of flutter format
parent 7003bfab
......@@ -199,7 +199,7 @@ List<FlutterCommand> generateCommands({
signals: globals.signals,
),
EmulatorsCommand(),
FormatCommand(verboseHelp: verboseHelp),
FormatCommand(),
GenerateCommand(),
GenerateLocalizationsCommand(
fileSystem: globals.fs,
......
......@@ -4,19 +4,15 @@
import 'package:args/args.dart';
import '../artifacts.dart';
import '../base/common.dart';
import '../globals.dart' as globals;
import '../runner/flutter_command.dart';
class FormatCommand extends FlutterCommand {
FormatCommand({required this.verboseHelp});
FormatCommand();
@override
ArgParser argParser = ArgParser.allowAnything();
final bool verboseHelp;
@override
final String name = 'format';
......@@ -24,53 +20,20 @@ class FormatCommand extends FlutterCommand {
List<String> get aliases => const <String>['dartfmt'];
@override
final String description = 'Format one or more Dart files.';
@override
String get category => FlutterCommandCategory.project;
String get description => deprecationWarning;
@override
String get invocation => '${runner?.executableName} $name <one or more paths>';
@override
final bool deprecated = true;
final bool hidden = 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';
return 'The "format" command is deprecated. Please use the "dart format" '
'sub-command instead, which has the same command-line usage as '
'"flutter format".\n';
}
@override
Future<FlutterCommandResult> runCommand() async {
final String dartBinary = globals.artifacts!.getArtifactPath(Artifact.engineDartBinary);
final List<String> command = <String>[
dartBinary,
'format',
];
final List<String> rest = argResults?.rest ?? <String>[];
if (rest.isEmpty) {
globals.printError(
'No files specified to be formatted.'
);
command.add('-h');
} else {
command.addAll(<String>[
for (String arg in rest)
if (arg == '--dry-run' || arg == '-n')
'--output=none'
else
arg,
]);
}
final int result = await globals.processUtils.stream(command);
if (result != 0) {
throwToolExit('Formatting failed: $result', exitCode: result);
}
return FlutterCommandResult.success();
throwToolExit(deprecationWarning);
}
}
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:args/command_runner.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/format.dart';
import 'package:flutter_tools/src/globals.dart' as globals;
import '../../src/common.dart';
import '../../src/context.dart';
import '../../src/test_flutter_command_runner.dart';
void main() {
group('format', () {
late Directory tempDir;
late BufferLogger logger;
setUp(() {
Cache.disableLocking();
tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_tools_format_test.');
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);
final File srcFile = globals.fs.file(globals.fs.path.join(projectPath, 'lib', 'main.dart'));
final String original = srcFile.readAsStringSync();
srcFile.writeAsStringSync(original.replaceFirst('main()', 'main( )'));
final FormatCommand command = FormatCommand(verboseHelp: false);
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['format', srcFile.path]);
final String formatted = srcFile.readAsStringSync();
expect(formatted, original);
}, overrides: <Type, Generator>{
Logger: () => logger,
});
testUsingContext('dry-run', () async {
final String projectPath = await createProject(tempDir);
final File srcFile = globals.fs.file(
globals.fs.path.join(projectPath, 'lib', 'main.dart'));
final String nonFormatted = srcFile.readAsStringSync().replaceFirst(
'main()', 'main( )');
srcFile.writeAsStringSync(nonFormatted);
final FormatCommand command = FormatCommand(verboseHelp: false);
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['format', '--dry-run', srcFile.path]);
final String shouldNotFormatted = srcFile.readAsStringSync();
expect(shouldNotFormatted, nonFormatted);
}, overrides: <Type, Generator>{
Logger: () => logger,
});
testUsingContext('dry-run with -n', () async {
final String projectPath = await createProject(tempDir);
final File srcFile = globals.fs.file(
globals.fs.path.join(projectPath, 'lib', 'main.dart'));
final String nonFormatted = srcFile.readAsStringSync().replaceFirst(
'main()', 'main( )');
srcFile.writeAsStringSync(nonFormatted);
final FormatCommand command = FormatCommand(verboseHelp: false);
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['format', '-n', srcFile.path]);
final String shouldNotFormatted = srcFile.readAsStringSync();
expect(shouldNotFormatted, nonFormatted);
});
testUsingContext('dry-run with set-exit-if-changed', () async {
final String projectPath = await createProject(tempDir);
final File srcFile = globals.fs.file(
globals.fs.path.join(projectPath, 'lib', 'main.dart'));
final String nonFormatted = srcFile.readAsStringSync().replaceFirst(
'main()', 'main( )');
srcFile.writeAsStringSync(nonFormatted);
final FormatCommand command = FormatCommand(verboseHelp: false);
final CommandRunner<void> runner = createTestCommandRunner(command);
expect(runner.run(<String>[
'format', '--dry-run', '--set-exit-if-changed', srcFile.path,
]), throwsException);
final String shouldNotFormatted = srcFile.readAsStringSync();
expect(shouldNotFormatted, nonFormatted);
});
testUsingContext('line-length', () async {
const int lineLengthShort = 50;
const int lineLengthLong = 120;
final String projectPath = await createProject(tempDir);
final File srcFile = globals.fs.file(
globals.fs.path.join(projectPath, 'lib', 'main.dart'));
final String nonFormatted = srcFile.readAsStringSync();
srcFile.writeAsStringSync(
nonFormatted.replaceFirst('main()',
'main(anArgument1, anArgument2, anArgument3, anArgument4, anArgument5)'));
final String nonFormattedWithLongLine = srcFile.readAsStringSync();
final FormatCommand command = FormatCommand(verboseHelp: false);
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['format', '--line-length', '$lineLengthLong', srcFile.path]);
final String notFormatted = srcFile.readAsStringSync();
expect(nonFormattedWithLongLine, notFormatted);
await runner.run(<String>['format', '--line-length', '$lineLengthShort', srcFile.path]);
final String shouldFormatted = srcFile.readAsStringSync();
expect(nonFormattedWithLongLine, isNot(shouldFormatted));
});
});
}
......@@ -23,7 +23,7 @@ void main() {
).forEach(runner.addCommand);
verifyCommandRunner(runner);
for (final Command<void> command in runner.commands.values) {
if(command.name == 'analyze') {
if (command.name == 'analyze') {
final AnalyzeCommand analyze = command as AnalyzeCommand;
expect(analyze.allProjectValidators().length, 2);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment