Skip to content
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

Closed
cfilipov opened this issue Aug 29, 2016 · 1 comment

Comments

@cfilipov
Copy link
Contributor

cfilipov commented Aug 29, 2016

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:

private_unit_tst: #typo
  severity: error
  regex: "(XCTestCase|MyUnitTestCase)"

The regex in that rule is supposed to limit the scope of this rule to classes that inherit from XCTestCase or MyUnitTestCase. However, I had a typo in the config, so this rule should never have caught any style violations in MyUnitTestCase, 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. But validateClass() has no way of signaling its caller (validateFile()) that it bailed out early because this is not an applicable subclass, so validateFile() 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 in validateFile(). At this point the validateClass() implementation becomes trivial so it can be moved into validateFile(), just after the isTestClass() check.

Side Note

I noticed that severity: error in the YAML example above had no effect unless I also included the regex key. I suspect I'm failing when reading that field in the config file and that's probably happening before the severity 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.

guard let configurationDict = configuration as? [String: AnyObject],
    regexString = configurationDict["regex"] as? String else {
        throw ConfigurationError.UnknownConfiguration
}

Easy to fix, will include that too.

@jpsim
Copy link
Collaborator

jpsim commented Aug 30, 2016

Nice find and nice work @cfilipov! It's an honour to have such strong contributors to this project 😊

@jpsim jpsim closed this as completed in a60bcdd Aug 31, 2016
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants