Unverified Commit 47fdf061 authored by Benedikt Ritter's avatar Benedikt Ritter
Browse files

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.
parent f3191c68
Loading
Loading
Loading
Loading
+7 −8
Original line number Diff line number Diff line
{
  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;
          }
+11 −0
Original line number Diff line number Diff line
plugins {
    id('application')
}

java {
    toolchain.languageVersion.set(JavaLanguageVersion.of(@JAVA_VERSION@))
}

application {
    mainClass = 'Main'
}
+5 −0
Original line number Diff line number Diff line
public class Main {
    public static void main(String[] args) {
        System.out.println("JAVA_VERSION: " + System.getProperty("java.version"));
    }
}
+0 −1
Original line number Diff line number Diff line
@@ -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