-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
CustomRulesTests.swift
169 lines (141 loc) · 6.47 KB
/
CustomRulesTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import Foundation
import SourceKittenFramework
@testable import SwiftLintFramework
import XCTest
class CustomRulesTests: XCTestCase {
func testCustomRuleConfigurationSetsCorrectly() {
let configDict = [
"my_custom_rule": [
"name": "MyCustomRule",
"message": "Message",
"regex": "regex",
"match_kinds": "comment",
"severity": "error"
]
]
var comp = RegexConfiguration(identifier: "my_custom_rule")
comp.name = "MyCustomRule"
comp.message = "Message"
comp.regex = regex("regex")
comp.severityConfiguration = SeverityConfiguration(.error)
comp.matchKinds = Set([SyntaxKind.comment])
var compRules = CustomRulesConfiguration()
compRules.customRuleConfigurations = [comp]
do {
var configuration = CustomRulesConfiguration()
try configuration.apply(configuration: configDict)
XCTAssertEqual(configuration, compRules)
} catch {
XCTFail("Did not configure correctly")
}
}
func testCustomRuleConfigurationThrows() {
let config = 17
var customRulesConfig = CustomRulesConfiguration()
checkError(ConfigurationError.unknownConfiguration) {
try customRulesConfig.apply(configuration: config)
}
}
func testCustomRuleConfigurationIgnoreInvalidRules() throws {
let configDict = [
"my_custom_rule": ["name": "MyCustomRule",
"message": "Message",
"regex": "regex",
"match_kinds": "comment",
"severity": "error"],
"invalid_rule": ["name": "InvalidRule"] // missing `regex`
]
var customRulesConfig = CustomRulesConfiguration()
try customRulesConfig.apply(configuration: configDict)
XCTAssertEqual(customRulesConfig.customRuleConfigurations.count, 1)
let identifier = customRulesConfig.customRuleConfigurations.first?.description.identifier
XCTAssertEqual(identifier, "my_custom_rule")
}
func testCustomRules() {
let (regexConfig, customRules) = getCustomRules()
let file = File(contents: "// My file with\n// a pattern")
XCTAssertEqual(customRules.validate(file: file),
[StyleViolation(ruleDescription: regexConfig.description,
severity: .warning,
location: Location(file: nil, line: 2, character: 6),
reason: regexConfig.message)])
}
func testLocalDisableCustomRule() {
let (_, customRules) = getCustomRules()
let file = File(contents: "//swiftlint:disable custom \n// file with a pattern")
XCTAssertEqual(customRules.validate(file: file), [])
}
func testLocalDisableCustomRuleWithMultipleRules() {
let (configs, customRules) = getCustomRulesWithTwoRules()
let file = File(contents: "//swiftlint:disable \(configs.1.identifier) \n// file with a pattern")
XCTAssertEqual(customRules.validate(file: file),
[StyleViolation(ruleDescription: configs.0.description,
severity: .warning,
location: Location(file: nil, line: 2, character: 16),
reason: configs.0.message)])
}
func testCustomRulesIncludedDefault() {
// Violation detected when included is omitted.
let (_, customRules) = getCustomRules()
let violations = customRules.validate(file: getTestTextFile())
XCTAssertEqual(violations.count, 1)
}
func testCustomRulesIncludedExcludesFile() {
var (regexConfig, customRules) = getCustomRules(["included": "\\.yml$"])
var customRuleConfiguration = CustomRulesConfiguration()
customRuleConfiguration.customRuleConfigurations = [regexConfig]
customRules.configuration = customRuleConfiguration
let violations = customRules.validate(file: getTestTextFile())
XCTAssertEqual(violations.count, 0)
}
func testCustomRulesExcludedExcludesFile() {
var (regexConfig, customRules) = getCustomRules(["excluded": "\\.txt$"])
var customRuleConfiguration = CustomRulesConfiguration()
customRuleConfiguration.customRuleConfigurations = [regexConfig]
customRules.configuration = customRuleConfiguration
let violations = customRules.validate(file: getTestTextFile())
XCTAssertEqual(violations.count, 0)
}
private func getCustomRules(_ extraConfig: [String: String] = [:]) -> (RegexConfiguration, CustomRules) {
var config = ["regex": "pattern",
"match_kinds": "comment"]
extraConfig.forEach { config[$0] = $1 }
var regexConfig = RegexConfiguration(identifier: "custom")
do {
try regexConfig.apply(configuration: config)
} catch {
XCTFail("Failed regex config")
}
var customRuleConfiguration = CustomRulesConfiguration()
customRuleConfiguration.customRuleConfigurations = [regexConfig]
var customRules = CustomRules()
customRules.configuration = customRuleConfiguration
return (regexConfig, customRules)
}
private func getCustomRulesWithTwoRules() -> ((RegexConfiguration, RegexConfiguration), CustomRules) {
let config1 = ["regex": "pattern",
"match_kinds": "comment"]
var regexConfig1 = RegexConfiguration(identifier: "custom1")
do {
try regexConfig1.apply(configuration: config1)
} catch {
XCTFail("Failed regex config")
}
let config2 = ["regex": "something",
"match_kinds": "comment"]
var regexConfig2 = RegexConfiguration(identifier: "custom2")
do {
try regexConfig2.apply(configuration: config2)
} catch {
XCTFail("Failed regex config")
}
var customRuleConfiguration = CustomRulesConfiguration()
customRuleConfiguration.customRuleConfigurations = [regexConfig1, regexConfig2]
var customRules = CustomRules()
customRules.configuration = customRuleConfiguration
return ((regexConfig1, regexConfig2), customRules)
}
private func getTestTextFile() -> File {
return File(path: "\(bundlePath)/test.txt")!
}
}