gradle: Improve toolchains test

Previously the test relied on the output of the javaToolchains task and
therefore on the way that toolchains are passed to Gradle. This was
brittle in case we want to change the way of how we supply toolchains to
Gradle (see e.g. #366929).

After this change, instead of inspecting the output of javaToolchains,
the toolchains test now tries to assemble a project using a toolchain
that was passed via the gradle.override.
This commit is contained in:
Benedikt Ritter
2025-04-25 20:56:01 +02:00
parent f3191c68ab
commit 47fdf06101
4 changed files with 23 additions and 9 deletions
@@ -1,5 +1,4 @@
{
jdk11,
jdk17,
jdk21,
jdk23,
@@ -288,27 +287,27 @@ rec {
unwrapped = gradle;
tests = {
toolchains =
let
javaVersion = lib.substring 0 2 (lib.getVersion jdk23);
in
runCommand "detects-toolchains-from-nix-env"
{
# Use JDKs that are not the default for any of the gradle versions
nativeBuildInputs = [
(gradle.override {
javaToolchains = [
jdk11
jdk23
];
})
];
src = ./tests/java-application;
src = ./tests/toolchains;
}
''
cp -a $src/* .
substituteInPlace ./build.gradle --replace-fail '@JAVA_VERSION@' '${javaVersion}'
env GRADLE_USER_HOME=$TMPDIR/gradle org.gradle.native.dir=$TMPDIR/native \
gradle javaToolchains --no-daemon --quiet --console plain > $out
cat $out | grep "Language Version: 11"
cat $out | grep "Detected by: environment variable 'JAVA_TOOLCHAIN_NIX_0'"
cat $out | grep "Language Version: 23"
cat $out | grep "Detected by: environment variable 'JAVA_TOOLCHAIN_NIX_1'"
gradle run --no-daemon --quiet --console plain > $out
grep -q "JAVA_VERSION: ${javaVersion}" $out || exit 1
'';
} // gradle.tests;
}
@@ -0,0 +1,11 @@
plugins {
id('application')
}
java {
toolchain.languageVersion.set(JavaLanguageVersion.of(@JAVA_VERSION@))
}
application {
mainClass = 'Main'
}
@@ -0,0 +1,5 @@
public class Main {
public static void main(String[] args) {
System.out.println("JAVA_VERSION: " + System.getProperty("java.version"));
}
}
-1
View File
@@ -8317,7 +8317,6 @@ with pkgs;
gnumake = callPackage ../development/tools/build-managers/gnumake { };
gradle-packages = import ../development/tools/build-managers/gradle {
inherit
jdk11
jdk17
jdk21
jdk23