Skip to content

Add export method for overriding environment #2

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

Merged
merged 3 commits into from
May 18, 2024
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
11 changes: 11 additions & 0 deletions Sources/DotEnvy/Documentation.docc/Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ func env() -> [String: String] {
}
```

If you want to override the process environment, you can use ``DotEnvironment/export(overwrite:)``:

```swift
import DotEnvy

func overrideEnv() throws {
let environment = try DotEnvironment.make()
environment.export()
}
```

## Topics

### Group
25 changes: 25 additions & 0 deletions Sources/DotEnvy/Load.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ extension DotEnvironment.Override {
}

extension DotEnvironment {
/// Export the `DotEnvironment` values into the process environment.
///
/// - Parameter overwrite: Indicates if the calculated value should overwrite any existing value
/// in the process environment.
public func export(overwrite: Bool = true) {
for value in self.merge() {
setenv(value.key, value.value, overwrite ? 1 : 0)
}
}

/// Load the contents of a dotenv file into a dictionary.
///
/// Defaults to loading `.env` from the current working directory. If the file does not exist, an
Expand Down Expand Up @@ -77,6 +87,21 @@ extension DotEnvironment {
}
}

/// Create a `DotEnvironment` from `source` and `overrides`.
///
/// - Parameter source: A string in dotenv format.
public static func make(
source: String,
overrides: DotEnvironment.Override = .process
) throws -> DotEnvironment {
do {
let values = try self.parse(string: source)
return DotEnvironment(environment: values, overrides: overrides)
} catch let error as ParseErrorWithLocation {
throw LoadError.parseError(error, source)
}
}

/// Create a `DotEnvironment` from `url` and `overrides`.
///
/// Defaults to loading `.env` from the current working directory and using the process environment
Expand Down
32 changes: 32 additions & 0 deletions Tests/DotEnvyTests/ExportTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@testable import DotEnvy
import XCTest

final class ExportTests: XCTestCase {
func testOverwrite() throws {
setenv("KEY1", "ENVVALUE1", 1)
let dotenv = try DotEnvironment.make(
source: """
KEY1=DOTENVVALUE1
KEY2=DOTENVVALUE2
""",
overrides: .none
)
dotenv.export(overwrite: true)
XCTAssertEqual(ProcessInfo.processInfo.environment["KEY1"], "DOTENVVALUE1")
XCTAssertEqual(ProcessInfo.processInfo.environment["KEY2"], "DOTENVVALUE2")
}

func testNoOverwrite() throws {
setenv("KEY1", "ENVVALUE1", 1)
let dotenv = try DotEnvironment.make(
source: """
KEY1=DOTENVVALUE1
KEY2=DOTENVVALUE2
""",
overrides: .none
)
dotenv.export(overwrite: false)
XCTAssertEqual(ProcessInfo.processInfo.environment["KEY1"], "ENVVALUE1")
XCTAssertEqual(ProcessInfo.processInfo.environment["KEY2"], "DOTENVVALUE2")
}
}
19 changes: 19 additions & 0 deletions Tests/DotEnvyTests/LoadTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ final class LoadTests: XCTestCase {
}
}

func testLoadErrorFromMakeSource() throws {
let source = """
KEY1=VALUE1
KEY2-VALUE2
KEY3=VALUE3
"""
XCTAssertThrowsError(try DotEnvironment.make(source: source)) { error in
guard let error = error as? LoadError else {
XCTFail("Unexpected error: \(error)")
return
}
guard case let .parseError(p, _) = error else {
XCTFail("Unexpected LoadError: \(error)")
return
}
XCTAssertEqual(p.error, .missingEquals)
}
}

func testMakeDotEnvironmentWithDefaultFile() throws {
try inTemporaryDirectory { tempDir in
try Data("""
Expand Down
Loading