From e6195b0c744ae2c7806a50684d90aafeeb44c341 Mon Sep 17 00:00:00 2001 From: Marc Hermans Date: Thu, 8 Aug 2024 20:42:16 +0200 Subject: [PATCH] Fix unit test. --- .../gradle/common/CommonProjectPlugin.java | 3 ++ .../common/util/ConfigurationUtils.java | 49 ++++++++++++++++--- .../neoforged/gradle/userdev/RunTests.groovy | 2 + .../convention/RunConventionTests.groovy | 4 +- .../definition/UserDevRuntimeDefinition.java | 25 +++++----- 5 files changed, 59 insertions(+), 24 deletions(-) diff --git a/common/src/main/java/net/neoforged/gradle/common/CommonProjectPlugin.java b/common/src/main/java/net/neoforged/gradle/common/CommonProjectPlugin.java index d20e7f17..6fab4c9f 100644 --- a/common/src/main/java/net/neoforged/gradle/common/CommonProjectPlugin.java +++ b/common/src/main/java/net/neoforged/gradle/common/CommonProjectPlugin.java @@ -21,6 +21,7 @@ import net.neoforged.gradle.common.services.caching.CachedExecutionService; import net.neoforged.gradle.common.tasks.CleanCache; import net.neoforged.gradle.common.tasks.DisplayMappingsLicenseTask; +import net.neoforged.gradle.common.util.ConfigurationUtils; import net.neoforged.gradle.common.util.run.RunsUtil; import net.neoforged.gradle.dsl.common.extensions.*; import net.neoforged.gradle.dsl.common.extensions.dependency.replacement.DependencyReplacement; @@ -119,6 +120,8 @@ public void apply(Project project) { sourceSet.getExtensions().add("runtimeDefinition", project.getObjects().property(CommonRuntimeDefinition.class)); }); + ConfigurationUtils.ensureReplacementConfigurationExists(project); + //Setup IDE specific unit test handling. UnitTestConfigurator.configureIdeUnitTests(project); diff --git a/common/src/main/java/net/neoforged/gradle/common/util/ConfigurationUtils.java b/common/src/main/java/net/neoforged/gradle/common/util/ConfigurationUtils.java index 6d033615..1bfd2592 100644 --- a/common/src/main/java/net/neoforged/gradle/common/util/ConfigurationUtils.java +++ b/common/src/main/java/net/neoforged/gradle/common/util/ConfigurationUtils.java @@ -17,13 +17,13 @@ import org.gradle.util.internal.GUtil; import java.io.File; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; import java.util.function.Function; public class ConfigurationUtils { + + private static final String NEOGRADLE_RUNTIME_REPLACEMENT = "neoGradleDependencyReplacementTarget"; + private ConfigurationUtils() { throw new IllegalStateException("Can not instantiate an instance of: ConfigurationUtils. This is a utility class"); } @@ -104,7 +104,34 @@ public static Configuration temporaryUnhandledConfiguration(final ConfigurationC UNHANDLED_CONFIGURATIONS.add(configuration); if (configuration.getDependencies().isEmpty()) { - DefaultGroovyMethods.addAll(configuration.getDependencies(), dependencies); + configuration.getDependencies().addAll(Arrays.asList(dependencies)); + configuration.setCanBeConsumed(false); + configuration.setCanBeResolved(true); + } + + + return configuration; + } + + /** + * Creates a configuration that can be resolved, but not consumed, but on which no dependency replacement is applied. + * + * @param configurations The configuration handler. + * @param context The context of the configuration + * @param dependencies The dependencies to add to the configuration + * @return The detached configuration + */ + public static Configuration temporaryUnhandledConfiguration(final ConfigurationContainer configurations, final String context, final Provider> dependencies) { + final String name = "neoGradleInternalUnhandled" + StringGroovyMethods.capitalize(context); + if (configurations.findByName(name) != null) { + return configurations.getByName(name); + } + + final Configuration configuration = configurations.create(name); + UNHANDLED_CONFIGURATIONS.add(configuration); + + if (configuration.getDependencies().isEmpty()) { + configuration.getDependencies().addAllLater(dependencies); configuration.setCanBeConsumed(false); configuration.setCanBeResolved(true); @@ -150,7 +177,7 @@ public static Provider getArtifactProvider(Project project, String context } public static List findReplacementConfigurations(final Project project, final Configuration configuration) { - final Set resultContainer = new HashSet(); + final Set resultContainer = new HashSet<>(); resultContainer.addAll(findCompileOnlyConfigurationForSourceSetReplacement(project, configuration)); resultContainer.addAll(findRuntimeOnlyConfigurationFromSourceSetReplacement(project, configuration)); @@ -195,7 +222,7 @@ public static List findCompileOnlyConfigurationForSourceSetReplac public static List findRuntimeOnlyConfigurationFromSourceSetReplacement(final Project project, final Configuration configuration) { final SourceSetContainer sourceSetContainer = project.getExtensions().getByType(SourceSetContainer.class); - final List targets = new ArrayList(); + final List targets = new ArrayList<>(); sourceSetContainer.forEach(sourceSet -> { final Configuration runtimeOnly = project.getConfigurations().findByName(sourceSet.getRuntimeOnlyConfigurationName()); @@ -210,7 +237,7 @@ public static List findRuntimeOnlyConfigurationFromSourceSetRepla final Set supers = getAllSuperConfigurations(runtimeClasspath); if (supers.contains(runtimeOnly) && supers.contains(configuration)) { - final Configuration reallyRuntimeOnly = project.getConfigurations().maybeCreate(getSourceSetName(sourceSet, "neoGradleDependencyReplacementTarget%s".formatted(StringUtils.capitalize(configuration.getName())))); + final Configuration reallyRuntimeOnly = project.getConfigurations().maybeCreate(getSourceSetName(sourceSet, "%s%s".formatted(NEOGRADLE_RUNTIME_REPLACEMENT, StringUtils.capitalize(sourceSet.getName())))); runtimeClasspath.extendsFrom(reallyRuntimeOnly); targets.add(reallyRuntimeOnly); } @@ -278,9 +305,15 @@ public static Configuration getSdkConfiguration(final SourceSet sourceSet) { return configuration; } + public static void ensureReplacementConfigurationExists(Project project) { + project.getExtensions().getByType(SourceSetContainer.class) + .all(sourceSet -> project.getConfigurations().maybeCreate(getSourceSetName(sourceSet, "%s%s".formatted(NEOGRADLE_RUNTIME_REPLACEMENT, StringUtils.capitalize(sourceSet.getName()))))); + } + public static String getTaskBaseName(final SourceSet sourceSet) { return sourceSet.getName().equals(SourceSet.MAIN_SOURCE_SET_NAME) ? "" : GUtil.toCamelCase(sourceSet.getName()); } private static Set UNHANDLED_CONFIGURATIONS = new HashSet(); + } diff --git a/userdev/src/functionalTest/groovy/net/neoforged/gradle/userdev/RunTests.groovy b/userdev/src/functionalTest/groovy/net/neoforged/gradle/userdev/RunTests.groovy index 6d6e7178..94cbdb4e 100644 --- a/userdev/src/functionalTest/groovy/net/neoforged/gradle/userdev/RunTests.groovy +++ b/userdev/src/functionalTest/groovy/net/neoforged/gradle/userdev/RunTests.groovy @@ -103,6 +103,8 @@ class RunTests extends BuilderBasedTestSpecification { it.tasks(':runData') //We are expecting this test to fail, since there is a mod without any files included so it is fine. it.shouldFail() + it.stacktrace() + it.debug() } then: diff --git a/userdev/src/functionalTest/groovy/net/neoforged/gradle/userdev/convention/RunConventionTests.groovy b/userdev/src/functionalTest/groovy/net/neoforged/gradle/userdev/convention/RunConventionTests.groovy index d34c0a57..14d0ce36 100644 --- a/userdev/src/functionalTest/groovy/net/neoforged/gradle/userdev/convention/RunConventionTests.groovy +++ b/userdev/src/functionalTest/groovy/net/neoforged/gradle/userdev/convention/RunConventionTests.groovy @@ -259,7 +259,7 @@ class RunConventionTests extends BuilderBasedTestSpecification { } afterEvaluate { - logger.lifecycle("Run contains cp entry: \${project.runs.client.dependencies.get().runtimeConfiguration.files.any { it.name.contains 'jgrapht' }}") + logger.lifecycle("Run contains cp entry: \${project.runs.client.dependencies.runtimeConfiguration.files.any { it.name.contains 'jgrapht' }}") } """) it.withToolchains() @@ -301,7 +301,7 @@ class RunConventionTests extends BuilderBasedTestSpecification { } afterEvaluate { - logger.lifecycle("Run contains cp entry: \${project.runs.client.dependencies.get().runtimeConfiguration.files.any { it.name.contains 'jgrapht' }}") + logger.lifecycle("Run contains cp entry: \${project.runs.client.dependencies.runtimeConfiguration.files.any { it.name.contains 'jgrapht' }}") } """) it.withToolchains() diff --git a/userdev/src/main/java/net/neoforged/gradle/userdev/runtime/definition/UserDevRuntimeDefinition.java b/userdev/src/main/java/net/neoforged/gradle/userdev/runtime/definition/UserDevRuntimeDefinition.java index abe7518b..ff1de7e2 100644 --- a/userdev/src/main/java/net/neoforged/gradle/userdev/runtime/definition/UserDevRuntimeDefinition.java +++ b/userdev/src/main/java/net/neoforged/gradle/userdev/runtime/definition/UserDevRuntimeDefinition.java @@ -6,6 +6,7 @@ import net.neoforged.gradle.common.runtime.definition.IDelegatingRuntimeDefinition; import net.neoforged.gradle.common.runtime.tasks.DownloadAssets; import net.neoforged.gradle.common.runtime.tasks.ExtractNatives; +import net.neoforged.gradle.common.util.ConfigurationUtils; import net.neoforged.gradle.common.util.run.RunsUtil; import net.neoforged.gradle.dsl.common.runs.run.DependencyHandler; import net.neoforged.gradle.dsl.common.runtime.definition.Definition; @@ -108,20 +109,16 @@ protected void buildRunInterpolationData(RunImpl run, @NotNull MapProperty modules.stream().map( - m -> getSpecification().getProject().getDependencies().create(m) - ).collect(Collectors.toList()) - ) - ); - } + final Configuration modulesCfg = ConfigurationUtils + .temporaryUnhandledConfiguration( + getSpecification().getProject().getConfigurations(), + String.format("moduleResolverForgeUserDev%s", getSpecification().getVersionedName()), + userdevConfiguration.getModules().map( + modules -> modules.stream().map( + m -> getSpecification().getProject().getDependencies().create(m) + ).collect(Collectors.toList()) + ) + ); interpolationData.put("modules", modulesCfg.getIncoming().getArtifacts().getResolvedArtifacts().map(artifacts -> artifacts.stream() .map(ResolvedArtifactResult::getFile)