|
| 1 | + |
| 2 | +/* |
| 3 | + This source file is part of the Swift.org open source project |
| 4 | + |
| 5 | + Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors |
| 6 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | + |
| 8 | + See http://swift.org/LICENSE.txt for license information |
| 9 | + See http://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 10 | + */ |
| 11 | + |
| 12 | +import class Foundation.FileManager |
| 13 | +import class Foundation.ProcessInfo |
| 14 | +import Testing |
| 15 | + |
| 16 | +extension Trait where Self == Testing.ConditionTrait { |
| 17 | + /// Skip test if the host operating system does not match the running OS. |
| 18 | + public static func requireHostOS(_ os: OperatingSystem, when condition: Bool = true) -> Self { |
| 19 | + enabled("This test requires a \(os) host OS.") { |
| 20 | + ProcessInfo.hostOperatingSystem == os && condition |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + /// Skip test if the host operating system matches the running OS. |
| 25 | + public static func skipHostOS(_ os: OperatingSystem, _ comment: Comment? = nil) -> Self { |
| 26 | + disabled(comment ?? "This test cannot run on a \(os) host OS.") { |
| 27 | + ProcessInfo.hostOperatingSystem == os |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + /// Skip test unconditionally |
| 32 | + public static func skip(_ comment: Comment? = nil) -> Self { |
| 33 | + disabled(comment ?? "Unconditional skip, a comment should be added for the reason") { true } |
| 34 | + } |
| 35 | + |
| 36 | + /// Skip test if the environment is self hosted. |
| 37 | + public static func skipSwiftCISelfHosted(_ comment: Comment? = nil) -> Self { |
| 38 | + disabled(comment ?? "SwiftCI is self hosted") { |
| 39 | + ProcessInfo.processInfo.environment["SWIFTCI_IS_SELF_HOSTED"] != nil |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /// Skip test if the test environment has a restricted network access, i.e. cannot get to internet. |
| 44 | + public static func requireUnrestrictedNetworkAccess(_ comment: Comment? = nil) -> Self { |
| 45 | + disabled(comment ?? "CI Environment has restricted network access") { |
| 46 | + ProcessInfo.processInfo.environment["SWIFTCI_RESTRICTED_NETWORK_ACCESS"] != nil |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /// Skip test if built by XCode. |
| 51 | + public static func skipIfXcodeBuilt() -> Self { |
| 52 | + disabled("Tests built by Xcode") { |
| 53 | + #if Xcode |
| 54 | + true |
| 55 | + #else |
| 56 | + false |
| 57 | + #endif |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// Constructs a condition trait that causes a test to be disabled if the Foundation process spawning implementation |
| 62 | + /// is not using `posix_spawn_file_actions_addchdir`. |
| 63 | + public static var requireThreadSafeWorkingDirectory: Self { |
| 64 | + disabled("Thread-safe process working directory support is unavailable.") { |
| 65 | + // Amazon Linux 2 has glibc 2.26, and glibc 2.29 is needed for posix_spawn_file_actions_addchdir_np support |
| 66 | + FileManager.default.contents(atPath: "/etc/system-release") |
| 67 | + .map { String(decoding: $0, as: UTF8.self) == "Amazon Linux release 2 (Karoo)\n" } ?? false |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments