Skip to content

Commit 035d272

Browse files
authored
Isolate test and integrationTest from server and env (#492)
`test` should contain strictly unit tests, which should not be sensitive to environment variables. Integration tests should test integration between classes, without a dependency on a real Develocity server or environment variables. Change `integrationTest` cases that rely on a response to use mockwebserver and ensure both test suites run with no environment variables set. Also refactor test fixtures configuration to a separate plugin and include them for `examplesTest`, whose `Resources.kt` is now moved to `testFixtures`.
1 parent 7739caf commit 035d272

File tree

16 files changed

+187
-52
lines changed

16 files changed

+187
-52
lines changed

build-logic/src/main/kotlin/com/gabrielfeo/integration-test-suite.gradle.kts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,14 @@ testing {
1313
}
1414
}
1515

16-
tasks.named("check") {
17-
dependsOn("integrationTest")
18-
}
19-
2016
kotlin {
2117
target {
2218
val main by compilations.getting
2319
val integrationTest by compilations.getting
24-
val test by compilations.getting
25-
val testFixtures by compilations.getting
26-
test.associateWith(main)
27-
test.associateWith(testFixtures)
2820
integrationTest.associateWith(main)
29-
integrationTest.associateWith(testFixtures)
30-
testFixtures.associateWith(main)
3121
}
3222
}
3323

34-
// TODO Unapply test-fixtures and delete the source set, since we're not publishing it?
35-
components.named<AdhocComponentWithVariants>("java") {
36-
withVariantsFromConfiguration(configurations["testFixturesApiElements"]) { skip() }
37-
withVariantsFromConfiguration(configurations["testFixturesRuntimeElements"]) { skip() }
24+
tasks.named("check") {
25+
dependsOn("integrationTest")
3826
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.gabrielfeo
2+
3+
plugins {
4+
id("org.jetbrains.kotlin.jvm")
5+
`java-test-fixtures`
6+
}
7+
8+
kotlin {
9+
target {
10+
val main by compilations.getting
11+
val testFixtures by compilations.getting
12+
testFixtures.associateWith(main)
13+
compilations.named { it.endsWith("test", ignoreCase = true) }.configureEach {
14+
associateWith(testFixtures)
15+
}
16+
}
17+
}
18+
19+
components.named<AdhocComponentWithVariants>("java") {
20+
withVariantsFromConfiguration(configurations["testFixturesApiElements"]) { skip() }
21+
withVariantsFromConfiguration(configurations["testFixturesRuntimeElements"]) { skip() }
22+
}

library/build.gradle.kts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ plugins {
77
id("com.gabrielfeo.develocity-api-code-generation")
88
id("com.gabrielfeo.integration-test-suite")
99
id("com.gabrielfeo.examples-test-suite")
10+
id("com.gabrielfeo.test-fixtures")
1011
}
1112

1213
// Order matters as this library is used as a Kotlin Jupyter kernel dependency (see #440)
@@ -37,6 +38,7 @@ dependencies {
3738
integrationTestImplementation(libs.kotlin.jupyter.testkit)
3839
integrationTestImplementation(libs.logback.core)
3940
integrationTestImplementation(libs.logback.classic)
41+
integrationTestImplementation(libs.okhttp.mockwebserver)
4042
}
4143

4244
val libraryPom = Action<MavenPom> {
@@ -119,9 +121,14 @@ tasks.withType<Test>().configureEach {
119121
"junit.jupiter.tempdir.cleanup.mode.default",
120122
System.getProperty("junit.jupiter.tempdir.cleanup.mode.default") ?: "always",
121123
)
122-
providers.environmentVariablesPrefixedBy("DEVELOCITY_API_").get().forEach { (name, value) ->
123-
inputs.property("${name}.hashCode", value.hashCode())
124-
}
124+
}
125+
126+
tasks.named<Test>("test") {
127+
environment = emptyMap()
128+
}
129+
130+
tasks.named<Test>("integrationTest") {
131+
environment = emptyMap()
125132
}
126133

127134
val publishUnsignedSnapshotDevelocityApiKotlinPublicationToMavenLocal by tasks.getting
@@ -130,4 +137,7 @@ tasks.named<Test>("examplesTest") {
130137
inputs.files(files(publishUnsignedSnapshotDevelocityApiKotlinPublicationToMavenLocal))
131138
.withPropertyName("snapshotPublicationArtifacts")
132139
.withNormalizer(ClasspathNormalizer::class)
140+
providers.environmentVariablesPrefixedBy("DEVELOCITY_API_").get().forEach { (name, value) ->
141+
inputs.property("${name}.hashCode", value.hashCode())
142+
}
133143
}

library/src/examplesTest/kotlin/com/gabrielfeo/develocity/api/example/gradle/ExampleGradleTaskTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import org.junit.jupiter.api.TestMethodOrder
99
import org.junit.jupiter.api.Test
1010
import org.junit.jupiter.api.io.TempDir
1111
import java.nio.file.Path
12+
import com.gabrielfeo.develocity.api.copyFromResources
1213
import com.gabrielfeo.develocity.api.example.BuildStartTime
13-
import com.gabrielfeo.develocity.api.example.copyFromResources
1414
import com.gabrielfeo.develocity.api.example.runInShell
1515
import kotlin.io.path.div
1616

library/src/examplesTest/kotlin/com/gabrielfeo/develocity/api/example/gradle/ExampleProjectTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import org.junit.jupiter.api.Test
1010
import org.junit.jupiter.api.io.TempDir
1111
import java.nio.file.Path
1212
import kotlin.io.path.div
13-
import com.gabrielfeo.develocity.api.example.copyFromResources
13+
import com.gabrielfeo.develocity.api.copyFromResources
1414
import com.gabrielfeo.develocity.api.example.runInShell
1515

1616
@TestMethodOrder(OrderAnnotation::class)

library/src/examplesTest/kotlin/com/gabrielfeo/develocity/api/example/notebook/Jupyter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.gabrielfeo.develocity.api.example.notebook
22

3+
import com.gabrielfeo.develocity.api.copyFromResources
34
import com.gabrielfeo.develocity.api.example.OutputStreams
4-
import com.gabrielfeo.develocity.api.example.copyFromResources
55
import com.gabrielfeo.develocity.api.example.runInShell
66
import java.nio.file.Path
77
import kotlin.io.path.div

library/src/examplesTest/kotlin/com/gabrielfeo/develocity/api/example/notebook/NotebooksTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.gabrielfeo.develocity.api.example.notebook
22

3+
import com.gabrielfeo.develocity.api.copyFromResources
34
import com.gabrielfeo.develocity.api.example.JsonAdapter
45
import com.gabrielfeo.develocity.api.example.Queries
5-
import com.gabrielfeo.develocity.api.example.copyFromResources
66
import org.junit.jupiter.api.Assertions.assertTrue
77
import org.junit.jupiter.api.BeforeEach
88
import org.junit.jupiter.api.Test

library/src/examplesTest/kotlin/com/gabrielfeo/develocity/api/example/script/ScriptsTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.gabrielfeo.develocity.api.example.script
22

3+
import com.gabrielfeo.develocity.api.copyFromResources
34
import com.gabrielfeo.develocity.api.example.Queries
4-
import com.gabrielfeo.develocity.api.example.copyFromResources
55
import com.gabrielfeo.develocity.api.example.runInShell
66
import org.junit.jupiter.api.Assertions.assertTrue
77
import org.junit.jupiter.api.BeforeEach

library/src/integrationTest/kotlin/com/gabrielfeo/develocity/api/DevelocityApiIntegrationTest.kt

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package com.gabrielfeo.develocity.api
33
import com.gabrielfeo.develocity.api.internal.*
44
import com.google.common.reflect.ClassPath
55
import kotlinx.coroutines.test.runTest
6+
import okhttp3.mockwebserver.MockResponse
7+
import okhttp3.mockwebserver.MockWebServer
68
import org.junit.jupiter.api.assertDoesNotThrow
79
import java.net.URI
810
import kotlin.reflect.KVisibility.PUBLIC
@@ -13,30 +15,63 @@ import kotlin.test.*
1315
@OptIn(ExperimentalStdlibApi::class)
1416
class DevelocityApiIntegrationTest {
1517

16-
@Test
17-
fun canFetchBuildsWithDefaultConfig() = runTest {
18-
env = RealEnv
19-
val api = DevelocityApi.newInstance(
20-
config = Config(
21-
cacheConfig = Config.CacheConfig(cacheEnabled = false)
22-
)
18+
private lateinit var mockWebServer: MockWebServer
19+
private lateinit var mockWebServerEnv: Env
20+
private val emptyEnv: Env = FakeEnv()
21+
22+
@BeforeTest
23+
fun setUp() {
24+
mockWebServer = MockWebServer().apply { start() }
25+
mockWebServerEnv = FakeEnv(
26+
"DEVELOCITY_URL" to mockWebServer.url("/").toString(),
27+
"DEVELOCITY_ACCESS_KEY" to "${mockWebServer.url("/").host}=foo",
28+
"DEVELOCITY_CACHE_ENABLED" to "false",
2329
)
24-
val builds = api.buildsApi.getBuilds(
25-
since = 0,
26-
maxBuilds = 5,
27-
query = """buildStartTime>-7d""",
30+
}
31+
32+
@AfterTest
33+
fun tearDown() {
34+
mockWebServer.shutdown()
35+
}
36+
37+
@Test
38+
fun canFetchBuildsWithEnvVarConfigAndEmptyBuildsResponse() = runTest {
39+
env = mockWebServerEnv
40+
mockWebServer.enqueue(MockResponse().setBody("[]"))
41+
val api = DevelocityApi.newInstance()
42+
val builds = api.buildsApi.getBuilds(fromInstant = 0)
43+
assertEquals(0, builds.size)
44+
api.shutdown()
45+
}
46+
47+
@Test
48+
fun canFetchBuildsWithEnvVarConfigAndNonEmptyBuildsResponse() = runTest {
49+
val response = requireResource("/response/api/builds/5-builds.json").readText()
50+
mockWebServer.enqueue(MockResponse().setBody(response))
51+
env = mockWebServerEnv
52+
val api = DevelocityApi.newInstance()
53+
val builds = api.buildsApi.getBuilds(fromInstant = 0)
54+
assertContentEquals(
55+
listOf(
56+
"67b3o5ld6iwc2",
57+
"e2bajrtqpe4bi",
58+
"rb5bbp6hxpcto",
59+
"gur3efx4fnqsc",
60+
"tw3yw5fhovwtq",
61+
),
62+
builds.map { it.id },
2863
)
29-
assertEquals(5, builds.size)
3064
api.shutdown()
3165
}
3266

3367
@Test
34-
fun canBuildNewInstanceWithPureCodeConfiguration() = runTest {
35-
env = FakeEnv()
68+
fun canFetchBuildsWithCodeConfig() = runTest {
69+
env = emptyEnv
70+
mockWebServer.enqueue(MockResponse().setBody("[]"))
3671
assertDoesNotThrow {
3772
val config = Config(
38-
server = URI("https://example.com/"),
39-
accessKey = { "example.com=example-token" }
73+
server = mockWebServer.url("/").toUri(),
74+
accessKey = { "${mockWebServer.url("/").host}=foo" },
4075
)
4176
DevelocityApi.newInstance(config)
4277
}

library/src/integrationTest/kotlin/com/gabrielfeo/develocity/api/LoggingIntegrationTest.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,14 @@ class LoggingIntegrationTest {
4747
})
4848
}
4949
recorder.start()
50-
env = RealEnv
50+
val mockWebServer = okhttp3.mockwebserver.MockWebServer()
51+
mockWebServer.enqueue(okhttp3.mockwebserver.MockResponse().setBody("[]"))
52+
mockWebServer.start()
53+
env = FakeEnv()
5154
api = DevelocityApi.newInstance(
5255
config = Config(
56+
server = mockWebServer.url("/").toUri(),
57+
accessKey = { "${mockWebServer.url("/").host}=foo" },
5358
cacheConfig = Config.CacheConfig(
5459
cacheEnabled = true,
5560
cacheDir = tempDir,

0 commit comments

Comments
 (0)