From 913e6a0b4bd9c1c0efd399cd870c47187e74d00e Mon Sep 17 00:00:00 2001 From: Garrett Moseke Date: Sat, 5 Oct 2024 16:09:05 +0200 Subject: [PATCH] Use Foundation.ProcessInfo for environment parsing (#573) --- Sources/Hummingbird/Environment.swift | 19 +++---------------- Tests/HummingbirdTests/EnvironmentTests.swift | 4 ++-- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/Sources/Hummingbird/Environment.swift b/Sources/Hummingbird/Environment.swift index 20686243..f4780517 100644 --- a/Sources/Hummingbird/Environment.swift +++ b/Sources/Hummingbird/Environment.swift @@ -21,6 +21,7 @@ import Darwin.C #else #error("Unsupported platform") #endif +import Foundation import HummingbirdCore import NIOCore @@ -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 } diff --git a/Tests/HummingbirdTests/EnvironmentTests.swift b/Tests/HummingbirdTests/EnvironmentTests.swift index a0f30539..fa49b0f4 100644 --- a/Tests/HummingbirdTests/EnvironmentTests.swift +++ b/Tests/HummingbirdTests/EnvironmentTests.swift @@ -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() {