Skip to content

Commit

Permalink
Merge pull request #55 from IBM-Swift/issue.threeOne
Browse files Browse the repository at this point in the history
Updates for 3.1 support
  • Loading branch information
Christian Compton authored Mar 28, 2017
2 parents 31c7a5d + 48dde63 commit 31fe029
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.2
3.1
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This library determines if you are running your application "locally" or on the
For the implementation of this Swift package, we used as inspiration a similar module that had been developed for Node.js applications, [node-cfenv](https://github.com/cloudfoundry-community/node-cfenv).

## Swift version
The latest version of Swift-cfenv works with the `3.0.2` version of the Swift binaries. You can download this version of the Swift binaries by following this [link](https://swift.org/download/#snapshots).
The latest version of Swift-cfenv works with the `3.1` version of the Swift binaries. You can download this version of the Swift binaries by following this [link](https://swift.org/download/#snapshots).

## Configuration
The latest version of Swift-cfenv relies on the [Configuration](https://github.com/IBM-Swift/Configuration) package to load and merge configuration data from multiple sources, such as environment variables or JSON files. In previous versions of Swift-cfenv, the library was responsible for accessing the environment variables directly. Moving forward, newer versions of Swift-cfenv will continue to depend on the configuration data loaded into a `ConfigurationManager` instance. For further details on the Configuration package, see its [README](https://github.com/IBM-Swift/Configuration) file.
Expand Down
8 changes: 2 additions & 6 deletions Sources/CloudFoundryEnv/AppEnv.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,16 @@ extension ConfigurationManager {
}

do {
#if os(Linux)
let regex = try RegularExpression(pattern: spec, options: RegularExpression.Options.caseInsensitive)
#else
let regex = try NSRegularExpression(pattern: spec, options: NSRegularExpression.Options.caseInsensitive)
#endif

for (name, serv) in services {
let numberOfMatches = regex.numberOfMatches(in: name, options: [], range: NSMakeRange(0, name.characters.count))
if numberOfMatches > 0 {
return serv
}
}
} catch let error as NSError {
Log.error("Error code: \(error.code)")
} catch {
Log.error("Error code: \(error)")
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/CloudFoundryEnv/JSONUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public struct JSONUtils {
public static func convertStringToJSON(text: String?) -> [String:Any]? {
let data = text?.data(using: String.Encoding.utf8)
guard let nsData = data else {
Log.error("Could not generate JSON object from string: \(text)")
Log.error("Could not generate JSON object from string: \(String(describing: text))")
return nil
}
if let json = try? JSONSerialization.jsonObject(with: nsData) {
Expand Down
21 changes: 15 additions & 6 deletions Tests/CloudFoundryEnvTests/MainTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import Configuration

@testable import CloudFoundryEnv

// Work-around due to bug in Swift 3.1
let configFileURL: URL = URL(fileURLWithPath: #file).appendingPathComponent("../resources/config.json").standardized
let configFilePath = "../resources/config.json"
let currentPath = #file

/**
* Useful online resources/tools:
* - Escape JSON: http://www.freeformatter.com/javascript-escape.html
Expand All @@ -48,9 +53,9 @@ class MainTests: XCTestCase {
}

//var jsonOptions: [String:Any] = [:]
let configFileURL: URL = URL(fileURLWithPath: #file).appendingPathComponent("../resources/config.json").standardized
let configFilePath = "../resources/config.json"
let currentPath = #file
// let configFileURL: URL = URL(fileURLWithPath: #file).appendingPathComponent("../resources/config.json").standardized
// let configFilePath = "../resources/config.json"
// let currentPath = #file

override func setUp() {
super.setUp()
Expand Down Expand Up @@ -127,6 +132,11 @@ class MainTests: XCTestCase {
// Use part of type/label used in Bluemix (prefix)
services = configManager.getServices(type: "cloudantNo")
XCTAssertEqual(services.count, 2, "There should be only 2 services in the services array.")
// Sort array before verifying the first element, since the array is not guaranteed to be
// in the same order every time this test is executed.
services.sort() { left, right in
return left.label < right.label
}
verifyService(service: services[0])
services = configManager.getServices(type: "alertnotification")
XCTAssertEqual(services.count, 1, "There should be only 1 service in the services array.")
Expand Down Expand Up @@ -199,9 +209,8 @@ class MainTests: XCTestCase {
// Case #5
replacements = "{ \"user\": \"username01\", \"password\": \"passw0rd\", \"port\": 9080, \"host\": \"bluemix.ibm.com\", \"scheme\": \"https\", \"query\": \"name0=value0&name1=value1\", \"queryItems\": [ { \"name\": \"name2\", \"value\": \"value2\" }, { \"name\": \"name3\", \"value\": \"value3\" } ] }"
try verifyServiceURLWithOptions(name: name, replacements: replacements, expectedServiceURL: "https://username01:[email protected]:9080?name2=value2&name3=value3")
} catch let error as NSError {
Log.error("Error domain: \(error.domain)")
Log.error("Error code: \(error.code)")
} catch {
Log.error("Error: \(error)")
XCTFail("Could not get AppEnv object!")
}
}
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# docker-compose up
app:
image: ibmcom/swift-ubuntu:latest
image: ibmcom/swift-ubuntu:3.1
ports:
- "8080:8080"
volumes:
Expand Down

0 comments on commit 31fe029

Please sign in to comment.