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

support add instructions at the end of tasks.test in the build.gradle.kts #9818

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static tech.jhipster.lite.module.domain.JHipsterModule.*;
import static tech.jhipster.lite.module.domain.mavenplugin.MavenBuildPhase.*;

import java.util.regex.Pattern;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.gradleplugin.GradleMainBuildPlugin;
import tech.jhipster.lite.module.domain.javabuild.ArtifactId;
Expand All @@ -12,8 +11,6 @@
import tech.jhipster.lite.module.domain.mavenplugin.MavenPlugin;
import tech.jhipster.lite.module.domain.mavenplugin.MavenPlugin.MavenPluginOptionalBuilder;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
import tech.jhipster.lite.module.domain.replacement.ElementReplacer;
import tech.jhipster.lite.module.domain.replacement.RegexReplacer;
import tech.jhipster.lite.shared.error.domain.Assert;

public class JacocoModuleFactory {
Expand All @@ -22,47 +19,6 @@ public class JacocoModuleFactory {
private static final GroupId JACOCO_GROUP = groupId("org.jacoco");
private static final ArtifactId JACOCO_ARTIFACT_ID = artifactId("jacoco-maven-plugin");
private static final VersionSlug JACOCO_VERSION = versionSlug(JACOCO);
private static final Pattern BUILD_GRADLE_TASK_TEST_NEEDLE = Pattern.compile(
"""
tasks.test {
filter {
includeTestsMatching("**Test*")
excludeTestsMatching("**IT*")
excludeTestsMatching("**CucumberTest*")
}
useJUnitPlatform()
}
""",
Pattern.LITERAL
);
private static final ElementReplacer EXISTING_BUILD_GRADLE_TASK_TEST_NEEDLE = new RegexReplacer(
(contentBeforeReplacement, replacement) -> BUILD_GRADLE_TASK_TEST_NEEDLE.matcher(contentBeforeReplacement).find(),
BUILD_GRADLE_TASK_TEST_NEEDLE
);
private static final String ADD_JACOCO_BUILD_GRADLE_TASK_TEST =
"""
tasks.test {
filter {
includeTestsMatching("**Test*")
excludeTestsMatching("**IT*")
excludeTestsMatching("**CucumberTest*")
}
useJUnitPlatform()
finalizedBy("jacocoTestReport")
}
""";
private static final String ADD_JACOCO_WITH_MIN_COVERAGE_CHECK_BUILD_GRADLE_TASK_TEST =
"""
tasks.test {
filter {
includeTestsMatching("**Test*")
excludeTestsMatching("**IT*")
excludeTestsMatching("**CucumberTest*")
}
useJUnitPlatform()
finalizedBy("jacocoTestCoverageVerification")
}
""";

public JHipsterModule buildJacocoModule(JHipsterModuleProperties properties) {
Assert.notNull("properties", properties);
Expand All @@ -76,11 +32,13 @@ public JHipsterModule buildJacocoModule(JHipsterModuleProperties properties) {
.gradlePlugins()
.plugin(gradleJacocoPlugin())
.and()
.optionalReplacements()
.in(path("build.gradle.kts"))
.add(EXISTING_BUILD_GRADLE_TASK_TEST_NEEDLE, ADD_JACOCO_BUILD_GRADLE_TASK_TEST)
.gradleConfigurations()
.addTasksTestInstruction(
"""
finalizedBy("jacocoTestReport")\
"""
)
.and()
.and()
.build();
//@formatter:on
}
Expand All @@ -97,11 +55,13 @@ public JHipsterModule buildJacocoWithMinCoverageCheckModule(JHipsterModuleProper
.gradlePlugins()
.plugin(gradleJacocoWithMinCoverageCheckPlugin())
.and()
.optionalReplacements()
.in(path("build.gradle.kts"))
.add(EXISTING_BUILD_GRADLE_TASK_TEST_NEEDLE, ADD_JACOCO_WITH_MIN_COVERAGE_CHECK_BUILD_GRADLE_TASK_TEST)
.gradleConfigurations()
.addTasksTestInstruction(
"""
finalizedBy("jacocoTestCoverageVerification")\
"""
)
.and()
.and()
.build();
//@formatter:on
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
.set(new PropertyKey(SPRING_PROFILES_ACTIVE), new PropertyValue(""))
.and()
.gradleConfigurations()
.configuration(
.addConfiguration(
"""
tasks.build {
dependsOn("processResources")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tech.jhipster.lite.module.domain.gradleconfiguration;

import tech.jhipster.lite.shared.error.domain.Assert;

record GradleTasksTestInstruction(String instruction) {
public GradleTasksTestInstruction {
Assert.notBlank("instruction", instruction);
}

public String get() {
return instruction();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,75 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Function;
import java.util.stream.Stream;
import tech.jhipster.lite.module.domain.JHipsterModule.JHipsterModuleBuilder;
import tech.jhipster.lite.module.domain.javabuild.command.AddGradleConfiguration;
import tech.jhipster.lite.module.domain.javabuild.command.AddGradleTasksTestInstruction;
import tech.jhipster.lite.module.domain.javabuild.command.JavaBuildCommands;
import tech.jhipster.lite.shared.error.domain.Assert;

public final class JHipsterModuleGradleConfigurations {

private final Collection<GradleConfiguration> configurations;
private final Collection<GradleTasksTestInstruction> tasksTestInstructions;

public JHipsterModuleGradleConfigurations(JHipsterModuleGradleConfigurationBuilder builder) {
Assert.notNull("configurations", builder.configurations);
Assert.notNull("tasksTestInstructions", builder.tasksTestInstructions);

this.configurations = builder.configurations;
this.tasksTestInstructions = builder.tasksTestInstructions;
}

public static JHipsterModuleGradleConfigurationBuilder builder(JHipsterModuleBuilder module) {
return new JHipsterModuleGradleConfigurationBuilder(module);
}

public JavaBuildCommands buildChanges() {
return new JavaBuildCommands(configurations.stream().map(configuration -> new AddGradleConfiguration(configuration.get())).toList());
return Stream.of(configurationToAddGradleConfiguration(), tasksTestInstructionsToAddTasksTestInstruction())
.flatMap(Function.identity())
.reduce(JavaBuildCommands.EMPTY, JavaBuildCommands::merge);
}

private Stream<JavaBuildCommands> configurationToAddGradleConfiguration() {
return Stream.of(
new JavaBuildCommands(configurations.stream().map(configuration -> new AddGradleConfiguration(configuration.get())).toList())
);
}

private Stream<JavaBuildCommands> tasksTestInstructionsToAddTasksTestInstruction() {
return Stream.of(
new JavaBuildCommands(
tasksTestInstructions.stream().map(tasksTestInstruction -> new AddGradleTasksTestInstruction(tasksTestInstruction.get())).toList()
)
);
}

public static final class JHipsterModuleGradleConfigurationBuilder {

private final JHipsterModuleBuilder module;
private final Collection<GradleConfiguration> configurations = new ArrayList<>();
private final Collection<GradleTasksTestInstruction> tasksTestInstructions = new ArrayList<>();

private JHipsterModuleGradleConfigurationBuilder(JHipsterModuleBuilder module) {
Assert.notNull("module", module);

this.module = module;
}

public JHipsterModuleGradleConfigurationBuilder configuration(String configuration) {
public JHipsterModuleGradleConfigurationBuilder addConfiguration(String configuration) {
configurations.add(new GradleConfiguration(configuration));

return this;
}

public JHipsterModuleGradleConfigurationBuilder addTasksTestInstruction(String instruction) {
tasksTestInstructions.add(new GradleTasksTestInstruction(instruction));

return this;
}

public JHipsterModuleBuilder and() {
return module;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tech.jhipster.lite.module.domain.javabuild.command;

import tech.jhipster.lite.shared.error.domain.Assert;

public record AddGradleTasksTestInstruction(String instruction) implements JavaBuildCommand {
public AddGradleTasksTestInstruction {
Assert.notNull("instruction", instruction);
}

public String get() {
return instruction;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public sealed interface JavaBuildCommand
RemoveJavaDependencyManagement,
SetBuildProperty,
SetVersion,
AddGradleConfiguration {}
AddGradleConfiguration,
AddGradleTasksTestInstruction {}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private void handle(JavaDependenciesCommandHandler handler, JavaBuildCommand com
case AddJavaBuildProfile addJavaBuildProfile -> handler.handle(addJavaBuildProfile);
case AddGradlePlugin addGradlePlugin -> handler.handle(addGradlePlugin);
case AddGradleConfiguration addGradleConfiguration -> handler.handle(addGradleConfiguration);
case AddGradleTasksTestInstruction addGradleTasksTestInstruction -> handler.handle(addGradleTasksTestInstruction);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ public interface JavaDependenciesCommandHandler {
void handle(AddGradlePlugin command);

void handle(AddGradleConfiguration command);

void handle(AddGradleTasksTestInstruction command);
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package tech.jhipster.lite.module.infrastructure.secondary.javadependency.gradle;

import static tech.jhipster.lite.module.domain.JHipsterModule.*;
import static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.always;
import static tech.jhipster.lite.module.infrastructure.secondary.javadependency.gradle.VersionsCatalog.libraryAlias;
import static tech.jhipster.lite.module.infrastructure.secondary.javadependency.gradle.VersionsCatalog.pluginAlias;
import static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.*;
import static tech.jhipster.lite.module.infrastructure.secondary.javadependency.gradle.VersionsCatalog.*;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import tech.jhipster.lite.module.domain.*;
import tech.jhipster.lite.module.domain.Indentation;
import tech.jhipster.lite.module.domain.JHipsterModuleContext;
import tech.jhipster.lite.module.domain.JHipsterProjectFilePath;
import tech.jhipster.lite.module.domain.buildproperties.BuildProperty;
import tech.jhipster.lite.module.domain.buildproperties.PropertyKey;
import tech.jhipster.lite.module.domain.file.*;
Expand Down Expand Up @@ -70,6 +73,7 @@ public class GradleCommandHandler implements JavaDependenciesCommandHandler {
"^// jhipster-needle-gradle-free-configuration-blocks$",
Pattern.MULTILINE
);
private static final Pattern GRADLE_TASKS_TEST_NEEDLE = Pattern.compile("^\\s+// jhipster-needle-gradle-tasks-test$", Pattern.MULTILINE);
private static final String PROFILE_CONDITIONAL_TEMPLATE =
"""
if (profiles.contains("%s")) {
Expand Down Expand Up @@ -527,4 +531,20 @@ public void handle(AddGradleConfiguration command) {
context
);
}

@Override
public void handle(AddGradleTasksTestInstruction command) {
MandatoryReplacer replacer = new MandatoryReplacer(
new RegexNeedleBeforeReplacer(
(contentBeforeReplacement, newText) -> !contentBeforeReplacement.contains(newText),
GRADLE_TASKS_TEST_NEEDLE
),
indentation.times(1) + command.get()
);
fileReplacer.handle(
projectFolder,
ContentReplacers.of(new MandatoryFileReplacer(new JHipsterProjectFilePath(BUILD_GRADLE_FILE), replacer)),
context
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import tech.jhipster.lite.module.domain.Indentation;
import tech.jhipster.lite.module.domain.buildproperties.*;
import tech.jhipster.lite.module.domain.buildproperties.BuildProperty;
import tech.jhipster.lite.module.domain.buildproperties.PropertyKey;
import tech.jhipster.lite.module.domain.buildproperties.PropertyValue;
import tech.jhipster.lite.module.domain.javabuild.MavenBuildExtension;
import tech.jhipster.lite.module.domain.javabuild.VersionSlug;
import tech.jhipster.lite.module.domain.javabuild.command.*;
import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileActivation;
import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileId;
import tech.jhipster.lite.module.domain.javadependency.*;
import tech.jhipster.lite.module.domain.javadependency.DependencyId;
import tech.jhipster.lite.module.domain.javadependency.JavaDependency;
import tech.jhipster.lite.module.domain.javadependency.JavaDependencyClassifier;
import tech.jhipster.lite.module.domain.javadependency.JavaDependencyScope;
import tech.jhipster.lite.module.domain.mavenplugin.*;
import tech.jhipster.lite.module.infrastructure.secondary.javadependency.JavaDependenciesCommandHandler;
import tech.jhipster.lite.shared.enumeration.domain.Enums;
Expand Down Expand Up @@ -340,6 +345,11 @@ public void handle(AddGradleConfiguration command) {
// Gradle commands are ignored
}

@Override
public void handle(AddGradleTasksTestInstruction command) {
// Gradle commands are ignored
}

private Plugin toMavenPlugin(AddMavenPlugin command) {
Plugin mavenPlugin = new Plugin();
mavenPlugin.setArtifactId(command.dependencyId().artifactId().get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ tasks.test {
excludeTestsMatching("**CucumberTest*")
}
useJUnitPlatform()
// jhipster-needle-gradle-tasks-test
}

val integrationTest = task<Test>("integrationTest") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,8 @@ void shouldBuildJacocoModule() {
)
.containing(
"""
tasks.test {
filter {
includeTestsMatching("**Test*")
excludeTestsMatching("**IT*")
excludeTestsMatching("**CucumberTest*")
}
useJUnitPlatform()
finalizedBy("jacocoTestReport")
}
// jhipster-needle-gradle-tasks-test
"""
)
.and()
Expand Down Expand Up @@ -252,15 +245,8 @@ void shouldBuildJacocoWithMinCoverageCheckModule() {
)
.containing(
"""
tasks.test {
filter {
includeTestsMatching("**Test*")
excludeTestsMatching("**IT*")
excludeTestsMatching("**CucumberTest*")
}
useJUnitPlatform()
finalizedBy("jacocoTestCoverageVerification")
}
// jhipster-needle-gradle-tasks-test
"""
)
.and()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static JHipsterModule module() {
.plugin(asciidoctorPlugin())
.and()
.gradleConfigurations()
.configuration(
.addConfiguration(
"""
tasks.build {
dependsOn("processResources")
Expand All @@ -120,6 +120,13 @@ public static JHipsterModule module() {
.plugin(jacocoGradlePlugin())
.plugin(checkstyleGradlePlugin())
.and()
.gradleConfigurations()
.addTasksTestInstruction(
"""
finalizedBy("jacocoTestReport")\
"""
)
.and()
.javaBuildProfiles()
.addProfile(localBuildProfile())
.activation(buildProfileActivation().activeByDefault(false))
Expand Down
Loading