From c347ccaa76ed14dbbcab8d7406967d15fe92814a Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 27 Sep 2024 12:17:29 -0700 Subject: [PATCH] Attempt to fix system tests following repository updates See gh-42333 --- buildSrc/SpringRepositorySupport.groovy | 15 +++++++++++++-- .../gradle/testkit/GradleBuildExtension.java | 19 +++++++++++++++++++ .../boot/image/paketo/settings.gradle | 1 + 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/buildSrc/SpringRepositorySupport.groovy b/buildSrc/SpringRepositorySupport.groovy index e17d4badbdf4..145d8dfae444 100644 --- a/buildSrc/SpringRepositorySupport.groovy +++ b/buildSrc/SpringRepositorySupport.groovy @@ -36,8 +36,19 @@ def apply(settings) { } private def property(settings, name) { - def parentValue = settings.gradle.parent?.rootProject?.findProperty(name) - return (parentValue != null) ? parentValue : settings.ext[name] + def value = settings.gradle.parent?.rootProject?.findProperty(name) + value = (value != null) ? value : settings.ext.find(name) + value = (value != null) ? value : loadProperty(settings, name) + return value +} + +private def loadProperty(settings, name) { + def scriptDir = new File(getClass().protectionDomain.codeSource.location.path).parent + new File(scriptDir, "../gradle.properties").withInputStream { + def properties = new Properties() + properties.load(it) + return properties.get(name) + } } return this diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-test-support/src/main/java/org/springframework/boot/testsupport/gradle/testkit/GradleBuildExtension.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-test-support/src/main/java/org/springframework/boot/testsupport/gradle/testkit/GradleBuildExtension.java index 88c7b8b6d1fe..8f0a6956acb0 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-test-support/src/main/java/org/springframework/boot/testsupport/gradle/testkit/GradleBuildExtension.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-test-support/src/main/java/org/springframework/boot/testsupport/gradle/testkit/GradleBuildExtension.java @@ -16,6 +16,7 @@ package org.springframework.boot.testsupport.gradle.testkit; +import java.io.File; import java.lang.reflect.Field; import java.net.URL; import java.util.regex.Pattern; @@ -25,6 +26,7 @@ import org.junit.jupiter.api.extension.Extension; import org.junit.jupiter.api.extension.ExtensionContext; +import org.springframework.util.Assert; import org.springframework.util.ReflectionUtils; /** @@ -43,6 +45,7 @@ public class GradleBuildExtension implements BeforeEachCallback, AfterEachCallba @Override public void beforeEach(ExtensionContext context) throws Exception { GradleBuild gradleBuild = extractGradleBuild(context); + gradleBuild.scriptProperty("parentRootDir", findParentRootDir().getAbsolutePath()); URL scriptUrl = findDefaultScript(context); if (scriptUrl != null) { gradleBuild.script(scriptUrl.getFile()); @@ -54,6 +57,22 @@ public void beforeEach(ExtensionContext context) throws Exception { gradleBuild.before(); } + private File findParentRootDir() { + File dir = new File("").getAbsoluteFile(); + int depth = 0; + while (dir != null && !hasGradleBuildFiles(dir)) { + Assert.state(depth++ < 5, "Unable to find parent root"); + dir = dir.getParentFile(); + } + Assert.state(dir != null, "Unable to find parent root"); + return dir; + } + + private boolean hasGradleBuildFiles(File dir) { + return new File(dir, "settings.gradle").exists() && new File(dir, "build.gradle").exists() + && new File(dir, "gradle.properties").exists(); + } + private GradleBuild extractGradleBuild(ExtensionContext context) throws Exception { Object testInstance = context.getRequiredTestInstance(); Field gradleBuildField = ReflectionUtils.findField(testInstance.getClass(), "gradleBuild"); diff --git a/spring-boot-system-tests/spring-boot-image-tests/src/systemTest/resources/org/springframework/boot/image/paketo/settings.gradle b/spring-boot-system-tests/spring-boot-image-tests/src/systemTest/resources/org/springframework/boot/image/paketo/settings.gradle index 87dfe2ab9afd..557a0234c4d8 100644 --- a/spring-boot-system-tests/spring-boot-image-tests/src/systemTest/resources/org/springframework/boot/image/paketo/settings.gradle +++ b/spring-boot-system-tests/spring-boot-image-tests/src/systemTest/resources/org/springframework/boot/image/paketo/settings.gradle @@ -1,4 +1,5 @@ pluginManagement { + evaluate(new File("{parentRootDir}/buildSrc/SpringRepositorySupport.groovy")).apply(this) repositories { exclusiveContent { forRepository {