Skip to content

Commit 018bf22

Browse files
committed
Simplify BuildToolHarness process/JDK helpers
Drop the unused MIN_EXTERNAL_JDK floor and the defensive ANSI stripping. Move exec into a companion object and replace the subprocess JDK probe (and execOutput) with Runtime.version().feature().
1 parent 02ccd3d commit 018bf22

1 file changed

Lines changed: 16 additions & 54 deletions

File tree

tests/buildTools/src/test/kotlin/tests/BuildToolHarness.kt

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,53 +18,6 @@ import org.junit.jupiter.api.Assumptions.assumeTrue
1818
import org.junit.jupiter.api.DynamicTest
1919
import org.junit.jupiter.api.DynamicTest.dynamicTest
2020

21-
/** Minimum major version of the JVM on PATH required to run these tests. */
22-
private const val MIN_EXTERNAL_JDK = 11
23-
24-
private val ANSI = Regex("\u001B\\[[;\\d]*m")
25-
26-
private fun stripAnsi(s: String): String = ANSI.replace(s, "")
27-
28-
/** Run an external command, inheriting stdio. Throws if it exits non-zero. */
29-
internal fun exec(command: List<String>, cwd: Path) {
30-
val exit = ProcessBuilder(command).directory(cwd.toFile()).inheritIO().start().waitFor()
31-
check(exit == 0) { "Command failed ($exit): ${command.joinToString(" ")}" }
32-
}
33-
34-
/** Run an external command and return its combined stdout/stderr. */
35-
private fun execOutput(command: List<String>, cwd: Path): String {
36-
val process = ProcessBuilder(command).directory(cwd.toFile()).redirectErrorStream(true).start()
37-
val output = process.inputStream.readBytes().toString(StandardCharsets.UTF_8)
38-
val exit = process.waitFor()
39-
check(exit == 0) { "Command failed ($exit): ${command.joinToString(" ")}\n$output" }
40-
return output
41-
}
42-
43-
/**
44-
* Major version of the JVM that `java` on PATH resolves to. Compiled and executed as a subprocess
45-
* because the test JVM may differ from PATH.
46-
*/
47-
internal val externalJavaVersion: Int by lazy {
48-
val tmp = Files.createTempDirectory("PrintJavaVersion")
49-
try {
50-
Files.writeString(
51-
tmp.resolve("PrintJavaVersion.java"),
52-
"""
53-
public class PrintJavaVersion {
54-
public static void main(String[] args) {
55-
System.out.print(Runtime.version().feature());
56-
}
57-
}
58-
"""
59-
.trimIndent(),
60-
)
61-
exec(listOf("javac", "PrintJavaVersion.java"), tmp)
62-
execOutput(listOf("java", "PrintJavaVersion"), tmp).trim().toInt()
63-
} finally {
64-
tmp.toFile().deleteRecursively()
65-
}
66-
}
67-
6821
/**
6922
* Base class for build-tool integration tests. Each `check*` helper returns a JUnit 5
7023
* [DynamicTest]; suites expose them from a `@TestFactory` method.
@@ -83,7 +36,7 @@ abstract class BuildToolHarness {
8336
standardError = stream,
8437
)
8538
val exit = app.run(arguments)
86-
return exit to stripAnsi(buffer.toString(StandardCharsets.UTF_8.name()))
39+
return exit to buffer.toString(StandardCharsets.UTF_8.name())
8740
}
8841

8942
private fun listScipShards(targetroot: Path): List<Path> {
@@ -162,13 +115,11 @@ abstract class BuildToolHarness {
162115
substitutions: Map<String, String> = emptyMap(),
163116
): DynamicTest =
164117
dynamicTest(name) {
165-
val supported =
166-
externalJavaVersion >= MIN_EXTERNAL_JDK &&
167-
(maxJdk == null || externalJavaVersion <= maxJdk)
118+
// Some build tools cap the JDK they support (e.g. Gradle 8.10 tops
119+
// out at JDK 21); skip rather than fail when the JDK is too new.
168120
assumeTrue(
169-
supported,
170-
"Test $name ignored: external JDK $externalJavaVersion outside range " +
171-
"[$MIN_EXTERNAL_JDK, ${maxJdk ?: "infinity"}]",
121+
maxJdk == null || currentJavaVersion <= maxJdk,
122+
"Test $name ignored: JDK $currentJavaVersion exceeds max $maxJdk",
172123
)
173124

174125
val base = newTempBase()
@@ -244,4 +195,15 @@ abstract class BuildToolHarness {
244195
base.toFile().deleteRecursively()
245196
}
246197
}
198+
199+
companion object {
200+
/** Major version of the JVM running these tests (e.g. 17, 21). */
201+
private val currentJavaVersion: Int = Runtime.version().feature()
202+
203+
/** Run an external command, inheriting stdio. Throws if it exits non-zero. */
204+
internal fun exec(command: List<String>, cwd: Path) {
205+
val exit = ProcessBuilder(command).directory(cwd.toFile()).inheritIO().start().waitFor()
206+
check(exit == 0) { "Command failed ($exit): ${command.joinToString(" ")}" }
207+
}
208+
}
247209
}

0 commit comments

Comments
 (0)