Skip to content

Commit 46190c8

Browse files
committed
Add support for swift-testing in BuildSettingsEnvironment
1 parent 59a394f commit 46190c8

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

Modules/Sources/BuildSettingsKit/BuildSettingsEnvironment.swift

+23-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,34 @@ enum BuildSettingsEnvironment {
66

77
static let current: BuildSettingsEnvironment = {
88
#if DEBUG
9-
let environment = ProcessInfo.processInfo.environment
10-
if environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" {
9+
let processInfo = ProcessInfo.processInfo
10+
if processInfo.isXcodePreview {
1111
return .preview
1212
}
13-
if NSClassFromString("XCTestCase") != nil {
13+
if processInfo.isTesting {
1414
fatalError("BuildSettings are unavailable when running unit tests. Make sure to inject the values manually in system under test.")
1515
}
1616
#endif
1717
return .live
1818
}()
1919
}
20+
21+
private extension ProcessInfo {
22+
var isXcodePreview: Bool {
23+
environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1"
24+
}
25+
26+
var isTesting: Bool {
27+
if environment.keys.contains("XCTestBundlePath") { return true }
28+
if environment.keys.contains("XCTestConfigurationFilePath") { return true }
29+
if environment.keys.contains("XCTestSessionIdentifier") { return true }
30+
31+
return arguments.contains { argument in
32+
let path = URL(fileURLWithPath: argument)
33+
return path.lastPathComponent == "swiftpm-testing-helper"
34+
|| argument == "--testing-library"
35+
|| path.lastPathComponent == "xctest"
36+
|| path.pathExtension == "xctest"
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)