Skip to content

Commit

Permalink
fix: Quoted variable #18
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Mar 1, 2019
1 parent c965ee4 commit 2d44b08
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 210 deletions.
206 changes: 0 additions & 206 deletions java-dotenv-3.3.1.pom

This file was deleted.

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>3.3.1</version>
<version>4.0.0</version>

<licenses>
<license>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class DotenvParser(
private val isWhiteSpace = { s: String -> """^\s*${'$'}""".toRegex().matches(s) }
private val isComment = { s: String -> s.startsWith("#") || s.startsWith("""//""") }
private val parseLine = { s: String -> """^\s*([\w.\-]+)\s*(=)\s*(.*)?\s*$""".toRegex().matchEntire(s) }

private val isQuoted = { s: String -> s.startsWith("\"") && s.endsWith("\"") }
/**
* Parses the contents of .env and returns the environment variable and associated value as a list of pairs
* @return a list of pair representing the values .env is contributing to the virtual environment
Expand All @@ -37,11 +37,16 @@ internal class DotenvParser(
val match = parseLine(it)
if (match != null) {
val (key, _, value) = match.destructured
Pair(key, value)
Pair(key, normalizeValue(value))
} else {
if (throwIfMalformed) throw DotEnvException("Malformed entry: $it")
else null
}
}
}

private fun normalizeValue(value: String): String {
val tr = value.trim()
return if (isQuoted(tr)) tr.substring(1 until value.length -1) else tr
}
}
18 changes: 17 additions & 1 deletion src/test/kotlin/tests/DslTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,30 @@ class DotEnvDslTest {
private val envVars = mapOf(
"MY_TEST_EV1" to "my test ev 1",
"MY_TEST_EV2" to "my test ev 2",
"WITHOUT_VALUE" to ""
"WITHOUT_VALUE" to "",
"QUOTED_EV1" to "jdbc:hive2://[domain]:10000/default;principal=hive/_HOST@[REALM]"
)

@test(expected = DotEnvException::class)
fun dotenvMalformed() {
dotenv()
}

@test
fun dotenvQuotedEv() {
val env = dotenv {
ignoreIfMalformed = true
}

envVars.forEach {
val expected = it.value
val actual = env[it.key]
assertEquals(expected, actual)
}

assertEquals("jdbc:hive2://[domain]:10000/default;principal=hive/_HOST@[REALM]", env["QUOTED_EV1"])
}

@test
fun dotenvIgnoreMalformed() {
val env = dotenv {
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/.env
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ WITHOUT_VALUE=

## Malformed EV!
MY_TEST_EV3

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

0 comments on commit 2d44b08

Please sign in to comment.