jamesfredley commented on code in PR #15403: URL: https://github.com/apache/grails-core/pull/15403#discussion_r2825054332
########## grails-gradle/plugins/src/test/java/org/grails/gradle/plugin/core/GrailsGradlePluginToolchainTest.java: ########## @@ -0,0 +1,399 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.gradle.plugin.core; + +import org.gradle.testkit.runner.BuildResult; +import org.gradle.testkit.runner.GradleRunner; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Properties; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Tests that {@link GrailsGradlePlugin} propagates the project's Java toolchain + * to JavaExec tasks via {@code javaLauncher.convention()}. + * + * <p>Without the fix, JavaExec tasks spawned by Grails (dbm-* migration + * commands, console, shell, application context commands) use the JDK + * running Gradle instead of the project's configured toolchain. This + * causes {@code UnsupportedClassVersionError} when the project targets + * a different JDK version than the one running Gradle.</p> + * + * <p>NOTE: Cannot use Spock here because grails-gradle-tasks transitively + * pulls {@code org.apache.groovy:groovy:4.0.30} which conflicts with + * Gradle's built-in {@code org.codehaus.groovy:groovy:3.0.25}.</p> + * + * @since 7.0.8 + */ +class GrailsGradlePluginToolchainTest { + + @TempDir + Path projectDir; + + private static final int CURRENT_JDK = Runtime.version().feature(); + private static final String GRAILS_VERSION = resolveGrailsVersion(); + + /** + * Reads {@code projectVersion} from the root {@code gradle.properties} so the + * test stays in sync with the actual build and is not tied to a hardcoded version. + */ + private static String resolveGrailsVersion() { Review Comment: Done. and are now passed as system properties from the test task configuration in , and read via in the base class. No more runtime discovery. ########## grails-gradle/plugins/src/test/java/org/grails/gradle/plugin/core/GrailsGradlePluginToolchainTest.java: ########## @@ -0,0 +1,399 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.gradle.plugin.core; + +import org.gradle.testkit.runner.BuildResult; +import org.gradle.testkit.runner.GradleRunner; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Properties; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Tests that {@link GrailsGradlePlugin} propagates the project's Java toolchain + * to JavaExec tasks via {@code javaLauncher.convention()}. + * + * <p>Without the fix, JavaExec tasks spawned by Grails (dbm-* migration + * commands, console, shell, application context commands) use the JDK + * running Gradle instead of the project's configured toolchain. This + * causes {@code UnsupportedClassVersionError} when the project targets + * a different JDK version than the one running Gradle.</p> + * + * <p>NOTE: Cannot use Spock here because grails-gradle-tasks transitively + * pulls {@code org.apache.groovy:groovy:4.0.30} which conflicts with + * Gradle's built-in {@code org.codehaus.groovy:groovy:3.0.25}.</p> + * + * @since 7.0.8 + */ +class GrailsGradlePluginToolchainTest { + + @TempDir + Path projectDir; + + private static final int CURRENT_JDK = Runtime.version().feature(); + private static final String GRAILS_VERSION = resolveGrailsVersion(); + + /** + * Reads {@code projectVersion} from the root {@code gradle.properties} so the + * test stays in sync with the actual build and is not tied to a hardcoded version. + */ + private static String resolveGrailsVersion() { + Properties props = new Properties(); + try (InputStream in = GrailsGradlePluginToolchainTest.class + .getClassLoader().getResourceAsStream("grails-gradle-plugins-project.properties")) { + if (in != null) { + props.load(in); + String v = props.getProperty("projectVersion"); + if (v != null && !v.isEmpty()) return v; + } + } catch (IOException ignored) { } + // Fallback: read from root gradle.properties relative to working directory + try { + Path root = Path.of(System.getProperty("user.dir")); + // Walk up until we find gradle.properties with projectVersion + for (Path dir = root; dir != null; dir = dir.getParent()) { + Path gp = dir.resolve("gradle.properties"); + if (Files.exists(gp)) { + Properties rootProps = new Properties(); + try (var reader = Files.newBufferedReader(gp)) { + rootProps.load(reader); + } + String v = rootProps.getProperty("projectVersion"); + if (v != null && !v.isEmpty()) return v; + } + } + } catch (IOException ignored) { } + return "7.0.8-SNAPSHOT"; + } + + @BeforeEach + void setup() throws IOException { Review Comment: Yes, `@TempDir` auto-deletes the directory and all contents after each test. Now using Spock's `@TempDir` which has the same behavior. ########## grails-gradle/plugins/src/test/java/org/grails/gradle/plugin/core/GrailsGradlePluginToolchainTest.java: ########## @@ -0,0 +1,399 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.gradle.plugin.core; + +import org.gradle.testkit.runner.BuildResult; +import org.gradle.testkit.runner.GradleRunner; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Properties; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Tests that {@link GrailsGradlePlugin} propagates the project's Java toolchain + * to JavaExec tasks via {@code javaLauncher.convention()}. + * + * <p>Without the fix, JavaExec tasks spawned by Grails (dbm-* migration + * commands, console, shell, application context commands) use the JDK + * running Gradle instead of the project's configured toolchain. This + * causes {@code UnsupportedClassVersionError} when the project targets + * a different JDK version than the one running Gradle.</p> + * + * <p>NOTE: Cannot use Spock here because grails-gradle-tasks transitively + * pulls {@code org.apache.groovy:groovy:4.0.30} which conflicts with + * Gradle's built-in {@code org.codehaus.groovy:groovy:3.0.25}.</p> + * + * @since 7.0.8 + */ +class GrailsGradlePluginToolchainTest { + + @TempDir + Path projectDir; + + private static final int CURRENT_JDK = Runtime.version().feature(); + private static final String GRAILS_VERSION = resolveGrailsVersion(); + + /** + * Reads {@code projectVersion} from the root {@code gradle.properties} so the + * test stays in sync with the actual build and is not tied to a hardcoded version. + */ + private static String resolveGrailsVersion() { + Properties props = new Properties(); + try (InputStream in = GrailsGradlePluginToolchainTest.class + .getClassLoader().getResourceAsStream("grails-gradle-plugins-project.properties")) { + if (in != null) { + props.load(in); + String v = props.getProperty("projectVersion"); + if (v != null && !v.isEmpty()) return v; + } + } catch (IOException ignored) { } + // Fallback: read from root gradle.properties relative to working directory + try { + Path root = Path.of(System.getProperty("user.dir")); + // Walk up until we find gradle.properties with projectVersion + for (Path dir = root; dir != null; dir = dir.getParent()) { + Path gp = dir.resolve("gradle.properties"); + if (Files.exists(gp)) { + Properties rootProps = new Properties(); + try (var reader = Files.newBufferedReader(gp)) { + rootProps.load(reader); + } + String v = rootProps.getProperty("projectVersion"); + if (v != null && !v.isEmpty()) return v; + } + } + } catch (IOException ignored) { } + return "7.0.8-SNAPSHOT"; + } + + @BeforeEach + void setup() throws IOException { + // Minimal Grails directory structure required by the plugin + Files.createDirectories(projectDir.resolve("grails-app/conf")); + Files.writeString(projectDir.resolve("grails-app/conf/application.yml"), ""); + Files.writeString(projectDir.resolve("settings.gradle"), "rootProject.name = 'test-toolchain'\n"); + // GrailsGradlePlugin.resolveGrailsVersion() requires this property + Files.writeString(projectDir.resolve("gradle.properties"), "grailsVersion=" + GRAILS_VERSION + "\n"); + } + + private BuildResult runBuild(String... args) { Review Comment: Done. Added `GradleSpecification` base class adapted from grails-gradle-publish. It handles temp dir setup, `GradleRunner` configuration, and copying test project fixtures from `src/test/resources/test-projects/` with placeholder replacement for `__PROJECT_VERSION__` and `__CURRENT_JDK__`. ########## grails-gradle/plugins/src/test/java/org/grails/gradle/plugin/core/GrailsGradlePluginToolchainTest.java: ########## @@ -0,0 +1,399 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.gradle.plugin.core; + +import org.gradle.testkit.runner.BuildResult; +import org.gradle.testkit.runner.GradleRunner; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Properties; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Tests that {@link GrailsGradlePlugin} propagates the project's Java toolchain + * to JavaExec tasks via {@code javaLauncher.convention()}. + * + * <p>Without the fix, JavaExec tasks spawned by Grails (dbm-* migration + * commands, console, shell, application context commands) use the JDK + * running Gradle instead of the project's configured toolchain. This + * causes {@code UnsupportedClassVersionError} when the project targets + * a different JDK version than the one running Gradle.</p> + * + * <p>NOTE: Cannot use Spock here because grails-gradle-tasks transitively + * pulls {@code org.apache.groovy:groovy:4.0.30} which conflicts with + * Gradle's built-in {@code org.codehaus.groovy:groovy:3.0.25}.</p> + * + * @since 7.0.8 + */ +class GrailsGradlePluginToolchainTest { + + @TempDir + Path projectDir; + + private static final int CURRENT_JDK = Runtime.version().feature(); + private static final String GRAILS_VERSION = resolveGrailsVersion(); + + /** + * Reads {@code projectVersion} from the root {@code gradle.properties} so the + * test stays in sync with the actual build and is not tied to a hardcoded version. + */ + private static String resolveGrailsVersion() { + Properties props = new Properties(); + try (InputStream in = GrailsGradlePluginToolchainTest.class + .getClassLoader().getResourceAsStream("grails-gradle-plugins-project.properties")) { + if (in != null) { + props.load(in); + String v = props.getProperty("projectVersion"); + if (v != null && !v.isEmpty()) return v; + } + } catch (IOException ignored) { } + // Fallback: read from root gradle.properties relative to working directory + try { + Path root = Path.of(System.getProperty("user.dir")); + // Walk up until we find gradle.properties with projectVersion + for (Path dir = root; dir != null; dir = dir.getParent()) { + Path gp = dir.resolve("gradle.properties"); + if (Files.exists(gp)) { + Properties rootProps = new Properties(); + try (var reader = Files.newBufferedReader(gp)) { + rootProps.load(reader); + } + String v = rootProps.getProperty("projectVersion"); + if (v != null && !v.isEmpty()) return v; + } + } + } catch (IOException ignored) { } + return "7.0.8-SNAPSHOT"; + } + + @BeforeEach + void setup() throws IOException { + // Minimal Grails directory structure required by the plugin + Files.createDirectories(projectDir.resolve("grails-app/conf")); + Files.writeString(projectDir.resolve("grails-app/conf/application.yml"), ""); + Files.writeString(projectDir.resolve("settings.gradle"), "rootProject.name = 'test-toolchain'\n"); + // GrailsGradlePlugin.resolveGrailsVersion() requires this property + Files.writeString(projectDir.resolve("gradle.properties"), "grailsVersion=" + GRAILS_VERSION + "\n"); + } + + private BuildResult runBuild(String... args) { + return GradleRunner.create() + .withProjectDir(projectDir.toFile()) + .withArguments(args) + .withPluginClasspath() + .forwardOutput() + .build(); + } + + // ---------------------------------------------------------------- + // Toolchain propagation tests + // ---------------------------------------------------------------- + + @Nested + @DisplayName("With toolchain configured") + class WithToolchain { + + @Test + @DisplayName("JavaExec tasks inherit project toolchain") + void javaExecInheritsToolchain() throws IOException { + Files.writeString(projectDir.resolve("build.gradle"), Review Comment: Done. All 8 test scenarios are now file-based fixtures under `src/test/resources/test-projects/` (toolchain-javaexec, toolchain-test, toolchain-command, toolchain-override, no-toolchain-javaexec, no-toolchain-web, fork-settings-defaults, fork-settings-custom). Each has its own `build.gradle`, `settings.gradle`, `gradle.properties`, and `grails-app/conf/application.yml`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
