-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Private Unit Test Rule: Rule is being evaluated on every function, not just tests #786
Comments
cfilipov
added a commit
to cfilipov/SwiftLint
that referenced
this issue
Aug 30, 2016
Nice find and nice work @cfilipov! It's an honour to have such strong contributors to this project 😊 |
jpsim
added a commit
that referenced
this issue
Aug 31, 2016
Fix #786: Private unit test rule not scoped to tests
norio-nomura
added a commit
that referenced
this issue
Sep 1, 2016
* master: (27 commits) Change `included` to `include` Update formatting fixup changelog entry from #789 add fix to CHANGELOG.md fix Mark rule case `// MARK: -` Update CHANGELOG Add unit test for issue #786 Fix #786: Private unit test rule not scoped to tests clarify that vertical_whitespace is on by default again fix for verticalspace regex bug fix setterAccesiblity typo Adding new configuration for private outlet rules to allow private(set) Add redundant nil coalesing operator rule release 0.12.0 make Vertical Whitespace rule opt-in move changelog entry to appropriate section Release 0.11.2 Fix long lines and unit test Fixed returns doc for init methods issue-557 Move CHANGELOG item to Breaking ...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Related to #761:
This is embarrassing... This new rule has a bug where it tries to validate instance methods even if they aren't part of an
XCTestCase
subclass. Unlike #782 I think this is unquestionably not what people would expect this rule to do so it should be fixed ASAP. I have a working fix but won't be able to create a pull request until I get home tonight (PST). Here's a brain dump for when I get back to this:I discovered this when trying to enable this rule for my custom subclass:
The regex in that rule is supposed to limit the scope of this rule to classes that inherit from
XCTestCase
orMyUnitTestCase
. However, I had a typo in the config, so this rule should never have caught any style violations inMyUnitTestCase
, but it did!In fact, in its current form, this rule will detect any instance method that begins with the word "test" and is marked
private
as a violation. It just so happens that this is uncommon enough that I never ran into it because methods beginning with "test" outside of unit tests are pretty rare.Knowing this, the bug is pretty easy to spot in the source code for this rule: The
validateClass()
method checks to see if this class is a test class before checking the visibility modifier, and bails out early if it's not a test class. ButvalidateClass()
has no way of signaling its caller (validateFile()
) that it bailed out early because this is not an applicable subclass, sovalidateFile()
just continues on looking at the substructure.The fix is simple, move the logic for matching the inherited types with the regex to a
isTestClass()
helper function and call that explictely invalidateFile()
. At this point thevalidateClass()
implementation becomes trivial so it can be moved intovalidateFile()
, just after theisTestClass()
check.Side Note
I noticed that
severity: error
in the YAML example above had no effect unless I also included theregex
key. I suspect I'm failing when reading that field in the config file and that's probably happening before theseverity
key is read.Edit: Yep. Missing regex causes
applyConfiguration()
to bail out at the beginning.This happens before any other parts of the config are applied.
Easy to fix, will include that too.
The text was updated successfully, but these errors were encountered: