Skip to content

Commit 9decbc4

Browse files
Make entire XCTWaiter and XCTestExpectation unavailable for WASI
1 parent c63a84f commit 9decbc4

File tree

5 files changed

+16
-44
lines changed

5 files changed

+16
-44
lines changed

Sources/XCTest/Private/XCTestCase.TearDownBlocksState.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ extension XCTestCase {
2828
@available(macOS 12.0, *)
2929
func appendAsync(_ block: @Sendable @escaping () async throws -> Void) {
3030
#if DISABLE_XCTWAITER
31-
XCTWaiter.subsystemQueue.sync {
31+
XCTestCase.subsystemQueue.sync {
3232
precondition(wasFinalized == false, "API violation -- attempting to add a teardown block after teardown blocks have been dequeued")
3333
blocks.append(block)
3434
}
@@ -40,14 +40,14 @@ extension XCTestCase {
4040
}
4141

4242
func append(_ block: @escaping () throws -> Void) {
43-
XCTWaiter.subsystemQueue.sync {
43+
XCTestCase.subsystemQueue.sync {
4444
precondition(wasFinalized == false, "API violation -- attempting to add a teardown block after teardown blocks have been dequeued")
4545
blocks.append(block)
4646
}
4747
}
4848

4949
func finalize() -> [TeardownBlock] {
50-
XCTWaiter.subsystemQueue.sync {
50+
XCTestCase.subsystemQueue.sync {
5151
precondition(wasFinalized == false, "API violation -- attempting to run teardown blocks after they've already run")
5252
wasFinalized = true
5353
return blocks

Sources/XCTest/Public/Asynchronous/XCTWaiter+Validation.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//
1010
// XCTWaiter+Validation.swift
1111
//
12+
#if !DISABLE_XCTWAITER
1213

1314
protocol XCTWaiterValidatableExpectation: Equatable {
1415
var isFulfilled: Bool { get }
@@ -87,3 +88,5 @@ extension XCTWaiter {
8788
return .incomplete
8889
}
8990
}
91+
92+
#endif

Sources/XCTest/Public/Asynchronous/XCTWaiter.swift

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//
1010
// XCTWaiter.swift
1111
//
12+
#if !DISABLE_XCTWAITER
1213

1314
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
1415
import CoreFoundation
@@ -117,9 +118,7 @@ open class XCTWaiter {
117118
private var state = State.ready
118119
internal var timeout: TimeInterval = 0
119120
internal var waitSourceLocation: SourceLocation?
120-
#if !DISABLE_XCTWAITER
121121
private weak var manager: WaiterManager<XCTWaiter>?
122-
#endif
123122
private var runLoop: RunLoop?
124123

125124
private weak var _delegate: XCTWaiterDelegate?
@@ -189,16 +188,9 @@ open class XCTWaiter {
189188
/// these environments. To ensure compatibility of tests between
190189
/// swift-corelibs-xctest and Apple XCTest, it is not recommended to pass
191190
/// explicit values for `file` and `line`.
192-
#if DISABLE_XCTWAITER
193-
@available(*, unavailable, message: "Expectation-based waiting is not available when using the Swift concurrency waiter.")
194-
#else
195191
@available(*, noasync, message: "Use await fulfillment(of:timeout:enforceOrder:) instead.")
196-
#endif
197192
@discardableResult
198193
open func wait(for expectations: [XCTestExpectation], timeout: TimeInterval, enforceOrder: Bool = false, file: StaticString = #file, line: Int = #line) -> Result {
199-
#if DISABLE_XCTWAITER
200-
fatalError("This method is not available when using the Swift concurrency waiter.")
201-
#else
202194
precondition(Set(expectations).count == expectations.count, "API violation - each expectation can appear only once in the 'expectations' parameter.")
203195

204196
self.timeout = timeout
@@ -260,7 +252,6 @@ open class XCTWaiter {
260252
}
261253

262254
return result
263-
#endif
264255
}
265256

266257
/// Wait on an array of expectations for up to the specified timeout, and optionally specify whether they
@@ -286,16 +277,9 @@ open class XCTWaiter {
286277
/// these environments. To ensure compatibility of tests between
287278
/// swift-corelibs-xctest and Apple XCTest, it is not recommended to pass
288279
/// explicit values for `file` and `line`.
289-
#if DISABLE_XCTWAITER
290-
@available(*, unavailable, message: "Expectation-based waiting is not available when using the Swift concurrency waiter.")
291-
#else
292280
@available(macOS 12.0, *)
293-
#endif
294281
@discardableResult
295282
open func fulfillment(of expectations: [XCTestExpectation], timeout: TimeInterval, enforceOrder: Bool = false, file: StaticString = #file, line: Int = #line) async -> Result {
296-
#if DISABLE_XCTWAITER
297-
fatalError("This method is not available when using the Swift concurrency waiter.")
298-
#else
299283
return await withCheckedContinuation { continuation in
300284
// This function operates by blocking a background thread instead of one owned by libdispatch or by the
301285
// Swift runtime (as used by Swift concurrency.) To ensure we use a thread owned by neither subsystem, use
@@ -305,7 +289,6 @@ open class XCTWaiter {
305289
continuation.resume(returning: result)
306290
}
307291
}
308-
#endif
309292
}
310293

311294
/// Convenience API to create an XCTWaiter which then waits on an array of expectations for up to the specified timeout, and optionally specify whether they
@@ -324,17 +307,9 @@ open class XCTWaiter {
324307
/// expectations are not fulfilled before the given timeout. Default is the line
325308
/// number of the call to this method in the calling file. It is rare to
326309
/// provide this parameter when calling this method.
327-
#if DISABLE_XCTWAITER
328-
@available(*, unavailable, message: "Expectation-based waiting is not available when using the Swift concurrency waiter.")
329-
#else
330310
@available(*, noasync, message: "Use await fulfillment(of:timeout:enforceOrder:) instead.")
331-
#endif
332311
open class func wait(for expectations: [XCTestExpectation], timeout: TimeInterval, enforceOrder: Bool = false, file: StaticString = #file, line: Int = #line) -> Result {
333-
#if DISABLE_XCTWAITER
334-
fatalError("This method is not available when using the Swift concurrency waiter.")
335-
#else
336312
return XCTWaiter().wait(for: expectations, timeout: timeout, enforceOrder: enforceOrder, file: file, line: line)
337-
#endif
338313
}
339314

340315
/// Convenience API to create an XCTWaiter which then waits on an array of expectations for up to the specified timeout, and optionally specify whether they
@@ -353,17 +328,9 @@ open class XCTWaiter {
353328
/// expectations are not fulfilled before the given timeout. Default is the line
354329
/// number of the call to this method in the calling file. It is rare to
355330
/// provide this parameter when calling this method.
356-
#if DISABLE_XCTWAITER
357-
@available(*, unavailable, message: "Expectation-based waiting is not available when using the Swift concurrency waiter.")
358-
#else
359331
@available(macOS 12.0, *)
360-
#endif
361332
open class func fulfillment(of expectations: [XCTestExpectation], timeout: TimeInterval, enforceOrder: Bool = false, file: StaticString = #file, line: Int = #line) async -> Result {
362-
#if DISABLE_XCTWAITER
363-
fatalError("This method is not available when using the Swift concurrency waiter.")
364-
#else
365333
return await XCTWaiter().fulfillment(of: expectations, timeout: timeout, enforceOrder: enforceOrder, file: file, line: line)
366-
#endif
367334
}
368335

369336
deinit {
@@ -372,7 +339,6 @@ open class XCTWaiter {
372339
}
373340
}
374341

375-
#if !DISABLE_XCTWAITER
376342
private func queue_configureExpectations(_ expectations: [XCTestExpectation]) {
377343
dispatchPrecondition(condition: .onQueue(XCTWaiter.subsystemQueue))
378344

@@ -448,11 +414,9 @@ open class XCTWaiter {
448414
queue_validateExpectationFulfillment(dueToTimeout: false)
449415
}
450416
}
451-
#endif
452417

453418
}
454419

455-
#if !DISABLE_XCTWAITER
456420
private extension XCTWaiter {
457421
func primitiveWait(using runLoop: RunLoop, duration timeout: TimeInterval) {
458422
// The contract for `primitiveWait(for:)` explicitly allows waiting for a shorter period than requested
@@ -473,7 +437,6 @@ private extension XCTWaiter {
473437
#endif
474438
}
475439
}
476-
#endif
477440

478441
extension XCTWaiter: Equatable {
479442
public static func == (lhs: XCTWaiter, rhs: XCTWaiter) -> Bool {
@@ -491,7 +454,6 @@ extension XCTWaiter: CustomStringConvertible {
491454
}
492455
}
493456

494-
#if !DISABLE_XCTWAITER
495457
extension XCTWaiter: ManageableWaiter {
496458
var isFinished: Bool {
497459
return XCTWaiter.subsystemQueue.sync {

Sources/XCTest/Public/Asynchronous/XCTestExpectation.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//
1010
// XCTestExpectation.swift
1111
//
12+
#if !DISABLE_XCTWAITER
1213

1314
/// Expectations represent specific conditions in asynchronous testing.
1415
open class XCTestExpectation: @unchecked Sendable {
@@ -320,3 +321,5 @@ extension XCTestExpectation: CustomStringConvertible {
320321
return expectationDescription
321322
}
322323
}
324+
325+
#endif

Sources/XCTest/Public/XCTestCase.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ open class XCTestCase: XCTest {
5454
return 1
5555
}
5656

57+
internal static let subsystemQueue = DispatchQueue(label: "org.swift.XCTestCase")
58+
59+
#if !DISABLE_XCTWAITER
5760
@MainActor
5861
internal var currentWaiter: XCTWaiter?
5962

@@ -87,6 +90,7 @@ open class XCTestCase: XCTest {
8790
}
8891
}
8992
}
93+
#endif
9094

9195
/// An internal object implementing performance measurements.
9296
internal var _performanceMeter: PerformanceMeter?
@@ -477,10 +481,10 @@ private final class ThrownErrorWrapper: @unchecked Sendable {
477481

478482
var error: Error? {
479483
get {
480-
XCTWaiter.subsystemQueue.sync { _error }
484+
XCTestCase.subsystemQueue.sync { _error }
481485
}
482486
set {
483-
XCTWaiter.subsystemQueue.sync { _error = newValue }
487+
XCTestCase.subsystemQueue.sync { _error = newValue }
484488
}
485489
}
486490
}

0 commit comments

Comments
 (0)