[flutter_tools] throw tool exit on invocation of flutter format (#121646)
[flutter_tools] throw tool exit on invocation of flutter format
This commit is contained in:
parent
7003bfab31
commit
7e151b4641
@ -199,7 +199,7 @@ List<FlutterCommand> generateCommands({
|
|||||||
signals: globals.signals,
|
signals: globals.signals,
|
||||||
),
|
),
|
||||||
EmulatorsCommand(),
|
EmulatorsCommand(),
|
||||||
FormatCommand(verboseHelp: verboseHelp),
|
FormatCommand(),
|
||||||
GenerateCommand(),
|
GenerateCommand(),
|
||||||
GenerateLocalizationsCommand(
|
GenerateLocalizationsCommand(
|
||||||
fileSystem: globals.fs,
|
fileSystem: globals.fs,
|
||||||
|
@ -4,19 +4,15 @@
|
|||||||
|
|
||||||
import 'package:args/args.dart';
|
import 'package:args/args.dart';
|
||||||
|
|
||||||
import '../artifacts.dart';
|
|
||||||
import '../base/common.dart';
|
import '../base/common.dart';
|
||||||
import '../globals.dart' as globals;
|
|
||||||
import '../runner/flutter_command.dart';
|
import '../runner/flutter_command.dart';
|
||||||
|
|
||||||
class FormatCommand extends FlutterCommand {
|
class FormatCommand extends FlutterCommand {
|
||||||
FormatCommand({required this.verboseHelp});
|
FormatCommand();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
ArgParser argParser = ArgParser.allowAnything();
|
ArgParser argParser = ArgParser.allowAnything();
|
||||||
|
|
||||||
final bool verboseHelp;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
final String name = 'format';
|
final String name = 'format';
|
||||||
|
|
||||||
@ -24,53 +20,20 @@ class FormatCommand extends FlutterCommand {
|
|||||||
List<String> get aliases => const <String>['dartfmt'];
|
List<String> get aliases => const <String>['dartfmt'];
|
||||||
|
|
||||||
@override
|
@override
|
||||||
final String description = 'Format one or more Dart files.';
|
String get description => deprecationWarning;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String get category => FlutterCommandCategory.project;
|
final bool hidden = true;
|
||||||
|
|
||||||
@override
|
|
||||||
String get invocation => '${runner?.executableName} $name <one or more paths>';
|
|
||||||
|
|
||||||
@override
|
|
||||||
final bool deprecated = true;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String get deprecationWarning {
|
String get deprecationWarning {
|
||||||
return '${globals.logger.terminal.warningMark} The "format" command is '
|
return 'The "format" command is deprecated. Please use the "dart format" '
|
||||||
'deprecated and will be removed in a future version of Flutter. '
|
'sub-command instead, which has the same command-line usage as '
|
||||||
'Please use the "dart format" sub-command instead, which takes all '
|
'"flutter format".\n';
|
||||||
'of the same command-line arguments as "flutter format".\n';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<FlutterCommandResult> runCommand() async {
|
Future<FlutterCommandResult> runCommand() async {
|
||||||
final String dartBinary = globals.artifacts!.getArtifactPath(Artifact.engineDartBinary);
|
throwToolExit(deprecationWarning);
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,147 +0,0 @@
|
|||||||
// 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);
|
).forEach(runner.addCommand);
|
||||||
verifyCommandRunner(runner);
|
verifyCommandRunner(runner);
|
||||||
for (final Command<void> command in runner.commands.values) {
|
for (final Command<void> command in runner.commands.values) {
|
||||||
if(command.name == 'analyze') {
|
if (command.name == 'analyze') {
|
||||||
final AnalyzeCommand analyze = command as AnalyzeCommand;
|
final AnalyzeCommand analyze = command as AnalyzeCommand;
|
||||||
expect(analyze.allProjectValidators().length, 2);
|
expect(analyze.allProjectValidators().length, 2);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user