-
Notifications
You must be signed in to change notification settings - Fork 13
/
ConfigurationManagerTest.swift
194 lines (154 loc) · 7.52 KB
/
ConfigurationManagerTest.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright IBM Corporation 2017
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import XCTest
import Foundation
@testable import Configuration
class ConfigurationManagerTest: XCTestCase {
static var allTests : [(String, (ConfigurationManagerTest) -> () throws -> Void)] {
return [
("testLoadSimple", testLoadSimple),
("testLoadFile", testLoadFile),
("testLoadData", testLoadData),
("testLoadRelative", testLoadRelative),
("testExternalExecutable", testExternalExecutable)
]
}
static let testJSONURL = URL(fileURLWithPath: #file).appendingPathComponent("../../../TestResources/test.json").standardized
static let symlinkToPWD = URL(fileURLWithPath: "test.json")
let jsonString = "{\n \"env\": \"<default>\",\n \"OAuth\": {\n \"name\": \"facebook\",\n \"configuration\": {\n \"clientID\": \"<default>\",\n \"clientSecret\": \"<default>\",\n \"profileFields\": [\"displayName\", \"emails\", \"id\", \"name\"],\n \"profileURL\": \"https://graph.facebook.com/v2.6/me\",\n \"scope\": [\"email\"],\n \"state\": true\n }\n },\n \"port\": \"<default>\"\n}"
// Copy test resource files over to the correct locations for testing
override class func setUp() {
do {
try FileManager.default.createSymbolicLink(at: symlinkToPWD, withDestinationURL: testJSONURL)
}
catch {
// Nothing we can do but leave a failure message
print(error)
}
}
// Delete test resource files copied in setUp()
override class func tearDown() {
do {
try FileManager.default.removeItem(at: symlinkToPWD)
}
catch {
// Nothing we can do but leave a failure message
print(error)
}
}
// Helper function to run shell commands
// Tip from http://stackoverflow.com/a/26973384
func shell(_ args: String..., environment: [String: String] = [:]) -> (Pipe, Pipe, Int32) {
#if os(Linux) && !swift(>=3.1)
let process = Task()
#else
let process = Process()
#endif
let errPipe = Pipe()
let outPipe = Pipe()
process.launchPath = "/usr/bin/env"
process.arguments = args
process.environment = environment
process.standardError = errPipe
process.standardOutput = outPipe
process.launch()
process.waitUntilExit()
return (errPipe, outPipe, process.terminationStatus)
}
func testLoadSimple() {
var manager: ConfigurationManager
// String
manager = ConfigurationManager().load("Hello world")
XCTAssertEqual(manager.getConfigs() as? String, "Hello world")
// Array
manager = ConfigurationManager().load([0, "1", "hello world"])
XCTAssertEqual(manager["2"] as? String, "hello world")
// Dictionary
manager = ConfigurationManager().load(["Hello": "World"])
XCTAssertEqual(manager["Hello"] as? String, "World")
}
func testLoadFile() {
var manager: ConfigurationManager
// JSON
manager = ConfigurationManager().load(file: "../../../TestResources/test.json", relativeFrom: .customPath(#file))
XCTAssertEqual(manager["OAuth:configuration:state"] as? Bool, true)
// PLIST
manager = ConfigurationManager().load(file: "../../../TestResources/test.plist", relativeFrom: .customPath(#file))
#if swift(>=4)
// Broken on Linux due to https://bugs.swift.org/browse/SR-3681
XCTAssertEqual(manager["OAuth:configuration:state"] as? Bool, true)
#else
XCTAssertEqual(manager["OAuth:configuration:scope:0"] as? String, "email")
#endif
}
func testLoadData() {
var manager: ConfigurationManager
// JSON
let jsonString = "{\"hello\": \"world\"}"
guard let jsonData = jsonString.data(using: .utf8) else {
XCTFail("Cannot convert \(jsonString) to Data")
return
}
manager = ConfigurationManager().load(data: jsonData)
XCTAssertEqual(manager["hello"] as? String, "world")
// PLIST
let plistString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n\t<key>hello</key>\n\t<string>world</string>\n</dict>\n</plist>\n"
guard let plistData = plistString.data(using: .utf8) else {
XCTFail("Cannot convert \(plistString) to Data")
return
}
manager = ConfigurationManager().load(data: plistData)
XCTAssertEqual(manager["hello"] as? String, "world")
}
func testLoadRelative() {
var manager: ConfigurationManager
manager = ConfigurationManager().load(file: "../../../TestResources/test.json", relativeFrom: .customPath(#file))
XCTAssertEqual(manager["OAuth:configuration:state"] as? Bool, true)
manager = ConfigurationManager().load(file: "test.json", relativeFrom: .pwd)
XCTAssertEqual(manager["OAuth:configuration:state"] as? Bool, true)
// .executable and .project are tested in TestProgram
}
func testExternalExecutable() {
let projectFolder = URL(fileURLWithPath: #file).appendingPathComponent("../../../").standardized
let testProgramURL = projectFolder.appendingPathComponent(".build/debug/TestProgram").standardized
var (errPipe, outPipe, exitCode): (Pipe, Pipe, Int32)
var output: String?, error: String?
#if os(Linux)
guard FileManager.default.fileExists(atPath: testProgramURL.path) else {
XCTFail("Test executable does not exist")
return
}
// Not possible to `swift build` in project folder because this test is
// ran from .build directory on Linux
#else
// Force rebuild of test executable on OSX
(errPipe, outPipe, exitCode) = shell("swift", "build", "-C", projectFolder.path)
output = String(data: outPipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)
print(output ?? "No stdout from `swift build -C \(projectFolder.path)`")
guard exitCode == 0 else {
XCTFail("Unable to build project")
return
}
#endif
// Run the test executable
(errPipe, outPipe, exitCode) = shell(testProgramURL.path, "--argv=\(jsonString)", environment: ["ENV": jsonString])
output = String(data: outPipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)
error = String(data: errPipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)
print(output ?? "No stdout from test executable")
XCTAssertEqual(exitCode, 0, "One or more external load assertions failed")
XCTAssertEqual(error, "", "External load test has non-empty error stream: \(String(describing: error))")
}
}