Skip to content

Commit c6a39c9

Browse files
committed
wip
1 parent 459cfef commit c6a39c9

File tree

1 file changed

+64
-55
lines changed

1 file changed

+64
-55
lines changed

Sources/IssueReporting/Internal/XCTest.swift

Lines changed: 64 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,31 @@ func _XCTFail(
1616
file: StaticString = #filePath,
1717
line: UInt = #line
1818
) {
19-
var pointer: UnsafeMutableRawPointer? {
20-
let symbol = "$s6XCTest7XCTFail_4file4lineySS_s12StaticStringVSutF"
21-
#if canImport(Darwin) || canImport(Glibc)
22-
return dlsym(dlopen("libXCTest.so", RTLD_NOW) ?? dlopen(nil, RTLD_NOW), symbol)
23-
#elseif canImport(WinSDK)
24-
guard let dll = LoadLibraryA("XCTest.dll") else { return nil }
25-
return GetProcAddress(dll, symbol)
26-
#else
27-
return nil
28-
#endif
29-
}
19+
#if DEBUG
20+
var pointer: UnsafeMutableRawPointer? {
21+
let symbol = "$s6XCTest7XCTFail_4file4lineySS_s12StaticStringVSutF"
22+
#if canImport(Darwin) || canImport(Glibc)
23+
return dlsym(dlopen("libXCTest.so", RTLD_NOW) ?? dlopen(nil, RTLD_NOW), symbol)
24+
#elseif canImport(WinSDK)
25+
guard let dll = LoadLibraryA("XCTest.dll") else { return nil }
26+
return GetProcAddress(dll, symbol)
27+
#else
28+
return nil
29+
#endif
30+
}
3031

31-
guard
32-
!_XCTExpectedFailure.isInFailingBlock,
33-
let pointer
34-
else { return }
35-
let XCTFail = unsafeBitCast(
36-
pointer,
37-
to: (@convention(thin) (String, StaticString, UInt) -> Void).self
38-
)
39-
XCTFail(message, file, line)
32+
guard
33+
!_XCTExpectedFailure.isInFailingBlock,
34+
let pointer
35+
else { return }
36+
let XCTFail = unsafeBitCast(
37+
pointer,
38+
to: (@convention(thin) (String, StaticString, UInt) -> Void).self
39+
)
40+
XCTFail(message, file, line)
41+
#else
42+
// TODO: Warn
43+
#endif
4044
}
4145

4246
@_transparent
@@ -49,45 +53,50 @@ func _XCTExpectFailure<R>(
4953
line: UInt,
5054
failingBlock: () throws -> R
5155
) rethrows -> R {
52-
guard enabled != false
53-
else {
54-
return try failingBlock()
55-
}
56-
#if _runtime(_ObjC)
57-
guard
58-
let xctExpectFailureInBlockPtr = dlsym(
59-
dlopen(nil, RTLD_NOW),
60-
"XCTExpectFailureWithOptionsInBlock"
61-
),
62-
let xctExpectedFailureOptions = NSClassFromString("XCTExpectedFailureOptions")
63-
as Any as? NSObjectProtocol,
64-
let options = strict ?? true
65-
? xctExpectedFailureOptions
66-
.perform(NSSelectorFromString("alloc"))?.takeUnretainedValue()
67-
.perform(NSSelectorFromString("init"))?.takeUnretainedValue()
68-
: xctExpectedFailureOptions
69-
.perform(NSSelectorFromString("nonStrictOptions"))?.takeUnretainedValue()
56+
#if DEBUG
57+
guard enabled != false
7058
else {
7159
return try failingBlock()
7260
}
73-
let xctExpectFailureInBlock = unsafeBitCast(
74-
xctExpectFailureInBlockPtr,
75-
to: (@convention(c) (String?, AnyObject, () -> Void) -> Void).self
76-
)
77-
var result: Result<R, any Error>!
78-
xctExpectFailureInBlock(failureReason, options) {
79-
result = Result { try failingBlock() }
80-
}
81-
return try result._rethrowGet()
61+
#if _runtime(_ObjC)
62+
guard
63+
let xctExpectFailureInBlockPtr = dlsym(
64+
dlopen(nil, RTLD_NOW),
65+
"XCTExpectFailureWithOptionsInBlock"
66+
),
67+
let xctExpectedFailureOptions = NSClassFromString("XCTExpectedFailureOptions")
68+
as Any as? NSObjectProtocol,
69+
let options = strict ?? true
70+
? xctExpectedFailureOptions
71+
.perform(NSSelectorFromString("alloc"))?.takeUnretainedValue()
72+
.perform(NSSelectorFromString("init"))?.takeUnretainedValue()
73+
: xctExpectedFailureOptions
74+
.perform(NSSelectorFromString("nonStrictOptions"))?.takeUnretainedValue()
75+
else {
76+
return try failingBlock()
77+
}
78+
let xctExpectFailureInBlock = unsafeBitCast(
79+
xctExpectFailureInBlockPtr,
80+
to: (@convention(c) (String?, AnyObject, () -> Void) -> Void).self
81+
)
82+
var result: Result<R, any Error>!
83+
xctExpectFailureInBlock(failureReason, options) {
84+
result = Result { try failingBlock() }
85+
}
86+
return try result._rethrowGet()
87+
#else
88+
_XCTFail(
89+
"XCTest's XCTExpectFailure is unavailable on this platform.",
90+
file: file,
91+
line: line
92+
)
93+
return try _XCTExpectedFailure.$isInFailingBlock.withValue(true) {
94+
try failingBlock()
95+
}
96+
#endif
8297
#else
83-
_XCTFail(
84-
"XCTest's XCTExpectFailure is unavailable on this platform.",
85-
file: file,
86-
line: line
87-
)
88-
return try _XCTExpectedFailure.$isInFailingBlock.withValue(true) {
89-
try failingBlock()
90-
}
98+
// TODO: Warn
99+
try failingBlock()
91100
#endif
92101
}
93102

0 commit comments

Comments
 (0)