add info extension
This commit was merged in pull request #2.
This commit is contained in:
@@ -10,6 +10,10 @@ The GitExtension enhances versioning by dynamically appending the Git hash befor
|
||||
|
||||
The InfoExtension provides information for name and version, vendor name, and UTC timestamp.
|
||||
|
||||
### Extension Config
|
||||
|
||||
The ConfigExtension provides a find or findOrDefault for getting a property or environment.
|
||||
|
||||
## Publish version catalog locally.
|
||||
|
||||
```shell
|
||||
|
||||
@@ -19,6 +19,7 @@ plugins {
|
||||
dependencies {
|
||||
implementation(aa.jgit)
|
||||
|
||||
testImplementation(aa.assertj)
|
||||
testImplementation(aa.junit.jupiter.params)
|
||||
testImplementation(aa.kotlin.junit5)
|
||||
testImplementation(aa.mockk)
|
||||
@@ -114,6 +115,8 @@ tasks {
|
||||
}
|
||||
}
|
||||
withType<Test> {
|
||||
// Set TEST_ENV environment variable for test execution
|
||||
environment["TEST_ENV"] = "lulz"
|
||||
useJUnitPlatform()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package ltd.lulz.plugin
|
||||
|
||||
import ltd.lulz.plugin.extension.ConfigExtension
|
||||
import ltd.lulz.plugin.extension.GitExtension
|
||||
import ltd.lulz.plugin.extension.InfoExtension
|
||||
import org.gradle.api.Plugin
|
||||
import org.gradle.api.Project
|
||||
import ltd.lulz.plugin.extension.ConfigExtension.Companion.PLUGIN_NAME as CONFIG_PLUGIN_NAME
|
||||
import ltd.lulz.plugin.extension.GitExtension.Companion.PLUGIN_NAME as GIT_PLUGIN_NAME
|
||||
import ltd.lulz.plugin.extension.InfoExtension.Companion.PLUGIN_NAME as INFO_PLUGIN_NAME
|
||||
|
||||
@@ -18,8 +20,14 @@ class CorePlugin : Plugin<Project> {
|
||||
) {
|
||||
gitExtension(project)
|
||||
infoExtension(project)
|
||||
configExtension(project)
|
||||
}
|
||||
|
||||
private fun configExtension(
|
||||
project: Project,
|
||||
): ConfigExtension = project.extensions
|
||||
.create(CONFIG_PLUGIN_NAME, ConfigExtension::class.java, project)
|
||||
|
||||
private fun infoExtension(
|
||||
project: Project,
|
||||
): InfoExtension = project.extensions
|
||||
|
||||
31
src/main/kotlin/ltd/lulz/plugin/extension/ConfigExtension.kt
Normal file
31
src/main/kotlin/ltd/lulz/plugin/extension/ConfigExtension.kt
Normal file
@@ -0,0 +1,31 @@
|
||||
package ltd.lulz.plugin.extension
|
||||
|
||||
import java.lang.System.getenv
|
||||
import org.gradle.api.Project
|
||||
|
||||
abstract class ConfigExtension(private val project: Project) {
|
||||
|
||||
companion object {
|
||||
const val PLUGIN_NAME = "config"
|
||||
const val EMPTY = ""
|
||||
}
|
||||
|
||||
fun find(
|
||||
property: String,
|
||||
environment: String,
|
||||
): String? = findProperty(property) ?: findEnvironment(environment)
|
||||
|
||||
fun findOrDefault(
|
||||
property: String,
|
||||
environment: String,
|
||||
default: String = EMPTY,
|
||||
): String = findProperty(property) ?: findEnvironment(environment) ?: default
|
||||
|
||||
private fun findProperty(
|
||||
property: String,
|
||||
) = project.findProperty(property)?.toString()
|
||||
|
||||
private fun findEnvironment(
|
||||
environment: String,
|
||||
): String? = getenv(environment)
|
||||
}
|
||||
112
src/test/kotlin/ltd/lulz/plugin/extension/ConfigExtensionTest.kt
Normal file
112
src/test/kotlin/ltd/lulz/plugin/extension/ConfigExtensionTest.kt
Normal file
@@ -0,0 +1,112 @@
|
||||
package ltd.lulz.plugin.extension
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.testfixtures.ProjectBuilder
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
/**
|
||||
* This test uses TEST_ENV set in gradle test task, and project extension to pretend to be a project property.
|
||||
*/
|
||||
class ConfigExtensionTest {
|
||||
|
||||
companion object {
|
||||
const val EXTENSION = "config"
|
||||
const val PLUGIN_ID = "ltd.lulz.plugin.core-plugin"
|
||||
const val PROJECT_NAME = "test-project"
|
||||
const val SNAPSHOT_VERSION = "0.0.0-SNAPSHOT"
|
||||
}
|
||||
|
||||
lateinit var project: Project
|
||||
|
||||
lateinit var configExtension: ConfigExtension
|
||||
|
||||
@BeforeEach
|
||||
fun beforeEach() {
|
||||
project = ProjectBuilder.builder()
|
||||
.withName(PROJECT_NAME)
|
||||
.build()
|
||||
project.version = SNAPSHOT_VERSION
|
||||
project.pluginManager.apply(PLUGIN_ID)
|
||||
project.extensions.add(
|
||||
"testProperty",
|
||||
object {
|
||||
override fun toString(): String = "lulz"
|
||||
},
|
||||
)
|
||||
configExtension = project.extensions.getByName(EXTENSION) as ConfigExtension
|
||||
}
|
||||
|
||||
@Nested
|
||||
inner class FindTest {
|
||||
|
||||
@Test
|
||||
fun `property or environment find property`() {
|
||||
// when
|
||||
val result = configExtension.find("testProperty", "TEST_PROPERTY")
|
||||
|
||||
// then
|
||||
assertThat(result).isEqualTo("lulz")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `property or environment find environment`() {
|
||||
// when
|
||||
val result = configExtension.find("test_env", "TEST_ENV")
|
||||
|
||||
// then
|
||||
assertThat(result).isEqualTo("lulz")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `property or environment find nothing`() {
|
||||
// when
|
||||
val result = configExtension.find("test", "TEST")
|
||||
|
||||
// then
|
||||
assertThat(result).isNull()
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
inner class FindOrDefaultTest {
|
||||
|
||||
@Test
|
||||
fun `property, environment, or default find property`() {
|
||||
// when
|
||||
val result = configExtension.findOrDefault("testProperty", "TEST_PROPERTY")
|
||||
|
||||
// then
|
||||
assertThat(result).isEqualTo("lulz")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `property, environment, or default find environment`() {
|
||||
// when
|
||||
val result = configExtension.findOrDefault("test_env", "TEST_ENV")
|
||||
|
||||
// then
|
||||
assertThat(result).isEqualTo("lulz")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `property, environment, or default find empty`() {
|
||||
// when
|
||||
val result = configExtension.findOrDefault("test", "TEST")
|
||||
|
||||
// then
|
||||
assertThat(result).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `property, environment, or default find defined response`() {
|
||||
// when
|
||||
val result = configExtension.findOrDefault("test", "TEST", "value")
|
||||
|
||||
// then
|
||||
assertThat(result).isEqualTo("value")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user