-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathSkippedTestSupport.swift
456 lines (391 loc) · 21.8 KB
/
SkippedTestSupport.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import class Foundation.FileManager
import class Foundation.ProcessInfo
package import SWBUtil
package import SWBCore
package import SWBProtocol
package import Testing
private import SWBLLBuild
package struct KnownSDK: ExpressibleByStringLiteral, Comparable, Sendable {
package let sdkName: String
package init(stringLiteral sdkName: String) {
self.sdkName = sdkName
}
package static func < (lhs: KnownSDK, rhs: KnownSDK) -> Bool {
lhs.sdkName < rhs.sdkName
}
}
extension KnownSDK {
package static var host: Self {
switch Result(catching: { try ProcessInfo.processInfo.hostOperatingSystem() }) {
case .success(.macOS):
return macOS
case .success(.iOS):
return iOS
case .success(.tvOS):
return tvOS
case .success(.watchOS):
return watchOS
case .success(.visionOS):
return xrOS
case .success(.windows):
return windows
case .success(.linux):
return linux
case .success(.android):
return android
case .success(.unknown), .failure:
preconditionFailure("Unknown host platform")
}
}
}
extension KnownSDK {
package static let macOS: Self = "macosx"
package static let iOS: Self = "iphoneos"
package static let tvOS: Self = "appletvos"
package static let watchOS: Self = "watchos"
package static let xrOS: Self = "xros"
package static let driverKit: Self = "driverkit"
}
extension KnownSDK {
package static let windows: Self = "windows"
package static let linux: Self = "linux"
package static let android: Self = "android"
package static let qnx: Self = "qnx"
package static let wasi: Self = "wasi"
}
package final class ConditionTraitContext: CoreBasedTests, Sendable {
package static let shared = ConditionTraitContext()
private enum LibclangState {
case uninitialized
case initialized(Libclang)
case failed
}
private let _libclang = AsyncLockedValue<LibclangState>(.uninitialized)
private init() {
}
package var libclang: Libclang? {
get async throws {
try await _libclang.withLock {
switch $0 {
case .uninitialized:
if let libclang = try await Libclang(path: libClangPath.str) {
$0 = .initialized(libclang)
libclang.leak()
return libclang
} else {
$0 = .failed
return nil
}
case let .initialized(libclang):
return libclang
case .failed:
return nil
}
}
}
}
}
extension Trait where Self == Testing.ConditionTrait {
/// Skips a test case that requires one or more SDKs if they are not all available.
package static func requireSDKs(_ knownSDKs: KnownSDK..., comment: Comment? = nil) -> Self {
enabled(comment != nil ? "required SDKs are not installed: \(comment?.description ?? "")" : "required SDKs are not installed.", {
let sdkRegistry = try await ConditionTraitContext.shared.getCore().sdkRegistry
let missingSDKs = await knownSDKs.asyncFilter { sdkRegistry.lookup($0.sdkName) == nil }.sorted()
return missingSDKs.isEmpty
})
}
/// Constructs a condition trait that causes a test to be disabled if not running on the specified host OS.
/// - parameter when: An additional constraint to apply such that the host OS requirement is only applied if this parameter is _also_ true. Defaults to true.
package static func requireHostOS(_ os: OperatingSystem, when condition: Bool = true) -> Self {
enabled("This test requires a \(os) host OS.", { try ProcessInfo.processInfo.hostOperatingSystem() == os && condition })
}
/// Constructs a condition trait that causes a test to be disabled if running on the specified host OS.
package static func skipHostOS(_ os: OperatingSystem, _ comment: Comment? = nil) -> Self {
disabled(comment ?? "This test cannot run on a \(os) host OS.", { try ProcessInfo.processInfo.hostOperatingSystem() == os })
}
/// Constructs a condition trait that causes a test to be disabled if process spawning is unavailable (for example, on Apple embedded platforms).
package static var requireProcessSpawning: Self {
disabled("Process spawning is unavailable.", { try ProcessInfo.processInfo.hostOperatingSystem().isAppleEmbedded || ProcessInfo.processInfo.isMacCatalystApp })
}
/// Constructs a condition trait that causes a test to be disabled if the Foundation process spawning implementation is not using `posix_spawn_file_actions_addchdir`.
package static var requireThreadSafeWorkingDirectory: Self {
disabled("Thread-safe process working directory support is unavailable.", {
// Amazon Linux 2 has glibc 2.26, and glibc 2.29 is needed for posix_spawn_file_actions_addchdir_np support
FileManager.default.contents(atPath: "/etc/system-release").map { String(decoding: $0, as: UTF8.self) == "Amazon Linux release 2 (Karoo)\n" } ?? false
})
}
/// Constructs a condition trait that causes a test to be disabled if the specified llbuild API version requirement is not met.
package static func requireLLBuild(apiVersion version: Int32) -> Self {
let llbuildVersion = llb_get_api_version()
return disabled("llbuild version \(llbuildVersion) is too old (\(version) or later required)", { llbuildVersion < version })
}
package static var skipSwiftPackage: Self {
#if SWIFT_PACKAGE
return disabled("Test is not supported when building Swift Build as a package")
#else
return enabled(if: true)
#endif
}
package static func requireClangFeatures(_ requiredFeatures: DiscoveredClangToolSpecInfo.FeatureFlag...) -> Self {
enabled("Clang compiler does not support features: \(requiredFeatures)") {
let features = try await ConditionTraitContext.shared.clangFeatures
return requiredFeatures.allSatisfy { features.has($0) }
}
}
package static func requireSwiftFeatures(_ requiredFeatures: DiscoveredSwiftCompilerToolSpecInfo.FeatureFlag...) -> Self {
enabled("Swift compiler does not support features: \(requiredFeatures)") {
let features = try await ConditionTraitContext.shared.swiftFeatures
return requiredFeatures.allSatisfy { features.has($0) }
}
}
package static func requireSDKImports() -> Self {
enabled("Linker does not support SDK imports") {
return try await ConditionTraitContext.shared.supportsSDKImports
}
}
package static func requireLocalFileSystem(_ sdks: RunDestinationInfo...) -> Self {
disabled("macOS SDK is on a remote filesystem") {
let core = try await ConditionTraitContext.shared.getCore()
return sdks.allSatisfy { localFS.isOnPotentiallyRemoteFileSystem(core.loadSDK($0).path) }
}
}
package static func requireSystemPackages(apt: String..., yum: String..., sourceLocation: SourceLocation = #_sourceLocation) -> Self {
enabled("required system packages are not installed") {
func checkInstalled(hostOS: OperatingSystem, packageManagerPath: Path, args: [String], packages: [String], regex: Regex<(Substring, name: Substring)>) async throws -> Bool {
if try ProcessInfo.processInfo.hostOperatingSystem() == hostOS && localFS.exists(packageManagerPath) {
var installedPackages: Set<String> = []
for line in try await runProcess([packageManagerPath.str] + args + packages).split(separator: "\n") {
if let packageName = try regex.firstMatch(in: line)?.output.name {
installedPackages.insert(String(packageName))
}
}
let uninstalledPackages = Set(packages).subtracting(installedPackages)
if !uninstalledPackages.isEmpty {
Issue.record("system packages are missing. Install via `\(packageManagerPath.basenameWithoutSuffix) install \(uninstalledPackages.sorted().joined(separator: " "))`", sourceLocation: sourceLocation)
return false
}
}
return true
}
let apt = try await checkInstalled(hostOS: .linux, packageManagerPath: Path("/usr/bin/apt"), args: ["list", "--installed", "apt"], packages: apt, regex: #/(?<name>.+)\//#)
// spelled `--installed` in newer versions of yum, but Amazon Linux 2 is on older versions
let yum = try await checkInstalled(hostOS: .linux, packageManagerPath: Path("/usr/bin/yum"), args: ["list", "installed", "yum"], packages: yum, regex: #/(?<name>.+)\./#)
return apt && yum
}
}
package static func skipIfEnvironment(key: EnvironmentKey, value: String) -> Self {
disabled("environment sets '\(key)' to '\(value)'") {
getEnvironmentVariable(key) == value
}
}
package static func skipIfEnvironmentVariableSet(key: EnvironmentKey) -> Self {
disabled("environment sets '\(key)'") {
getEnvironmentVariable(key) != nil
}
}
}
// MARK: Condition traits for Xcode and SDK version requirements
extension Trait where Self == Testing.ConditionTrait {
/// Constructs a condition trait that causes a test to be disabled if running against the exact given version of Xcode.
package static func skipXcodeBuildVersion(_ version: String, sourceLocation: SourceLocation = #_sourceLocation) -> Self {
skipXcodeBuildVersions(in: try {
let v: ProductBuildVersion = try ProductBuildVersion(version)
return v...v
}(), sourceLocation: sourceLocation)
}
/// Constructs a condition trait that causes a test to be disabled if running against the exact given version of Xcode.
package static func skipXcodeBuildVersion(_ version: ProductBuildVersion, sourceLocation: SourceLocation = #_sourceLocation) -> Self {
skipXcodeBuildVersions(in: version...version, sourceLocation: sourceLocation)
}
/// Constructs a condition trait that causes a test to be disabled if running against a version of Xcode within the given range.
package static func skipXcodeBuildVersions<R: RangeExpression>(in range: @Sendable @autoclosure @escaping () throws -> R, sourceLocation: SourceLocation = #_sourceLocation) -> Self where R.Bound == ProductBuildVersion {
disabled("Xcode version is not suitable", sourceLocation: sourceLocation, {
return try await range().contains(InstalledXcode.currentlySelected().productBuildVersion())
})
}
/// Constructs a condition trait that causes a test to be disabled if not running against at least the given version of Xcode.
package static func requireMinimumXcodeBuildVersion(_ version: String, sourceLocation: SourceLocation = #_sourceLocation) -> Self {
requireXcodeBuildVersions(in: try ProductBuildVersion(version)..., sourceLocation: sourceLocation)
}
package static func requireXcode16(sourceLocation: SourceLocation = #_sourceLocation) -> Self {
enabled("Xcode version is not suitable", sourceLocation: sourceLocation, {
guard let installedVersion = try? await InstalledXcode.currentlySelected().productBuildVersion() else {
return true
}
return installedVersion > (try ProductBuildVersion("16A242d"))
})
}
/// Constructs a condition trait that causes a test to be disabled if not running against at least the given version of Xcode.
package static func requireMinimumXcodeBuildVersion(_ version: ProductBuildVersion, sourceLocation: SourceLocation = #_sourceLocation) -> Self {
requireXcodeBuildVersions(in: version..., sourceLocation: sourceLocation)
}
/// Constructs a condition trait that causes a test to be disabled if not running against a version of Xcode within the given range.
package static func requireXcodeBuildVersions<R: RangeExpression>(in range: @Sendable @autoclosure @escaping () throws -> R, sourceLocation: SourceLocation = #_sourceLocation) -> Self where R.Bound == ProductBuildVersion {
enabled("Xcode version is not suitable", sourceLocation: sourceLocation, {
return try await range().contains(InstalledXcode.currentlySelected().productBuildVersion())
})
}
/// Constructs a condition trait that causes a test to be disabled if not running against a version of Xcode including at least the given version of a particular SDK.
package static func requireMinimumSDKBuildVersion(sdkName: String, requiredVersion: String, sourceLocation: SourceLocation = #_sourceLocation) -> Self {
requireMinimumSDKBuildVersion(sdkName: sdkName, requiredVersion: try ProductBuildVersion(requiredVersion), sourceLocation: sourceLocation)
}
/// Constructs a condition trait that causes a test to be disabled if not running against a version of Xcode including at least the given version of a particular SDK.
package static func requireMinimumSDKBuildVersion(sdkName: String, requiredVersion: @Sendable @autoclosure @escaping () throws -> ProductBuildVersion, sourceLocation: SourceLocation = #_sourceLocation) -> Self {
disabled("SDK build version is too old", sourceLocation: sourceLocation, {
let sdkVersion = try await InstalledXcode.currentlySelected().productBuildVersion(sdkCanonicalName: sdkName)
return try sdkVersion < requiredVersion()
})
}
/// Constructs a condition trait that causes a test to be disabled if not running against a version of Xcode including the SDK which is equal to or newer than at least one of the given versions within the same release.
package static func requireMinimumSDKBuildVersion(sdkName: String, requiredVersions: [String], sourceLocation: SourceLocation = #_sourceLocation) -> Self {
requireMinimumSDKBuildVersion(sdkName: sdkName, requiredVersions: try requiredVersions.map { try ProductBuildVersion($0) }, sourceLocation: sourceLocation)
}
/// Constructs a condition trait that causes a test to be disabled if not running against a version of Xcode including the SDK which is equal to or newer than at least one of the given versions within the same release.
package static func requireMinimumSDKBuildVersion(sdkName: String, requiredVersions: @Sendable @autoclosure @escaping () throws -> [ProductBuildVersion], sourceLocation: SourceLocation = #_sourceLocation) -> Self {
disabled("SDK build version is too old", sourceLocation: sourceLocation, {
let sdkVersion = try await InstalledXcode.currentlySelected().productBuildVersion(sdkCanonicalName: sdkName)
// For each required version, check to see if it is from the same release as the SDK version. If it is, then we will check against it.
for requiredVersion in try requiredVersions() {
if sdkVersion.major == requiredVersion.major, sdkVersion.train == requiredVersion.train {
return sdkVersion < requiredVersion
}
}
// If the SDK version is not from the same release as any of required versions, then we assume we don't need to skip. This is to handle the common case where we've moved on to newer releases and don't want to be forced to clean up these skips as soon as we do so. It assumes we won't start running the test against older releases of the SDK.
// We could do something more sophisticated here to handle versions outside of the specific releases we were passed, but this meets our needs for now.
return false
})
}
}
extension Trait where Self == Testing.ConditionTrait {
package static var skipDeveloperDirectoryWithEqualSign: Self {
disabled {
try await ConditionTraitContext.shared.getCore().developerPath.str.contains("=")
}
}
package static var requireStructuredDiagnostics: Self {
enabled("clang is too old to support structured scanner diagnostics") {
try #require(try await ConditionTraitContext.shared.libclang).supportsStructuredDiagnostics
}
}
package static var requireDependencyScanner: Self {
disabled {
let libclang = try #require(try await ConditionTraitContext.shared.libclang)
do {
let _ = try libclang.createScanner()
} catch DependencyScanner.Error.featureUnsupported {
// libclang at `libClangPath.str` does not have up-to-date dependency scanner (rdar://68233820)
return true
}
return false
}
}
package static var requireCompilationCaching: Self {
enabled("compilation caching is not supported") {
try await ConditionTraitContext.shared.supportsCompilationCaching
}
}
package static var requireDependencyScannerPlusCaching: Self {
disabled {
let libclang = try #require(try await ConditionTraitContext.shared.libclang)
return try withTemporaryDirectory { tmpDirPath in
do {
let casOpts = try ClangCASOptions(libclang).setOnDiskPath(tmpDirPath.str)
let casDBs = try ClangCASDatabases(options: casOpts)
let _ = try libclang.createScanner(casDBs: casDBs)
} catch DependencyScanner.Error.featureUnsupported {
// libclang at `libClangPath.str` does not have up-to-date dependency scanner (rdar://68233820)
return true
} catch ClangCASDatabases.Error.featureUnsupported {
// libclang at `libClangPath.str` does not have up-to-date caching support (rdar://102786106)
return true
}
return false
}
}
}
package static var requireCASPlugin: Self {
enabled("libclang does not support CAS plugins") { try await casOptions().canUseCASPlugin }
}
package static var requireCASUpToDate: Self {
enabled("libclang does not support CAS up-to-date checks") { try await casOptions().canCheckCASUpToDate }
}
package static var requireLocalizedStringExtraction: Self {
enabled("swift-driver does not support localized string extraction") {
LibSwiftDriver.supportsDriverFlag(spelled: "-emit-localized-strings")
}
}
}
package func casOptions() async throws -> (canUseCASPlugin: Bool, canUseCASPruning: Bool, canCheckCASUpToDate: Bool) {
let libclang = try #require(try await ConditionTraitContext.shared.libclang)
let core = try await ConditionTraitContext.shared.getCore()
let canUseCASPlugin = libclang.supportsCASPlugin && localFS.exists(core.developerPath.join("usr/lib/libToolchainCASPlugin.dylib"))
let canUseCASPruning = libclang.supportsCASPruning
let canCheckCASUpToDate = libclang.supportsCASUpToDateChecks
return (canUseCASPlugin, canUseCASPruning, canCheckCASUpToDate)
}
extension Libclang {
var supportsCASPlugin: Bool {
do {
let casOpts = try ClangCASOptions(self)
casOpts.setPluginPath("/tmp")
return true
} catch ClangCASDatabases.Error.featureUnsupported {
return false
} catch {
Issue.record("unexpected error: \(error)")
return false
}
}
}
fileprivate enum XcodeVersionInfoProvider {
case coreBasedTests(any CoreBasedTests)
case installedXcode(InstalledXcode)
case noXcode
func xcodeProductBuildVersion() async throws -> ProductBuildVersion {
switch self {
case let .coreBasedTests(testCase):
return try await testCase.getCore().xcodeProductBuildVersion
case let .installedXcode(xcode):
return try xcode.productBuildVersion()
case .noXcode:
return try ProductBuildVersion("99T999") // same fallback version that Core uses
}
}
func sdkProductBuildVersion(canonicalName sdkName: String) async throws -> ProductBuildVersion {
switch self {
case let .coreBasedTests(testCase):
guard let sdk = try await testCase.getCore().sdkRegistry.lookup(nameOrPath: sdkName, basePath: .root) else {
throw StubError.error("couldn't lookup \(sdkName) SDK")
}
guard let sdkBuildVersion = try sdk.productBuildVersion.map({ try ProductBuildVersion($0) }) else {
throw StubError.error("couldn't determine build version of \(sdkName) SDK")
}
return sdkBuildVersion
case let .installedXcode(xcode):
return try await xcode.productBuildVersion(sdkCanonicalName: sdkName)
case .noXcode:
throw StubError.error("couldn't determine build version of \(sdkName) SDK")
}
}
func hasSDK(canonicalName sdkName: String) async -> Bool {
switch self {
case let .coreBasedTests(testCase):
return await (try? testCase.getCore().sdkRegistry.lookup(sdkName)) != nil
case let .installedXcode(xcode):
return await xcode.hasSDK(sdkCanonicalName: sdkName)
case .noXcode:
return sdkName == KnownSDK.host.sdkName
}
}
}