Skip to content

Commit

Permalink
(fix) #25 Dotenv entries() result is not overridden by environment va…
Browse files Browse the repository at this point in the history
…riables
  • Loading branch information
Carmine DiMascio committed Mar 27, 2019
1 parent 8d17952 commit 54f38c0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion java-dotenv-5.0.0.pom → java-dotenv-5.0.1.pom
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>io.github.cdimascio</groupId>
<artifactId>java-dotenv</artifactId>
<version>5.0.0</version>
<version>5.0.1</version>

<licenses>
<license>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>io.github.cdimascio</groupId>
<artifactId>java-dotenv</artifactId>
<version>5.0.0</version>
<version>5.0.1</version>

<licenses>
<license>
Expand Down
12 changes: 10 additions & 2 deletions src/main/kotlin/io/github/cdimascio/dotenv/Dotenv.kt
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,18 @@ data class DotenvEntry(val key: String, val value: String)

private class DotenvImpl(envVars: List<Pair<String, String>>) : Dotenv() {
private val map = envVars.associateBy({ it.first }, { it.second })
private val set = Collections.unmodifiableSet(
map.entries.map { DotenvEntry(it.key, it.value) }.toSet()
private val set: Set<DotenvEntry> = Collections.unmodifiableSet(
buildEnvEntries().map { DotenvEntry(it.key, it.value) }.toSet()
)

override fun entries() = set
override fun get(envName: String): String? = System.getenv(envName) ?: map[envName]

private fun buildEnvEntries(): Map<String, String> {
val envMap = map.toMap(mutableMapOf())
System.getenv().entries.forEach {
envMap[it.key] = it.value
}
return envMap
}
}
8 changes: 7 additions & 1 deletion src/test/kotlin/tests/DslTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.github.cdimascio.dotenv.DotEnvException
import io.github.cdimascio.dotenv.Dotenv
import io.github.cdimascio.dotenv.dotenv
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import org.junit.Test as test
Expand All @@ -16,6 +17,10 @@ class DotEnvDslTest {
"QUOTED_EV1" to "jdbc:hive2://[domain]:10000/default;principal=hive/_HOST@[REALM]"
)

private val envVarsOverridenByHostEnv = mapOf(
"HOME" to "dotenv_test_home"
)

@test(expected = DotEnvException::class)
fun dotenvMalformed() {
dotenv()
Expand Down Expand Up @@ -69,7 +74,6 @@ class DotEnvDslTest {
ignoreIfMalformed = true
}
assertEquals("my test ev 1", env["MY_TEST_EV1"])

assertHostEnvVar(env)
}

Expand All @@ -83,6 +87,8 @@ class DotEnvDslTest {
for (e in env.entries()) {
assertEquals(env[e.key], e.value)
}
assertHostEnvVar(env)
assertNotEquals(envVarsOverridenByHostEnv["HOME"], env["HOME"])
}

@test
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/.env
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ WITHOUT_VALUE=
MY_TEST_EV3

QUOTED_EV1="jdbc:hive2://[domain]:10000/default;principal=hive/_HOST@[REALM]"

## Test ENV that is expected to be overriden by HOME in host env
HOME=dotenv_test_home

0 comments on commit 54f38c0

Please sign in to comment.