Skip to content

Commit 5fdb6f7

Browse files
authored
Add testSuiteAccessControl rule (#334)
1 parent 95c9105 commit 5fdb6f7

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ let package = Package(
4747

4848
.binaryTarget(
4949
name: "swiftformat",
50-
url: "https://github.com/calda/SwiftFormat-nightly/releases/download/2025-10-17/SwiftFormat.artifactbundle.zip",
51-
checksum: "f86382d573a5cd7a62bbbe6ab7c932aa266506498f078e5f151529283d4ac06a"
50+
url: "https://github.com/calda/SwiftFormat-nightly/releases/download/2025-10-22/SwiftFormat.artifactbundle.zip",
51+
checksum: "4b730f982ec7b26e958ac662fb53670a12c6ab4692f08e281c379821aeb0f080"
5252
),
5353

5454
.binaryTarget(

README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4689,6 +4689,95 @@ _You can enable the following settings in Xcode by running [this script](resourc
46894689
```
46904690
</details>
46914691

4692+
* <a id='test-suite-access-control'></a>(<a href='#test-suite-access-control'>link</a>) **In test suites, test cases should be `internal`, and helper methods and properties should be `private`**. [![SwiftFormat: testSuiteAccessControl](https://img.shields.io/badge/SwiftFormat-testSuiteAccessControl-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#testSuiteAccessControl)
4693+
4694+
<details>
4695+
4696+
#### Why?
4697+
Test suites and test cases don't need to be `public` to be picked up by XCTest / Swift Testing, so should be `internal`.
4698+
4699+
Helpers and stored properties should be `private` since they are not accessed outside of the test suite.
4700+
4701+
```swift
4702+
import Testing
4703+
4704+
// WRONG
4705+
struct SpaceshipTests {
4706+
let spaceship = Spaceship()
4707+
4708+
func launchSpaceship() {
4709+
spaceship.launch()
4710+
}
4711+
4712+
@Test
4713+
func spaceshipCanLaunch() {
4714+
launchSpaceship()
4715+
#expect(spaceship.hasLaunched)
4716+
}
4717+
}
4718+
4719+
// RIGHT
4720+
struct SpaceshipTests {
4721+
4722+
// MARK: Internal
4723+
4724+
@Test
4725+
func spaceshipCanLaunch() {
4726+
launchSpaceship()
4727+
#expect(spaceship.hasLaunched)
4728+
}
4729+
4730+
// MARK: Private
4731+
4732+
private let spaceship = Spaceship()
4733+
4734+
private func launchSpaceship() {
4735+
spaceship.launch()
4736+
}
4737+
4738+
}
4739+
```
4740+
4741+
```swift
4742+
import XCTest
4743+
4744+
// WRONG
4745+
final class SpaceshipTests: XCTestCase {
4746+
let spaceship = Spaceship()
4747+
4748+
func launchSpaceship() {
4749+
spaceship.launch()
4750+
}
4751+
4752+
func testSpaceshipCanLaunch() {
4753+
launchSpaceship()
4754+
XCTAssertTrue(spaceship.hasLaunched)
4755+
}
4756+
}
4757+
4758+
// RIGHT
4759+
final class SpaceshipTests: XCTestCase {
4760+
4761+
// MARK: Internal
4762+
4763+
func testSpaceshipCanLaunch() {
4764+
launchSpaceship()
4765+
XCTAssertTrue(spaceship.hasLaunched)
4766+
}
4767+
4768+
// MARK: Private
4769+
4770+
private let spaceship = Spaceship()
4771+
4772+
private func launchSpaceship() {
4773+
spaceship.launch()
4774+
}
4775+
4776+
}
4777+
```
4778+
4779+
</details>
4780+
46924781
* <a id='avoid-force-unwrap-in-tests'></a>(<a href='#avoid-force-unwrap-in-tests'>link</a>) **Avoid force-unwrapping in unit tests**. Force-unwrapping (`!`) will crash your test suite. Use safe alternatives like `try XCTUnwrap` or `try #require`, which will throw an error instead, or standard optional unwrapping (`?`). [![SwiftFormat: noForceUnwrapInTests](https://img.shields.io/badge/SwiftFormat-noForceUnwrapInTests-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#noForceUnwrapInTests)
46934782

46944783
<details>

Sources/AirbnbSwiftFormatTool/airbnb.swiftformat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
--rules redundantThrows
139139
--rules redundantAsync
140140
--rules noGuardInTests
141+
--rules testSuiteAccessControl
141142
--rules validateTestCases
142143
--rules redundantMemberwiseInit
143144
--rules redundantBreak

0 commit comments

Comments
 (0)