Skip to content

Commit fd066c2

Browse files
committed
Adopt Swift Testing
1 parent aebc360 commit fd066c2

File tree

142 files changed

+6379
-4857
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

142 files changed

+6379
-4857
lines changed

.bazelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ build:macos --crosstool_top=@local_config_apple_cc//:toolchain
99
build:macos --host_crosstool_top=@local_config_apple_cc//:toolchain
1010

1111
build --macos_minimum_os=13.5 --host_macos_minimum_os=13.5
12+
test --macos_minimum_os=14.8 --host_macos_minimum_os=14.8 # Swift Testing libraries are compiled for macOS 14+.
1213
build --disk_cache=~/.bazel_cache
1314
build --experimental_remote_cache_compression
1415
build --remote_build_event_upload=minimal

.swift-format

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"indentation" : {
3+
"spaces" : 4
4+
},
5+
"lineLength": 120,
6+
"rules" : {
7+
"NoAccessLevelOnExtensionDeclaration": false
8+
}
9+
}

.swift-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6.2

.swiftlint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ file_name:
7373
- RuleConfigurationMacros.swift
7474
- SwiftSyntax+SwiftLint.swift
7575
- TestHelpers.swift
76+
- Traits.swift
7677
excluded_paths:
7778
- Tests/GeneratedTests/GeneratedTests_\d\d\.swift
7879
- Tests/FileSystemAccessTests/.+\.swift

Source/SwiftLintCore/Helpers/Macros.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ public macro SwiftSyntaxRule(foldExpressions: Bool = false,
7878
)
7979

8080
@attached(body)
81-
macro TemporaryDirectory() = #externalMacro(
81+
package macro TemporaryDirectory() = #externalMacro(
8282
module: "SwiftLintCoreMacros",
8383
type: "TemporaryDirectory"
8484
)
8585

8686
@attached(body)
87-
macro WorkingDirectory(path: String) = #externalMacro(
87+
package macro WorkingDirectory(path: String) = #externalMacro(
8888
module: "SwiftLintCoreMacros",
8989
type: "WorkingDirectory"
9090
)

Source/SwiftLintCoreMacros/DisabledWithoutSourceKit.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ enum DisabledWithoutSourceKit: ExtensionMacro {
2121
try ExtensionDeclSyntax("""
2222
extension \(type) {
2323
private static let postMessage: Void = {
24-
Issue.genericWarning(\(raw: message)).print()
24+
SwiftLintCore.Issue.genericWarning(\(raw: message)).print()
2525
}()
2626
2727
func notifyRuleDisabledOnce() {

Source/SwiftLintCoreMacros/RuleConfigurationMacros.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ enum AutoConfigParser: MemberMacro {
4949
}
5050
}
5151
return [
52-
DeclSyntax(try FunctionDeclSyntax("mutating func apply(configuration: Any) throws(Issue)") {
52+
DeclSyntax(try FunctionDeclSyntax("mutating func apply(configuration: Any) throws(SwiftLintCore.Issue)") {
5353
for option in nonInlinedOptions {
5454
"""
5555
if $\(raw: option).key.isEmpty {
@@ -61,7 +61,7 @@ enum AutoConfigParser: MemberMacro {
6161
"""
6262
do {
6363
try \(raw: option).apply(configuration, ruleID: Parent.identifier)
64-
} catch let issue where issue == Issue.nothingApplied(ruleID: Parent.identifier) {
64+
} catch let issue where issue == SwiftLintCore.Issue.nothingApplied(ruleID: Parent.identifier) {
6565
// Acceptable. Continue.
6666
}
6767
"""
@@ -83,7 +83,10 @@ enum AutoConfigParser: MemberMacro {
8383
"""
8484
if !supportedKeys.isSuperset(of: configuration.keys) {
8585
let unknownKeys = Set(configuration.keys).subtracting(supportedKeys)
86-
Issue.invalidConfigurationKeys(ruleID: Parent.identifier, keys: unknownKeys).print()
86+
SwiftLintCore.Issue.invalidConfigurationKeys(
87+
ruleID: Parent.identifier,
88+
keys: unknownKeys
89+
).print()
8790
}
8891
"""
8992
"""
@@ -115,7 +118,7 @@ enum AcceptableByConfigurationElement: ExtensionMacro {
115118
try ExtensionDeclSyntax("""
116119
extension \(type): AcceptableByConfigurationElement {
117120
\(raw: accessLevel)func asOption() -> OptionType { .symbol(rawValue) }
118-
\(raw: accessLevel)init(fromAny value: Any, context ruleID: String) throws(Issue) {
121+
\(raw: accessLevel)init(fromAny value: Any, context ruleID: String) throws(SwiftLintCore.Issue) {
119122
if let value = value as? String, let newSelf = Self(rawValue: value) {
120123
self = newSelf
121124
} else {

Source/SwiftLintFramework/LintOrAnalyzeCommand.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ package struct LintOrAnalyzeOptions {
125125
package struct LintOrAnalyzeCommand {
126126
package static func run(_ options: LintOrAnalyzeOptions) async throws {
127127
if let workingDirectory = options.workingDirectory {
128+
let currentDirectory = FileManager.default.currentDirectoryPath
129+
defer {
130+
if !FileManager.default.changeCurrentDirectoryPath(currentDirectory) {
131+
queuedFatalError("Could not change back to the original directory '\(currentDirectory)'.")
132+
}
133+
}
128134
if !FileManager.default.changeCurrentDirectoryPath(workingDirectory) {
129135
throw SwiftLintError.usageError(
130136
description: """

Source/swiftlint-dev/Rules+Register.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,11 @@ private extension SwiftLintDev.Rules.Register {
134134
// swiftlint:disable:next blanket_disable_command superfluous_disable_command
135135
// swiftlint:disable single_test_class type_name
136136
137+
import TestHelpers
138+
import Testing
139+
137140
@testable import SwiftLintBuiltInRules
138141
@testable import SwiftLintCore
139-
import TestHelpers
140142
141143
\(testClassesString)
142144
@@ -189,8 +191,10 @@ private extension SwiftLintDev.Rules.Register {
189191
let shardRules = rulesContext.shardRules(forIndex: shardIndex)
190192

191193
let testClasses = shardRules.map { testName in """
192-
final class \(testName)GeneratedTests: SwiftLintTestCase {
193-
func testWithDefaultConfiguration() {
194+
@Suite(.rulesRegistered)
195+
struct \(testName)GeneratedTests {
196+
@Test
197+
func withDefaultConfiguration() {
194198
verifyRule(\(testName).description)
195199
}
196200
}
Lines changed: 71 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
1-
@testable import SwiftLintBuiltInRules
21
import TestHelpers
2+
import Testing
3+
4+
@testable import SwiftLintBuiltInRules
35

4-
final class AttributesRuleTests: SwiftLintTestCase {
5-
func testAttributesWithAlwaysOnSameLine() {
6+
@Suite(.rulesRegistered)
7+
struct AttributesRuleTests {
8+
@Test
9+
func attributesWithAlwaysOnSameLine() {
610
// Test with custom `always_on_same_line`
711
let nonTriggeringExamples = [
812
Example("@objc var x: String"),
913
Example("@objc func foo()"),
1014
Example("@nonobjc\n func foo()"),
11-
Example("""
12-
class Foo {
13-
@objc private var object: RLMWeakObjectHandle?
14-
@objc private var property: RLMProperty?
15-
}
16-
"""),
17-
Example("""
18-
@objc(XYZFoo) class Foo: NSObject {}
19-
"""),
15+
Example(
16+
"""
17+
class Foo {
18+
@objc private var object: RLMWeakObjectHandle?
19+
@objc private var property: RLMProperty?
20+
}
21+
"""),
22+
Example(
23+
"""
24+
@objc(XYZFoo) class Foo: NSObject {}
25+
"""),
2026
]
2127
let triggeringExamples = [
2228
Example("@objc\n ↓var x: String"),
@@ -28,11 +34,13 @@ final class AttributesRuleTests: SwiftLintTestCase {
2834
.with(triggeringExamples: triggeringExamples)
2935
.with(nonTriggeringExamples: nonTriggeringExamples)
3036

31-
verifyRule(alwaysOnSameLineDescription,
32-
ruleConfiguration: ["always_on_same_line": ["@objc"]])
37+
verifyRule(
38+
alwaysOnSameLineDescription,
39+
ruleConfiguration: ["always_on_same_line": ["@objc"]])
3340
}
3441

35-
func testAttributesWithAlwaysOnLineAbove() {
42+
@Test
43+
func attributesWithAlwaysOnLineAbove() {
3644
// Test with custom `always_on_line_above`
3745
let nonTriggeringExamples = [
3846
Example("@objc\n var x: String"),
@@ -49,43 +57,49 @@ final class AttributesRuleTests: SwiftLintTestCase {
4957
.with(triggeringExamples: triggeringExamples)
5058
.with(nonTriggeringExamples: nonTriggeringExamples)
5159

52-
verifyRule(alwaysOnNewLineDescription,
53-
ruleConfiguration: ["always_on_line_above": ["@objc"]])
60+
verifyRule(
61+
alwaysOnNewLineDescription,
62+
ruleConfiguration: ["always_on_line_above": ["@objc"]])
5463
}
5564

56-
func testAttributesWithAttributesOnLineAboveButOnOtherDeclaration() {
65+
@Test
66+
func attributesWithAttributesOnLineAboveButOnOtherDeclaration() {
5767
let nonTriggeringExamples = [
58-
Example("""
59-
@IBDesignable open class TagListView: UIView {
60-
@IBInspectable open dynamic var textColor: UIColor = UIColor.white {
61-
didSet {}
68+
Example(
69+
"""
70+
@IBDesignable open class TagListView: UIView {
71+
@IBInspectable open dynamic var textColor: UIColor = UIColor.white {
72+
didSet {}
73+
}
6274
}
63-
}
64-
"""),
65-
Example("""
66-
@objc public protocol TagListViewDelegate {
67-
@objc optional func tagDidSelect(_ title: String, sender: TagListView)
68-
@objc optional func tagDidDeselect(_ title: String, sender: TagListView)
69-
}
70-
"""),
75+
"""),
76+
Example(
77+
"""
78+
@objc public protocol TagListViewDelegate {
79+
@objc optional func tagDidSelect(_ title: String, sender: TagListView)
80+
@objc optional func tagDidDeselect(_ title: String, sender: TagListView)
81+
}
82+
"""),
7183
]
7284

7385
let triggeringExamples = [
74-
Example("""
75-
@IBDesignable open class TagListView: UIView {
76-
@IBInspectable
77-
open dynamic ↓var textColor: UIColor = UIColor.white {
78-
didSet {}
86+
Example(
87+
"""
88+
@IBDesignable open class TagListView: UIView {
89+
@IBInspectable
90+
open dynamic ↓var textColor: UIColor = UIColor.white {
91+
didSet {}
92+
}
7993
}
80-
}
81-
"""),
82-
Example("""
83-
@objc public protocol TagListViewDelegate {
84-
@objc
85-
optional ↓func tagDidSelect(_ title: String, sender: TagListView)
86-
@objc optional func tagDidDeselect(_ title: String, sender: TagListView)
87-
}
88-
"""),
94+
"""),
95+
Example(
96+
"""
97+
@objc public protocol TagListViewDelegate {
98+
@objc
99+
optional ↓func tagDidSelect(_ title: String, sender: TagListView)
100+
@objc optional func tagDidDeselect(_ title: String, sender: TagListView)
101+
}
102+
"""),
89103
]
90104

91105
let alwaysOnNewLineDescription = AttributesRule.description
@@ -102,26 +116,29 @@ final class AttributesRuleTests: SwiftLintTestCase {
102116
)
103117
}
104118

105-
func testAttributesWithArgumentsAlwaysOnLineAboveFalse() {
119+
@Test
120+
func attributesWithArgumentsAlwaysOnLineAboveFalse() {
106121
let nonTriggeringExamples = [
107122
Example("@Environment(\\.presentationMode) private var presentationMode")
108123
]
109124
let triggeringExamples = [
110125
Example("""
111-
@Environment(\\.presentationMode)
112-
private ↓var presentationMode
113-
"""),
126+
@Environment(\\.presentationMode)
127+
private ↓var presentationMode
128+
"""),
114129
]
115130

116131
let argumentsAlwaysOnLineDescription = AttributesRule.description
117132
.with(triggeringExamples: triggeringExamples)
118133
.with(nonTriggeringExamples: nonTriggeringExamples)
119134

120-
verifyRule(argumentsAlwaysOnLineDescription,
121-
ruleConfiguration: ["attributes_with_arguments_always_on_line_above": false])
135+
verifyRule(
136+
argumentsAlwaysOnLineDescription,
137+
ruleConfiguration: ["attributes_with_arguments_always_on_line_above": false])
122138
}
123139

124-
func testAttributesWithArgumentsAlwaysOnLineAboveTrue() {
140+
@Test
141+
func attributesWithArgumentsAlwaysOnLineAboveTrue() {
125142
let nonTriggeringExamples = [
126143
Example("@Environment(\\.presentationMode)\nprivate var presentationMode")
127144
]
@@ -133,7 +150,8 @@ final class AttributesRuleTests: SwiftLintTestCase {
133150
.with(triggeringExamples: triggeringExamples)
134151
.with(nonTriggeringExamples: nonTriggeringExamples)
135152

136-
verifyRule(argumentsAlwaysOnLineDescription,
137-
ruleConfiguration: ["attributes_with_arguments_always_on_line_above": true])
153+
verifyRule(
154+
argumentsAlwaysOnLineDescription,
155+
ruleConfiguration: ["attributes_with_arguments_always_on_line_above": true])
138156
}
139157
}

0 commit comments

Comments
 (0)