Skip to content

Commit e4ba799

Browse files
authored
Test example scripts against latest code (#456)
Add a new test case that verifies running the example Kotlin script against the current library code, i.e. a SNAPSHOT artifact compiled just before the tests and published to Maven local. Prevents regresions like #426 (Kotlin notebooks issue) for Kotlin scripts. Follow-up to #432 for example-scripts.
1 parent 8f20142 commit e4ba799

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

examples/build.gradle.kts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@ project("example-project") {
1717

1818
val exampleTestTasks = ArrayList<TaskProvider<*>>()
1919

20-
exampleTestTasks += tasks.register<Exec>("runExampleScript") {
21-
group = "Application"
22-
description = "Runs the './example-scripts/example-script.main.kts' script"
23-
commandLine("kotlinc", "-script", file("./example-scripts/example-script.main.kts"))
24-
environment("JAVA_OPTS", "-Xmx1g")
25-
}
26-
2720
exampleTestTasks += tasks.register("runExampleProject") {
2821
group = "Application"
2922
description = "Runs examples/example-project"

library/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,5 @@ tasks.named<Test>("examplesTest") {
113113
inputs.files(files(publishUnsignedSnapshotDevelocityApiKotlinPublicationToMavenLocal))
114114
.withPropertyName("snapshotPublicationArtifacts")
115115
.withNormalizer(ClasspathNormalizer::class)
116+
maxParallelForks = 4
116117
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.gabrielfeo.develocity.api.example.script
2+
3+
import com.gabrielfeo.develocity.api.example.copyFromResources
4+
import com.gabrielfeo.develocity.api.example.runInShell
5+
import org.junit.jupiter.api.Assertions.assertTrue
6+
import org.junit.jupiter.api.BeforeEach
7+
import org.junit.jupiter.api.Test
8+
import org.junit.jupiter.api.io.TempDir
9+
import java.nio.file.Path
10+
import kotlin.io.path.div
11+
12+
class ScriptsTest {
13+
14+
@TempDir
15+
lateinit var tempDir: Path
16+
17+
@BeforeEach
18+
fun setup() {
19+
copyFromResources("/examples", tempDir)
20+
}
21+
22+
@Test
23+
fun testMostFrequentBuildsScript() {
24+
val script = tempDir / "examples/example-scripts/example-script.main.kts"
25+
val replacedScript = forceUseOfMavenLocalSnapshotArtifact(script)
26+
val output = runInShell(tempDir, "kotlin '$replacedScript'").trim()
27+
val tableRegex = Regex("""(?ms)^[-]+\nMost frequent builds:\n\s*\n(.+\|\s*\d+\s*\n?)+""")
28+
assertTrue(tableRegex.containsMatchIn(output)) {
29+
"Expected match for pattern '$tableRegex' in output '$output'"
30+
}
31+
}
32+
33+
/**
34+
* Replaces the dependency declaration in the script with a SNAPSHOT version and adds the maven local repository.
35+
*/
36+
private fun forceUseOfMavenLocalSnapshotArtifact(scriptPath: Path): Path {
37+
val mavenLocal = checkNotNull(System.getProperty("user.home")).let { "$it/.m2/repository" }
38+
val scriptText = scriptPath.toFile().readText()
39+
val replaced = scriptText.replace(
40+
Regex("@file:DependsOn\\([^)]+\\)"),
41+
"""
42+
@file:DependsOn("com.gabrielfeo:develocity-api-kotlin:SNAPSHOT")
43+
@file:Repository("file://$mavenLocal")
44+
""".trimIndent(),
45+
)
46+
val replacedPath = tempDir.resolve("examples/example-scripts/example-script-SNAPSHOT.main.kts")
47+
replacedPath.toFile().writeText(replaced)
48+
return replacedPath
49+
}
50+
}

0 commit comments

Comments
 (0)