File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
Sources/FoundationExtensions/General Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -84,3 +84,48 @@ let formatter: DateFormatter = {
8484} ( )
8585#endif
8686#endif
87+
88+ #if os(WASI)
89+ public let isTesting = false
90+ #else
91+ import Foundation
92+
93+ /// Whether or not the current process is running tests.
94+ ///
95+ /// You can use this information to prevent application code from running when hosting tests. For
96+ /// example, you can wrap your app entry point:
97+ ///
98+ /// ```swift
99+ /// import IssueReporting
100+ ///
101+ /// @main
102+ /// struct MyApp: App {
103+ /// var body: some Scene {
104+ /// WindowGroup {
105+ /// if !isTesting {
106+ /// MyRootView()
107+ /// }
108+ /// }
109+ /// }
110+ /// }
111+ ///
112+ /// To detect if the current task is running inside a test, use ``TestContext/current``, instead.
113+ public let isTesting = ProcessInfo . processInfo. isTesting
114+
115+ extension ProcessInfo {
116+ fileprivate var isTesting : Bool {
117+ if environment. keys. contains ( " XCTestBundlePath " ) { return true }
118+ if environment. keys. contains ( " XCTestBundleInjectPath " ) { return true }
119+ if environment. keys. contains ( " XCTestConfigurationFilePath " ) { return true }
120+ if environment. keys. contains ( " XCTestSessionIdentifier " ) { return true }
121+
122+ return arguments. contains { argument in
123+ let path = URL ( fileURLWithPath: argument)
124+ return path. lastPathComponent == " swiftpm-testing-helper "
125+ || argument == " --testing-library "
126+ || path. lastPathComponent == " xctest "
127+ || path. pathExtension == " xctest "
128+ }
129+ }
130+ }
131+ #endif
You can’t perform that action at this time.
0 commit comments