Skip to content

Commit

Permalink
Fix a case were lazy dependencies prevented the IDEA unit test global…
Browse files Browse the repository at this point in the history
… run from being registered.

Fix all tests.
  • Loading branch information
marchermans committed Aug 10, 2024
1 parent e6195b0 commit 92403a3
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ public abstract class ReplacementLogic implements ConfigurableDSLElement<Depende
public ReplacementLogic(Project project) {
this.project = project;

//Wire up a replacement handler to each configuration for when a dependency is added.
this.project.getConfigurations().configureEach(this::handleConfiguration);
//Collection holder of all custom dependency replacement handlers.
this.dependencyReplacementHandlers = this.project.getObjects().domainObjectContainer(DependencyReplacementHandler.class, name -> this.project.getObjects().newInstance(Handler.class, this.project, name));

//Wire up a replacement handler to each configuration for when a dependency is added.
this.project.getConfigurations().configureEach(this::handleConfiguration);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ public void idea(Project project, Project rootProject, IdeaModel idea, ProjectSe
final ExtensionAware ideaModelExtensions = (ExtensionAware) idea;
final Run ideaDefaultUnitTestRun = ideaModelExtensions.getExtensions().getByType(Run.class);

//We finally know that the user wants this to be registered, additionally the IDE integration resolved the lazy dependencies
//here, so we can now with a gentle heart register the run type, and the run configuration conversion.
RunManager runManager = project.getExtensions().getByType(RunManager.class);
runManager.addInternal(ideaDefaultUnitTestRun);

createIdeaRun(project, ideaDefaultUnitTestRun, ideaRuns, true);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public Provider<Set<FileSystemLocation>> getSdkClasspathElements() {
}

@Override
public void runType(@NotNull String string) {
public void runType(@NotNull String name) {
getConfigureFromTypeWithName().set(false); // Don't re-configure
specifications.addAll(getRunTypesByName(name));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ public static void configureIdeUnitTests(final Project project) {

ideaDefaultTestRun.getIsJUnit().set(true);
ideaDefaultTestRun.runType("junit");

RunManager runManager = project.getExtensions().getByType(RunManager.class);
runManager.addInternal(ideaDefaultTestRun);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static String createTaskName(final String prefix, final Run run) {
}

public static void configure(Project project, Run run, boolean isInternal) {
RunsUtil.configureModSourceDefaults(project, run, isInternal);
RunsUtil.configureModSourceDefaults(project, run);

run.configure();

Expand Down Expand Up @@ -131,12 +131,7 @@ public static void ensureMacOsSupport(Run run) {
}
}

public static void configureModSourceDefaults(Project project, Run run, boolean isInternal) {
if (isInternal) {
return;
}

// We add default junit sourcesets here because we need to know the type of the run first
public static void configureModSourceDefaults(Project project, Run run) {
final Conventions conventions = project.getExtensions().getByType(Subsystems.class).getConventions();
if (conventions.getSourceSets().getShouldMainSourceSetBeAutomaticallyAddedToRuns().get()) {
//We always register main
Expand Down
4 changes: 1 addition & 3 deletions userdev/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@ dependencies {
api project(':common')
api project(':neoform')
api project(':dsl-userdev')
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,11 @@ class RunTests extends BuilderBasedTestSpecification {
classpathFile.text.contains("org.jgrapht${File.separator}jgrapht-core")
}

@Override
protected File getTestTempDirectory() {
return new File("build", "unit-testing-2")
}

def "userdev supports unit testing"() {
given:
def project = create("userdev_supports_unit_tests", {
Expand All @@ -394,10 +399,22 @@ class RunTests extends BuilderBasedTestSpecification {
}
}
test {
useJUnitPlatform()
}
dependencies {
implementation 'net.neoforged:neoforge:+'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
""")
//We need to add a manifest.mf file to the test source set
it.file("src/test/resources/META-INF/MANIFEST.MF", """Manifest-Version: 1.0
|FMLModType: GAMELIBRARY
|""".stripMargin())

it.file("src/test/java/net/test/TestTest.java",
"""
package net.test;
Expand Down Expand Up @@ -427,11 +444,11 @@ class RunTests extends BuilderBasedTestSpecification {

when:
def run = project.run {
it.tasks(':test')
it.tasks(":testJunit")
}

then:
run.task(':test').outcome == TaskOutcome.SUCCESS
run.task(':testJunit').outcome == TaskOutcome.SUCCESS
}

def "runs with no modsource create problem"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ class SourceSetConventionTests 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 @@ -530,7 +530,7 @@ class SourceSetConventionTests 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

0 comments on commit 92403a3

Please sign in to comment.