Skip to content

Commit

Permalink
Fix for .env parsing (#188) (#189)
Browse files Browse the repository at this point in the history
* add test that shows bug

* fix for comment parsing issue

* fixed formatting

---------

Co-authored-by: Simon Leeb <[email protected]>
  • Loading branch information
sliemeobn and Simon Leeb authored Apr 29, 2023
1 parent 661a18f commit 4a04e53
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Sources/Hummingbird/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ public struct HBEnvironment: Sendable, Decodable, ExpressibleByDictionaryLiteral
var parser = HBParser(dotEnv)
var state: DotEnvParserState = .readingKey
do {
parser.read(while: \.isWhitespace)
while !parser.reachedEnd() {
parser.read(while: \.isWhitespace)

switch state {
case .readingKey:
// check for comment
Expand All @@ -171,6 +172,7 @@ public struct HBEnvironment: Sendable, Decodable, ExpressibleByDictionaryLiteral
parser.moveToEnd()
break
}
continue
}
let key = try parser.read(until: { $0.isWhitespace || $0 == "=" }).string
state = .skippingEquals(key: key)
Expand All @@ -192,7 +194,6 @@ public struct HBEnvironment: Sendable, Decodable, ExpressibleByDictionaryLiteral
dotEnvDictionary[key.lowercased()] = value
state = .readingKey
}
parser.read(while: \.isWhitespace)
}
guard case .readingKey = state else { throw Error.dotEnvParseError }
} catch {
Expand Down
11 changes: 11 additions & 0 deletions Tests/HummingbirdTests/EnvironmentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ final class EnvironmentTests: XCTestCase {
XCTAssertEqual(result["credentials"], "sdkfjh")
}

func testDotEnvCommentAndEmptyLine() throws {
let dotenv = """
FOO=BAR
#BAZ=
"""
let result = try HBEnvironment.parseDotEnv(dotenv)
XCTAssertEqual(result["foo"], "BAR")
XCTAssertEqual(result.count, 1)
}

func testDotEnvOverridingEnvironment() throws {
let dotenv = """
TEST_VAR=testDotEnvOverridingEnvironment
Expand Down

0 comments on commit 4a04e53

Please sign in to comment.