Skip to content

Commit 891e755

Browse files
authored
Fix configuration via code (#78)
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.
1 parent 9e064e4 commit 891e755

File tree

10 files changed

+77
-44
lines changed

10 files changed

+77
-44
lines changed

library/build.gradle.kts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ testing {
188188
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.1")
189189
}
190190
}
191-
withType<JvmTestSuite> {
191+
withType<JvmTestSuite>().configureEach {
192192
useKotlinTest()
193193
}
194194
}
@@ -198,7 +198,13 @@ kotlin {
198198
target {
199199
val main by compilations.getting
200200
val integrationTest by compilations.getting
201+
val test by compilations.getting
202+
val testFixtures by compilations.getting
203+
test.associateWith(main)
204+
test.associateWith(testFixtures)
201205
integrationTest.associateWith(main)
206+
integrationTest.associateWith(testFixtures)
207+
testFixtures.associateWith(main)
202208
}
203209
}
204210

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
package com.gabrielfeo.gradle.enterprise.api.internal
22

3+
import com.gabrielfeo.gradle.enterprise.api.Config
34
import com.gabrielfeo.gradle.enterprise.api.GradleEnterpriseApi
45
import kotlinx.coroutines.test.runTest
5-
import kotlin.test.Test
6+
import org.junit.jupiter.api.assertDoesNotThrow
67
import kotlin.test.assertEquals
8+
import kotlin.test.Test
79

810
class GradleEnterpriseApiIntegrationTest {
911

1012
@Test
11-
fun canFetchBuilds() = runTest {
13+
fun canFetchBuildsWithDefaultInstance() = runTest {
14+
env = RealEnv
15+
keychain = RealKeychain(RealSystemProperties)
1216
val builds = GradleEnterpriseApi.buildsApi.getBuilds(since = 0, maxBuilds = 1)
1317
assertEquals(1, builds.size)
1418
GradleEnterpriseApi.shutdown()
1519
}
20+
21+
@Test
22+
fun canBuildNewInstanceWithPureCodeConfiguration() = runTest {
23+
env = FakeEnv()
24+
keychain = FakeKeychain()
25+
assertDoesNotThrow {
26+
val config = Config(
27+
apiUrl = "https://google.com/api/",
28+
apiToken = { "" },
29+
)
30+
GradleEnterpriseApi.newInstance(config)
31+
}
32+
}
1633
}

library/src/integrationTest/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/KeychainIntegrationTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.gabrielfeo.gradle.enterprise.api.internal
22

33
import com.gabrielfeo.gradle.enterprise.api.internal.keychain
4-
import kotlin.test.Test
5-
import kotlin.test.assertFalse
6-
import kotlin.test.assertIs
4+
import kotlin.test.*
75

86
internal class KeychainIntegrationTest {
97

108
@Test
119
fun getApiToken() {
10+
env = RealEnv
11+
keychain = RealKeychain(RealSystemProperties)
1212
val result = keychain.get("gradle-enterprise-api-token")
1313
assertIs<KeychainResult.Success>(result)
1414
assertFalse(result.token.isNullOrBlank(), "Keychain returned null or blank")

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,13 @@ interface GradleEnterpriseApi {
6565
}
6666

6767
internal class RealGradleEnterpriseApi(
68-
override val config: Config = Config(),
68+
customConfig: Config? = null,
6969
) : GradleEnterpriseApi {
7070

71+
override val config by lazy {
72+
customConfig ?: Config()
73+
}
74+
7175
private val okHttpClient by lazy {
7276
buildOkHttpClient(config = config)
7377
}
Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,2 @@
11
package com.gabrielfeo.gradle.enterprise.api.internal
22

3-
class FakeEnv(
4-
vararg vars: Pair<String, String?>,
5-
) : Env {
6-
7-
private val vars = vars.toMap(HashMap())
8-
9-
override fun get(name: String) = vars[name]
10-
11-
operator fun set(name: String, value: String?) = vars.put(name, value)
12-
operator fun contains(name: String) = name in vars
13-
}
Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,2 @@
11
package com.gabrielfeo.gradle.enterprise.api.internal
22

3-
internal class FakeKeychain(
4-
vararg entries: Pair<String, String>,
5-
) : Keychain {
6-
7-
private val entries = entries.toMap()
8-
9-
override fun get(entry: String) =
10-
entries[entry]?.let { KeychainResult.Success(it) }
11-
?: KeychainResult.Error("entry $entry not mocked")
12-
}
Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,2 @@
11
package com.gabrielfeo.gradle.enterprise.api.internal
22

3-
class FakeSystemProperties(
4-
vararg vars: Pair<String, String?>,
5-
) : SystemProperties {
6-
7-
companion object {
8-
val macOs = FakeSystemProperties("os.name" to "Mac OS X")
9-
val linux = FakeSystemProperties("os.name" to "Linux")
10-
}
11-
12-
private val vars = vars.toMap(HashMap())
13-
14-
override fun get(name: String) = vars[name]
15-
16-
operator fun set(name: String, value: String?) = vars.put(name, value)
17-
operator fun contains(name: String) = name in vars
18-
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.gabrielfeo.gradle.enterprise.api.internal
2+
3+
class FakeEnv(
4+
vararg vars: Pair<String, String?>,
5+
) : Env {
6+
7+
private val vars = vars.toMap(HashMap())
8+
9+
override fun get(name: String) = vars[name]
10+
11+
operator fun set(name: String, value: String?) = vars.put(name, value)
12+
operator fun contains(name: String) = name in vars
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.gabrielfeo.gradle.enterprise.api.internal
2+
3+
internal class FakeKeychain(
4+
vararg entries: Pair<String, String>,
5+
) : Keychain {
6+
7+
private val entries = entries.toMap()
8+
9+
override fun get(entry: String) =
10+
entries[entry]?.let { KeychainResult.Success(it) }
11+
?: KeychainResult.Error("entry $entry not mocked")
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.gabrielfeo.gradle.enterprise.api.internal
2+
3+
class FakeSystemProperties(
4+
vararg vars: Pair<String, String?>,
5+
) : SystemProperties {
6+
7+
companion object {
8+
val macOs = FakeSystemProperties("os.name" to "Mac OS X")
9+
val linux = FakeSystemProperties("os.name" to "Linux")
10+
}
11+
12+
private val vars = vars.toMap(HashMap())
13+
14+
override fun get(name: String) = vars[name]
15+
16+
operator fun set(name: String, value: String?) = vars.put(name, value)
17+
operator fun contains(name: String) = name in vars
18+
}

0 commit comments

Comments
 (0)