Skip to content

Commit

Permalink
Fix unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
marchermans committed Aug 8, 2024
1 parent 2551855 commit e6195b0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -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<? extends Iterable<Dependency>> 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);
Expand Down Expand Up @@ -150,7 +177,7 @@ public static Provider<File> getArtifactProvider(Project project, String context
}

public static List<Configuration> findReplacementConfigurations(final Project project, final Configuration configuration) {
final Set<Configuration> resultContainer = new HashSet<Configuration>();
final Set<Configuration> resultContainer = new HashSet<>();

resultContainer.addAll(findCompileOnlyConfigurationForSourceSetReplacement(project, configuration));
resultContainer.addAll(findRuntimeOnlyConfigurationFromSourceSetReplacement(project, configuration));
Expand Down Expand Up @@ -195,7 +222,7 @@ public static List<Configuration> findCompileOnlyConfigurationForSourceSetReplac

public static List<Configuration> findRuntimeOnlyConfigurationFromSourceSetReplacement(final Project project, final Configuration configuration) {
final SourceSetContainer sourceSetContainer = project.getExtensions().getByType(SourceSetContainer.class);
final List<Configuration> targets = new ArrayList<Configuration>();
final List<Configuration> targets = new ArrayList<>();

sourceSetContainer.forEach(sourceSet -> {
final Configuration runtimeOnly = project.getConfigurations().findByName(sourceSet.getRuntimeOnlyConfigurationName());
Expand All @@ -210,7 +237,7 @@ public static List<Configuration> findRuntimeOnlyConfigurationFromSourceSetRepla

final Set<Configuration> 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);
}
Expand Down Expand Up @@ -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<Configuration> UNHANDLED_CONFIGURATIONS = new HashSet<Configuration>();

}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -108,20 +109,16 @@ protected void buildRunInterpolationData(RunImpl run, @NotNull MapProperty<Strin

if (userdevConfiguration.getModules() != null && !userdevConfiguration.getModules().get().isEmpty()) {
final String name = String.format("moduleResolverForgeUserDev%s", getSpecification().getVersionedName());
final Configuration modulesCfg;
if (getSpecification().getProject().getConfigurations().getNames().contains(name)) {
modulesCfg = getSpecification().getProject().getConfigurations().getByName(name);
} else {
modulesCfg = getSpecification().getProject().getConfigurations().create(name);
modulesCfg.setCanBeResolved(true);
modulesCfg.getDependencies().addAllLater(
userdevConfiguration.getModules().map(
modules -> 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)
Expand Down

0 comments on commit e6195b0

Please sign in to comment.