16

I am looking to disable a couple of SwiftLint rules for the unit tests within my application.

For example I am wanting to disable the weak_delegate rule for my unit tests.

Having looked at the SwiftLint docs I think it may be possible by defining a custom weak_delegate rule and excluding the path to my unit tests.

https://github.com/realm/SwiftLint#defining-custom-rules

bencallis
  • 3,478
  • 4
  • 32
  • 58

4 Answers4

14

You can disable them at a local level using:

//swiftlint:disable weak_delegate
let someDelete: someDelegate?
//swiftlint:enable weak_delegate

or at target level, by modifying your .swiftlint.yml file (hidden)

weak_delegate:
    excluded: ".*Test\\.swift" //regex path to your tests folder

or at project level, by modifying your .swiftlint.yml file (hidden)

disabled_rules:
 - weak_delegate
Durdu
  • 4,649
  • 2
  • 27
  • 47
11

Best way to exclude some rules for test target is the nested configuration: you add a second .swiftlint.yml to root of your tests directory with rules to disable.

disabled_rules:
    - weak_delegate
    - cyclomatic_complexity
    - force_unwrapping
    - function_body_length
Barbara K
  • 613
  • 6
  • 7
  • 2
    Important detail: If you want to use use nested configurations, you can't use the -- config parameter. Check the linked documentation provider by @Barbara K for more info. – Fernando Cardenas Feb 24 '22 at 09:54
6

Add this to your .swiftlint.yml:

weak_delegate:
    excluded: ".*Test\\.swift" //regex path to your tests folder
Vladimir Kaltyrin
  • 621
  • 1
  • 4
  • 16
0

Change the script in Build Phases to point only to the folder of your source code:

if which swiftlint > /dev/null; then
  swiftlint --path "$SRCROOT"/[name_of_source_folder]
else
  echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi

Adding the flag --path "$SRCROOT"/path_to_folder will exclude the test targets.

Camilo Ortegón
  • 3,414
  • 3
  • 26
  • 34