Skip to content

Commit 5b0d042

Browse files
Add --select-unique-id support to ConsoleLauncher (#4093)
Resolves #3484
1 parent f9461c8 commit 5b0d042

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-5.12.0-M1.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ JUnit repository on GitHub.
3434
* Add support for passing line and column number to `ConsoleLauncher` via
3535
`--select-file` and `--select-resource`.
3636
* `ConsoleLauncher` now accepts multiple values for all `--select` options.
37+
* Add `--select-unique-id` support to ConsoleLauncher.
3738

3839

3940
[[release-notes-5.12.0-M1-junit-jupiter]]

junit-platform-console/src/main/java/org/junit/platform/console/options/SelectorConverter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod;
1818
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectModule;
1919
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectPackage;
20+
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId;
2021
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectUri;
2122

2223
import java.net.URI;
@@ -34,6 +35,7 @@
3435
import org.junit.platform.engine.discovery.MethodSelector;
3536
import org.junit.platform.engine.discovery.ModuleSelector;
3637
import org.junit.platform.engine.discovery.PackageSelector;
38+
import org.junit.platform.engine.discovery.UniqueIdSelector;
3739
import org.junit.platform.engine.discovery.UriSelector;
3840

3941
import picocli.CommandLine.ITypeConverter;
@@ -113,6 +115,13 @@ public IterationSelector convert(String value) {
113115
}
114116
}
115117

118+
static class UniqueId implements ITypeConverter<UniqueIdSelector> {
119+
@Override
120+
public UniqueIdSelector convert(String value) {
121+
return selectUniqueId(value);
122+
}
123+
}
124+
116125
static class Identifier implements ITypeConverter<DiscoverySelectorIdentifier> {
117126
@Override
118127
public DiscoverySelectorIdentifier convert(String value) {

junit-platform-console/src/main/java/org/junit/platform/console/options/TestDiscoveryOptions.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.junit.platform.engine.discovery.MethodSelector;
3636
import org.junit.platform.engine.discovery.ModuleSelector;
3737
import org.junit.platform.engine.discovery.PackageSelector;
38+
import org.junit.platform.engine.discovery.UniqueIdSelector;
3839
import org.junit.platform.engine.discovery.UriSelector;
3940

4041
/**
@@ -58,6 +59,7 @@ public class TestDiscoveryOptions {
5859
private List<MethodSelector> selectedMethods = emptyList();
5960
private List<ClasspathResourceSelector> selectedClasspathResources = emptyList();
6061
private List<IterationSelector> selectedIterations = emptyList();
62+
private List<UniqueIdSelector> selectedUniqueIds = emptyList();
6163
private List<DiscoverySelectorIdentifier> selectorIdentifiers = emptyList();
6264

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

187+
public List<UniqueIdSelector> getSelectedUniqueIds() {
188+
return selectedUniqueIds;
189+
}
190+
191+
public void setSelectedUniqueId(List<UniqueIdSelector> selectedUniqueIds) {
192+
this.selectedUniqueIds = selectedUniqueIds;
193+
}
194+
185195
public List<DiscoverySelectorIdentifier> getSelectorIdentifiers() {
186196
return selectorIdentifiers;
187197
}

junit-platform-console/src/main/java/org/junit/platform/console/options/TestDiscoveryOptionsMixin.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.junit.platform.engine.discovery.MethodSelector;
2828
import org.junit.platform.engine.discovery.ModuleSelector;
2929
import org.junit.platform.engine.discovery.PackageSelector;
30+
import org.junit.platform.engine.discovery.UniqueIdSelector;
3031
import org.junit.platform.engine.discovery.UriSelector;
3132

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

150+
@Option(names = { "--select-unique-id",
151+
"--uid" }, paramLabel = "UNIQUE-ID", arity = "1..*", converter = SelectorConverter.UniqueId.class, //
152+
description = "Select a unique id for test discovery. This option can be repeated.")
153+
private final List<UniqueIdSelector> selectedUniqueIds = new ArrayList<>();
154+
149155
@Option(names = "--select", paramLabel = "PREFIX:VALUE", arity = "1..*", converter = SelectorConverter.Identifier.class, //
150156
description = "Select via a prefixed identifier (e.g. method:com.acme.Foo#m selects the m() method in the com.acme.Foo class). "
151157
+ "This option can be repeated.")
@@ -168,6 +174,7 @@ private void applyTo(TestDiscoveryOptions result) {
168174
result.setSelectedClasspathResources(
169175
merge(this.selectedClasspathResources, this.selectedClasspathResources2));
170176
result.setSelectedIterations(merge(this.selectedIterations, this.selectedIterations2));
177+
result.setSelectedUniqueId(this.selectedUniqueIds);
171178
result.setSelectorIdentifiers(this.selectorIdentifiers);
172179
}
173180
}
@@ -210,12 +217,12 @@ static class FilterOptions {
210217
@Option(names = {
211218
"--include-methodname" }, paramLabel = "PATTERN", arity = "1", description = "Provide a regular expression to include only methods whose fully qualified names without parameters match. " //
212219
+ "When this option is repeated, all patterns will be combined using OR semantics.")
213-
private List<String> includeMethodNamePatterns = new ArrayList<>();
220+
private final List<String> includeMethodNamePatterns = new ArrayList<>();
214221

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

220227
@Option(names = { "-t",
221228
"--include-tag" }, paramLabel = "TAG", arity = "1", description = "Provide a tag or tag expression to include only tests whose tags match. "

platform-tests/src/test/java/org/junit/platform/console/options/CommandLineOptionsParsingTests.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod;
2727
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectModule;
2828
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectPackage;
29+
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId;
2930
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectUri;
3031

3132
import java.io.File;
@@ -555,6 +556,24 @@ void parseInvalidIterationSelectors() {
555556
assertOptionWithMissingRequiredArgumentThrowsException("-i", "--select-iteration");
556557
}
557558

559+
@ParameterizedTest
560+
@EnumSource
561+
void parseValidUniqueIdSelectors(ArgsType type) {
562+
// @formatter:off
563+
assertAll(
564+
() -> assertEquals(List.of(selectUniqueId("[engine:junit-jupiter]/[class:MyClass]/[method:myMethod]")), type.parseArgLine("--uid [engine:junit-jupiter]/[class:MyClass]/[method:myMethod]").discovery.getSelectedUniqueIds()),
565+
() -> 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()),
566+
() -> 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()),
567+
() -> 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())
568+
);
569+
// @formatter:on
570+
}
571+
572+
@Test
573+
void parseInvalidUniqueIdSelectors() {
574+
assertOptionWithMissingRequiredArgumentThrowsException("--uid", "--select-unique-id");
575+
}
576+
558577
@ParameterizedTest
559578
@EnumSource
560579
void parseClasspathScanningEntries(ArgsType type) {
@@ -637,7 +656,8 @@ void parseValidSelectorIdentifier(ArgsType type) {
637656
() -> assertEquals(List.of(selectPackage("com.acme.foo")), parseIdentifiers(type,"--select package:com.acme.foo")),
638657
() -> assertEquals(List.of(selectModule("com.acme.foo")), parseIdentifiers(type,"--select module:com.acme.foo")),
639658
() -> assertEquals(List.of(selectDirectory("foo/bar")), parseIdentifiers(type,"--select directory:foo/bar")),
640-
() -> assertEquals(List.of(selectFile("foo.txt"), selectUri("file:///foo.txt")), parseIdentifiers(type,"--select file:foo.txt --select uri:file:///foo.txt"))
659+
() -> assertEquals(List.of(selectFile("foo.txt"), selectUri("file:///foo.txt")), parseIdentifiers(type,"--select file:foo.txt --select uri:file:///foo.txt")),
660+
() -> assertEquals(List.of(selectUniqueId("[engine:junit-jupiter]/[class:MyClass]/[method:myMethod]")), parseIdentifiers(type,"--select uid:[engine:junit-jupiter]/[class:MyClass]/[method:myMethod]"))
641661
);
642662
// @formatter:on
643663
}

0 commit comments

Comments
 (0)