diff --git a/README.md b/README.md index 1e1e6bb..3c88743 100644 --- a/README.md +++ b/README.md @@ -239,7 +239,7 @@ import PackageDescription let package = Package( name: "SomeProject", dependencies: [ - .package(url: "https://github.com/dankinsoid/swift-api-client.git", from: "0.42.0") + .package(url: "https://github.com/dankinsoid/swift-api-client.git", from: "0.43.0") ], targets: [ .target( diff --git a/Sources/SwiftAPIClient/APIClientConfigs.swift b/Sources/SwiftAPIClient/APIClientConfigs.swift index 1149af8..2de8c05 100644 --- a/Sources/SwiftAPIClient/APIClientConfigs.swift +++ b/Sources/SwiftAPIClient/APIClientConfigs.swift @@ -67,5 +67,42 @@ public func valueFor( #endif } -private let _XCTIsTesting: Bool = ProcessInfo.processInfo.environment.keys.contains("XCTestBundlePath") -private let _isPreview: Bool = ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" +public let _isPreview: Bool = ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" + +#if !os(WASI) +public let _XCTIsTesting: Bool = { + ProcessInfo.processInfo.environment.keys.contains("XCTestBundlePath") + || ProcessInfo.processInfo.environment.keys.contains("XCTestConfigurationFilePath") + || ProcessInfo.processInfo.environment.keys.contains("XCTestSessionIdentifier") + || (ProcessInfo.processInfo.arguments.first + .flatMap(URL.init(fileURLWithPath:)) + .map { $0.lastPathComponent == "xctest" || $0.pathExtension == "xctest" } + ?? false) + || XCTCurrentTestCase != nil +}() +#else +public let _XCTIsTesting = false +#endif + +#if canImport(ObjectiveC) +private var XCTCurrentTestCase: AnyObject? { + guard + let XCTestObservationCenter = NSClassFromString("XCTestObservationCenter"), + let XCTestObservationCenter = XCTestObservationCenter as Any as? NSObjectProtocol, + let shared = XCTestObservationCenter.perform(Selector(("sharedTestObservationCenter")))? + .takeUnretainedValue(), + let observers = shared.perform(Selector(("observers")))? + .takeUnretainedValue() as? [AnyObject], + let observer = + observers + .first(where: { NSStringFromClass(type(of: $0)) == "XCTestMisuseObserver" }), + let currentTestCase = observer.perform(Selector(("currentTestCase")))? + .takeUnretainedValue() + else { return nil } + return currentTestCase +} +#else +private var XCTCurrentTestCase: AnyObject? { + nil +} +#endif