Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Add JUnit Support V2 #183

Merged
merged 12 commits into from
May 26, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,14 @@
import org.gradle.api.attributes.AttributeContainer;
import org.gradle.api.attributes.Category;
import org.gradle.api.component.AdhocComponentWithVariants;
import org.gradle.api.initialization.Settings;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.tasks.Delete;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.internal.DefaultTaskExecutionRequest;
import org.gradle.plugins.ide.eclipse.EclipsePlugin;
import org.gradle.plugins.ide.eclipse.model.EclipseModel;
import org.gradle.plugins.ide.idea.IdeaPlugin;
import org.gradle.plugins.ide.idea.model.IdeaModel;
import org.jetbrains.gradle.ext.IdeaExtPlugin;
import org.jetbrains.gradle.ext.ProjectSettings;

import java.util.*;

Expand Down Expand Up @@ -179,8 +175,10 @@ private void configureSourceSetConventions(Project project, Conventions conventi

ProjectUtils.afterEvaluate(project, () -> {
project.getExtensions().configure(RunsConstants.Extensions.RUNS, (Action<NamedDomainObjectContainer<Run>>) runs -> runs.configureEach(run -> {
if (sourceSets.getShouldMainSourceSetBeAutomaticallyAddedToRuns().get())
if (sourceSets.getShouldMainSourceSetBeAutomaticallyAddedToRuns().get()) {
//We always register main
run.getModSources().add(project.getExtensions().getByType(SourceSetContainer.class).getByName("main"));
}

if (sourceSets.getShouldSourceSetsLocalRunRuntimesBeAutomaticallyAddedToRuns().get() && configurations.getIsEnabled().get())
run.getModSources().get().forEach(sourceSet -> {
Expand Down Expand Up @@ -329,6 +327,14 @@ private void applyAfterEvaluate(final Project project) {
if (run instanceof RunImpl) {
run.configure();

// We add default junit sourcesets here because we need to know the type of the run first
final Conventions conventions = project.getExtensions().getByType(Subsystems.class).getConventions();
if (conventions.getIsEnabled().get() && conventions.getSourceSets().getIsEnabled().get() && conventions.getSourceSets().getShouldMainSourceSetBeAutomaticallyAddedToRuns().get()) {
if (run.getIsJUnit().get()) {
run.getUnitTestSources().add(project.getExtensions().getByType(SourceSetContainer.class).getByName("test"));
}
}

if (run.getConfigureFromDependencies().get()) {
final RunImpl runImpl = (RunImpl) run;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public RunsExtension(WithEnabledProperty parent) {
super(parent, "runs");

getShouldDefaultRunsBeCreated().convention(getBooleanProperty("create-default-run-per-type").orElse(true));
getShouldDefaultTestTaskBeReused().convention(getBooleanProperty("reuse-default-test-task").orElse(true));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -130,6 +129,11 @@ public void idea(Project project, IdeaModel idea, ProjectSettings ideaExtension)
final String runName = StringUtils.capitalize(project.getName() + ": " + StringUtils.capitalize(nameWithoutSpaces));

final RunImpl runImpl = (RunImpl) run;

//Do not generate a run configuration for unit tests
if (runImpl.getIsJUnit().get())
return;

final IdeaRunExtension runIdeaConfig = run.getExtensions().getByType(IdeaRunExtension.class);
final TaskProvider<?> ideBeforeRunTask = createIdeBeforeRunTask(project, nameWithoutSpaces, run, runImpl);

Expand Down Expand Up @@ -164,6 +168,11 @@ public void eclipse(Project project, EclipseModel eclipse) {
final String runName = StringUtils.capitalize(project.getName() + " - " + StringUtils.capitalize(name.replace(" ", "-")));

final RunImpl runImpl = (RunImpl) run;

//Do not generate a run configuration for unit tests
if (runImpl.getIsJUnit().get())
return;

final TaskProvider<?> ideBeforeRunTask = createIdeBeforeRunTask(project, name, run, runImpl);
final List<TaskProvider<?>> copyProcessResourcesTasks = createEclipseCopyResourcesTasks(eclipse, run);
ideBeforeRunTask.configure(task -> copyProcessResourcesTasks.forEach(t -> task.dependsOn(t)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import net.neoforged.gradle.dsl.common.runs.run.Run;
import net.neoforged.gradle.dsl.common.runs.type.RunType;
import net.neoforged.gradle.util.StringCapitalizationUtils;
import org.apache.ivy.util.StringUtils;
import org.codehaus.groovy.util.StringUtil;
import org.gradle.api.GradleException;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.Project;
Expand Down Expand Up @@ -53,6 +51,7 @@ public RunImpl(final Project project, final String name) {
getIsServer().convention(false);
getIsDataGenerator().convention(false);
getIsGameTest().convention(false);
getIsJUnit().convention(false);
getShouldBuildAllProjects().convention(false);
getDependencies().convention(project.getObjects().newInstance(DependencyHandlerImpl.class, project, String.format("RunRuntimeDependencies%s", StringCapitalizationUtils.capitalize(name))));

Expand Down Expand Up @@ -166,6 +165,7 @@ public void configureInternally(final @NotNull RunType spec) {
getIsServer().convention(spec.getIsServer());
getIsDataGenerator().convention(spec.getIsDataGenerator());
getIsGameTest().convention(spec.getIsGameTest());
getIsJUnit().convention(spec.getIsJUnit());
getClasspath().from(spec.getClasspath());

if (spec.getRunAdapter().isPresent()) {
Expand Down
Loading
Loading