Skip to content

Commit b36f057

Browse files
author
Andrew Theis
committed
Cleanup makeTest function
1 parent 8daee2b commit b36f057

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift

+21-27
Original file line numberDiff line numberDiff line change
@@ -459,41 +459,35 @@ class PostgreSQLConnectionTests: XCTestCase {
459459

460460
extension PostgreSQLConnection {
461461
/// Creates a test event loop and psql client.
462-
static func makeTest(transportConfig: PostgreSQLTransportConfig? = nil) throws -> PostgreSQLConnection {
462+
static func makeTest() throws -> PostgreSQLConnection {
463463
#if Xcode
464-
let hostname = (try? Process.execute("docker-machine", "ip")) ?? "192.168.99.100"
465-
let hostnameSSL = hostname
466-
let portSSL = 5433
467-
let password: String? = nil
464+
return try _makeTest(hostname: self.dockerMachineHostname)
468465
#else
469-
let hostname = "localhost"
470-
let hostnameSSL = "localhost-ssl"
471-
let portSSL = 5432
472-
let password: String?
473-
474-
if transportConfig != nil {
475-
password = "vapor_password"
476-
} else {
477-
password = nil
478-
}
466+
return try _makeTest(hostname: "localhost")
467+
#endif
468+
}
469+
470+
/// Creates a test event loop and psql client over ssl
471+
static func makeTest(transportConfig: PostgreSQLTransportConfig) throws -> PostgreSQLConnection {
472+
#if Xcode
473+
return try _makeTest(hostname: self.dockerMachineHostname, port: 5433, transportConfig: transportConfig)
474+
#else
475+
return try _makeTest(hostname: "localhost-ssl", password: "vapor_password", transportConfig: transportConfig)
479476
#endif
480-
477+
}
478+
479+
private static func _makeTest(hostname: String, password: String? = nil, port: Int = 5432, transportConfig: PostgreSQLTransportConfig = .cleartext) throws -> PostgreSQLConnection {
481480
let group = MultiThreadedEventLoopGroup(numThreads: 1)
482-
var client: PostgreSQLConnection
483-
484-
if let transportConfig = transportConfig {
485-
client = try PostgreSQLConnection.connect(hostname: hostnameSSL, port: portSSL, transportConfig: transportConfig, on: group) { error in
486-
XCTFail("\(error)")
487-
}.wait()
488-
} else {
489-
client = try PostgreSQLConnection.connect(hostname: hostname, on: group) { error in
490-
XCTFail("\(error)")
481+
let client = try PostgreSQLConnection.connect(hostname: hostname, port: port, transportConfig: transportConfig, on: group) { error in
482+
XCTFail("\(error)")
491483
}.wait()
492-
}
493-
494484
_ = try client.authenticate(username: "vapor_username", database: "vapor_database", password: password).wait()
495485
return client
496486
}
487+
488+
private static var dockerMachineHostname: String {
489+
return (try? Process.execute("docker-machine", "ip")) ?? "192.168.99.100"
490+
}
497491
}
498492

499493
func +=<T>(lhs: inout [T], rhs: T) {

0 commit comments

Comments
 (0)