Skip to content

Commit

Permalink
Update available to fix build for non-macOS Apple platforms (#1746)
Browse files Browse the repository at this point in the history
## Motivation

The package currently doesn't build for non-macOS Apple platforms, e.g. iOS, because of missing `@available` annotations, mostly in tests.

## Modifications

- Add missing `@available` annotations.
- Use `#if os(macOS) || os(Linux)` in test utils that require `Foundation.Process`. Ideally we'd use `#if canImport(Foundation.Process)`, but the version of `swift-format` used by this project doesn't understand it.

## Result

Code and tests can build for, and run on, other platforms, e.g. iOS.
  • Loading branch information
simonjbeaumont authored Dec 19, 2023
1 parent 4570d0f commit 4bd354e
Show file tree
Hide file tree
Showing 28 changed files with 55 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Sources/Examples/PacketCapture/PacketCapture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import NIOExtras
import NIOPosix

@main
@available(macOS 10.15, *)
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
struct PCAP: AsyncParsableCommand {
@Option(help: "The port to connect to")
var port = 1234
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

#if os(macOS) || os(Linux) // swift-format doesn't like canImport(Foundation.Process)

import XCTest

@testable import GRPCCodeGen
Expand Down Expand Up @@ -517,3 +519,5 @@ extension SnippetBasedTranslatorTests {
)
}
}

#endif // os(macOS) || os(Linux)
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

#if os(macOS) || os(Linux) // swift-format doesn't like canImport(Foundation.Process)

import XCTest

private func diff(expected: String, actual: String) throws -> String {
Expand Down Expand Up @@ -65,3 +68,5 @@ internal func XCTAssertEqualWithDiff(
line: line
)
}

#endif // os(macOS) || os(Linux)
1 change: 1 addition & 0 deletions Tests/GRPCCoreTests/Call/Client/ClientResponseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import XCTest

@testable import GRPCCore

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
final class ClientResponseTests: XCTestCase {
func testAcceptedSingleResponseConvenienceMethods() {
let response = ClientResponse.Single(
Expand Down
1 change: 1 addition & 0 deletions Tests/GRPCCoreTests/Call/Server/ServerRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
@_spi(Testing) import GRPCCore
import XCTest

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
final class ServerRequestTests: XCTestCase {
func testSingleToStreamConversion() async throws {
let single = ServerRequest.Single(metadata: ["bar": "baz"], message: "foo")
Expand Down
1 change: 1 addition & 0 deletions Tests/GRPCCoreTests/Call/Server/ServerResponseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
@_spi(Testing) import GRPCCore
import XCTest

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
final class ServerResponseTests: XCTestCase {
func testSingleConvenienceInit() {
var response = ServerResponse.Single(
Expand Down
1 change: 1 addition & 0 deletions Tests/GRPCCoreTests/ServerErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import GRPCCore
import XCTest

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
final class ServerErrorTests: XCTestCase {
func testCopyOnWrite() {
// ServerError has a heap based storage, so check CoW semantics are correctly implemented.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import XCTest

@testable import GRPCCore

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
internal final class AsyncSequenceOfOneTests: XCTestCase {
func testSuccessPath() async throws {
let sequence = RPCAsyncSequence.one("foo")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,7 @@ final class BufferedStreamTests: XCTestCase {
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension BufferedStream.Source.WriteResult {
func assertIsProducerMore() {
switch self {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
* limitations under the License.
*/

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension AsyncSequence {
func collect() async throws -> [Element] {
return try await self.reduce(into: []) { $0.append($1) }
}
}

#if swift(<5.9)
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension AsyncStream {
static func makeStream(
of elementType: Element.Type = Element.self,
Expand All @@ -34,6 +36,7 @@ extension AsyncStream {
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension AsyncThrowingStream {
static func makeStream(
of elementType: Element.Type = Element.self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import Atomics
import GRPCCore

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension ClientInterceptor where Self == RejectAllClientInterceptor {
static func rejectAll(with error: RPCError) -> Self {
return RejectAllClientInterceptor(error: error, throw: false)
Expand All @@ -27,13 +28,15 @@ extension ClientInterceptor where Self == RejectAllClientInterceptor {

}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension ClientInterceptor where Self == RequestCountingClientInterceptor {
static func requestCounter(_ counter: ManagedAtomic<Int>) -> Self {
return RequestCountingClientInterceptor(counter: counter)
}
}

/// Rejects all RPCs with the provided error.
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
struct RejectAllClientInterceptor: ClientInterceptor {
/// The error to reject all RPCs with.
let error: RPCError
Expand Down Expand Up @@ -62,6 +65,7 @@ struct RejectAllClientInterceptor: ClientInterceptor {
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
struct RequestCountingClientInterceptor: ClientInterceptor {
/// The number of requests made.
let counter: ManagedAtomic<Int>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import Atomics
import GRPCCore

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension ServerInterceptor where Self == RejectAllServerInterceptor {
static func rejectAll(with error: RPCError) -> Self {
return RejectAllServerInterceptor(error: error, throw: false)
Expand All @@ -26,13 +27,15 @@ extension ServerInterceptor where Self == RejectAllServerInterceptor {
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension ServerInterceptor where Self == RequestCountingServerInterceptor {
static func requestCounter(_ counter: ManagedAtomic<Int>) -> Self {
return RequestCountingServerInterceptor(counter: counter)
}
}

/// Rejects all RPCs with the provided error.
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
struct RejectAllServerInterceptor: ServerInterceptor {
/// The error to reject all RPCs with.
let error: RPCError
Expand Down Expand Up @@ -61,6 +64,7 @@ struct RejectAllServerInterceptor: ServerInterceptor {
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
struct RequestCountingServerInterceptor: ServerInterceptor {
/// The number of requests made.
let counter: ManagedAtomic<Int>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
import GRPCCore

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension RPCAsyncSequence {
static func elements(_ elements: Element...) -> Self {
return .elements(elements)
Expand Down
3 changes: 3 additions & 0 deletions Tests/GRPCCoreTests/Test Utilities/RPCWriter+Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import GRPCCore
import XCTest

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension RPCWriter {
/// Returns a writer which calls `XCTFail(_:)` on every write.
static func failTestOnWrite(elementType: Element.Type = Element.self) -> Self {
Expand All @@ -28,12 +29,14 @@ extension RPCWriter {
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
private struct FailOnWrite<Element>: RPCWriterProtocol {
func write(contentsOf elements: some Sequence<Element>) async throws {
XCTFail("Unexpected write")
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
private struct AsyncStreamGatheringWriter<Element>: RPCWriterProtocol {
let continuation: AsyncStream<Element>.Continuation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import GRPCCore
import XCTest

@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
struct BinaryEcho: RegistrableRPCService {
func get(
_ request: ServerRequest.Single<[UInt8]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct ThrowOnStreamCreationTransport: ClientTransport {
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
struct ThrowOnRunServerTransport: ServerTransport {
func listen() async throws -> RPCAsyncSequence<RPCStream<Inbound, Outbound>> {
throw RPCError(
Expand All @@ -63,6 +64,7 @@ struct ThrowOnRunServerTransport: ServerTransport {
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
struct ThrowOnSignalServerTransport: ServerTransport {
let signal: AsyncStream<Void>

Expand Down
6 changes: 6 additions & 0 deletions Tests/GRPCCoreTests/Test Utilities/XCTest+Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func XCTAssertDescription(
XCTAssertEqual(String(describing: subject), expected, file: file, line: line)
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
func XCTAssertThrowsErrorAsync<T>(
_ expression: () async throws -> T,
errorHandler: (Error) -> Void
Expand All @@ -50,6 +51,7 @@ func XCTAssertThrowsError<T, E: Error>(
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
func XCTAssertThrowsErrorAsync<T, E: Error>(
ofType: E.Type = E.self,
_ expression: () async throws -> T,
Expand Down Expand Up @@ -78,6 +80,7 @@ func XCTAssertThrowsRPCError<T>(
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
func XCTAssertThrowsRPCErrorAsync<T>(
_ expression: () async throws -> T,
errorHandler: (RPCError) -> Void
Expand All @@ -92,6 +95,7 @@ func XCTAssertThrowsRPCErrorAsync<T>(
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
func XCTAssertRejected<T>(
_ response: ClientResponse.Stream<T>,
errorHandler: (RPCError) -> Void
Expand Down Expand Up @@ -128,6 +132,7 @@ func XCTAssertMetadata(
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
func XCTAssertMetadata(
_ part: RPCRequestPart?,
metadataHandler: (Metadata) async throws -> Void = { _ in }
Expand All @@ -152,6 +157,7 @@ func XCTAssertMessage(
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
func XCTAssertMessage(
_ part: RPCRequestPart?,
messageHandler: ([UInt8]) async throws -> Void = { _ in }
Expand Down
1 change: 1 addition & 0 deletions Tests/GRPCCoreTests/TimeoutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import XCTest

@testable import GRPCCore

@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
final class TimeoutTests: XCTestCase {
func testDecodeInvalidTimeout_Empty() {
let timeoutHeader = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import XCTest
@testable import GRPCCore
@testable import GRPCInProcessTransport

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
final class InProcessServerTransportTests: XCTestCase {
func testStartListening() async throws {
let transport = InProcessServerTransport()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func XCTAssertThrowsError<T, E: Error>(
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
func XCTAssertThrowsErrorAsync<T, E: Error>(
ofType: E.Type = E.self,
_ expression: () async throws -> T,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ internal final class ServerHandlerStateMachineTests: GRPCTestCase {

// MARK: - Action Assertions

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension ServerHandlerStateMachine.HandleMetadataAction {
func assertInvokeHandler() {
XCTAssertEqual(self, .invokeHandler)
Expand All @@ -275,6 +276,7 @@ extension ServerHandlerStateMachine.HandleMetadataAction {
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension ServerHandlerStateMachine.HandleMessageAction {
func assertForward() {
XCTAssertEqual(self, .forward)
Expand All @@ -285,6 +287,7 @@ extension ServerHandlerStateMachine.HandleMessageAction {
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension ServerHandlerStateMachine.SendMessageAction {
func assertInterceptHeadersThenMessage(_ verify: (HPACKHeaders) -> Void = { _ in }) {
switch self {
Expand All @@ -304,6 +307,7 @@ extension ServerHandlerStateMachine.SendMessageAction {
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension ServerHandlerStateMachine.SendStatusAction {
func assertIntercept(_ verify: (HPACKHeaders) -> Void = { _ in }) {
switch self {
Expand All @@ -319,6 +323,7 @@ extension ServerHandlerStateMachine.SendStatusAction {
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension ServerHandlerStateMachine.CancelAction {
func assertNone() {
XCTAssertEqual(self, .none)
Expand Down
2 changes: 1 addition & 1 deletion Tests/GRPCTests/EchoMetadataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ internal final class EchoMetadataTests: GRPCTestCase {
self.testServiceDescriptor(Echo_EchoClientMetadata.serviceDescriptor)
self.testServiceDescriptor(Echo_EchoServerMetadata.serviceDescriptor)

if #available(macOS 12, *) {
if #available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) {
self.testServiceDescriptor(Echo_EchoAsyncClient.serviceDescriptor)
}
}
Expand Down
1 change: 1 addition & 0 deletions Tests/GRPCTests/GRPCAsyncClientCallTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ private actor RequestResponseCounter {
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
private final class AsyncEchoProvider: Echo_EchoAsyncProvider {
let headers: HPACKHeaders
let sendTwice: Bool
Expand Down
1 change: 1 addition & 0 deletions Tests/GRPCTests/GRPCAsyncServerHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ class AsyncServerHandlerTests: GRPCTestCase {
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
internal final class AsyncResponseStream: GRPCServerResponseWriter {
private let source:
NIOAsyncSequenceProducer<
Expand Down
2 changes: 1 addition & 1 deletion Tests/GRPCTests/GRPCNetworkFrameworkTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import NIOTransportServices
import Security
import XCTest

@available(macOS 10.14, iOS 12.0, watchOS 6.0, tvOS 12.0, *)
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
final class GRPCNetworkFrameworkTests: GRPCTestCase {
private var server: Server!
private var client: ClientConnection!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import XCTest

@testable import GRPCReflectionService

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
final class ReflectionServiceIntegrationTests: GRPCTestCase {
private var server: Server?
private var channel: GRPCChannel?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import XCTest

@testable import GRPCReflectionService

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
final class ReflectionServiceUnitTests: GRPCTestCase {
/// Testing the fileDescriptorDataByFilename dictionary of the ReflectionServiceData object.
func testFileDescriptorDataByFilename() throws {
Expand Down
Loading

0 comments on commit 4bd354e

Please sign in to comment.