Skip to content

Commit

Permalink
Use Foundation.ProcessInfo for environment parsing (#573)
Browse files Browse the repository at this point in the history
  • Loading branch information
GNMoseke authored Oct 5, 2024
1 parent 71bb4a4 commit 913e6a0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 18 deletions.
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

0 comments on commit 913e6a0

Please sign in to comment.