diff --git a/src/main/kotlin/io/hndrs/gradle/plugin/git/properties/data/GitConfigPropertiesProvider.kt b/src/main/kotlin/io/hndrs/gradle/plugin/git/properties/data/GitConfigPropertiesProvider.kt index e882e49..e9b877c 100644 --- a/src/main/kotlin/io/hndrs/gradle/plugin/git/properties/data/GitConfigPropertiesProvider.kt +++ b/src/main/kotlin/io/hndrs/gradle/plugin/git/properties/data/GitConfigPropertiesProvider.kt @@ -23,8 +23,9 @@ class GitConfigPropertiesProvider( return runCatching { URI(this).userInfo?.let { this.replace(it, "").replace("@", "") - } - }.getOrDefault(this) + } ?: this + }.getOrElse { null } + } companion object { diff --git a/src/test/kotlin/io/hndrs/gradle/plugin/git/properties/data/GitConfigPropertiesProviderTest.kt b/src/test/kotlin/io/hndrs/gradle/plugin/git/properties/data/GitConfigPropertiesProviderTest.kt index 9f2516d..3ed86e8 100644 --- a/src/test/kotlin/io/hndrs/gradle/plugin/git/properties/data/GitConfigPropertiesProviderTest.kt +++ b/src/test/kotlin/io/hndrs/gradle/plugin/git/properties/data/GitConfigPropertiesProviderTest.kt @@ -5,20 +5,25 @@ import io.kotest.matchers.shouldBe import io.mockk.every import io.mockk.mockk import org.eclipse.jgit.api.Git +import org.eclipse.jgit.lib.StoredConfig class GitConfigPropertiesProviderTest : StringSpec({ + val storedConfig = mockk() { + every { load() } returns Unit + } val git = mockk() { - every { repository.config } returns mockk(relaxed = true) { - every { getString("user", null, "email") } returns "john.smith@gradlemail.com" - every { getString("user", null, "name") } returns "John Smith" - every { getString("remote", "origin", "url") } returns "https://name:password@github.com/hndrs/gradle-git-properties-plugin.git" - } + every { repository.config } returns storedConfig } val underTest = GitConfigPropertiesProvider(git) - "resolves git branch via git" { + "resolves git config properties (remote origin with credentials)" { + + every { storedConfig.getString("user", null, "email") } returns "john.smith@gradlemail.com" + every { storedConfig.getString("user", null, "name") } returns "John Smith" + every { storedConfig.getString("remote", "origin", "url") } returns "https://name:password@github.com/hndrs/gradle-git-properties-plugin.git" + underTest.get() shouldBe mapOf( "git.build.user.email" to "john.smith@gradlemail.com", "git.build.user.name" to "John Smith", @@ -26,4 +31,42 @@ class GitConfigPropertiesProviderTest : StringSpec({ ) } + "resolves git config properties (remote with git/ssh )" { + + every { storedConfig.getString("user", null, "email") } returns "john.smith@gradlemail.com" + every { storedConfig.getString("user", null, "name") } returns "John Smith" + every { storedConfig.getString("remote", "origin", "url") } returns "git@github.com/hndrs/gradle-git-properties-plugin.git" + + underTest.get() shouldBe mapOf( + "git.build.user.email" to "john.smith@gradlemail.com", + "git.build.user.name" to "John Smith", + "git.remote.origin.url" to "git@github.com/hndrs/gradle-git-properties-plugin.git" + ) + } + + "resolves git config properties (with unrecognized data)" { + + every { storedConfig.getString("user", null, "email") } returns "john.smith@gradlemail.com" + every { storedConfig.getString("user", null, "name") } returns "John Smith" + every { storedConfig.getString("remote", "origin", "url") } returns "someUnrelatedString" + + underTest.get() shouldBe mapOf( + "git.build.user.email" to "john.smith@gradlemail.com", + "git.build.user.name" to "John Smith", + "git.remote.origin.url" to "someUnrelatedString" + ) + } + + "resolves git config properties (with exception on parsing data)" { + + every { storedConfig.getString("user", null, "email") } returns "john.smith@gradlemail.com" + every { storedConfig.getString("user", null, "name") } returns "John Smith" + every { storedConfig.getString("remote", "origin", "url") } returns "https>" + + underTest.get() shouldBe mapOf( + "git.build.user.email" to "john.smith@gradlemail.com", + "git.build.user.name" to "John Smith", + "git.remote.origin.url" to null + ) + } }) \ No newline at end of file