-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
268 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package co.uzzu.dotenv | ||
|
||
/** | ||
* Provides environment variable | ||
*/ | ||
interface EnvProvider { | ||
/** | ||
* @return A environment variable for name. | ||
*/ | ||
fun getenv(name: String): String? | ||
|
||
/** | ||
* @return All environment variables. | ||
*/ | ||
fun getenv(): Map<String, String> | ||
} | ||
|
||
/** | ||
* EnvProvider implementation using System#getenv | ||
*/ | ||
class SystemEnvProvider : EnvProvider { | ||
override fun getenv(name: String): String? = System.getenv(name) | ||
override fun getenv(): Map<String, String> = System.getenv() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package co.uzzu.dotenv | ||
|
||
class InMemoryEnvProvider( | ||
private val map: Map<String, String> | ||
) : EnvProvider { | ||
override fun getenv(name: String): String? = map[name] | ||
override fun getenv(): Map<String, String> = map | ||
} |
96 changes: 96 additions & 0 deletions
96
plugin/src/test/kotlin/co/uzzu/dotenv/gradle/DotEnvPropertyTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package co.uzzu.dotenv.gradle | ||
|
||
import co.uzzu.dotenv.InMemoryEnvProvider | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.assertFalse | ||
import org.junit.jupiter.api.Assertions.assertNull | ||
import org.junit.jupiter.api.Assertions.assertThrows | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.Test | ||
|
||
class DotEnvPropertyTest { | ||
|
||
@Test | ||
fun isPresentByEnv() { | ||
val envProvider = InMemoryEnvProvider(mapOf("FOO" to "env")) | ||
val property = DotEnvProperty(envProvider, "FOO", null) | ||
assertTrue(property.isPresent) | ||
} | ||
|
||
@Test | ||
fun isPresentByDotEnv() { | ||
val envProvider = InMemoryEnvProvider(mapOf()) | ||
val property = DotEnvProperty(envProvider, "FOO", "dotenv") | ||
assertTrue(property.isPresent) | ||
} | ||
|
||
@Test | ||
fun isNotPresent() { | ||
val envProvider = InMemoryEnvProvider(mapOf()) | ||
val property = DotEnvProperty(envProvider, "FOO", null) | ||
assertFalse(property.isPresent) | ||
} | ||
|
||
@Test | ||
fun valueFromEnv() { | ||
val envProvider = InMemoryEnvProvider(mapOf("FOO" to "env")) | ||
val property = DotEnvProperty(envProvider, "FOO", "dotenv") | ||
assertEquals("env", property.value) | ||
} | ||
|
||
@Test | ||
fun valueFromDotEnv() { | ||
val envProvider = InMemoryEnvProvider(mapOf()) | ||
val property = DotEnvProperty(envProvider, "FOO", "dotenv") | ||
assertEquals("dotenv", property.value) | ||
} | ||
|
||
@Test | ||
fun throwIfNoVariables() { | ||
val envProvider = InMemoryEnvProvider(mapOf()) | ||
val property = DotEnvProperty(envProvider, "FOO", null) | ||
assertThrows(IllegalStateException::class.java) { property.value } | ||
} | ||
|
||
@Test | ||
fun valueOrElseFromEnv() { | ||
val envProvider = InMemoryEnvProvider(mapOf("FOO" to "env")) | ||
val property = DotEnvProperty(envProvider, "FOO", "dotenv") | ||
assertEquals("env", property.value) | ||
} | ||
|
||
@Test | ||
fun valueOrElseFromDotEnv() { | ||
val envProvider = InMemoryEnvProvider(mapOf()) | ||
val property = DotEnvProperty(envProvider, "FOO", "dotenv") | ||
assertEquals("dotenv", property.value) | ||
} | ||
|
||
@Test | ||
fun valueOrElse() { | ||
val envProvider = InMemoryEnvProvider(mapOf()) | ||
val property = DotEnvProperty(envProvider, "FOO", null) | ||
assertEquals("default", property.orElse("default")) | ||
} | ||
|
||
@Test | ||
fun fetchOrNullFromEnv() { | ||
val envProvider = InMemoryEnvProvider(mapOf("FOO" to "env")) | ||
val property = DotEnvProperty(envProvider, "FOO", "dotenv") | ||
assertEquals("env", property.orNull()) | ||
} | ||
|
||
@Test | ||
fun fetchOrNullFromDotEnv() { | ||
val envProvider = InMemoryEnvProvider(mapOf()) | ||
val property = DotEnvProperty(envProvider, "FOO", "dotenv") | ||
assertEquals("dotenv", property.orNull()) | ||
} | ||
|
||
@Test | ||
fun fetchOrNull() { | ||
val envProvider = InMemoryEnvProvider(mapOf()) | ||
val property = DotEnvProperty(envProvider, "FOO", null) | ||
assertNull(property.orNull()) | ||
} | ||
} |
Oops, something went wrong.