Skip to content

Commit bc569c2

Browse files
authored
Don't fail eagerly from missing token (#73)
Using a method reference of `retrofit` for the lazy delegates of properties causes `retrofit` to be eagerly initialized.
1 parent c956790 commit bc569c2

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/GradleEnterpriseApi.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ interface GradleEnterpriseApi {
6464

6565
}
6666

67-
private class RealGradleEnterpriseApi(
67+
internal class RealGradleEnterpriseApi(
6868
override val config: Config = Config(),
6969
) : GradleEnterpriseApi {
7070

@@ -80,10 +80,10 @@ private class RealGradleEnterpriseApi(
8080
)
8181
}
8282

83-
override val buildsApi: BuildsApi by lazy(retrofit::create)
84-
override val buildCacheApi: BuildCacheApi by lazy(retrofit::create)
85-
override val metaApi: MetaApi by lazy(retrofit::create)
86-
override val testDistributionApi: TestDistributionApi by lazy(retrofit::create)
83+
override val buildsApi: BuildsApi by lazy { retrofit.create() }
84+
override val buildCacheApi: BuildCacheApi by lazy { retrofit.create() }
85+
override val metaApi: MetaApi by lazy { retrofit.create() }
86+
override val testDistributionApi: TestDistributionApi by lazy { retrofit.create() }
8787

8888
override fun shutdown() {
8989
okHttpClient.run {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.gabrielfeo.gradle.enterprise.api
2+
3+
import com.gabrielfeo.gradle.enterprise.api.internal.*
4+
import org.junit.jupiter.api.assertDoesNotThrow
5+
import org.junit.jupiter.api.assertThrows
6+
import kotlin.test.Test
7+
import kotlin.test.assertContains
8+
9+
class GradleEnterpriseApiTest {
10+
11+
@Test
12+
fun `Fails eagerly if no API URL`() {
13+
env = FakeEnv()
14+
keychain = FakeKeychain()
15+
systemProperties = FakeSystemProperties.linux
16+
val error = assertThrows<Exception> {
17+
GradleEnterpriseApi.newInstance(Config())
18+
}
19+
error.assertRootMessageContains("GRADLE_ENTERPRISE_API_URL")
20+
}
21+
22+
@Test
23+
fun `Fails lazily if no API token`() {
24+
env = FakeEnv("GRADLE_ENTERPRISE_API_URL" to "example-url")
25+
keychain = FakeKeychain()
26+
systemProperties = FakeSystemProperties.linux
27+
val api = assertDoesNotThrow {
28+
GradleEnterpriseApi.newInstance(Config())
29+
}
30+
val error = assertThrows<Exception> {
31+
api.buildsApi.toString()
32+
}
33+
error.assertRootMessageContains("GRADLE_ENTERPRISE_API_TOKEN")
34+
}
35+
36+
private fun Throwable.assertRootMessageContains(text: String) {
37+
cause?.assertRootMessageContains(text) ?: assertContains(message.orEmpty(), text)
38+
}
39+
}

0 commit comments

Comments
 (0)