Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux Compatability #55

Merged
merged 19 commits into from
May 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: objective-c
language: generic
osx_image: xcode9.3
sudo: false
env:
Expand All @@ -8,20 +8,24 @@ env:
- FASTLANE_LANE=ci_commit
matrix:
include:
- osx_image: xcode9.3
- os: osx
env: FASTLANE_LANE=code_coverage FASTLANE_ENV=default
- osx_image: xcode9.3
- os: osx
env: FASTLANE_ENV=ios93
- osx_image: xcode9.3
- os: osx
env: FASTLANE_ENV=tvos92
- osx_image: xcode9.3
- os: osx
env: FASTLANE_ENV=osx
- os: linux
sudo: required
dist: trusty
env: SWIFT_VERSION=4.1
before_install:
- make install
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then ./script/travis-install-linux; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ./script/travis-install-macos; fi
script:
- set -o pipefail
- bundle exec fastlane $FASTLANE_LANE configuration:Debug --env $FASTLANE_ENV
- bundle exec fastlane $FASTLANE_LANE configuration:Release --env $FASTLANE_ENV
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then ./script/travis-script-linux; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ./script/travis-script-macos; fi
after_success:
- if [ "$FASTLANE_LANE" == "code_coverage" ]; then
make post_coverage;
Expand Down
2 changes: 1 addition & 1 deletion NBNRequestKit.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Pod::Spec.new do |s|
s.license = { :type => "MIT", :file => "LICENSE" }
s.module_name = "RequestKit"
s.requires_arc = true
s.source_files = 'RequestKit/*.swift'
s.source_files = 'Sources/RequestKit/*.swift'
s.ios.deployment_target = '8.0'
s.osx.deployment_target = '10.10'
s.watchos.deployment_target = '2.0'
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let package = Package(
.library(name: "RequestKit", targets: ["RequestKit"])
],
targets: [
.target(name: "RequestKit", dependencies: [], path: "RequestKit"),
.testTarget(name: "RequestKitTests", dependencies: ["RequestKit"], path: "RequestKitTests")
.target(name: "RequestKit", dependencies: []),
.testTarget(name: "RequestKitTests", dependencies: ["RequestKit"])
]
)
317 changes: 142 additions & 175 deletions RequestKit.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public extension JSONPostRouter {
let task = session.uploadTask(with: request, fromData: data) { data, response, error in
if let response = response as? HTTPURLResponse {
if !response.wasSuccessful {
var userInfo = [String: AnyObject]()
if let data = data, let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: AnyObject] {
userInfo[RequestKitErrorKey] = json as AnyObject?
var userInfo = [String: Any]()
if let data = data, let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
userInfo[RequestKitErrorKey] = json as Any?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think something's broken here, I am no longer receiving userInfo dicts on my errors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Linux or iOS?

Copy link
Contributor Author

@tiferrei tiferrei May 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both, I'm afraid. Here are the CI logs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason the platform checks fixed it 👍

} else if let data = data, let string = String(data: data, encoding: String.Encoding.utf8) {
userInfo[RequestKitErrorKey] = string as AnyObject?
userInfo[RequestKitErrorKey] = string as Any?
}
let error = NSError(domain: self.configuration.errorDomain, code: response.statusCode, userInfo: userInfo)
completion(nil, error)
Expand Down Expand Up @@ -67,11 +67,11 @@ public extension JSONPostRouter {
let task = session.uploadTask(with: request, fromData: data) { data, response, error in
if let response = response as? HTTPURLResponse {
if !response.wasSuccessful {
var userInfo = [String: AnyObject]()
if let data = data, let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: AnyObject] {
userInfo[RequestKitErrorKey] = json as AnyObject?
var userInfo = [String: Any]()
if let data = data, let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
userInfo[RequestKitErrorKey] = json as Any?
} else if let data = data, let string = String(data: data, encoding: String.Encoding.utf8) {
userInfo[RequestKitErrorKey] = string as AnyObject?
userInfo[RequestKitErrorKey] = string as Any?
}
let error = NSError(domain: self.configuration.errorDomain, code: response.statusCode, userInfo: userInfo)
completion(nil, error)
Expand Down
File renamed without changes.
14 changes: 7 additions & 7 deletions RequestKit/Router.swift → Sources/RequestKit/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public extension Router {
var parameters = encoding == .json ? [:] : params

if let accessToken = configuration.accessToken, configuration.authorizationHeader == nil {
parameters[configuration.accessTokenFieldName] = accessToken as AnyObject?
parameters[configuration.accessTokenFieldName] = accessToken as Any?
}
let components = URLComponents(url: url!, resolvingAgainstBaseURL: true)

Expand Down Expand Up @@ -162,9 +162,9 @@ public extension Router {
let task = session.dataTask(with: request) { data, response, err in
if let response = response as? HTTPURLResponse {
if response.wasSuccessful == false {
var userInfo = [String: AnyObject]()
if let data = data, let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: AnyObject] {
userInfo[RequestKitErrorKey] = json as AnyObject?
var userInfo = [String: Any]()
if let data = data, let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
userInfo[RequestKitErrorKey] = json as Any?
}
let error = NSError(domain: self.configuration.errorDomain, code: response.statusCode, userInfo: userInfo)
completion(nil, error)
Expand Down Expand Up @@ -197,9 +197,9 @@ public extension Router {
let task = session.dataTask(with: request) { data, response, err in
if let response = response as? HTTPURLResponse {
if response.wasSuccessful == false {
var userInfo = [String: AnyObject]()
if let data = data, let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: AnyObject] {
userInfo[RequestKitErrorKey] = json as AnyObject?
var userInfo = [String: Any]()
if let data = data, let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
userInfo[RequestKitErrorKey] = json as Any?
}
let error = NSError(domain: self.configuration.errorDomain, code: response.statusCode, userInfo: userInfo)
completion(error)
Expand Down
4 changes: 4 additions & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import XCTest
@testable import RequestKitTests

XCTMain(RequestKitTests.allTests())
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ import XCTest
import RequestKit

class ConfigurationTests: XCTestCase {
static var allTests = [
("testDefaultImplementation", testDefaultImplementation),
("testCustomImplementation", testCustomImplementation),
("testAuthorizationHeaderConfiguration", testAuthorizationHeaderConfiguration),
("testLinuxTestSuiteIncludesAllTests", testLinuxTestSuiteIncludesAllTests)
]

func testLinuxTestSuiteIncludesAllTests() {
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
let thisClass = type(of: self)
let linuxCount = thisClass.allTests.count
let darwinCount = thisClass.defaultTestSuite.tests.count
XCTAssertEqual(linuxCount, darwinCount, "\(darwinCount - linuxCount) tests are missing from allTests")
#endif
}

func testDefaultImplementation() {
let config = TestConfiguration("1234", url: "https://github.com")
XCTAssertEqual(config.apiEndpoint, "https://github.com")
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
import RequestKit
import XCTest
import Foundation

class JSONPostRouterTests: XCTestCase {
static var allTests = [
("testJSONPostJSONError", testJSONPostJSONError),
("testJSONPostStringError", testJSONPostStringError),
("testLinuxTestSuiteIncludesAllTests", testLinuxTestSuiteIncludesAllTests)
]

func testLinuxTestSuiteIncludesAllTests() {
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
let thisClass = type(of: self)
let linuxCount = thisClass.allTests.count
let darwinCount = thisClass.defaultTestSuite.tests.count
XCTAssertEqual(linuxCount, darwinCount, "\(darwinCount - linuxCount) tests are missing from allTests")
#endif
}

func testJSONPostJSONError() {
let jsonDict = ["message": "Bad credentials", "documentation_url": "https://developer.github.com/v3"]
let jsonString = String(data: try! JSONSerialization.data(withJSONObject: jsonDict, options: JSONSerialization.WritingOptions()), encoding: String.Encoding.utf8)
Expand All @@ -10,10 +26,10 @@ class JSONPostRouterTests: XCTestCase {
switch response {
case .success:
XCTAssert(false, "should not retrieve a succesful response")
case .failure(let error as NSError):
XCTAssertEqual(error.code, 401)
XCTAssertEqual(error.domain, "com.nerdishbynature.RequestKitTests")
XCTAssertEqual((error.userInfo[RequestKitErrorKey] as? [String: String]) ?? [:], jsonDict)
case .failure(let error):
XCTAssertEqual(Helper.getNSError(from: error)?.code, 401)
XCTAssertEqual(Helper.getNSError(from: error)?.domain, "com.nerdishbynature.RequestKitTests")
XCTAssertEqual((Helper.getNSError(from: error)?.userInfo[RequestKitErrorKey] as? [String: String]) ?? [:], jsonDict)
}
}
XCTAssertNotNil(task)
Expand All @@ -27,10 +43,10 @@ class JSONPostRouterTests: XCTestCase {
switch response {
case .success:
XCTAssert(false, "should not retrieve a succesful response")
case .failure(let error as NSError):
XCTAssertEqual(error.code, 401)
XCTAssertEqual(error.domain, "com.nerdishbynature.RequestKitTests")
XCTAssertEqual((error.userInfo[RequestKitErrorKey] as? String) ?? "", errorString)
case .failure(let error):
XCTAssertEqual(Helper.getNSError(from: error)?.code, 401)
XCTAssertEqual(Helper.getNSError(from: error)?.domain, "com.nerdishbynature.RequestKitTests")
XCTAssertEqual((Helper.getNSError(from: error)?.userInfo[RequestKitErrorKey] as? String) ?? "", errorString)
}
}
XCTAssertNotNil(task)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
import XCTest
import RequestKit
import Foundation

class RouterTests: XCTestCase {
static var allTests = [
("testRequest", testRequest),
("testRequestWithAuthorizationHeader", testRequestWithAuthorizationHeader),
("testRequestWithCustomHeaders", testRequestWithCustomHeaders),
("testWasSuccessful", testWasSuccessful),
("testURLComponents", testURLComponents),
("testFormEncodedRouteRequest", testFormEncodedRouteRequest),
("testErrorWithJSON", testErrorWithJSON),
("testLoadAndIgnoreResponseBody", testLoadAndIgnoreResponseBody),
("testErrorWithLoadAndIgnoreResponseBody", testErrorWithLoadAndIgnoreResponseBody),
("testLinuxTestSuiteIncludesAllTests", testLinuxTestSuiteIncludesAllTests)
]

func testLinuxTestSuiteIncludesAllTests() {
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
let thisClass = type(of: self)
let linuxCount = thisClass.allTests.count
let darwinCount = thisClass.defaultTestSuite.tests.count
XCTAssertEqual(linuxCount, darwinCount, "\(darwinCount - linuxCount) tests are missing from allTests")
#endif
}

lazy var router: TestRouter = {
let config = TestConfiguration("1234", url: "https://example.com/api/v1/")
let router = TestRouter.testRoute(config)
Expand Down Expand Up @@ -70,10 +93,10 @@ class RouterTests: XCTestCase {
switch response {
case .success:
XCTAssert(false, "should not retrieve a succesful response")
case .failure(let error as NSError):
XCTAssertEqual(error.code, 401)
XCTAssertEqual(error.domain, "com.nerdishbynature.RequestKitTests")
XCTAssertEqual((error.userInfo[RequestKitErrorKey] as? [String: String]) ?? [:], jsonDict)
case .failure(let error):
XCTAssertEqual(Helper.getNSError(from: error)?.code, 401)
XCTAssertEqual(Helper.getNSError(from: error)?.domain, "com.nerdishbynature.RequestKitTests")
XCTAssertEqual((Helper.getNSError(from: error)?.userInfo[RequestKitErrorKey] as? [String: String]) ?? [:], jsonDict)
}
}
XCTAssertNotNil(task)
Expand Down Expand Up @@ -110,11 +133,11 @@ class RouterTests: XCTestCase {
switch response {
case .success:
XCTAssert(false, "should not retrieve a successful response")
case .failure(let error as NSError):
case .failure(let error):
receivedFailureResponse = true
XCTAssertEqual(error.code, 401)
XCTAssertEqual(error.domain, "com.nerdishbynature.RequestKitTests")
XCTAssertEqual((error.userInfo[RequestKitErrorKey] as? [String: String]) ?? [:], jsonDict)
XCTAssertEqual(Helper.getNSError(from: error)?.code, 401)
XCTAssertEqual(Helper.getNSError(from: error)?.domain, "com.nerdishbynature.RequestKitTests")
XCTAssertEqual((Helper.getNSError(from: error)?.userInfo[RequestKitErrorKey] as? [String: String]) ?? [:], jsonDict)
}
}
XCTAssertNotNil(task)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ internal class Helper {
let data = try! Data(contentsOf: URL(fileURLWithPath: path))
return try! JSONDecoder().decode(T.self, from: data)
}

public static func getNSError(from error: Error?) -> NSError? {
#if os(Linux)
return (error as? NSError)
#else
return (error as NSError?)
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TestInterface {
return TestInterfaceConfiguration(url: "https://example.com")
}

func postJSON(_ session: RequestKitURLSession = URLSession.shared, completion: @escaping (_ response: Response<[String: AnyObject]>) -> Void) -> URLSessionDataTaskProtocol? {
func postJSON(_ session: RequestKitURLSession, completion: @escaping (_ response: Response<[String: AnyObject]>) -> Void) -> URLSessionDataTaskProtocol? {
let router = JSONTestRouter.testPOST(configuration)
return router.postJSON(session, expectedResultType: [String: AnyObject].self) { json, error in
if let error = error {
Expand All @@ -28,7 +28,7 @@ class TestInterface {
}
}

func getJSON(_ session: RequestKitURLSession = URLSession.shared, completion: @escaping (_ response: Response<[String: String]>) -> Void) -> URLSessionDataTaskProtocol? {
func getJSON(_ session: RequestKitURLSession, completion: @escaping (_ response: Response<[String: String]>) -> Void) -> URLSessionDataTaskProtocol? {
let router = JSONTestRouter.testGET(configuration)
return router.load(session, expectedResultType: [String: String].self) { json, error in
if let error = error {
Expand All @@ -41,7 +41,7 @@ class TestInterface {
}
}

func loadAndIgnoreResponseBody(_ session: RequestKitURLSession = URLSession.shared, completion: @escaping (_ response: Response<Void>) -> Void) -> URLSessionDataTaskProtocol? {
func loadAndIgnoreResponseBody(_ session: RequestKitURLSession, completion: @escaping (_ response: Response<Void>) -> Void) -> URLSessionDataTaskProtocol? {
let router = JSONTestRouter.testPOST(configuration)
return router.load(session) { error in
if let error = error {
Expand Down
11 changes: 11 additions & 0 deletions Tests/RequestKitTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import XCTest

#if !(os(macOS) || os(iOS) || os(tvOS) || os(watchOS))
public func allTests() -> [XCTestCaseEntry] {
return [
testCase(RouterTests.allTests),
testCase(ConfigurationTests.allTests),
testCase(JSONPostRouterTests.allTests)
]
}
#endif
6 changes: 6 additions & 0 deletions script/travis-install-linux
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
set -e

# See: https://github.com/kylef/swiftenv/wiki/Travis-CI
curl -sL https://gist.github.com/kylef/5c0475ff02b7c7671d2a/raw/621ef9b29bbb852fdfd2e10ed147b321d792c1e4/swiftenv-install.sh | bash

5 changes: 5 additions & 0 deletions script/travis-install-macos
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env sh

sudo gem install bundler
bundle install

5 changes: 5 additions & 0 deletions script/travis-script-linux
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env sh

. ~/.swiftenv/init
swift test

6 changes: 6 additions & 0 deletions script/travis-script-macos
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env sh

set -o pipefail
bundle exec fastlane $FASTLANE_LANE configuration:Debug --env $FASTLANE_ENV
bundle exec fastlane $FASTLANE_LANE configuration:Release --env $FASTLANE_ENV