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

Add rule to check for private unit tests #761

Merged
merged 7 commits into from
Aug 23, 2016
Merged

Add rule to check for private unit tests #761

merged 7 commits into from
Aug 23, 2016

Conversation

cfilipov
Copy link
Contributor

@cfilipov cfilipov commented Aug 19, 2016

If an XCTest method is marked private it will be silently skipped, no warning.

Example:

class MyTest: XCTestCase {
    private func testFoo() {
        // this test will not run
    }
    func testBar() {
        // this test will run
    }
}

Likewise, if the XCTest subclass is marked private all test methods on that class will be skipped.

private class MyOtherTest: XCTestCase {
    // none of the tests from `MyOtherTest` will run
}

It is bad practice to intentionally commit a private test for the same reason that it is a bad practice to commit commented-out code. This type of mistake is very easy to miss, so a SwiftLint rule has been created to catch instances of private tests.

@codecov-io
Copy link

codecov-io commented Aug 19, 2016

Current coverage is 88.63% (diff: 67.79%)

Merging #761 into master will decrease coverage by 0.88%

@@             master       #761   diff @@
==========================================
  Files            77         79     +2   
  Lines          2633       2745   +112   
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits           2357       2433    +76   
- Misses          276        312    +36   
  Partials          0          0          

Powered by Codecov. Last update 8389aa2...caa6d5e

@@ -54,17 +54,9 @@ extension File {
}

public enum AccessControlLevel: String {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be public?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be public?

I don't know, it was already public, I didn't change this line, just changed the raw values.

@masters3d
Copy link
Contributor

You need to update the changelog.
Does Linux XCTest act the same way?
What about filePrivate? Is the story the same?
https://github.com/apple/swift-evolution/blob/master/proposals/0025-scoped-access-level.md

@cfilipov
Copy link
Contributor Author

Does Linux XCTest act the same way?

Surprisingly, yes. For Linux, XCTest needs to explictely return all tests, so you would think it would at least result in an error. But if you mark the test as private, it just doesn't get run, also no warning or error.

What about filePrivate? Is the story the same?

Yes. However, that's a Swift 3 feature, so not part of this PR.

@@ -54,17 +54,9 @@ extension File {
}

public enum AccessControlLevel: String {
case Private = "private"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the distinction between rawValue and sourcekitValue() was intentional here. We sometimes use AccessControlLevel.description in violation messages (I think).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't realize that, but indeed it looks like this is the case in the init method of MissingDocsRule where the configuration is mapped over. I'll change the rawValue back and add a new init fromSourcekitValue. It's worth noting however: AccessControlLevel seems to be inconsistent with how it's done in other parts of the code. KindType and SwiftDeclarationKind both use the sourcekit value strings as their rawValue.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be better to keep your changes but add explicit conformance to CustomStringConvertible and declare var description: String with the short/lowercase member names.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could't actually find specific instances where description was used on AccessControlLevel, but that's easy to miss since there are several way description can be used implicitly that's hard to search for. I went ahead and made it conform to CustomStringConvertible as suggested which returns the short strings. I also had to modify MissingDocsRule.init(configuration:) because that relied on the rawValue being the shorter version of the string:

let acl = array.flatMap(AccessControlLevel.init(description:))

Since AccessControlLevel needs to be able to be instantiated from the short strings I also head to create an init method for that. Couldn't think of a better name so hopefully init(description:) will suffice.

@jpsim
Copy link
Collaborator

jpsim commented Aug 21, 2016

Thanks for the PR @cfilipov! Looks great. I just had two minor comments, and this will need to be rebased.

@jpsim jpsim merged commit 6dfaa0e into realm:master Aug 23, 2016
@jpsim
Copy link
Collaborator

jpsim commented Aug 23, 2016

This is fantastic @cfilipov. I really appreciate your work on this.

@cfilipov
Copy link
Contributor Author

Wonderful! Thanks for merging this in! I should probably create a ticket to track the inclusion of the check for fileprivate when Swiftlint is ready for Swift 3.

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

Successfully merging this pull request may close these issues.

4 participants