[tool] Add optional swift-format support (#5204)

Adds support for swift-format in the `format` command. For now this is optional, so is only triggered if the flag is explictly passed. In the future, once we have CI support for swift-format, we will likely want to make it on-by-default as with the other formatters.

Part of https://github.com/flutter/flutter/issues/41129
This commit is contained in:
stuartmorgan
2023-10-23 09:10:19 -07:00
committed by GitHub
parent 4bf51144c2
commit e73b364bc7
2 changed files with 97 additions and 6 deletions

View File

@ -428,6 +428,71 @@ void main() {
]));
});
group('swift-format', () {
test('formats Swift if --swift-format flag is provided', () async {
const List<String> files = <String>[
'macos/foo.swift',
];
final RepositoryPackage plugin = createFakePlugin(
'a_plugin',
packagesDir,
extraFiles: files,
);
await runCapturingPrint(
runner, <String>['format', '--swift-format=/path/to/swift-format']);
expect(
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
ProcessCall(
'/path/to/swift-format',
<String>['-i', ...getPackagesDirRelativePaths(plugin, files)],
packagesDir.path),
]));
});
test('skips Swift if --swift-format flag is not provided', () async {
const List<String> files = <String>[
'macos/foo.swift',
];
createFakePlugin(
'a_plugin',
packagesDir,
extraFiles: files,
);
await runCapturingPrint(runner, <String>['format']);
expect(processRunner.recordedCalls, orderedEquals(<ProcessCall>[]));
});
test('fails if swift-format fails', () async {
const List<String> files = <String>[
'macos/foo.swift',
];
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
processRunner.mockProcessesForExecutable['swift-format'] =
<FakeProcessInfo>[
FakeProcessInfo(MockProcess(exitCode: 1), <String>['-i']),
];
Error? commandError;
final List<String> output = await runCapturingPrint(
runner, <String>['format', '--swift-format=swift-format'],
errorHandler: (Error e) {
commandError = e;
});
expect(commandError, isA<ToolExit>());
expect(
output,
containsAllInOrder(<Matcher>[
contains('Failed to format Swift files: exit code 1.'),
]));
});
});
test('skips known non-repo files', () async {
const List<String> skipFiles = <String>[
'/example/build/SomeFramework.framework/Headers/SomeFramework.h',