Skip to content

Commit

Permalink
Merge branch '3.3.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
philwebb committed Sep 27, 2024
2 parents 8282a90 + e086439 commit 7fe7c61
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
15 changes: 13 additions & 2 deletions buildSrc/SpringRepositorySupport.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand All @@ -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());
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pluginManagement {
evaluate(new File("{parentRootDir}/buildSrc/SpringRepositorySupport.groovy")).apply(this)
repositories {
exclusiveContent {
forRepository {
Expand Down

0 comments on commit 7fe7c61

Please sign in to comment.