Skip to content

Commit f37db92

Browse files
Add support for passing line and column number to ConsoleLauncher via --select-file and --select-resource (#4044)
Resolves #3483
1 parent 750243a commit f37db92

File tree

7 files changed

+42
-12
lines changed

7 files changed

+42
-12
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ JUnit repository on GitHub.
3131
calling the internal `ReflectionUtils.makeAccessible(Field)` method directly.
3232
* Support both the primitive type `void` and the wrapper type `Void` in the internal
3333
`ReflectionUtils` to allow `String` to `Class` conversion in parameterized tests.
34+
* Add support for passing line and column number to `ConsoleLauncher` via
35+
`--select-file` and `--select-resource`.
3436

3537

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

junit-platform-engine/src/main/java/org/junit/platform/engine/support/descriptor/ResourceUtils.java renamed to junit-platform-commons/src/main/java/org/junit/platform/commons/util/ResourceUtils.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@
88
* https://www.eclipse.org/legal/epl-v20.html
99
*/
1010

11-
package org.junit.platform.engine.support.descriptor;
11+
package org.junit.platform.commons.util;
12+
13+
import static org.apiguardian.api.API.Status.INTERNAL;
1214

1315
import java.net.URI;
1416

15-
import org.junit.platform.commons.util.Preconditions;
16-
import org.junit.platform.commons.util.StringUtils;
17+
import org.apiguardian.api.API;
1718

1819
/**
1920
* Collection of static utility methods for working with resources.
2021
*
21-
* @since 1.3
22+
* @since 1.3 (originally in org.junit.platform.engine.support.descriptor)
2223
*/
23-
final class ResourceUtils {
24+
@API(status = INTERNAL, since = "1.12")
25+
public final class ResourceUtils {
2426

2527
private ResourceUtils() {
2628
/* no-op */
@@ -33,8 +35,10 @@ private ResourceUtils() {
3335
* @param uri the {@code URI} from which to strip the query component
3436
* @return a new {@code URI} with the query component removed, or the
3537
* original {@code URI} unmodified if it does not have a query component
38+
*
39+
* @since 1.3
3640
*/
37-
static URI stripQueryComponent(URI uri) {
41+
public static URI stripQueryComponent(URI uri) {
3842
Preconditions.notNull(uri, "URI must not be null");
3943

4044
if (StringUtils.isBlank(uri.getQuery())) {

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@
1919
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectPackage;
2020
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectUri;
2121

22+
import java.net.URI;
23+
2224
import org.junit.platform.commons.PreconditionViolationException;
25+
import org.junit.platform.commons.util.ResourceUtils;
2326
import org.junit.platform.engine.DiscoverySelectorIdentifier;
2427
import org.junit.platform.engine.discovery.ClassSelector;
2528
import org.junit.platform.engine.discovery.ClasspathResourceSelector;
2629
import org.junit.platform.engine.discovery.DirectorySelector;
2730
import org.junit.platform.engine.discovery.DiscoverySelectors;
31+
import org.junit.platform.engine.discovery.FilePosition;
2832
import org.junit.platform.engine.discovery.FileSelector;
2933
import org.junit.platform.engine.discovery.IterationSelector;
3034
import org.junit.platform.engine.discovery.MethodSelector;
@@ -53,8 +57,12 @@ public UriSelector convert(String value) {
5357
static class File implements ITypeConverter<FileSelector> {
5458
@Override
5559
public FileSelector convert(String value) {
56-
return selectFile(value);
60+
URI uri = URI.create(value);
61+
String path = ResourceUtils.stripQueryComponent(uri).getPath();
62+
FilePosition filePosition = FilePosition.fromQuery(uri.getQuery()).orElse(null);
63+
return selectFile(path, filePosition);
5764
}
65+
5866
}
5967

6068
static class Directory implements ITypeConverter<DirectorySelector> {
@@ -88,7 +96,10 @@ public MethodSelector convert(String value) {
8896
static class ClasspathResource implements ITypeConverter<ClasspathResourceSelector> {
8997
@Override
9098
public ClasspathResourceSelector convert(String value) {
91-
return selectClasspathResource(value);
99+
URI uri = URI.create(value);
100+
String path = ResourceUtils.stripQueryComponent(uri).getPath();
101+
FilePosition filePosition = FilePosition.fromQuery(uri.getQuery()).orElse(null);
102+
return selectClasspathResource(path, filePosition);
92103
}
93104
}
94105

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ static class SelectorOptions {
7676
private final List<UriSelector> selectedUris2 = new ArrayList<>();
7777

7878
@Option(names = { "-f",
79-
"--select-file" }, paramLabel = "FILE", arity = "1", converter = SelectorConverter.File.class, description = "Select a file for test discovery. This option can be repeated.")
79+
"--select-file" }, paramLabel = "FILE", arity = "1", converter = SelectorConverter.File.class, //
80+
description = "Select a file for test discovery. "
81+
+ "The line and column numbers can be provided as URI query parameters (e.g. foo.txt?line=12&column=34). "
82+
+ "This option can be repeated.")
8083
private final List<FileSelector> selectedFiles = new ArrayList<>();
8184

8285
@Option(names = { "--f", "-select-file" }, arity = "1", hidden = true, converter = SelectorConverter.File.class)
@@ -123,7 +126,10 @@ static class SelectorOptions {
123126
private final List<MethodSelector> selectedMethods2 = new ArrayList<>();
124127

125128
@Option(names = { "-r",
126-
"--select-resource" }, paramLabel = "RESOURCE", arity = "1", converter = SelectorConverter.ClasspathResource.class, description = "Select a classpath resource for test discovery. This option can be repeated.")
129+
"--select-resource" }, paramLabel = "RESOURCE", arity = "1", converter = SelectorConverter.ClasspathResource.class, //
130+
description = "Select a classpath resource for test discovery. "
131+
+ "The line and column numbers can be provided as URI query parameters (e.g. foo.csv?line=12&column=34). "
132+
+ "This option can be repeated.")
127133
private final List<ClasspathResourceSelector> selectedClasspathResources = new ArrayList<>();
128134

129135
@Option(names = { "--r",

junit-platform-engine/src/main/java/org/junit/platform/engine/support/descriptor/ClasspathResourceSource.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.apiguardian.api.API;
2020
import org.junit.platform.commons.PreconditionViolationException;
2121
import org.junit.platform.commons.util.Preconditions;
22+
import org.junit.platform.commons.util.ResourceUtils;
2223
import org.junit.platform.commons.util.ToStringBuilder;
2324
import org.junit.platform.engine.TestSource;
2425

junit-platform-engine/src/main/java/org/junit/platform/engine/support/descriptor/UriSource.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.apiguardian.api.API;
2121
import org.junit.platform.commons.logging.LoggerFactory;
2222
import org.junit.platform.commons.util.Preconditions;
23+
import org.junit.platform.commons.util.ResourceUtils;
2324
import org.junit.platform.engine.TestSource;
2425

2526
/**

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.junit.jupiter.params.provider.EnumSource;
4545
import org.junit.platform.engine.DiscoverySelector;
4646
import org.junit.platform.engine.discovery.DiscoverySelectors;
47+
import org.junit.platform.engine.discovery.FilePosition;
4748

4849
/**
4950
* @since 1.10
@@ -382,7 +383,9 @@ void parseValidFileSelectors(ArgsType type) {
382383
() -> assertEquals(List.of(selectFile("foo.txt")), type.parseArgLine("-select-file=foo.txt").discovery.getSelectedFiles()),
383384
() -> assertEquals(List.of(selectFile("foo.txt")), type.parseArgLine("--select-file foo.txt").discovery.getSelectedFiles()),
384385
() -> assertEquals(List.of(selectFile("foo.txt")), type.parseArgLine("--select-file=foo.txt").discovery.getSelectedFiles()),
385-
() -> assertEquals(List.of(selectFile("foo.txt"), selectFile("bar.csv")), type.parseArgLine("-f foo.txt -f bar.csv").discovery.getSelectedFiles())
386+
() -> assertEquals(List.of(selectFile("foo.txt"), selectFile("bar.csv")), type.parseArgLine("-f foo.txt -f bar.csv").discovery.getSelectedFiles()),
387+
() -> assertEquals(List.of(selectFile("foo.txt", FilePosition.from(5))), type.parseArgLine("-f foo.txt?line=5").discovery.getSelectedFiles()),
388+
() -> assertEquals(List.of(selectFile("foo.txt", FilePosition.from(12, 34))), type.parseArgLine("-f foo.txt?line=12&column=34").discovery.getSelectedFiles())
386389
);
387390
// @formatter:on
388391
}
@@ -509,7 +512,9 @@ void parseValidClasspathResourceSelectors(ArgsType type) {
509512
() -> assertEquals(List.of(selectClasspathResource("/foo.csv")), type.parseArgLine("-select-resource=/foo.csv").discovery.getSelectedClasspathResources()),
510513
() -> assertEquals(List.of(selectClasspathResource("/foo.csv")), type.parseArgLine("--select-resource /foo.csv").discovery.getSelectedClasspathResources()),
511514
() -> assertEquals(List.of(selectClasspathResource("/foo.csv")), type.parseArgLine("--select-resource=/foo.csv").discovery.getSelectedClasspathResources()),
512-
() -> assertEquals(List.of(selectClasspathResource("/foo.csv"), selectClasspathResource("bar.json")), type.parseArgLine("-r /foo.csv -r bar.json").discovery.getSelectedClasspathResources())
515+
() -> assertEquals(List.of(selectClasspathResource("/foo.csv"), selectClasspathResource("bar.json")), type.parseArgLine("-r /foo.csv -r bar.json").discovery.getSelectedClasspathResources()),
516+
() -> assertEquals(List.of(selectClasspathResource("/foo.csv", FilePosition.from(5))), type.parseArgLine("-r /foo.csv?line=5").discovery.getSelectedClasspathResources()),
517+
() -> assertEquals(List.of(selectClasspathResource("/foo.csv", FilePosition.from(12, 34))), type.parseArgLine("-r /foo.csv?line=12&column=34").discovery.getSelectedClasspathResources())
513518
);
514519
// @formatter:on
515520
}

0 commit comments

Comments
 (0)