jdaugherty commented on code in PR #15403:
URL: https://github.com/apache/grails-core/pull/15403#discussion_r2817726232


##########
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:
   Let's pass this value via the gradle configuration of the Test itself?  No 
reason to be dynamically looking this up when it's available at the time of 
configuration in the test task.



##########
grails-gradle/plugins/build.gradle:
##########
@@ -56,6 +56,15 @@ dependencies {
     implementation 'org.springframework.boot:spring-boot-gradle-plugin'
     implementation 'org.springframework.boot:spring-boot-loader-tools'
     implementation 'io.spring.gradle:dependency-management-plugin'
+
+    // Testing - Gradle TestKit is auto-added by java-gradle-plugin
+    // NOTE: Cannot use Spock here because grails-gradle-tasks transitively
+    // pulls org.apache.groovy:groovy:4.0.30 which conflicts with

Review Comment:
   why is grails-gradle-tasks using groovy 4?  just exclude it since we target 
groovy 3 with gradle.



##########
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:
   Does `@TempDir` remove this when the test concludes? 



##########
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:
   There's a pretty good base class in the grails publish project that is meant 
to handle a lot of this boilerplate. Let's just copy it to grails-core



##########
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:
   If you copy the spec from grails publish, these can be actual files checked 
in that are easy to modify over time instead of being hard coded as strings



##########
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 {

Review Comment:
   Why not use spock? 



-- 
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]

Reply via email to