|
1 | 1 | @preconcurrency import Foundation |
2 | | -import IssueReporting |
3 | 2 |
|
4 | 3 | extension Notification.Name { |
5 | 4 | @available(*, deprecated, message: "Use IssueReporting module from https://github.com/pointfreeco/swift-issue-reporting") |
@@ -84,3 +83,48 @@ let formatter: DateFormatter = { |
84 | 83 | }() |
85 | 84 | #endif |
86 | 85 | #endif |
| 86 | + |
| 87 | +#if os(WASI) |
| 88 | + public let isTesting = false |
| 89 | +#else |
| 90 | + import Foundation |
| 91 | + |
| 92 | + /// Whether or not the current process is running tests. |
| 93 | + /// |
| 94 | + /// You can use this information to prevent application code from running when hosting tests. For |
| 95 | + /// example, you can wrap your app entry point: |
| 96 | + /// |
| 97 | + /// ```swift |
| 98 | + /// import IssueReporting |
| 99 | + /// |
| 100 | + /// @main |
| 101 | + /// struct MyApp: App { |
| 102 | + /// var body: some Scene { |
| 103 | + /// WindowGroup { |
| 104 | + /// if !isTesting { |
| 105 | + /// MyRootView() |
| 106 | + /// } |
| 107 | + /// } |
| 108 | + /// } |
| 109 | + /// } |
| 110 | + /// |
| 111 | + /// To detect if the current task is running inside a test, use ``TestContext/current``, instead. |
| 112 | + public let isTesting = ProcessInfo.processInfo.isTesting |
| 113 | + |
| 114 | + extension ProcessInfo { |
| 115 | + fileprivate var isTesting: Bool { |
| 116 | + if environment.keys.contains("XCTestBundlePath") { return true } |
| 117 | + if environment.keys.contains("XCTestBundleInjectPath") { return true } |
| 118 | + if environment.keys.contains("XCTestConfigurationFilePath") { return true } |
| 119 | + if environment.keys.contains("XCTestSessionIdentifier") { return true } |
| 120 | + |
| 121 | + return arguments.contains { argument in |
| 122 | + let path = URL(fileURLWithPath: argument) |
| 123 | + return path.lastPathComponent == "swiftpm-testing-helper" |
| 124 | + || argument == "--testing-library" |
| 125 | + || path.lastPathComponent == "xctest" |
| 126 | + || path.pathExtension == "xctest" |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +#endif |
0 commit comments