Skip to content

Commit c3be0da

Browse files
committed
Configure per test db
1 parent 78e1061 commit c3be0da

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

Sources/App/configure.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import FluentPostgresDriver
1717
import Vapor
1818

1919
@discardableResult
20-
public func configure(_ app: Application) async throws -> String {
20+
public func configure(_ app: Application, databasePort: Int? = nil) async throws -> String {
2121
#if DEBUG && os(macOS)
2222
// The bundle is only loaded if /Applications/InjectionIII.app exists on the local development machine.
2323
// Requires InjectionIII 4.7.3 or higher to be loaded for compatibility with Package.swift files.
@@ -52,7 +52,7 @@ public func configure(_ app: Application) async throws -> String {
5252

5353
guard
5454
let host = Environment.get("DATABASE_HOST"),
55-
let port = Environment.get("DATABASE_PORT").flatMap(Int.init),
55+
let port = databasePort ?? Environment.get("DATABASE_PORT").flatMap(Int.init),
5656
let username = Environment.get("DATABASE_USERNAME"),
5757
let password = Environment.get("DATABASE_PASSWORD"),
5858
let database = Environment.get("DATABASE_NAME")

Tests/AppTests/AnalyzerTests.swift

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import Testing
16-
1715
@testable import App
1816

1917
import Fluent
18+
import FluentPostgresDriver
19+
import NIOConcurrencyHelpers
2020
import SPIManifest
21-
@preconcurrency import ShellOut
2221
import SnapshotTesting
22+
import Testing
2323
import Vapor
24-
import NIOConcurrencyHelpers
24+
25+
@preconcurrency import ShellOut
26+
2527

2628
extension ShellOutCommand {
2729
static func launchDB(port: Int) -> ShellOutCommand {
@@ -46,9 +48,9 @@ extension ShellOutCommand {
4648
}
4749

4850

49-
private func setupApp(_ environment: Environment) async throws -> Application {
51+
private func setupApp(_ environment: Environment, databasePort: Int) async throws -> Application {
5052
let app = try await Application.make(environment)
51-
try await configure(app)
53+
try await configure(app, databasePort: databasePort)
5254

5355
// Silence app logging
5456
app.logger = .init(label: "noop") { _ in SwiftLogNoOpLogHandler() }
@@ -59,11 +61,13 @@ private func setupApp(_ environment: Environment) async throws -> Application {
5961
return app
6062
}
6163

64+
6265
private func relaunchDB(on port: Int) async throws {
6366
_ = try? await ShellOut.shellOut(to: .removeDB(port: port))
6467
try await ShellOut.shellOut(to: .launchDB(port: port))
6568
}
6669

70+
6771
private func setupDB(on port: Int, app: Application) async throws {
6872
let deadline = Date.now.addingTimeInterval(1)
6973
var dbIsReady = false
@@ -76,12 +80,19 @@ private func setupDB(on port: Int, app: Application) async throws {
7680
try #require(dbIsReady)
7781
}
7882

83+
84+
private let dbIndex = ActorIsolated(0)
85+
86+
7987
private func withApp(_ environment: Environment, _ test: (Application, CapturingLogger) async throws -> Void) async throws {
80-
let port = 5432
81-
setenv("DATABASE_PORT", "\(port)", 1)
88+
let dbOffset = await dbIndex.withValue { index in
89+
index = index + 1
90+
return index
91+
}
92+
let port = 10_000 + dbOffset
8293
try await relaunchDB(on: port)
8394

84-
let app = try await setupApp(environment)
95+
let app = try await setupApp(environment, databasePort: port)
8596

8697
let logger = CapturingLogger()
8798
Current.setLogger(.init(label: "test", factory: { _ in logger }))

0 commit comments

Comments
 (0)