Skip to content

Commit

Permalink
Fix tests and a crash under linux and swift 6 (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpierreroy authored Sep 26, 2024
1 parent f0bc00d commit a888472
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
12 changes: 6 additions & 6 deletions Sources/AppStoreConnect/Networking/Transport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ extension URLSession: Transport {
public func send(request: URLRequest, decoder: JSONDecoder) async throws -> Response<Data> {
// These depend on swift-corelibs-foundation, which have not implemented the
// Task-based API for URLSession.
#if (os(Linux) || os(Windows)) && swift(<6.0)
#if (os(Linux) || os(Windows))
return try await withCheckedThrowingContinuation { continuation in
send(request: request, decoder: decoder, completion: continuation.resume)
send(request: request, decoder: decoder) { result in continuation.resume(with: result) }
}
#else
let (data, urlResponse) = try await data(for: request)
Expand Down Expand Up @@ -120,9 +120,9 @@ extension URLSession: Transport {
public func download(request: URLRequest) async throws -> Response<URL> {
// These depend on swift-corelibs-foundation, which have not implemented the
// Task-based API for URLSession.
#if (os(Linux) || os(Windows)) && swift(<6.0)
#if (os(Linux) || os(Windows))
return try await withCheckedThrowingContinuation { continuation in
download(request: request, completion: continuation.resume)
download(request: request) { result in continuation.resume(with: result) }
}
#else
let (fileURL, urlResponse) = try await download(for: request)
Expand Down Expand Up @@ -191,9 +191,9 @@ extension URLSession: Transport {
public func upload(request: URLRequest, data: Data, decoder: JSONDecoder) async throws -> Response<Data> {
// These depend on swift-corelibs-foundation, which have not implemented the
// Task-based API for URLSession.
#if (os(Linux) || os(Windows)) && swift(<6.0)
#if (os(Linux) || os(Windows))
return try await withCheckedThrowingContinuation { continuation in
upload(request: request, data: data, decoder: decoder, completion: continuation.resume)
upload(request: request, data: data, decoder: decoder) { result in continuation.resume(with: result) }
}
#else
let (responseData, urlResponse) = try await upload(for: request, from: data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import XCTest
#endif

final class AppStoreConnectClientTests: XCTestCase {
private struct TestData {
private struct TestData: Sendable {
enum Case {
case success
case successPaginated
Expand Down
26 changes: 13 additions & 13 deletions Tests/AppStoreConnectTests/TransportTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extension URLRequest {
}

class TransportTests: XCTestCase {
private func createSession() -> URLSession {
private static func createSession() -> URLSession {
let config = URLSessionConfiguration.ephemeral
config.protocolClasses = [MockURLProtocol.self]

Expand Down Expand Up @@ -88,15 +88,15 @@ class TransportTests: XCTestCase {
func testURLSessionSendRequest() async throws {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom(decodeISO8601Date(with:))
_ = try await createSession()
_ = try await Self.createSession()
.send(request: .testSendAsync, decoder: decoder)
}

func testURLSessionSendRequestFailure() async throws {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom(decodeISO8601Date(with:))
try await XCTAssertThrowsError(
await createSession()
await Self.createSession()
.send(request: .testSendAsyncError, decoder: decoder)
)
}
Expand All @@ -105,7 +105,7 @@ class TransportTests: XCTestCase {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom(decodeISO8601Date(with:))
let expectation = XCTestExpectation(description: "test-send-closure")
createSession()
Self.createSession()
.send(request: .testSendClosure, decoder: decoder) { result in
XCTAssertNoThrow({ try result.get() })
expectation.fulfill()
Expand All @@ -116,26 +116,26 @@ class TransportTests: XCTestCase {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom(decodeISO8601Date(with:))
let expectation = XCTestExpectation(description: "test-send-closure-error")
createSession()
Self.createSession()
.send(request: .testSendClosureError, decoder: decoder) { result in
expectation.fulfill()
}
}

func testURLSessionDownloadRequest() async throws {
_ = try await createSession()
_ = try await Self.createSession()
.download(request: .testDownloadAsync)
}

func testURLSessionDownloadRequestFailure() async throws {
try await XCTAssertThrowsError(
await createSession().download(request: .testDownloadAsyncError)
await Self.createSession().download(request: .testDownloadAsyncError)
)
}

func testURLSessionDownloadRequestCompletion() {
let expectation = XCTestExpectation(description: "test-download-closure")
createSession()
Self.createSession()
.download(request: .testDownloadClosure) { result in
XCTAssertNoThrow({ try result.get() })
expectation.fulfill()
Expand All @@ -144,7 +144,7 @@ class TransportTests: XCTestCase {

func testURLSessionDownloadRequestCompletionFailure() {
let expectation = XCTestExpectation(description: "test-download-closure-error")
createSession()
Self.createSession()
.download(request: .testDownloadClosureError) { result in
expectation.fulfill()
}
Expand All @@ -153,15 +153,15 @@ class TransportTests: XCTestCase {
func testURLSessionUploadRequest() async throws {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom(decodeISO8601Date(with:))
_ = try await createSession()
_ = try await Self.createSession()
.upload(request: .testUploadAsync, data: Data(), decoder: decoder)
}

func testURLSessionUploadRequestFailure() async throws {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom(decodeISO8601Date(with:))
try await XCTAssertThrowsError(
await createSession()
await Self.createSession()
.upload(request: .testUploadAsyncError, data: Data(), decoder: decoder)
)
}
Expand All @@ -170,7 +170,7 @@ class TransportTests: XCTestCase {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom(decodeISO8601Date(with:))
let expectation = XCTestExpectation(description: "test-upload-closure")
createSession()
Self.createSession()
.upload(request: .testUploadClosure, data: Data(), decoder: decoder) { result in
XCTAssertNoThrow({ try result.get() })
expectation.fulfill()
Expand All @@ -181,7 +181,7 @@ class TransportTests: XCTestCase {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom(decodeISO8601Date(with:))
let expectation = XCTestExpectation(description: "test-upload-closure-error")
createSession()
Self.createSession()
.upload(request: .testUploadClosureError, data: Data(), decoder: decoder) { result in
expectation.fulfill()
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Mocks/MockData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Foundation
import FoundationNetworking
#endif

public struct MockResources {
public struct MockResources: Sendable {
public struct Content: Codable, Equatable, Sendable {
public var name: String
public var age: Int
Expand Down
4 changes: 2 additions & 2 deletions Tests/Mocks/XCTAsserts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import XCTest

// swift-format-ignore: AlwaysUseLowerCamelCase
public func XCTAssertThrowsError<T>(
_ expression: @autoclosure () async throws -> T,
_ expression: @autoclosure @Sendable () async throws -> T,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath,
line: UInt = #line,
_ errorHandler: (_ error: any Error) -> Void = { _ in }
_ errorHandler: @Sendable (_ error: any Error) -> Void = { _ in }
) async {
do {
_ = try await expression()
Expand Down

0 comments on commit a888472

Please sign in to comment.