Skip to content

Commit 289f9fd

Browse files
committed
Update tests to throw XCTSkip when required frameworks are unvailable rather than disabiling them altogether
1 parent 15e3089 commit 289f9fd

9 files changed

+160
-31
lines changed

Tests/GRDBTests/DatabaseMigratorTests.swift

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import XCTest
22
import GRDB
33

4-
#if !os(Android)
54
class DatabaseMigratorTests : GRDBTestCase {
65
// Test passes if it compiles.
76
// See <https://github.com/groue/GRDB.swift/issues/1541>
@@ -44,9 +43,13 @@ class DatabaseMigratorTests : GRDBTestCase {
4443
func testEmptyMigratorPublisher() throws {
4544
func test(writer: some DatabaseWriter) throws {
4645
let migrator = DatabaseMigrator()
46+
#if !canImport(Combine)
47+
throw XCTSkip("Combine not supported on platform")
48+
#else
4749
let publisher = migrator.migratePublisher(writer)
4850
let recorder = publisher.record()
4951
try wait(for: recorder.single, timeout: 1)
52+
#endif
5053
}
5154

5255
try Test(test).run { try DatabaseQueue() }
@@ -176,23 +179,31 @@ class DatabaseMigratorTests : GRDBTestCase {
176179
}
177180

178181
do {
182+
#if !canImport(Combine)
183+
throw XCTSkip("Combine not supported on platform")
184+
#else
179185
let publisher = migrator.migratePublisher(writer)
180186
let recorder = publisher.record()
181187
try wait(for: recorder.single, timeout: 1)
182188
try writer.read { db in
183189
XCTAssertTrue(try db.tableExists("persons"))
184190
XCTAssertTrue(try db.tableExists("pets"))
185191
}
192+
#endif
186193
}
187194

188195
do {
196+
#if !canImport(Combine)
197+
throw XCTSkip("Combine not supported on platform")
198+
#else
189199
let publisher = migrator2.migratePublisher(writer)
190200
let recorder = publisher.record()
191201
try wait(for: recorder.single, timeout: 1)
192202
try writer.read { db in
193203
XCTAssertTrue(try db.tableExists("persons"))
194204
XCTAssertFalse(try db.tableExists("pets"))
195205
}
206+
#endif
196207
}
197208
}
198209

@@ -206,6 +217,9 @@ class DatabaseMigratorTests : GRDBTestCase {
206217
let migrator = DatabaseMigrator()
207218
let expectation = self.expectation(description: "")
208219
let semaphore = DispatchSemaphore(value: 0)
220+
#if !canImport(Combine)
221+
throw XCTSkip("Combine not supported on platform")
222+
#else
209223
let cancellable = migrator.migratePublisher(writer).sink(
210224
receiveCompletion: { _ in },
211225
receiveValue: { _ in
@@ -216,6 +230,7 @@ class DatabaseMigratorTests : GRDBTestCase {
216230
semaphore.signal()
217231
waitForExpectations(timeout: 5, handler: nil)
218232
cancellable.cancel()
233+
#endif
219234
}
220235

221236
try Test(test).run { try DatabaseQueue() }
@@ -229,6 +244,9 @@ class DatabaseMigratorTests : GRDBTestCase {
229244
migrator.registerMigration("first", migrate: { _ in })
230245
let expectation = self.expectation(description: "")
231246
let semaphore = DispatchSemaphore(value: 0)
247+
#if !canImport(Combine)
248+
throw XCTSkip("Combine not supported on platform")
249+
#else
232250
let cancellable = migrator.migratePublisher(writer).sink(
233251
receiveCompletion: { _ in },
234252
receiveValue: { _ in
@@ -239,6 +257,7 @@ class DatabaseMigratorTests : GRDBTestCase {
239257
semaphore.signal()
240258
waitForExpectations(timeout: 5, handler: nil)
241259
cancellable.cancel()
260+
#endif
242261
}
243262

244263
try Test(test).run { try DatabaseQueue() }
@@ -247,11 +266,14 @@ class DatabaseMigratorTests : GRDBTestCase {
247266
}
248267

249268
func testMigratorPublisherDefaultScheduler() throws {
250-
func test<Writer: DatabaseWriter>(writer: Writer) {
269+
func test<Writer: DatabaseWriter>(writer: Writer) throws {
251270
var migrator = DatabaseMigrator()
252271
migrator.registerMigration("first", migrate: { _ in })
253272
let expectation = self.expectation(description: "")
254273
expectation.expectedFulfillmentCount = 2 // value + completion
274+
#if !canImport(Combine)
275+
throw XCTSkip("Combine not supported on platform")
276+
#else
255277
let cancellable = migrator.migratePublisher(writer).sink(
256278
receiveCompletion: { completion in
257279
dispatchPrecondition(condition: .onQueue(.main))
@@ -264,6 +286,7 @@ class DatabaseMigratorTests : GRDBTestCase {
264286

265287
waitForExpectations(timeout: 5, handler: nil)
266288
cancellable.cancel()
289+
#endif
267290
}
268291

269292
try Test(test).run { try DatabaseQueue() }
@@ -272,12 +295,15 @@ class DatabaseMigratorTests : GRDBTestCase {
272295
}
273296

274297
func testMigratorPublisherCustomScheduler() throws {
275-
func test<Writer: DatabaseWriter>(writer: Writer) {
298+
func test<Writer: DatabaseWriter>(writer: Writer) throws {
276299
var migrator = DatabaseMigrator()
277300
migrator.registerMigration("first", migrate: { _ in })
278301
let queue = DispatchQueue(label: "test")
279302
let expectation = self.expectation(description: "")
280303
expectation.expectedFulfillmentCount = 2 // value + completion
304+
#if !canImport(Combine)
305+
throw XCTSkip("Combine not supported on platform")
306+
#else
281307
let cancellable = migrator.migratePublisher(writer, receiveOn: queue).sink(
282308
receiveCompletion: { completion in
283309
dispatchPrecondition(condition: .onQueue(queue))
@@ -290,6 +316,7 @@ class DatabaseMigratorTests : GRDBTestCase {
290316

291317
waitForExpectations(timeout: 5, handler: nil)
292318
cancellable.cancel()
319+
#endif
293320
}
294321

295322
try Test(test).run { try DatabaseQueue() }
@@ -1111,4 +1138,3 @@ class DatabaseMigratorTests : GRDBTestCase {
11111138
} catch DatabaseError.SQLITE_CONSTRAINT_FOREIGNKEY { }
11121139
}
11131140
}
1114-
#endif

Tests/GRDBTests/DatabasePoolConcurrencyTests.swift

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import Dispatch
33
import Foundation
44
@testable import GRDB
55

6-
#if canImport(Darwin) // needs NSFileCoordinator
76
class DatabasePoolConcurrencyTests: GRDBTestCase {
87

98
func testDatabasePoolFundamental1() throws {
@@ -880,8 +879,10 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
880879

881880
// This test CAN break in future releases: the dispatch queue labels
882881
// are documented to be a debug-only tool.
882+
#if canImport(Darwin) // __dispatch_queue_get_label unavailable on non-Darwin platforms
883883
let label = String(utf8String: __dispatch_queue_get_label(nil))
884884
XCTAssertEqual(label, "GRDB.DatabasePool.writer")
885+
#endif
885886
}
886887

887888
let s1 = DispatchSemaphore(value: 0)
@@ -893,9 +894,11 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
893894

894895
// This test CAN break in future releases: the dispatch queue labels
895896
// are documented to be a debug-only tool.
897+
#if canImport(Darwin) // __dispatch_queue_get_label unavailable on non-Darwin platforms
896898
let label = String(utf8String: __dispatch_queue_get_label(nil))
897899
XCTAssertEqual(label, "GRDB.DatabasePool.reader.1")
898-
900+
#endif
901+
899902
_ = s1.signal()
900903
_ = s2.wait(timeout: .distantFuture)
901904
}
@@ -909,8 +912,10 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
909912

910913
// This test CAN break in future releases: the dispatch queue labels
911914
// are documented to be a debug-only tool.
915+
#if canImport(Darwin) // __dispatch_queue_get_label unavailable on non-Darwin platforms
912916
let label = String(utf8String: __dispatch_queue_get_label(nil))
913917
XCTAssertEqual(label, "GRDB.DatabasePool.reader.2")
918+
#endif
914919
}
915920
}
916921
let blocks = [block1, block2]
@@ -928,8 +933,10 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
928933

929934
// This test CAN break in future releases: the dispatch queue labels
930935
// are documented to be a debug-only tool.
936+
#if canImport(Darwin) // __dispatch_queue_get_label unavailable on non-Darwin platforms
931937
let label = String(utf8String: __dispatch_queue_get_label(nil))
932938
XCTAssertEqual(label, "Toreador.writer")
939+
#endif
933940
}
934941

935942
let s1 = DispatchSemaphore(value: 0)
@@ -941,9 +948,11 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
941948

942949
// This test CAN break in future releases: the dispatch queue labels
943950
// are documented to be a debug-only tool.
951+
#if canImport(Darwin) // __dispatch_queue_get_label unavailable on non-Darwin platforms
944952
let label = String(utf8String: __dispatch_queue_get_label(nil))
945953
XCTAssertEqual(label, "Toreador.reader.1")
946-
954+
#endif
955+
947956
_ = s1.signal()
948957
_ = s2.wait(timeout: .distantFuture)
949958
}
@@ -957,8 +966,10 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
957966

958967
// This test CAN break in future releases: the dispatch queue labels
959968
// are documented to be a debug-only tool.
969+
#if canImport(Darwin) // __dispatch_queue_get_label unavailable on non-Darwin platforms
960970
let label = String(utf8String: __dispatch_queue_get_label(nil))
961971
XCTAssertEqual(label, "Toreador.reader.2")
972+
#endif
962973
}
963974
}
964975
let blocks = [block1, block2]
@@ -1056,6 +1067,9 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
10561067
// > because DISPATCH_QUEUE_OVERCOMMIT is not a public API. I don't
10571068
// > know of a way to get a reference to the overcommit queue using
10581069
// > only public APIs.
1070+
#if !canImport(Darwin)
1071+
throw XCTSkip("__dispatch_get_global_queue unavailable")
1072+
#else
10591073
let DISPATCH_QUEUE_OVERCOMMIT: UInt = 2
10601074
let targetQueue = __dispatch_get_global_queue(
10611075
Int(qos.qosClass.rawValue.rawValue),
@@ -1069,6 +1083,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
10691083
try dbPool.read { _ in
10701084
dispatchPrecondition(condition: .onQueue(targetQueue))
10711085
}
1086+
#endif
10721087
}
10731088

10741089
try test(qos: .background)
@@ -1255,6 +1270,9 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
12551270
// MARK: - Concurrent opening
12561271

12571272
func testConcurrentOpening() throws {
1273+
#if !canImport(ObjectiveC)
1274+
throw XCTSkip("NSFileCoordinator unavailable")
1275+
#else
12581276
for _ in 0..<50 {
12591277
let dbDirectoryName = "DatabasePoolConcurrencyTests-\(ProcessInfo.processInfo.globallyUniqueString)"
12601278
let directoryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
@@ -1278,13 +1296,17 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
12781296
XCTAssert(poolError ?? coordinatorError == nil)
12791297
}
12801298
}
1299+
#endif
12811300
}
12821301

12831302
// MARK: - NSFileCoordinator sample code tests
12841303

12851304
// Test for sample code in Documentation.docc/DatabaseSharing.md.
12861305
// This test passes if this method compiles
12871306
private func openSharedDatabase(at databaseURL: URL) throws -> DatabasePool {
1307+
#if !canImport(ObjectiveC)
1308+
throw XCTSkip("NSFileCoordinator unavailable")
1309+
#else
12881310
let coordinator = NSFileCoordinator(filePresenter: nil)
12891311
var coordinatorError: NSError?
12901312
var dbPool: DatabasePool?
@@ -1300,6 +1322,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
13001322
throw error
13011323
}
13021324
return dbPool!
1325+
#endif
13031326
}
13041327

13051328
// Test for sample code in Documentation.docc/DatabaseSharing.md.
@@ -1314,6 +1337,9 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
13141337
// Test for sample code in Documentation.docc/DatabaseSharing.md.
13151338
// This test passes if this method compiles
13161339
private func openSharedReadOnlyDatabase(at databaseURL: URL) throws -> DatabasePool? {
1340+
#if !canImport(ObjectiveC)
1341+
throw XCTSkip("NSFileCoordinator unavailable")
1342+
#else
13171343
let coordinator = NSFileCoordinator(filePresenter: nil)
13181344
var coordinatorError: NSError?
13191345
var dbPool: DatabasePool?
@@ -1329,6 +1355,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
13291355
throw error
13301356
}
13311357
return dbPool
1358+
#endif
13321359
}
13331360

13341361
// Test for sample code in Documentation.docc/DatabaseSharing.md.
@@ -1349,4 +1376,3 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
13491376
}
13501377
}
13511378
}
1352-
#endif

Tests/GRDBTests/DatabaseQueueTests.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import XCTest
22
import Dispatch
33
import GRDB
44

5-
#if canImport(Darwin) // needed for __dispatch_get_global_queue
65
class DatabaseQueueTests: GRDBTestCase {
76
func testJournalModeConfiguration() throws {
87
do {
@@ -132,8 +131,10 @@ class DatabaseQueueTests: GRDBTestCase {
132131

133132
// This test CAN break in future releases: the dispatch queue labels
134133
// are documented to be a debug-only tool.
134+
#if canImport(Darwin) // __dispatch_queue_get_label unavailable on non-Darwin platforms
135135
let label = String(utf8String: __dispatch_queue_get_label(nil))
136136
XCTAssertEqual(label, "GRDB.DatabaseQueue")
137+
#endif
137138
}
138139
}
139140

@@ -147,8 +148,10 @@ class DatabaseQueueTests: GRDBTestCase {
147148

148149
// This test CAN break in future releases: the dispatch queue labels
149150
// are documented to be a debug-only tool.
151+
#if canImport(Darwin) // __dispatch_queue_get_label unavailable on non-Darwin platforms
150152
let label = String(utf8String: __dispatch_queue_get_label(nil))
151153
XCTAssertEqual(label, "Toreador")
154+
#endif
152155
}
153156
}
154157

@@ -240,6 +243,9 @@ class DatabaseQueueTests: GRDBTestCase {
240243
// > because DISPATCH_QUEUE_OVERCOMMIT is not a public API. I don't
241244
// > know of a way to get a reference to the overcommit queue using
242245
// > only public APIs.
246+
#if !canImport(Darwin)
247+
throw XCTSkip("__dispatch_get_global_queue unavailable")
248+
#else
243249
let DISPATCH_QUEUE_OVERCOMMIT: UInt = 2
244250
let targetQueue = __dispatch_get_global_queue(
245251
Int(qos.qosClass.rawValue.rawValue),
@@ -253,6 +259,7 @@ class DatabaseQueueTests: GRDBTestCase {
253259
try dbQueue.read { _ in
254260
dispatchPrecondition(condition: .onQueue(targetQueue))
255261
}
262+
#endif
256263
}
257264

258265
try test(qos: .background)
@@ -474,4 +481,3 @@ class DatabaseQueueTests: GRDBTestCase {
474481
dbQueue.releaseMemory()
475482
}
476483
}
477-
#endif

0 commit comments

Comments
 (0)