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

Use Foundation.ProcessInfo for environment parsing #573

Merged
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
19 changes: 3 additions & 16 deletions Sources/Hummingbird/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Darwin.C
#else
#error("Unsupported platform")
#endif
import Foundation
import HummingbirdCore
import NIOCore

Expand Down Expand Up @@ -111,22 +112,8 @@ public struct Environment: Sendable, Decodable, ExpressibleByDictionaryLiteral {
/// Construct environment variable map
static func getEnvironment() -> [String: String] {
var values: [String: String] = [:]
let equalSign = Character("=")
#if canImport(Musl)
guard let envp = environ else { return [:] }
#else
let envp = environ
#endif
var idx = 0

while let entry = envp.advanced(by: idx).pointee {
let entry = String(cString: entry)
if let i = entry.firstIndex(of: equalSign) {
let key = String(entry.prefix(upTo: i))
let value = String(entry.suffix(from: i).dropFirst())
values[key.lowercased()] = value
}
idx += 1
for item in ProcessInfo.processInfo.environment {
values[item.key.lowercased()] = item.value
}
return values
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/HummingbirdTests/EnvironmentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ final class EnvironmentTests: XCTestCase {

func testSetForAllEnvironments() {
var env = Environment()
env.set("TEST_VAR", value: "testSet")
env.set("TEST_VAR_E1", value: "testSet")
let env2 = Environment()
XCTAssertEqual(env2.get("TEST_VAR"), "testSet")
XCTAssertEqual(env2.get("TEST_VAR_E1"), "testSet")
}

func testLogLevel() {
Expand Down
Loading