Skip to content

Commit e526fde

Browse files
authored
Repair GenerationTests on Windows (#6354)
1 parent 2e92f75 commit e526fde

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

Source/SwiftLintBuiltInRules/Rules/Lint/UnusedImportRuleExamples.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ struct UnusedImportRuleExamples {
190190
"allowed_transitive_imports": ["CoreFoundation", "Dispatch"],
191191
] as [String: any Sendable],
192192
],
193-
] as [String: any Sendable], testMultiByteOffsets: false, testOnLinux: false):
193+
] as [String: any Sendable], testMultiByteOffsets: false, testOnLinux: false, testOnWindows: false):
194194
Example("""
195195
import Foundation
196196
typealias Foo = CFArray
@@ -202,7 +202,7 @@ struct UnusedImportRuleExamples {
202202
dispatchMain()
203203
""", configuration: [
204204
"require_explicit_imports": true
205-
], testMultiByteOffsets: false, testOnLinux: false):
205+
], testMultiByteOffsets: false, testOnLinux: false, testOnWindows: false):
206206
Example("""
207207
import CoreFoundation
208208
import Dispatch
@@ -236,7 +236,7 @@ struct UnusedImportRuleExamples {
236236
class A {}
237237
""", configuration: [
238238
"require_explicit_imports": true
239-
], testMultiByteOffsets: false, testOnLinux: false):
239+
], testMultiByteOffsets: false, testOnLinux: false, testOnWindows: false):
240240
Example("""
241241
import CoreFoundation
242242
import Foundation

Source/SwiftLintCore/Models/Example.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public struct Example: Sendable {
2828
package private(set) var testDisableCommand: Bool
2929
/// Whether the example should be tested on Linux
3030
public private(set) var testOnLinux: Bool
31+
/// Whether the example should be tested on Windows
32+
public private(set) var testOnWindows: Bool
3133
/// The path to the file where the example was created
3234
public private(set) var file: StaticString
3335
/// The line in the file where the example was created
@@ -68,13 +70,15 @@ public extension Example {
6870
testWrappingInString: Bool = true,
6971
testDisableCommand: Bool = true,
7072
testOnLinux: Bool = true,
73+
testOnWindows: Bool = true,
7174
file: StaticString = #filePath,
7275
line: UInt = #line,
7376
excludeFromDocumentation: Bool = false) {
7477
self.code = code
7578
self.configuration = configuration
7679
self.testMultiByteOffsets = testMultiByteOffsets
7780
self.testOnLinux = testOnLinux
81+
self.testOnWindows = testOnWindows
7882
self.file = file
7983
self.line = line
8084
self.excludeFromDocumentation = excludeFromDocumentation

Tests/TestHelpers/TestHelpers.swift

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,45 @@ import XCTest
44

55
import SwiftLintFramework
66

7+
#if os(Windows)
8+
private struct PlatformInfo {
9+
struct DefaultProperties {
10+
let versionXCTest: String
11+
let versionSwiftTesting: String
12+
let swiftFlags: [String]?
13+
}
14+
15+
let defaults: DefaultProperties
16+
}
17+
18+
extension PlatformInfo.DefaultProperties: Decodable {
19+
enum CodingKeys: String, CodingKey {
20+
case versionXCTest = "XCTEST_VERSION"
21+
case versionSwiftTesting = "SWIFT_TESTING_VERSION"
22+
case swiftFlags = "SWIFTC_FLAGS"
23+
}
24+
}
25+
26+
extension PlatformInfo: Decodable {
27+
enum CodingKeys: String, CodingKey {
28+
case defaults = "DefaultProperties"
29+
}
30+
}
31+
32+
private let info: PlatformInfo = {
33+
let sdk = URL(fileURLWithPath: sdkPath(), isDirectory: true)
34+
.deletingLastPathComponent()
35+
.deletingLastPathComponent()
36+
.deletingLastPathComponent()
37+
.appendingPathComponent("Info.plist")
38+
guard let data = try? Data(contentsOf: sdk),
39+
let info = try? PropertyListDecoder().decode(PlatformInfo.self, from: data) else {
40+
fatalError("invalid platform SDK - couldn't decode \(sdk.path)")
41+
}
42+
return info
43+
}()
44+
#endif
45+
746
// swiftlint:disable file_length
847

948
private let violationMarker = ""
@@ -28,12 +67,29 @@ private extension SwiftLintFile {
2867
.appendingPathComponent("Library")
2968
.appendingPathComponent("Frameworks")
3069
.path
31-
return [
32-
"-F",
33-
frameworks,
70+
71+
let arguments = [
72+
"-F", frameworks,
3473
"-sdk", sdk,
35-
"-j4", path!,
74+
"-Xfrontend", "-enable-objc-interop",
75+
"-j4",
76+
path!,
3677
]
78+
#if os(Windows)
79+
let XCTestPath = URL(fileURLWithPath: sdk, isDirectory: true)
80+
.deletingLastPathComponent()
81+
.deletingLastPathComponent()
82+
.appendingPathComponent("Library")
83+
.appendingPathComponent("XCTest-\(info.defaults.versionXCTest)")
84+
.appendingPathComponent("usr")
85+
.appendingPathComponent("lib")
86+
.appendingPathComponent("swift")
87+
.appendingPathComponent("windows")
88+
.path
89+
return ["-I", XCTestPath] + arguments
90+
#else
91+
return arguments
92+
#endif
3793
}
3894
}
3995

@@ -265,6 +321,11 @@ private func testCorrection(_ correction: (Example, Example),
265321
guard correction.0.testOnLinux else {
266322
return
267323
}
324+
#endif
325+
#if os(Windows)
326+
guard correction.0.testOnWindows else {
327+
return
328+
}
268329
#endif
269330
var config = configuration
270331
if let correctionConfiguration = correction.0.configuration,

0 commit comments

Comments
 (0)