Skip to content

Commit 1de099f

Browse files
committed
Fail if testing, runtime warn otherwise (#1185)
* Fail if testing, runtime warn otherwise Doing both at once is noisy in tests. * wip
1 parent f6b4eb8 commit 1de099f

File tree

1 file changed

+37
-35
lines changed

1 file changed

+37
-35
lines changed
Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
#if DEBUG
22
#if canImport(os)
3-
import os
4-
import XCTestDynamicOverlay
3+
import os
4+
import XCTestDynamicOverlay
55

6-
// NB: Xcode runtime warnings offer a much better experience than traditional assertions and
7-
// breakpoints, but Apple provides no means of creating custom runtime warnings ourselves.
8-
// To work around this, we hook into SwiftUI's runtime issue delivery mechanism, instead.
9-
//
10-
// Feedback filed: https://gist.github.com/stephencelis/a8d06383ed6ccde3e5ef5d1b3ad52bbc
11-
private let rw = (
12-
dso: { () -> UnsafeMutableRawPointer in
13-
let count = _dyld_image_count()
14-
for i in 0..<count {
15-
if let name = _dyld_get_image_name(i) {
16-
let swiftString = String(cString: name)
17-
if swiftString.hasSuffix("/SwiftUI") {
18-
if let header = _dyld_get_image_header(i) {
19-
return UnsafeMutableRawPointer(mutating: UnsafeRawPointer(header))
20-
}
6+
// NB: Xcode runtime warnings offer a much better experience than traditional assertions and
7+
// breakpoints, but Apple provides no means of creating custom runtime warnings ourselves.
8+
// To work around this, we hook into SwiftUI's runtime issue delivery mechanism, instead.
9+
//
10+
// Feedback filed: https://gist.github.com/stephencelis/a8d06383ed6ccde3e5ef5d1b3ad52bbc
11+
private let rw = (
12+
dso: { () -> UnsafeMutableRawPointer in
13+
let count = _dyld_image_count()
14+
for i in 0..<count {
15+
if let name = _dyld_get_image_name(i) {
16+
let swiftString = String(cString: name)
17+
if swiftString.hasSuffix("/SwiftUI") {
18+
if let header = _dyld_get_image_header(i) {
19+
return UnsafeMutableRawPointer(mutating: UnsafeRawPointer(header))
2120
}
2221
}
2322
}
24-
return UnsafeMutableRawPointer(mutating: #dsohandle)
25-
}(),
26-
log: OSLog(subsystem: "com.apple.runtime-issues", category: "ComposableArchitecture")
27-
)
28-
#endif
23+
}
24+
return UnsafeMutableRawPointer(mutating: #dsohandle)
25+
}(),
26+
log: OSLog(subsystem: "com.apple.runtime-issues", category: "ComposableArchitecture")
27+
)
28+
#endif
2929
#endif
3030

3131
@inline(__always)
@@ -34,18 +34,20 @@ func runtimeWarning(
3434
_ args: @autoclosure () -> [CVarArg] = []
3535
) {
3636
#if DEBUG
37-
#if canImport(os)
38-
let message = message()
39-
unsafeBitCast(
40-
os_log as (OSLogType, UnsafeRawPointer, OSLog, StaticString, CVarArg...) -> Void,
41-
to: ((OSLogType, UnsafeRawPointer, OSLog, StaticString, [CVarArg]) -> Void).self
42-
)(.fault, rw.dso, rw.log, message, args())
43-
XCTFail(String(format: "\(message)", arguments: args()))
44-
#else
45-
let strMessage = message().withUTF8Buffer {
46-
String(decoding: $0, as: UTF8.self)
47-
}
48-
print(String(format: strMessage, arguments: args()))
49-
#endif
37+
if _XCTIsTesting {
38+
XCTFail(String(format: "\(message())", arguments: args()))
39+
} else {
40+
#if canImport(os)
41+
unsafeBitCast(
42+
os_log as (OSLogType, UnsafeRawPointer, OSLog, StaticString, CVarArg...) -> Void,
43+
to: ((OSLogType, UnsafeRawPointer, OSLog, StaticString, [CVarArg]) -> Void).self
44+
)(.fault, rw.dso, rw.log, message(), args())
45+
#else
46+
let strMessage = message().withUTF8Buffer {
47+
String(decoding: $0, as: UTF8.self)
48+
}
49+
print(String(format: strMessage, arguments: args()))
50+
#endif
51+
}
5052
#endif
5153
}

0 commit comments

Comments
 (0)