Skip to content

Commit ebce1c5

Browse files
committed
Fix ByteBufferTests when compiling with Swift 6 but running on macOS 14.
Writing `@available(someOS...)` on an XCTest test method only affects whether the compiler allows known-available APIs to be called, but it doesn't actually prevent the test from running on older OSes (XCTest doesn't see the annotation since it's not part of the Objective-C runtime metadata). So if this code is compiled with Swift 6 but run on an older OS, it will segfault when it tries to call an API that doesn't exist on that platform. These tests need to use an `#available` guard instead to make the decision to skip them at runtime.
1 parent ae60dc3 commit ebce1c5

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

Diff for: Tests/NIOCoreTests/ByteBufferTest.swift

+18-6
Original file line numberDiff line numberDiff line change
@@ -1315,9 +1315,11 @@ class ByteBufferTest: XCTestCase {
13151315
XCTAssertEqual("a", buf.readString(length: 1))
13161316
}
13171317

1318-
@available(macOS 15, iOS 18, tvOS 18, watchOS 11, *)
13191318
func testReadUTF8ValidatedString() throws {
13201319
#if compiler(>=6)
1320+
guard #available(macOS 15, iOS 18, tvOS 18, watchOS 11, *) else {
1321+
throw XCTSkip("'readUTF8ValidatedString' is only available in Swift 6 and later")
1322+
}
13211323
buf.clear()
13221324
let expected = "hello"
13231325
buf.writeString(expected)
@@ -1330,9 +1332,11 @@ class ByteBufferTest: XCTestCase {
13301332
#endif // compiler(>=6)
13311333
}
13321334

1333-
@available(macOS 15, iOS 18, tvOS 18, watchOS 11, *)
13341335
func testGetUTF8ValidatedString() throws {
13351336
#if compiler(>=6)
1337+
guard #available(macOS 15, iOS 18, tvOS 18, watchOS 11, *) else {
1338+
throw XCTSkip("'getUTF8ValidatedString' is only available in Swift 6 and later")
1339+
}
13361340
buf.clear()
13371341
let expected = "hello, goodbye"
13381342
buf.writeString(expected)
@@ -1343,9 +1347,11 @@ class ByteBufferTest: XCTestCase {
13431347
#endif // compiler(>=6)
13441348
}
13451349

1346-
@available(macOS 15, iOS 18, tvOS 18, watchOS 11, *)
13471350
func testReadUTF8InvalidString() throws {
13481351
#if compiler(>=6)
1352+
guard #available(macOS 15, iOS 18, tvOS 18, watchOS 11, *) else {
1353+
throw XCTSkip("'readUTF8ValidatedString' is only available in Swift 6 and later")
1354+
}
13491355
buf.clear()
13501356
buf.writeBytes([UInt8](repeating: 255, count: 16))
13511357
XCTAssertThrowsError(try buf.readUTF8ValidatedString(length: 16)) { error in
@@ -4222,8 +4228,10 @@ extension ByteBufferTest {
42224228
// MARK: - peekUTF8ValidatedString Tests (available in Swift 6+)
42234229

42244230
#if compiler(>=6)
4225-
@available(macOS 15, iOS 18, tvOS 18, watchOS 11, *)
42264231
func testPeekUTF8ValidatedString_Normal() throws {
4232+
guard #available(macOS 15, iOS 18, tvOS 18, watchOS 11, *) else {
4233+
throw XCTSkip("'peekUTF8ValidatedString' is only available in Swift 6 and later")
4234+
}
42274235
var buffer = ByteBuffer()
42284236
let testString = "UTF8 Validated"
42294237
let written = buffer.writeString(testString)
@@ -4232,17 +4240,21 @@ extension ByteBufferTest {
42324240
XCTAssertEqual(buffer.readerIndex, 0, "Reader index should remain unchanged.")
42334241
}
42344242

4235-
@available(macOS 15, iOS 18, tvOS 18, watchOS 11, *)
42364243
func testPeekUTF8ValidatedString_Empty() throws {
4244+
guard #available(macOS 15, iOS 18, tvOS 18, watchOS 11, *) else {
4245+
throw XCTSkip("'peekUTF8ValidatedString' is only available in Swift 6 and later")
4246+
}
42374247
var buffer = ByteBuffer()
42384248
_ = buffer.writeString("")
42394249
let peeked = try buffer.peekUTF8ValidatedString(length: 0)
42404250
XCTAssertEqual(peeked, "", "peekUTF8ValidatedString() should return an empty string when no bytes are written.")
42414251
XCTAssertEqual(buffer.readerIndex, 0, "Reader index should remain unchanged for empty peek.")
42424252
}
42434253

4244-
@available(macOS 15, iOS 18, tvOS 18, watchOS 11, *)
42454254
func testPeekUTF8ValidatedString_Repeated() throws {
4255+
guard #available(macOS 15, iOS 18, tvOS 18, watchOS 11, *) else {
4256+
throw XCTSkip("'peekUTF8ValidatedString' is only available in Swift 6 and later")
4257+
}
42464258
var buffer = ByteBuffer()
42474259
let testString = "Repeat UTF8"
42484260
let written = buffer.writeString(testString)

0 commit comments

Comments
 (0)