[tool] Improve Swift lint error message (#8074)
If the Swift linter finds issues, log a message that communicates that rather than sounding like the linter failed to run.
diff --git a/script/tool/lib/src/format_command.dart b/script/tool/lib/src/format_command.dart
index a646cbf..5fbcd93 100644
--- a/script/tool/lib/src/format_command.dart
+++ b/script/tool/lib/src/format_command.dart
@@ -35,6 +35,7 @@
const int _exitDependencyMissing = 7;
const int _exitSwiftFormatFailed = 8;
const int _exitKotlinFormatFailed = 9;
+const int _exitSwiftLintFoundIssues = 10;
final Uri _javaFormatterUrl = Uri.https('github.com',
'/google/google-java-format/releases/download/google-java-format-1.3/google-java-format-1.3-all-deps.jar');
@@ -239,7 +240,10 @@
'--strict',
],
files: swiftFiles);
- if (lintExitCode != 0) {
+ if (lintExitCode == 1) {
+ printError('Swift linter found issues. See above for linter output.');
+ throw ToolExit(_exitSwiftLintFoundIssues);
+ } else if (lintExitCode != 0) {
printError('Failed to lint Swift files: exit code $lintExitCode.');
throw ToolExit(_exitSwiftFormatFailed);
}
diff --git a/script/tool/test/format_command_test.dart b/script/tool/test/format_command_test.dart
index 670cc0c..eb4f3bb 100644
--- a/script/tool/test/format_command_test.dart
+++ b/script/tool/test/format_command_test.dart
@@ -643,7 +643,7 @@
]));
});
- test('fails if swift-format lint fails', () async {
+ test('fails if swift-format lint finds issues', () async {
const List<String> files = <String>[
'macos/foo.swift',
];
@@ -675,7 +675,43 @@
expect(
output,
containsAllInOrder(<Matcher>[
- contains('Failed to lint Swift files: exit code 1.'),
+ contains('Swift linter found issues. See above for linter output.'),
+ ]));
+ });
+
+ test('fails if swift-format lint fails', () async {
+ const List<String> files = <String>[
+ 'macos/foo.swift',
+ ];
+ final RepositoryPackage plugin =
+ createFakePlugin('a_plugin', packagesDir, extraFiles: files);
+ fakePubGet(plugin);
+
+ processRunner.mockProcessesForExecutable['swift-format'] =
+ <FakeProcessInfo>[
+ FakeProcessInfo(MockProcess(),
+ <String>['--version']), // check for working swift-format
+ FakeProcessInfo(MockProcess(), <String>['-i']),
+ FakeProcessInfo(MockProcess(exitCode: 99), <String>[
+ 'lint',
+ '--parallel',
+ '--strict',
+ ]),
+ ];
+ Error? commandError;
+ final List<String> output = await runCapturingPrint(runner, <String>[
+ 'format',
+ '--swift',
+ '--swift-format-path=swift-format'
+ ], errorHandler: (Error e) {
+ commandError = e;
+ });
+
+ expect(commandError, isA<ToolExit>());
+ expect(
+ output,
+ containsAllInOrder(<Matcher>[
+ contains('Failed to lint Swift files: exit code 99.'),
]));
});