Skip to content

Commit

Permalink
Support tag expressions
Browse files Browse the repository at this point in the history
Fixes #33515
  • Loading branch information
stuartwdouglas committed May 22, 2023
1 parent d73267b commit 8d0c8a4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -37,8 +35,6 @@
import org.jboss.logging.Logger;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Tags;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.TestTemplate;
Expand All @@ -57,6 +53,7 @@
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.PostDiscoveryFilter;
import org.junit.platform.launcher.TagFilter;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.TestPlan;
Expand Down Expand Up @@ -163,9 +160,9 @@ public FilterResult apply(TestDescriptor testDescriptor) {
launchBuilder.filters(testClassUsages.getTestsToRun(classScanResult.getChangedClassNames(), testState));
}
if (!includeTags.isEmpty()) {
launchBuilder.filters(new TagFilter(false, includeTags));
launchBuilder.filters(TagFilter.includeTags(new ArrayList<>(includeTags)));
} else if (!excludeTags.isEmpty()) {
launchBuilder.filters(new TagFilter(true, excludeTags));
launchBuilder.filters(TagFilter.excludeTags(new ArrayList<>(excludeTags)));
}
if (include != null) {
launchBuilder.filters(new RegexFilter(false, include));
Expand Down Expand Up @@ -852,71 +849,6 @@ public Builder setFailingTestsOnly(boolean failingTestsOnly) {
}
}

private static class TagFilter implements PostDiscoveryFilter {

final boolean exclude;
final Set<String> tags;

private TagFilter(boolean exclude, Set<String> tags) {
this.exclude = exclude;
this.tags = tags;
}

@Override
public FilterResult apply(TestDescriptor testDescriptor) {
if (testDescriptor.getSource().isPresent()) {
if (testDescriptor.getSource().get() instanceof MethodSource) {
MethodSource methodSource = (MethodSource) testDescriptor.getSource().get();
Method m = methodSource.getJavaMethod();
FilterResult res = filterTags(m);
if (res != null) {
return res;
}
res = filterTags(methodSource.getJavaClass());
if (res != null) {
return res;
}
return FilterResult.includedIf(exclude);
}
}
return FilterResult.included("not a method");
}

public FilterResult filterTags(AnnotatedElement clz) {
List<Tag> all = new ArrayList<>();
gatherTags(clz, all);
if (all.isEmpty())
return null;
for (Tag i : all) {
if (tags.contains(i.value())) {
return FilterResult.includedIf(!exclude);
}
}
return FilterResult.includedIf(exclude);
}

private void gatherTags(AnnotatedElement clz, List<Tag> all) {
Tag tag = clz.getAnnotation(Tag.class);
Tags tagsAnn = clz.getAnnotation(Tags.class);
if (tag != null) {
all.add(tag);
} else if (tagsAnn != null) {
all.addAll(List.of(tagsAnn.value()));
}
if (clz instanceof Class) {
if (((Class<?>) clz).isAnnotation()) {
//only scan meta annotations one level deep
return;
}
}

//meta annotations can also provide tags
for (var a : clz.getAnnotations()) {
gatherTags(a.annotationType(), all);
}
}
}

private static class RegexFilter implements PostDiscoveryFilter {

final boolean exclude;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public class TestConfig {
public boolean displayTestOutput;

/**
* Tags that should be included for continuous testing.
* Tags that should be included for continuous testing. This supports JUnit Tag Expressions.
*
* @see <a href="https://junit.org/junit5/docs/current/user-guide/#running-tests-tag-expressions">JUnit Tag Expressions</a>
*/
@ConfigItem
public Optional<List<String>> includeTags;
Expand All @@ -51,7 +53,11 @@ public class TestConfig {
*
* This is ignored if include-tags has been set.
*
* Defaults to 'slow'
* Defaults to 'slow'.
*
* This supports JUnit Tag Expressions.
*
* @see <a href="https://junit.org/junit5/docs/current/user-guide/#running-tests-tag-expressions">JUnit Tag Expressions</a>
*/
@ConfigItem(defaultValue = "slow")
public Optional<List<String>> excludeTags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ ConsoleCommandBuildItem testConsoleCommand(CombinedIndexBuildItem indexBuildItem
}

@GroupCommandDefinition(name = "test", description = "Test Commands", groupCommands = { TagsCommand.class,
PatternCommand.class })
PatternCommand.class }, generateHelp = true)
public static class TestCommand implements Command {

@Override
Expand All @@ -208,7 +208,7 @@ public CommandResult execute(CommandInvocation commandInvocation) throws Command
}

@GroupCommandDefinition(name = "tags", description = "Tag Commands", groupCommands = { IncludeTagsCommand.class,
ExcludeTagsCommand.class })
ExcludeTagsCommand.class }, generateHelp = true)
public static class TagsCommand implements Command {

@Override
Expand All @@ -217,7 +217,7 @@ public CommandResult execute(CommandInvocation commandInvocation) throws Command
}
}

@CommandDefinition(name = "include", description = "Sets the current included tags")
@CommandDefinition(name = "include", description = "Sets the current included tags, this supports JUnit tag expressions.")
public static class IncludeTagsCommand extends TestSelectionCommand {

@Arguments(completer = TagCompleter.class)
Expand All @@ -243,7 +243,7 @@ protected void configure(TestSupport testSupport) {
}
}

@CommandDefinition(name = "exclude", description = "Sets the current excluded tags")
@CommandDefinition(name = "exclude", description = "Sets the current excluded tags, this supports JUnit tag expressions.")
public static class ExcludeTagsCommand extends TestSelectionCommand {

@Arguments(completer = TagCompleter.class)
Expand Down Expand Up @@ -271,7 +271,7 @@ protected void configure(TestSupport testSupport) {

@GroupCommandDefinition(name = "pattern", description = "Include/Exclude pattern Commands", groupCommands = {
IncludePatternCommand.class,
ExcludePatternCommand.class })
ExcludePatternCommand.class }, generateHelp = true)
public static class PatternCommand implements Command {

@Override
Expand Down Expand Up @@ -302,7 +302,7 @@ protected void configure(TestSupport testSupport) {
}
}

@CommandDefinition(name = "exclude", description = "Sets the current excluded tags")
@CommandDefinition(name = "exclude", description = "Sets the current exclude pattern")
public static class ExcludePatternCommand extends TestSelectionCommand {

@Argument(completer = TagCompleter.class)
Expand Down

0 comments on commit 8d0c8a4

Please sign in to comment.