Skip to content

Commit

Permalink
Fix configuration via code (#78)
Browse files Browse the repository at this point in the history
Related to #73. The DefaultInstance would be initialized with the
default Config eagerly when building a new instance with configuration
via code. If there were no URL environment variable set, the
DefaultInstance init would throw due to a missing URL in its config.

Fix by ensuring that the default Config is initialized lazily.
  • Loading branch information
gabrielfeo authored May 25, 2023
1 parent 9e064e4 commit 891e755
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 44 deletions.
8 changes: 7 additions & 1 deletion library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ testing {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.1")
}
}
withType<JvmTestSuite> {
withType<JvmTestSuite>().configureEach {
useKotlinTest()
}
}
Expand All @@ -198,7 +198,13 @@ kotlin {
target {
val main by compilations.getting
val integrationTest by compilations.getting
val test by compilations.getting
val testFixtures by compilations.getting
test.associateWith(main)
test.associateWith(testFixtures)
integrationTest.associateWith(main)
integrationTest.associateWith(testFixtures)
testFixtures.associateWith(main)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
package com.gabrielfeo.gradle.enterprise.api.internal

import com.gabrielfeo.gradle.enterprise.api.Config
import com.gabrielfeo.gradle.enterprise.api.GradleEnterpriseApi
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import org.junit.jupiter.api.assertDoesNotThrow
import kotlin.test.assertEquals
import kotlin.test.Test

class GradleEnterpriseApiIntegrationTest {

@Test
fun canFetchBuilds() = runTest {
fun canFetchBuildsWithDefaultInstance() = runTest {
env = RealEnv
keychain = RealKeychain(RealSystemProperties)
val builds = GradleEnterpriseApi.buildsApi.getBuilds(since = 0, maxBuilds = 1)
assertEquals(1, builds.size)
GradleEnterpriseApi.shutdown()
}

@Test
fun canBuildNewInstanceWithPureCodeConfiguration() = runTest {
env = FakeEnv()
keychain = FakeKeychain()
assertDoesNotThrow {
val config = Config(
apiUrl = "https://google.com/api/",
apiToken = { "" },
)
GradleEnterpriseApi.newInstance(config)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.gabrielfeo.gradle.enterprise.api.internal

import com.gabrielfeo.gradle.enterprise.api.internal.keychain
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertIs
import kotlin.test.*

internal class KeychainIntegrationTest {

@Test
fun getApiToken() {
env = RealEnv
keychain = RealKeychain(RealSystemProperties)
val result = keychain.get("gradle-enterprise-api-token")
assertIs<KeychainResult.Success>(result)
assertFalse(result.token.isNullOrBlank(), "Keychain returned null or blank")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ interface GradleEnterpriseApi {
}

internal class RealGradleEnterpriseApi(
override val config: Config = Config(),
customConfig: Config? = null,
) : GradleEnterpriseApi {

override val config by lazy {
customConfig ?: Config()
}

private val okHttpClient by lazy {
buildOkHttpClient(config = config)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,2 @@
package com.gabrielfeo.gradle.enterprise.api.internal

class FakeEnv(
vararg vars: Pair<String, String?>,
) : Env {

private val vars = vars.toMap(HashMap())

override fun get(name: String) = vars[name]

operator fun set(name: String, value: String?) = vars.put(name, value)
operator fun contains(name: String) = name in vars
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,2 @@
package com.gabrielfeo.gradle.enterprise.api.internal

internal class FakeKeychain(
vararg entries: Pair<String, String>,
) : Keychain {

private val entries = entries.toMap()

override fun get(entry: String) =
entries[entry]?.let { KeychainResult.Success(it) }
?: KeychainResult.Error("entry $entry not mocked")
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,2 @@
package com.gabrielfeo.gradle.enterprise.api.internal

class FakeSystemProperties(
vararg vars: Pair<String, String?>,
) : SystemProperties {

companion object {
val macOs = FakeSystemProperties("os.name" to "Mac OS X")
val linux = FakeSystemProperties("os.name" to "Linux")
}

private val vars = vars.toMap(HashMap())

override fun get(name: String) = vars[name]

operator fun set(name: String, value: String?) = vars.put(name, value)
operator fun contains(name: String) = name in vars
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.gabrielfeo.gradle.enterprise.api.internal

class FakeEnv(
vararg vars: Pair<String, String?>,
) : Env {

private val vars = vars.toMap(HashMap())

override fun get(name: String) = vars[name]

operator fun set(name: String, value: String?) = vars.put(name, value)
operator fun contains(name: String) = name in vars
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.gabrielfeo.gradle.enterprise.api.internal

internal class FakeKeychain(
vararg entries: Pair<String, String>,
) : Keychain {

private val entries = entries.toMap()

override fun get(entry: String) =
entries[entry]?.let { KeychainResult.Success(it) }
?: KeychainResult.Error("entry $entry not mocked")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.gabrielfeo.gradle.enterprise.api.internal

class FakeSystemProperties(
vararg vars: Pair<String, String?>,
) : SystemProperties {

companion object {
val macOs = FakeSystemProperties("os.name" to "Mac OS X")
val linux = FakeSystemProperties("os.name" to "Linux")
}

private val vars = vars.toMap(HashMap())

override fun get(name: String) = vars[name]

operator fun set(name: String, value: String?) = vars.put(name, value)
operator fun contains(name: String) = name in vars
}

0 comments on commit 891e755

Please sign in to comment.