diff --git a/CHANGELOG.md b/CHANGELOG.md index eb7b255..aca6df1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Added + +- `(Map) env.allVariablesOrNull()` to get all environment variables includes variables specified in `.env` includes null variables. + - The Plugin set key if defined in .env template files, but it could not be retrieved as nullable value entries by using allVariables() + - By using allVariablesOrNull instead of allVariables, it is possible to retrieve all environment variables, including those that are only defined in the .env template (which means their values are null). + +### Deprecated + +- `val (Map) env.allVariables` + - Replace to use a method `(Map) env.allVariables()` instead. + ## [2.0.0] - 2022-03-02 ### Added diff --git a/README.md b/README.md index 1963ff7..9c421de 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,8 @@ Support subproject-only variables and extensions. if environment variable was not set. - `(String?) env.fetchOrNull(name: String)` : Returns an environment variable, or null if environment variable was not set. -- `(Map + + /** + * @return All environment variables includes null values. + */ + fun getenvOrNull(): Map } /** @@ -20,5 +25,10 @@ interface EnvProvider { */ class SystemEnvProvider : EnvProvider { override fun getenv(name: String): String? = System.getenv(name) - override fun getenv(): Map = System.getenv().filterValues { it != null } + + override fun getenv(): Map = System.getenv() + .mapNotNull { (key, value) -> value?.let { key to it } } + .toMap() + + override fun getenvOrNull(): Map = System.getenv() } diff --git a/plugin/src/main/kotlin/co/uzzu/dotenv/gradle/DotEnvProperties.kt b/plugin/src/main/kotlin/co/uzzu/dotenv/gradle/DotEnvProperties.kt index 02171ec..ff0f5ae 100644 --- a/plugin/src/main/kotlin/co/uzzu/dotenv/gradle/DotEnvProperties.kt +++ b/plugin/src/main/kotlin/co/uzzu/dotenv/gradle/DotEnvProperties.kt @@ -12,6 +12,7 @@ open class DotEnvRoot( /** * @return All environment variables which are merged with variables specified in .env files. */ + @Deprecated("Replace to use DotEnvRoot#allVariables()") val allVariables: Map get() { val results = envProvider.getenv().toMutableMap() @@ -55,6 +56,34 @@ open class DotEnvRoot( fun fetchOrNull(name: String): String? = envProvider.getenv()[name] ?: dotenvMap[name] + + /** + * @return All environment variables which are merged with variables specified in .env files. + */ + fun allVariables(): Map { + val results = envProvider.getenv().toMutableMap() + dotenvMap.forEach { (key, value) -> + if (value != null && results[key] == null) { + results[key] = value + } + } + return results.toMap() + } + + /** + * @return All environment variables which are merged with variables specified in .env files, and which includes null values. + * The Plugin set key if defined in .env template files, but it could not be retrieved as nullable value entries by using allVariables() + * By using allVariablesOrNull instead of allVariables, it is possible to retrieve all environment variables, including those that are only defined in the .env template (which means their values are null). + */ + fun allVariablesOrNull(): Map { + val results = envProvider.getenvOrNull().toMutableMap() + dotenvMap.forEach { (key, value) -> + if (results[key] == null) { + results[key] = value + } + } + return results.toMap() + } } /** diff --git a/plugin/src/test/kotlin/co/uzzu/dotenv/InMemoryEnvProvider.kt b/plugin/src/test/kotlin/co/uzzu/dotenv/InMemoryEnvProvider.kt index 8dd3414..f54e919 100644 --- a/plugin/src/test/kotlin/co/uzzu/dotenv/InMemoryEnvProvider.kt +++ b/plugin/src/test/kotlin/co/uzzu/dotenv/InMemoryEnvProvider.kt @@ -1,8 +1,13 @@ package co.uzzu.dotenv class InMemoryEnvProvider( - private val map: Map + private val map: Map ) : EnvProvider { override fun getenv(name: String): String? = map[name] + override fun getenv(): Map = map + .mapNotNull { (key, value) -> value?.let { key to it } } + .toMap() + + override fun getenvOrNull(): Map = map.toMap() } diff --git a/plugin/src/test/kotlin/co/uzzu/dotenv/gradle/AllVariablesOrNullTest.kt b/plugin/src/test/kotlin/co/uzzu/dotenv/gradle/AllVariablesOrNullTest.kt new file mode 100644 index 0000000..074e52e --- /dev/null +++ b/plugin/src/test/kotlin/co/uzzu/dotenv/gradle/AllVariablesOrNullTest.kt @@ -0,0 +1,48 @@ +package co.uzzu.dotenv.gradle + +import assertk.assertThat +import assertk.assertions.contains +import org.gradle.testkit.runner.GradleRunner +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.io.File + +class AllVariablesOrNullTest { + + @TempDir + lateinit var projectDir: File + + @Test + fun nullValuesDefinedByTemplate() { + RootProject(projectDir) { + settingsGradle() + buildGradle( + """ + plugins { + base + id("co.uzzu.dotenv.gradle") + } + println("Result: ${'$'}{env.allVariablesOrNull().filterKeys { it == "HOGE" }}") + """.trimIndent() + ) + file( + ".env.template", + """ + HOGE= + """.trimIndent() + ) + file( + ".env", + """ + """.trimIndent() + ) + } + + val result = GradleRunner.create() + .withPluginClasspath() + .withProjectDir(projectDir) + .build() + + assertThat(result.output).contains("Result: {HOGE=null}") + } +} diff --git a/plugin/src/test/kotlin/co/uzzu/dotenv/gradle/BasicUsageTest.kt b/plugin/src/test/kotlin/co/uzzu/dotenv/gradle/BasicUsageTest.kt index 414c8b5..c73d3c3 100644 --- a/plugin/src/test/kotlin/co/uzzu/dotenv/gradle/BasicUsageTest.kt +++ b/plugin/src/test/kotlin/co/uzzu/dotenv/gradle/BasicUsageTest.kt @@ -10,7 +10,6 @@ import org.junit.jupiter.api.Test import org.junit.jupiter.api.io.TempDir import java.io.File -@Suppress("UnstableApiUsage") // GradleRunner#withPluginClasspath class BasicUsageTest { @TempDir diff --git a/plugin/src/test/kotlin/co/uzzu/dotenv/gradle/DotEnvRootTest.kt b/plugin/src/test/kotlin/co/uzzu/dotenv/gradle/DotEnvRootTest.kt index 7505281..43132e5 100644 --- a/plugin/src/test/kotlin/co/uzzu/dotenv/gradle/DotEnvRootTest.kt +++ b/plugin/src/test/kotlin/co/uzzu/dotenv/gradle/DotEnvRootTest.kt @@ -111,7 +111,7 @@ class DotEnvRootTest { "QUUX" to null ) ) - val allVariables = root.allVariables + val allVariables = root.allVariables() assertAll( { assertEquals("env", allVariables["FOO"]) }, { assertEquals("env", allVariables["BAR"]) }, @@ -121,4 +121,35 @@ class DotEnvRootTest { { assertEquals(null, allVariables["CORGE"]) } ) } + + @Test + fun allVariablesOrNull() { + val envProvider = InMemoryEnvProvider( + mapOf( + "FOO" to "env", + "BAR" to "env", + "CORGE" to null, + ) + ) + val root = DotEnvRoot( + envProvider, + mapOf( + "BAR" to "dotenv", + "BAZ" to "dotenv", + "QUX" to "dotenv", + "QUUX" to null, + ) + ) + val allVariables = root.allVariablesOrNull() + assertAll( + { assertEquals("env", allVariables["FOO"]) }, + { assertEquals("env", allVariables["BAR"]) }, + { assertEquals("dotenv", allVariables["BAZ"]) }, + { assertEquals("dotenv", allVariables["QUX"]) }, + { assertEquals(null, allVariables["QUUX"]) }, + { assertTrue(allVariables.containsKey("QUUX")) }, + { assertEquals(null, allVariables["CORGE"]) }, + { assertTrue(allVariables.containsKey("CORGE")) }, + ) + } } diff --git a/plugin/src/test/kotlin/co/uzzu/dotenv/gradle/HierarchicalDotEnvDefinitionsTest.kt b/plugin/src/test/kotlin/co/uzzu/dotenv/gradle/HierarchicalDotEnvDefinitionsTest.kt index 4a2b00a..470eaf8 100644 --- a/plugin/src/test/kotlin/co/uzzu/dotenv/gradle/HierarchicalDotEnvDefinitionsTest.kt +++ b/plugin/src/test/kotlin/co/uzzu/dotenv/gradle/HierarchicalDotEnvDefinitionsTest.kt @@ -8,7 +8,6 @@ import org.junit.jupiter.api.Test import org.junit.jupiter.api.io.TempDir import java.io.File -@Suppress("UnstableApiUsage") // GradleRunner#withPluginClasspath class HierarchicalDotEnvDefinitionsTest { @TempDir