Skip to content

Commit d1992a4

Browse files
[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.
1 parent d345826 commit d1992a4

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

script/tool/lib/src/format_command.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const int _exitGitFailed = 6;
3535
const int _exitDependencyMissing = 7;
3636
const int _exitSwiftFormatFailed = 8;
3737
const int _exitKotlinFormatFailed = 9;
38+
const int _exitSwiftLintFoundIssues = 10;
3839

3940
final Uri _javaFormatterUrl = Uri.https('github.com',
4041
'/google/google-java-format/releases/download/google-java-format-1.3/google-java-format-1.3-all-deps.jar');
@@ -239,7 +240,10 @@ class FormatCommand extends PackageLoopingCommand {
239240
'--strict',
240241
],
241242
files: swiftFiles);
242-
if (lintExitCode != 0) {
243+
if (lintExitCode == 1) {
244+
printError('Swift linter found issues. See above for linter output.');
245+
throw ToolExit(_exitSwiftLintFoundIssues);
246+
} else if (lintExitCode != 0) {
243247
printError('Failed to lint Swift files: exit code $lintExitCode.');
244248
throw ToolExit(_exitSwiftFormatFailed);
245249
}

script/tool/test/format_command_test.dart

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ void main() {
643643
]));
644644
});
645645

646-
test('fails if swift-format lint fails', () async {
646+
test('fails if swift-format lint finds issues', () async {
647647
const List<String> files = <String>[
648648
'macos/foo.swift',
649649
];
@@ -675,7 +675,43 @@ void main() {
675675
expect(
676676
output,
677677
containsAllInOrder(<Matcher>[
678-
contains('Failed to lint Swift files: exit code 1.'),
678+
contains('Swift linter found issues. See above for linter output.'),
679+
]));
680+
});
681+
682+
test('fails if swift-format lint fails', () async {
683+
const List<String> files = <String>[
684+
'macos/foo.swift',
685+
];
686+
final RepositoryPackage plugin =
687+
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
688+
fakePubGet(plugin);
689+
690+
processRunner.mockProcessesForExecutable['swift-format'] =
691+
<FakeProcessInfo>[
692+
FakeProcessInfo(MockProcess(),
693+
<String>['--version']), // check for working swift-format
694+
FakeProcessInfo(MockProcess(), <String>['-i']),
695+
FakeProcessInfo(MockProcess(exitCode: 99), <String>[
696+
'lint',
697+
'--parallel',
698+
'--strict',
699+
]),
700+
];
701+
Error? commandError;
702+
final List<String> output = await runCapturingPrint(runner, <String>[
703+
'format',
704+
'--swift',
705+
'--swift-format-path=swift-format'
706+
], errorHandler: (Error e) {
707+
commandError = e;
708+
});
709+
710+
expect(commandError, isA<ToolExit>());
711+
expect(
712+
output,
713+
containsAllInOrder(<Matcher>[
714+
contains('Failed to lint Swift files: exit code 99.'),
679715
]));
680716
});
681717

0 commit comments

Comments
 (0)