Prechádzať zdrojové kódy

Replace Some Platform Checks With canImport Checks (#3756)

### Issue Link :link:
This PR replaces #3741.

### Goals :soccer:
This PR builds on #3741 to replace many platform `#if os` checks with
`#if canImport` for the relevant frameworks.

---------

Co-authored-by: brennobemoura <37243584+brennobemoura@users.noreply.github.com>
Jon Shier 2 rokov pred
rodič
commit
9f828f62e2

+ 12 - 8
Source/AFError.swift

@@ -24,6 +24,10 @@
 
 import Foundation
 
+#if canImport(Security)
+import Security
+#endif
+
 /// `AFError` is the error type returned by Alamofire. It encompasses a few different types of errors, each with
 /// their own associated reasons.
 public enum AFError: Error {
@@ -129,7 +133,7 @@ public enum AFError: Error {
         case invalidEmptyResponse(type: String)
     }
 
-    #if !(os(Linux) || os(Windows))
+    #if canImport(Security)
     /// Underlying reason a server trust evaluation error occurred.
     public enum ServerTrustFailureReason {
         /// The output of a server trust evaluation.
@@ -211,7 +215,7 @@ public enum AFError: Error {
     case responseValidationFailed(reason: ResponseValidationFailureReason)
     /// Response serialization failed.
     case responseSerializationFailed(reason: ResponseSerializationFailureReason)
-    #if !(os(Linux) || os(Windows))
+    #if canImport(Security)
     /// `ServerTrustEvaluating` instance threw an error during trust evaluation.
     case serverTrustEvaluationFailed(reason: ServerTrustFailureReason)
     #endif
@@ -314,7 +318,7 @@ extension AFError {
         return false
     }
 
-    #if !(os(Linux) || os(Windows))
+    #if canImport(Security)
     /// Returns whether the instance is `.serverTrustEvaluationFailed`. When `true`, the `underlyingError` property will
     /// contain the associated value.
     public var isServerTrustEvaluationError: Bool {
@@ -393,7 +397,7 @@ extension AFError {
             return reason.underlyingError
         case let .responseSerializationFailed(reason):
             return reason.underlyingError
-        #if !(os(Linux) || os(Windows))
+        #if canImport(Security)
         case let .serverTrustEvaluationFailed(reason):
             return reason.underlyingError
         #endif
@@ -451,7 +455,7 @@ extension AFError {
         return destination
     }
 
-    #if !(os(Linux) || os(Windows))
+    #if canImport(Security)
     /// The download resume data of any underlying network error. Only produced by `DownloadRequest`s.
     public var downloadResumeData: Data? {
         (underlyingError as? URLError)?.userInfo[NSURLSessionDownloadTaskResumeData] as? Data
@@ -610,7 +614,7 @@ extension AFError.ResponseSerializationFailureReason {
     }
 }
 
-#if !(os(Linux) || os(Windows))
+#if canImport(Security)
 extension AFError.ServerTrustFailureReason {
     var output: AFError.ServerTrustFailureReason.Output? {
         switch self {
@@ -688,7 +692,7 @@ extension AFError: LocalizedError {
             """
         case let .sessionInvalidated(error):
             return "Session was invalidated with error: \(error?.localizedDescription ?? "No description.")"
-        #if !(os(Linux) || os(Windows))
+        #if canImport(Security)
         case let .serverTrustEvaluationFailed(reason):
             return "Server trust evaluation failed due to reason: \(reason.localizedDescription)"
         #endif
@@ -822,7 +826,7 @@ extension AFError.ResponseValidationFailureReason {
     }
 }
 
-#if !(os(Linux) || os(Windows))
+#if canImport(Security)
 extension AFError.ServerTrustFailureReason {
     var localizedDescription: String {
         switch self {

+ 3 - 3
Source/MultipartFormData.swift

@@ -24,9 +24,9 @@
 
 import Foundation
 
-#if os(iOS) || os(watchOS) || os(tvOS)
+#if canImport(MobileCoreServices)
 import MobileCoreServices
-#elseif os(macOS)
+#elseif canImport(CoreServices)
 import CoreServices
 #endif
 
@@ -586,7 +586,7 @@ extension MultipartFormData {
     // MARK: - Private - Mime Type
 
     private func mimeType(forPathExtension pathExtension: String) -> String {
-        #if !(os(Linux) || os(Windows))
+        #if canImport(CoreServices) || canImport(MobileCoreServices)
         if
             let id = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension as CFString, nil)?.takeRetainedValue(),
             let contentType = UTTypeCopyPreferredTagWithClass(id, kUTTagClassMIMEType)?.takeRetainedValue() {

+ 1 - 1
Source/NetworkReachabilityManager.swift

@@ -22,7 +22,7 @@
 //  THE SOFTWARE.
 //
 
-#if !(os(watchOS) || os(Linux) || os(Windows))
+#if canImport(SystemConfiguration)
 
 import Foundation
 import SystemConfiguration

+ 2 - 2
Source/Protected.swift

@@ -55,7 +55,7 @@ extension NSLock: Lock {}
 
 #endif
 
-#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
+#if canImport(Darwin)
 /// An `os_unfair_lock` wrapper.
 final class UnfairLock: Lock {
     private let unfairLock: os_unfair_lock_t
@@ -84,7 +84,7 @@ final class UnfairLock: Lock {
 @propertyWrapper
 @dynamicMemberLookup
 final class Protected<T> {
-    #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
+    #if canImport(Darwin)
     private let lock = UnfairLock()
     #elseif os(Linux) || os(Windows)
     private let lock = NSLock()

+ 3 - 3
Source/Request.swift

@@ -1315,7 +1315,7 @@ public final class DataStreamRequest: Request {
 
     func didReceive(data: Data) {
         $streamMutableState.write { state in
-            #if !(os(Linux) || os(Windows))
+            #if !canImport(FoundationNetworking) // If we not using swift-corelibs-foundation.
             if let stream = state.outputStream {
                 underlyingQueue.async {
                     var bytes = Array(data)
@@ -1356,7 +1356,7 @@ public final class DataStreamRequest: Request {
         return self
     }
 
-    #if !(os(Linux) || os(Windows))
+    #if !canImport(FoundationNetworking) // If we not using swift-corelibs-foundation.
     /// Produces an `InputStream` that receives the `Data` received by the instance.
     ///
     /// - Note: The `InputStream` produced by this method must have `open()` called before being able to read `Data`.
@@ -1554,7 +1554,7 @@ public class DownloadRequest: Request {
     ///
     /// - Note: For more information about `resumeData`, see [Apple's documentation](https://developer.apple.com/documentation/foundation/urlsessiondownloadtask/1411634-cancel).
     public var resumeData: Data? {
-        #if !(os(Linux) || os(Windows))
+        #if !canImport(FoundationNetworking) // If we not using swift-corelibs-foundation.
         return $mutableDownloadState.resumeData ?? error?.downloadResumeData
         #else
         return $mutableDownloadState.resumeData

+ 2 - 2
Source/ServerTrustEvaluation.swift

@@ -48,7 +48,7 @@ open class ServerTrustManager {
         self.evaluators = evaluators
     }
 
-    #if !(os(Linux) || os(Windows))
+    #if canImport(Security)
     /// Returns the `ServerTrustEvaluating` value for the given host, if one is set.
     ///
     /// By default, this method will return the policy that perfectly matches the given host. Subclasses could override
@@ -91,7 +91,7 @@ public protocol ServerTrustEvaluating {
 
 // MARK: - Server Trust Evaluators
 
-#if !(os(Linux) || os(Windows))
+#if canImport(Security)
 /// An evaluator which uses the default server trust evaluation while allowing you to control whether to validate the
 /// host provided by the challenge. Applications are encouraged to always validate the host in production environments
 /// to guarantee the validity of the server's certificate chain.

+ 2 - 2
Source/SessionDelegate.swift

@@ -94,7 +94,7 @@ extension SessionDelegate: URLSessionTaskDelegate {
         case NSURLAuthenticationMethodHTTPBasic, NSURLAuthenticationMethodHTTPDigest, NSURLAuthenticationMethodNTLM,
              NSURLAuthenticationMethodNegotiate:
             evaluation = attemptCredentialAuthentication(for: challenge, belongingTo: task)
-        #if !(os(Linux) || os(Windows))
+        #if canImport(Security)
         case NSURLAuthenticationMethodServerTrust:
             evaluation = attemptServerTrustAuthentication(with: challenge)
         case NSURLAuthenticationMethodClientCertificate:
@@ -111,7 +111,7 @@ extension SessionDelegate: URLSessionTaskDelegate {
         completionHandler(evaluation.disposition, evaluation.credential)
     }
 
-    #if !(os(Linux) || os(Windows))
+    #if canImport(Security)
     /// Evaluates the server trust `URLAuthenticationChallenge` received.
     ///
     /// - Parameter challenge: The `URLAuthenticationChallenge`.

+ 1 - 1
Tests/AFError+AlamofireTests.swift

@@ -335,7 +335,7 @@ extension AFError.ResponseValidationFailureReason {
 
 // MARK: -
 
-#if !(os(Linux) || os(Windows))
+#if canImport(Security)
 extension AFError.ServerTrustFailureReason {
     var isNoRequiredEvaluator: Bool {
         if case .noRequiredEvaluator = self { return true }

+ 2 - 1
Tests/AuthenticationInterceptorTests.swift

@@ -289,7 +289,8 @@ final class AuthenticationInterceptorTestCase: BaseTestCase {
 
     // MARK: - Tests - Retry
 
-    #if !(os(Linux) || os(Windows)) // URLRequest to /invalid/path is a fatal error.
+    // If we not using swift-corelibs-foundation where URLRequest to /invalid/path is a fatal error.
+    #if !canImport(FoundationNetworking)
     func testThatInterceptorDoesNotRetryWithoutResponse() {
         // Given
         let credential = TestCredential()

+ 1 - 1
Tests/DataStreamTests.swift

@@ -321,7 +321,7 @@ final class DataStreamTests: BaseTestCase {
         XCTAssertNil(decodingError)
     }
 
-    #if !(os(Linux) || os(Windows))
+    #if !canImport(FoundationNetworking) // If we not using swift-corelibs-foundation.
     func testThatDataStreamRequestProducesWorkingInputStream() {
         // Given
         let expect = expectation(description: "stream complete")

+ 1 - 1
Tests/DownloadTests.swift

@@ -557,7 +557,7 @@ final class DownloadResumeDataTestCase: BaseTestCase {
 
         XCTAssertNotNil(response?.resumeData)
         XCTAssertNotNil(download.resumeData)
-        #if !(os(Linux) || os(Windows))
+        #if !canImport(FoundationNetworking) // If we not using swift-corelibs-foundation.
         XCTAssertNotNil(download.error?.downloadResumeData)
         XCTAssertEqual(download.error?.downloadResumeData, response?.resumeData)
         #endif

+ 1 - 1
Tests/NetworkReachabilityManagerTests.swift

@@ -22,7 +22,7 @@
 //  THE SOFTWARE.
 //
 
-#if canImport(SytemConfiguration)
+#if canImport(SystemConfiguration)
 
 @testable import Alamofire
 import Foundation

+ 1 - 1
Tests/ServerTrustEvaluatorTests.swift

@@ -22,7 +22,7 @@
 //  THE SOFTWARE.
 //
 
-#if !(os(Linux) || os(Windows))
+#if canImport(Security)
 
 import Alamofire
 import Foundation

+ 1 - 1
Tests/TLSEvaluationTests.swift

@@ -22,7 +22,7 @@
 //  THE SOFTWARE.
 //
 
-#if !(os(Linux) || os(Windows))
+#if canImport(Security)
 
 import Alamofire
 import Foundation