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

Adds quick_discouraged_pending_test opt-in rule #1910

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
[Ornithologist Coder](https://github.com/ornithocoder)
[#1905](https://github.com/realm/SwiftLint/issues/1905)

* Add `quick_discouraged_pending_test` opt-in rule which warns against
pending tests in Quick tests.
[Ornithologist Coder](https://github.com/ornithocoder)
[#1909](https://github.com/realm/SwiftLint/issues/1909)

* Add `override_in_extension` opt-in rule that warns against overriding
declarations in an `extension`.
[Marcelo Fabri](https://github.com/marcelofabri)
Expand Down
108 changes: 108 additions & 0 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
* [Protocol Property Accessors Order](#protocol-property-accessors-order)
* [Quick Discouraged Call](#quick-discouraged-call)
* [Quick Discouraged Focused Test](#quick-discouraged-focused-test)
* [Quick Discouraged Pending Test](#quick-discouraged-pending-test)
* [Redundant Discardable Let](#redundant-discardable-let)
* [Redundant Nil Coalescing](#redundant-nil-coalescing)
* [Redundant Optional Initialization](#redundant-optional-initialization)
Expand Down Expand Up @@ -9499,6 +9500,113 @@ class TotoTests: QuickSpec {



## Quick Discouraged Pending Test

Identifier | Enabled by default | Supports autocorrection | Kind
--- | --- | --- | ---
`quick_discouraged_pending_test` | Disabled | No | lint

Discouraged pending test. This test won't run while it's marked as pending.

### Examples

<details>
<summary>Non Triggering Examples</summary>

```swift
class TotoTests: QuickSpec {
override func spec() {
describe("foo") {
describe("bar") { }
context("bar") {
it("bar") { }
}
it("bar") { }
}
}
}

```

</details>
<details>
<summary>Triggering Examples</summary>

```swift
class TotoTests: QuickSpec {
override func spec() {
↓xdescribe("foo") { }
}
}

```

```swift
class TotoTests: QuickSpec {
override func spec() {
↓xcontext("foo") { }
}
}

```

```swift
class TotoTests: QuickSpec {
override func spec() {
↓xit("foo") { }
}
}

```

```swift
class TotoTests: QuickSpec {
override func spec() {
describe("foo") {
↓xit("bar") { }
}
}
}

```

```swift
class TotoTests: QuickSpec {
override func spec() {
context("foo") {
↓xit("bar") { }
}
}
}

```

```swift
class TotoTests: QuickSpec {
override func spec() {
describe("foo") {
context("bar") {
↓xit("toto") { }
}
}
}
}

```

```swift
class TotoTests: QuickSpec {
override func spec() {
↓pending("foo")
}
}

```

</details>



## Redundant Discardable Let

Identifier | Enabled by default | Supports autocorrection | Kind
Expand Down
1 change: 1 addition & 0 deletions Source/SwiftLintFramework/Models/MasterRuleList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public let masterRuleList = RuleList(rules: [
ProtocolPropertyAccessorsOrderRule.self,
QuickDiscouragedCallRule.self,
QuickDiscouragedFocusedTestRule.self,
QuickDiscouragedPendingTestRule.self,
RedundantDiscardableLetRule.self,
RedundantNilCoalescingRule.self,
RedundantOptionalInitializationRule.self,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// QuickDiscouragedPendingTestRule.swift
// SwiftLint
//
// Created by Ornithologist Coder on 10/15/17.
// Copyright © 2017 Realm. All rights reserved.
//

import Foundation
import SourceKittenFramework

public struct QuickDiscouragedPendingTestRule: OptInRule, ConfigurationProviderRule {
public var configuration = SeverityConfiguration(.warning)

public init() {}

public static let description = RuleDescription(
identifier: "quick_discouraged_pending_test",
name: "Quick Discouraged Pending Test",
description: "Discouraged pending test. This test won't run while it's marked as pending.",
kind: .lint,
nonTriggeringExamples: QuickDiscouragedPendingTestRuleExamples.nonTriggeringExamples,
triggeringExamples: QuickDiscouragedPendingTestRuleExamples.triggeringExamples
)

public func validate(file: File) -> [StyleViolation] {
let testClasses = file.structure.dictionary.substructure.filter {
return $0.inheritedTypes.contains("QuickSpec") &&
$0.kind.flatMap(SwiftDeclarationKind.init) == .class
}

let specDeclarations = testClasses.flatMap { classDict in
return classDict.substructure.filter {
return $0.name == "spec()" && $0.enclosedVarParameters.isEmpty &&
$0.kind.flatMap(SwiftDeclarationKind.init) == .functionMethodInstance &&
$0.enclosedSwiftAttributes.contains("source.decl.attribute.override")
}
}

return specDeclarations.flatMap {
validate(file: file, dictionary: $0)
}
}

private func validate(file: File, dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
return dictionary.substructure.flatMap { subDict -> [StyleViolation] in
var violations = validate(file: file, dictionary: subDict)

if let kindString = subDict.kind,
let kind = SwiftExpressionKind(rawValue: kindString) {
violations += validate(file: file, kind: kind, dictionary: subDict)
}

return violations
}
}

private func validate(file: File,
kind: SwiftExpressionKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
guard
kind == .call,
let name = dictionary.name,
let offset = dictionary.offset,
QuickPendingCallKind(rawValue: name) != nil else { return [] }

return [StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, byteOffset: offset))]
}
}

private enum QuickPendingCallKind: String {
case pending
case xdescribe
case xcontext
case xit
Copy link
Collaborator

Choose a reason for hiding this comment

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

probably xitBehavesLike should be here too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting. Quick's SDL.swift doesn't have xitBehavesLike. It's defined as a #define in QCKSDL.h. I'll add it.

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// QuickDiscouragedPendingTestRuleExamples.swift
// SwiftLint
//
// Created by Ornithologist Coder on 10/16/17.
// Copyright © 2017 Realm. All rights reserved.
//

import Foundation

internal struct QuickDiscouragedPendingTestRuleExamples {
static let nonTriggeringExamples = [
"class TotoTests: QuickSpec {\n" +
" override func spec() {\n" +
" describe(\"foo\") {\n" +
" describe(\"bar\") { } \n" +
" context(\"bar\") {\n" +
" it(\"bar\") { }\n" +
" }\n" +
" it(\"bar\") { }\n" +
" }\n" +
" }\n" +
"}\n"
]

static let triggeringExamples = [
"class TotoTests: QuickSpec {\n" +
" override func spec() {\n" +
" ↓xdescribe(\"foo\") { }\n" +
" }\n" +
"}\n",
"class TotoTests: QuickSpec {\n" +
" override func spec() {\n" +
" ↓xcontext(\"foo\") { }\n" +
" }\n" +
"}\n",
"class TotoTests: QuickSpec {\n" +
" override func spec() {\n" +
" ↓xit(\"foo\") { }\n" +
" }\n" +
"}\n",
"class TotoTests: QuickSpec {\n" +
" override func spec() {\n" +
" describe(\"foo\") {\n" +
" ↓xit(\"bar\") { }\n" +
" }\n" +
" }\n" +
"}\n",
"class TotoTests: QuickSpec {\n" +
" override func spec() {\n" +
" context(\"foo\") {\n" +
" ↓xit(\"bar\") { }\n" +
" }\n" +
" }\n" +
"}\n",
"class TotoTests: QuickSpec {\n" +
" override func spec() {\n" +
" describe(\"foo\") {\n" +
" context(\"bar\") {\n" +
" ↓xit(\"toto\") { }\n" +
" }\n" +
" }\n" +
" }\n" +
"}\n",
"class TotoTests: QuickSpec {\n" +
" override func spec() {\n" +
" ↓pending(\"foo\")\n" +
" }\n" +
"}\n"
]
}
8 changes: 8 additions & 0 deletions SwiftLint.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
57ED827B1CF656E3002B3513 /* JUnitReporter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57ED82791CF65183002B3513 /* JUnitReporter.swift */; };
621061BF1ED57E640082D51E /* MultilineParametersRuleExamples.swift in Sources */ = {isa = PBXBuildFile; fileRef = 621061BE1ED57E640082D51E /* MultilineParametersRuleExamples.swift */; };
62329C2B1F30B2310035737E /* DiscouragedDirectInitRuleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62AF35D71F30B183009B11EE /* DiscouragedDirectInitRuleTests.swift */; };
623675B01F960C5C009BE6F3 /* QuickDiscouragedPendingTestRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 623675AF1F960C5C009BE6F3 /* QuickDiscouragedPendingTestRule.swift */; };
623675B21F962FC4009BE6F3 /* QuickDiscouragedPendingTestRuleExamples.swift in Sources */ = {isa = PBXBuildFile; fileRef = 623675B11F962FC4009BE6F3 /* QuickDiscouragedPendingTestRuleExamples.swift */; };
623E36F01F3DB1B1002E5B71 /* QuickDiscouragedCallRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 623E36EF1F3DB1B1002E5B71 /* QuickDiscouragedCallRule.swift */; };
623E36F21F3DB988002E5B71 /* QuickDiscouragedCallRuleExamples.swift in Sources */ = {isa = PBXBuildFile; fileRef = 623E36F11F3DB988002E5B71 /* QuickDiscouragedCallRuleExamples.swift */; };
6250D32A1ED4DFEB00735129 /* MultilineParametersRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6238AE411ED4D734006C3601 /* MultilineParametersRule.swift */; };
Expand Down Expand Up @@ -403,6 +405,8 @@
5499CA971A2394B700783309 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
57ED82791CF65183002B3513 /* JUnitReporter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JUnitReporter.swift; sourceTree = "<group>"; };
621061BE1ED57E640082D51E /* MultilineParametersRuleExamples.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MultilineParametersRuleExamples.swift; sourceTree = "<group>"; };
623675AF1F960C5C009BE6F3 /* QuickDiscouragedPendingTestRule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuickDiscouragedPendingTestRule.swift; sourceTree = "<group>"; };
623675B11F962FC4009BE6F3 /* QuickDiscouragedPendingTestRuleExamples.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuickDiscouragedPendingTestRuleExamples.swift; sourceTree = "<group>"; };
6238AE411ED4D734006C3601 /* MultilineParametersRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MultilineParametersRule.swift; sourceTree = "<group>"; };
623E36EF1F3DB1B1002E5B71 /* QuickDiscouragedCallRule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuickDiscouragedCallRule.swift; sourceTree = "<group>"; };
623E36F11F3DB988002E5B71 /* QuickDiscouragedCallRuleExamples.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuickDiscouragedCallRuleExamples.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1073,6 +1077,8 @@
623E36F11F3DB988002E5B71 /* QuickDiscouragedCallRuleExamples.swift */,
62E54FED1F93AD57005B367B /* QuickDiscouragedFocusedTestRule.swift */,
626C16E01F948E1C00BB7475 /* QuickDiscouragedFocusedTestRuleExamples.swift */,
623675AF1F960C5C009BE6F3 /* QuickDiscouragedPendingTestRule.swift */,
623675B11F962FC4009BE6F3 /* QuickDiscouragedPendingTestRuleExamples.swift */,
D4C889701E385B7B00BAE88D /* RedundantDiscardableLetRule.swift */,
24B4DF0B1D6DFA370097803B /* RedundantNilCoalescingRule.swift */,
D4B022951E0EF80C007E5297 /* RedundantOptionalInitializationRule.swift */,
Expand Down Expand Up @@ -1442,6 +1448,7 @@
2E02005F1C54BF680024D09D /* CyclomaticComplexityRule.swift in Sources */,
D4FBADD01E00DA0400669C73 /* OperatorUsageWhitespaceRule.swift in Sources */,
D4C4A3521DEFBBB700E0E04C /* FileHeaderConfiguration.swift in Sources */,
623675B01F960C5C009BE6F3 /* QuickDiscouragedPendingTestRule.swift in Sources */,
D47079AD1DFE2FA700027086 /* EmptyParametersRule.swift in Sources */,
E87E4A091BFB9CAE00FCFE46 /* SyntaxKind+SwiftLint.swift in Sources */,
3B0B14541C505D6300BE82F7 /* SeverityConfiguration.swift in Sources */,
Expand Down Expand Up @@ -1480,6 +1487,7 @@
D4EA77C81F817FD200C315FB /* UnneededBreakInSwitchRule.swift in Sources */,
006204DC1E1E492F00FFFBE1 /* VerticalWhitespaceConfiguration.swift in Sources */,
E88198441BEA93D200333A11 /* ColonRule.swift in Sources */,
623675B21F962FC4009BE6F3 /* QuickDiscouragedPendingTestRuleExamples.swift in Sources */,
D403A4A31F4DB5510020CA02 /* PatternMatchingKeywordsRule.swift in Sources */,
E809EDA11B8A71DF00399043 /* Configuration.swift in Sources */,
D4DABFD51E2B350F009617B6 /* TrailingClosureRule.swift in Sources */,
Expand Down
4 changes: 4 additions & 0 deletions Tests/SwiftLintFrameworkTests/RulesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ class RulesTests: XCTestCase {
verifyRule(QuickDiscouragedFocusedTestRule.description)
}

func testQuickDiscouragedPendingTest() {
verifyRule(QuickDiscouragedPendingTestRule.description)
}

func testRedundantDiscardableLet() {
verifyRule(RedundantDiscardableLetRule.description)
}
Expand Down