nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.63k stars 623 forks source link

Add --strict option to emit non-zero exit code after applying changes #1676

Closed calda closed 2 months ago

calda commented 2 months ago

This PR adds a new --strict option that causes SwiftFormat to emit a non-zero exit code if it applies any formatting changes.

Within Airbnb we want our tooling to be able to detect when running SwiftFormat applies formatting changes. Historically we've done this by running SwiftFormat twice: once with --lint to determine whether or not there are changes needing formatting, and then again without --lint. This isn't very efficient since it requires running SwiftFormat twice, so instead we'd like to just run SwiftFormat a single time. A --strict option is really helpful for this.


I experimented with adding a test for this in CommandLineTests, but after some tinkering didn't find an easy way to set up the test case. I see there's no test case for the --lenient flag for example. Happy to revist this if you'd like.

I have confirmed that this functionality works in an executable build, and validated that it unlocks the workflow that we're interested in using within Airbnb.

codecov[bot] commented 2 months ago

Codecov Report

Attention: Patch coverage is 84.61538% with 2 lines in your changes are missing coverage. Please review.

Project coverage is 95.17%. Comparing base (906a222) to head (3c4adb3).

Files Patch % Lines
Sources/CommandLine.swift 84.61% 2 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## develop #1676 +/- ## =========================================== - Coverage 95.20% 95.17% -0.03% =========================================== Files 20 20 Lines 22923 22932 +9 =========================================== + Hits 21823 21825 +2 - Misses 1100 1107 +7 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.