diff --git a/Modules/RSCore/Sources/RSCore/Shared/Platform.swift b/Modules/RSCore/Sources/RSCore/Shared/Platform.swift index 70a67bba6..0643013ef 100644 --- a/Modules/RSCore/Sources/RSCore/Shared/Platform.swift +++ b/Modules/RSCore/Sources/RSCore/Shared/Platform.swift @@ -13,6 +13,34 @@ public enum Platform { private static let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "Platform") + /// Returns true if the app is currently running unit tests. + public static var isRunningUnitTests: Bool { + return _isRunningUnitTests + } + + private static let _isRunningUnitTests: Bool = { + // Check multiple indicators to be super-reliable + let environment = ProcessInfo.processInfo.environment + + // XCTest sets this environment variable + if environment["XCTestConfigurationFilePath"] != nil { + return true + } + + // Check if XCTest framework is loaded + if NSClassFromString("XCTestCase") != nil { + return true + } + + // Check command line arguments for test-related flags + let arguments = ProcessInfo.processInfo.arguments + if arguments.contains("-XCTest") || arguments.contains("test") { + return true + } + + return false + }() + /// Get the path to a subfolder of the application's data folder (often `Application Support`). /// - Parameters: /// - appName: The name of the application.