SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

Add `test_parent_classes` option to `single_test_class`, `balanced_xctest_lifecycle`, and `empty_xctest` rules

Open mildm8nnered opened this issue 3 years ago • 1 comments

Adds a test_parent_classes option to the single_test_class, balanced_xctest_lifecycle, and empty_xctest rules, resolving the remaining rules mentioned in https://github.com/realm/SwiftLint/issues/4200.

Very similar to #4214 but see notes below.

For example, given

class MyTestCase: XCTestCase { }

class SwiftLintDemoTests: XCTestCase {
    // Unbalanced
    override func setUpWithError() throws {
        XCTFail("Hello")
    }

    // Empty
    func testExample() throws {

    }
}

// Second test class
class SwiftLintDemoTests2: XCTestCase {
    func testExample() throws {
        XCTFail("Hello")
    }
}

The following configurations will pick up the violations:

opt_in_rules:
  - balanced_xctest_lifecycle
  - single_test_class
  - empty_xctest_method

balanced_xctest_lifecycle:
  test_parent_classes: ["MyTestCase"]

single_test_class:
  test_parent_classes: ["MyTestCase"]

empty_xctest_method:
  test_parent_classes: ["MyTestCase"]

Notes

There is a lot of redundancy in the configurations. BalancedXCTestLifecycleConfiguration.swift and EmptyXCTestMethodConfiguration.swift are identical, and SingleTestClassConfiguration.swift differs only in including QuickSpec as an additional default parent class.

I didn't roll anything up as yet, but obviously it would be trivial to either have an abstract parent class, or to have two of the configurations inherit from the other one, and to take the default parent classes as a constructor argument.

Also, I had to implement makeViolation(file: SwiftLintFile, position: AbsolutePosition) -> StyleViolation myself in EmptyXCTestMethodRule.swift because my rule's configuration was no longer a SeverityConfiguration - I wonder if there some way I could have avoided having to do that.

mildm8nnered avatar Sep 21 '22 18:09 mildm8nnered

1 Warning
:warning: This PR may need tests.
12 Messages
:book: Linting Aerial with this PR took 1.2s vs 1.18s on main (1% slower)
:book: Linting Alamofire with this PR took 1.48s vs 1.47s on main (0% slower)
:book: Linting Firefox with this PR took 6.67s vs 6.66s on main (0% slower)
:book: Linting Kickstarter with this PR took 9.5s vs 9.52s on main (0% faster)
:book: Linting Moya with this PR took 0.67s vs 0.67s on main (0% slower)
:book: Linting Nimble with this PR took 0.65s vs 0.64s on main (1% slower)
:book: Linting Quick with this PR took 0.3s vs 0.29s on main (3% slower)
:book: Linting Realm with this PR took 11.27s vs 11.1s on main (1% slower)
:book: Linting SourceKitten with this PR took 0.53s vs 0.52s on main (1% slower)
:book: Linting Sourcery with this PR took 2.32s vs 2.34s on main (0% faster)
:book: Linting Swift with this PR took 4.41s vs 4.41s on main (0% slower)
:book: Linting WordPress with this PR took 10.96s vs 10.96s on main (0% slower)

Generated by :no_entry_sign: Danger

SwiftLintBot avatar Sep 21 '22 18:09 SwiftLintBot

Replaced by #4262

mildm8nnered avatar Oct 01 '22 23:10 mildm8nnered