Skip to content

Commit

Permalink
Add --select-unique-id support to ConsoleLauncher (#4093)
Browse files Browse the repository at this point in the history
Resolves #3484
  • Loading branch information
juliette-derancourt authored Oct 24, 2024
1 parent f9461c8 commit 5b0d042
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ JUnit repository on GitHub.
* Add support for passing line and column number to `ConsoleLauncher` via
`--select-file` and `--select-resource`.
* `ConsoleLauncher` now accepts multiple values for all `--select` options.
* Add `--select-unique-id` support to ConsoleLauncher.


[[release-notes-5.12.0-M1-junit-jupiter]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectModule;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectPackage;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectUri;

import java.net.URI;
Expand All @@ -34,6 +35,7 @@
import org.junit.platform.engine.discovery.MethodSelector;
import org.junit.platform.engine.discovery.ModuleSelector;
import org.junit.platform.engine.discovery.PackageSelector;
import org.junit.platform.engine.discovery.UniqueIdSelector;
import org.junit.platform.engine.discovery.UriSelector;

import picocli.CommandLine.ITypeConverter;
Expand Down Expand Up @@ -113,6 +115,13 @@ public IterationSelector convert(String value) {
}
}

static class UniqueId implements ITypeConverter<UniqueIdSelector> {
@Override
public UniqueIdSelector convert(String value) {
return selectUniqueId(value);
}
}

static class Identifier implements ITypeConverter<DiscoverySelectorIdentifier> {
@Override
public DiscoverySelectorIdentifier convert(String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.junit.platform.engine.discovery.MethodSelector;
import org.junit.platform.engine.discovery.ModuleSelector;
import org.junit.platform.engine.discovery.PackageSelector;
import org.junit.platform.engine.discovery.UniqueIdSelector;
import org.junit.platform.engine.discovery.UriSelector;

/**
Expand All @@ -58,6 +59,7 @@ public class TestDiscoveryOptions {
private List<MethodSelector> selectedMethods = emptyList();
private List<ClasspathResourceSelector> selectedClasspathResources = emptyList();
private List<IterationSelector> selectedIterations = emptyList();
private List<UniqueIdSelector> selectedUniqueIds = emptyList();
private List<DiscoverySelectorIdentifier> selectorIdentifiers = emptyList();

private List<String> includedClassNamePatterns = singletonList(STANDARD_INCLUDE_PATTERN);
Expand Down Expand Up @@ -182,6 +184,14 @@ public void setSelectedIterations(List<IterationSelector> selectedIterations) {
this.selectedIterations = selectedIterations;
}

public List<UniqueIdSelector> getSelectedUniqueIds() {
return selectedUniqueIds;
}

public void setSelectedUniqueId(List<UniqueIdSelector> selectedUniqueIds) {
this.selectedUniqueIds = selectedUniqueIds;
}

public List<DiscoverySelectorIdentifier> getSelectorIdentifiers() {
return selectorIdentifiers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.junit.platform.engine.discovery.MethodSelector;
import org.junit.platform.engine.discovery.ModuleSelector;
import org.junit.platform.engine.discovery.PackageSelector;
import org.junit.platform.engine.discovery.UniqueIdSelector;
import org.junit.platform.engine.discovery.UriSelector;

import picocli.CommandLine;
Expand Down Expand Up @@ -146,6 +147,11 @@ static class SelectorOptions {
"-select-iteration" }, arity = "1..*", hidden = true, converter = SelectorConverter.Iteration.class)
private final List<IterationSelector> selectedIterations2 = new ArrayList<>();

@Option(names = { "--select-unique-id",
"--uid" }, paramLabel = "UNIQUE-ID", arity = "1..*", converter = SelectorConverter.UniqueId.class, //
description = "Select a unique id for test discovery. This option can be repeated.")
private final List<UniqueIdSelector> selectedUniqueIds = new ArrayList<>();

@Option(names = "--select", paramLabel = "PREFIX:VALUE", arity = "1..*", converter = SelectorConverter.Identifier.class, //
description = "Select via a prefixed identifier (e.g. method:com.acme.Foo#m selects the m() method in the com.acme.Foo class). "
+ "This option can be repeated.")
Expand All @@ -168,6 +174,7 @@ private void applyTo(TestDiscoveryOptions result) {
result.setSelectedClasspathResources(
merge(this.selectedClasspathResources, this.selectedClasspathResources2));
result.setSelectedIterations(merge(this.selectedIterations, this.selectedIterations2));
result.setSelectedUniqueId(this.selectedUniqueIds);
result.setSelectorIdentifiers(this.selectorIdentifiers);
}
}
Expand Down Expand Up @@ -210,12 +217,12 @@ static class FilterOptions {
@Option(names = {
"--include-methodname" }, paramLabel = "PATTERN", arity = "1", description = "Provide a regular expression to include only methods whose fully qualified names without parameters match. " //
+ "When this option is repeated, all patterns will be combined using OR semantics.")
private List<String> includeMethodNamePatterns = new ArrayList<>();
private final List<String> includeMethodNamePatterns = new ArrayList<>();

@Option(names = {
"--exclude-methodname" }, paramLabel = "PATTERN", arity = "1", description = "Provide a regular expression to exclude those methods whose fully qualified names without parameters match. " //
+ "When this option is repeated, all patterns will be combined using OR semantics.")
private List<String> excludeMethodNamePatterns = new ArrayList<>();
private final List<String> excludeMethodNamePatterns = new ArrayList<>();

@Option(names = { "-t",
"--include-tag" }, paramLabel = "TAG", arity = "1", description = "Provide a tag or tag expression to include only tests whose tags match. "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectModule;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectPackage;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectUri;

import java.io.File;
Expand Down Expand Up @@ -555,6 +556,24 @@ void parseInvalidIterationSelectors() {
assertOptionWithMissingRequiredArgumentThrowsException("-i", "--select-iteration");
}

@ParameterizedTest
@EnumSource
void parseValidUniqueIdSelectors(ArgsType type) {
// @formatter:off
assertAll(
() -> assertEquals(List.of(selectUniqueId("[engine:junit-jupiter]/[class:MyClass]/[method:myMethod]")), type.parseArgLine("--uid [engine:junit-jupiter]/[class:MyClass]/[method:myMethod]").discovery.getSelectedUniqueIds()),
() -> assertEquals(List.of(selectUniqueId("[engine:junit-jupiter]/[class:MyClass]/[method:myMethod]")), type.parseArgLine("--select-unique-id [engine:junit-jupiter]/[class:MyClass]/[method:myMethod]").discovery.getSelectedUniqueIds()),
() -> assertEquals(List.of(selectUniqueId("[engine:junit-jupiter]/[class:MyClass1]"), selectUniqueId("[engine:junit-jupiter]/[class:MyClass2]")), type.parseArgLine("--uid [engine:junit-jupiter]/[class:MyClass1] --uid [engine:junit-jupiter]/[class:MyClass2]").discovery.getSelectedUniqueIds()),
() -> assertEquals(List.of(selectUniqueId("[engine:junit-jupiter]/[class:MyClass1]"), selectUniqueId("[engine:junit-jupiter]/[class:MyClass2]")), type.parseArgLine("--uid [engine:junit-jupiter]/[class:MyClass1] [engine:junit-jupiter]/[class:MyClass2]").discovery.getSelectedUniqueIds())
);
// @formatter:on
}

@Test
void parseInvalidUniqueIdSelectors() {
assertOptionWithMissingRequiredArgumentThrowsException("--uid", "--select-unique-id");
}

@ParameterizedTest
@EnumSource
void parseClasspathScanningEntries(ArgsType type) {
Expand Down Expand Up @@ -637,7 +656,8 @@ void parseValidSelectorIdentifier(ArgsType type) {
() -> assertEquals(List.of(selectPackage("com.acme.foo")), parseIdentifiers(type,"--select package:com.acme.foo")),
() -> assertEquals(List.of(selectModule("com.acme.foo")), parseIdentifiers(type,"--select module:com.acme.foo")),
() -> assertEquals(List.of(selectDirectory("foo/bar")), parseIdentifiers(type,"--select directory:foo/bar")),
() -> assertEquals(List.of(selectFile("foo.txt"), selectUri("file:///foo.txt")), parseIdentifiers(type,"--select file:foo.txt --select uri:file:///foo.txt"))
() -> assertEquals(List.of(selectFile("foo.txt"), selectUri("file:///foo.txt")), parseIdentifiers(type,"--select file:foo.txt --select uri:file:///foo.txt")),
() -> assertEquals(List.of(selectUniqueId("[engine:junit-jupiter]/[class:MyClass]/[method:myMethod]")), parseIdentifiers(type,"--select uid:[engine:junit-jupiter]/[class:MyClass]/[method:myMethod]"))
);
// @formatter:on
}
Expand Down

0 comments on commit 5b0d042

Please sign in to comment.