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

Add support for passing line and column number to ConsoleLauncher via --select-file and --select-resource #4044

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ JUnit repository on GitHub.

* Fix support for disabling ANSI colors on the console when the `NO_COLOR` environment
variable is available.
* Add support for passing line and column number to ConsoleLauncher via `--select-file`
and `--select-resource`.

[[release-notes-5.12.0-M1-junit-platform-deprecations-and-breaking-changes]]
==== Deprecations and Breaking Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectPackage;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectUri;

import java.net.URI;

import org.junit.platform.commons.PreconditionViolationException;
import org.junit.platform.commons.util.StringUtils;
import org.junit.platform.engine.DiscoverySelectorIdentifier;
import org.junit.platform.engine.discovery.ClassSelector;
import org.junit.platform.engine.discovery.ClasspathResourceSelector;
import org.junit.platform.engine.discovery.DirectorySelector;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.engine.discovery.FilePosition;
import org.junit.platform.engine.discovery.FileSelector;
import org.junit.platform.engine.discovery.IterationSelector;
import org.junit.platform.engine.discovery.MethodSelector;
Expand Down Expand Up @@ -53,8 +57,12 @@ public UriSelector convert(String value) {
static class File implements ITypeConverter<FileSelector> {
@Override
public FileSelector convert(String value) {
return selectFile(value);
URI uri = URI.create(value);
String path = stripQueryComponent(uri).getPath();
FilePosition filePosition = FilePosition.fromQuery(uri.getQuery()).orElse(null);
return selectFile(path, filePosition);
}

}

static class Directory implements ITypeConverter<DirectorySelector> {
Expand Down Expand Up @@ -88,7 +96,10 @@ public MethodSelector convert(String value) {
static class ClasspathResource implements ITypeConverter<ClasspathResourceSelector> {
@Override
public ClasspathResourceSelector convert(String value) {
return selectClasspathResource(value);
URI uri = URI.create(value);
String path = stripQueryComponent(uri).getPath();
FilePosition filePosition = FilePosition.fromQuery(uri.getQuery()).orElse(null);
return selectClasspathResource(path, filePosition);
}
}

Expand All @@ -109,4 +120,12 @@ public DiscoverySelectorIdentifier convert(String value) {
}
}

private static URI stripQueryComponent(URI uri) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is a duplicate of the one in org.junit.platform.engine.support.descriptor.ResourceUtils, but I don't think there is a way of using the same in both places, is it?

if (StringUtils.isBlank(uri.getQuery())) {
return uri;
}

String uriAsString = uri.toString();
return URI.create(uriAsString.substring(0, uriAsString.indexOf('?')));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ static class SelectorOptions {
private final List<UriSelector> selectedUris2 = new ArrayList<>();

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

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

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

@Option(names = { "--r",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.platform.engine.DiscoverySelector;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.engine.discovery.FilePosition;

/**
* @since 1.10
Expand Down Expand Up @@ -340,7 +341,9 @@ void parseValidFileSelectors(ArgsType type) {
() -> assertEquals(List.of(selectFile("foo.txt")), type.parseArgLine("-select-file=foo.txt").discovery.getSelectedFiles()),
() -> assertEquals(List.of(selectFile("foo.txt")), type.parseArgLine("--select-file foo.txt").discovery.getSelectedFiles()),
() -> assertEquals(List.of(selectFile("foo.txt")), type.parseArgLine("--select-file=foo.txt").discovery.getSelectedFiles()),
() -> assertEquals(List.of(selectFile("foo.txt"), selectFile("bar.csv")), type.parseArgLine("-f foo.txt -f bar.csv").discovery.getSelectedFiles())
() -> assertEquals(List.of(selectFile("foo.txt"), selectFile("bar.csv")), type.parseArgLine("-f foo.txt -f bar.csv").discovery.getSelectedFiles()),
() -> assertEquals(List.of(selectFile("foo.txt", FilePosition.from(5))), type.parseArgLine("-f foo.txt?line=5").discovery.getSelectedFiles()),
() -> assertEquals(List.of(selectFile("foo.txt", FilePosition.from(12, 34))), type.parseArgLine("-f foo.txt?line=12&column=34").discovery.getSelectedFiles())
);
// @formatter:on
}
Expand Down Expand Up @@ -467,7 +470,9 @@ void parseValidClasspathResourceSelectors(ArgsType type) {
() -> assertEquals(List.of(selectClasspathResource("/foo.csv")), type.parseArgLine("-select-resource=/foo.csv").discovery.getSelectedClasspathResources()),
() -> assertEquals(List.of(selectClasspathResource("/foo.csv")), type.parseArgLine("--select-resource /foo.csv").discovery.getSelectedClasspathResources()),
() -> assertEquals(List.of(selectClasspathResource("/foo.csv")), type.parseArgLine("--select-resource=/foo.csv").discovery.getSelectedClasspathResources()),
() -> assertEquals(List.of(selectClasspathResource("/foo.csv"), selectClasspathResource("bar.json")), type.parseArgLine("-r /foo.csv -r bar.json").discovery.getSelectedClasspathResources())
() -> assertEquals(List.of(selectClasspathResource("/foo.csv"), selectClasspathResource("bar.json")), type.parseArgLine("-r /foo.csv -r bar.json").discovery.getSelectedClasspathResources()),
() -> assertEquals(List.of(selectClasspathResource("/foo.csv", FilePosition.from(5))), type.parseArgLine("-r /foo.csv?line=5").discovery.getSelectedClasspathResources()),
() -> assertEquals(List.of(selectClasspathResource("/foo.csv", FilePosition.from(12, 34))), type.parseArgLine("-r /foo.csv?line=12&column=34").discovery.getSelectedClasspathResources())
);
// @formatter:on
}
Expand Down