Skip to content

Commit

Permalink
Accept option arguments for --scan-classpath (step 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Nov 12, 2016
1 parent a872481 commit adaf70d
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 31 deletions.
Expand Up @@ -15,6 +15,9 @@
import java.io.File;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;

import joptsimple.OptionParser;
import joptsimple.OptionSet;
Expand All @@ -40,7 +43,8 @@ class AvailableOptions {
private final OptionSpec<Path> reportsDir;

// Selectors
private final OptionSpec<Void> scanClasspath;
private final OptionSpec<Path> selectedClasspathEntries;
// TODO remove this
private final OptionSpec<String> arguments;
private final OptionSpec<URI> selectedUris;
private final OptionSpec<String> selectedFiles;
Expand Down Expand Up @@ -87,9 +91,14 @@ class AvailableOptions {

// --- Selectors -------------------------------------------------------

scanClasspath = parser.accepts("scan-class-path", //
"Scan entire classpath or explicit classpath roots.");
selectedClasspathEntries = parser.acceptsAll(asList("scan-class-path", "scan-classpath"), //
"Scan entire classpath or explicit classpath roots.") //
.withOptionalArg() //
.withValuesConvertedBy(new PathConverter()) //
.withValuesSeparatedBy(File.pathSeparatorChar) //
.describedAs("path1" + File.pathSeparator + "path2" + File.pathSeparator + "...");

// TODO remove this
arguments = parser.nonOptions("If --scan-class-path has been specified, non-option arguments represent "
+ "explicit classpath roots that should be considered for scanning "
+ "or none if the entire classpath should be scanned.");
Expand Down Expand Up @@ -166,8 +175,12 @@ CommandLineOptions toCommandLineOptions(OptionSet detectedOptions) {
result.setReportsDir(detectedOptions.valueOf(this.reportsDir));

// Selectors
result.setScanClasspath(detectedOptions.has(this.scanClasspath));
result.setArguments(detectedOptions.valuesOf(this.arguments));
result.setScanClasspath(detectedOptions.has(this.selectedClasspathEntries));
// TODO simplify this
List<Path> selectedClasspathEntries = new LinkedList<>(detectedOptions.valuesOf(this.selectedClasspathEntries));
detectedOptions.valuesOf(this.arguments).stream().map(Paths::get).forEach(selectedClasspathEntries::add);
result.setSelectedClasspathEntries(selectedClasspathEntries);

result.setSelectedUris(detectedOptions.valuesOf(this.selectedUris));
result.setSelectedFiles(detectedOptions.valuesOf(this.selectedFiles));
result.setSelectedDirectories(detectedOptions.valuesOf(this.selectedDirectories));
Expand Down
Expand Up @@ -36,7 +36,8 @@ public class CommandLineOptions {
private boolean hideDetails;

private boolean scanClasspath;
private List<String> arguments = emptyList();
private List<Path> selectedClasspathEntries = emptyList();

private List<URI> selectedUris = emptyList();
private List<String> selectedFiles = emptyList();
private List<String> selectedDirectories = emptyList();
Expand Down Expand Up @@ -206,12 +207,12 @@ public void setReportsDir(Path reportsDir) {
this.reportsDir = reportsDir;
}

public List<String> getArguments() {
return this.arguments;
public List<Path> getSelectedClasspathEntries() {
return this.selectedClasspathEntries;
}

public void setArguments(List<String> arguments) {
this.arguments = arguments;
public void setSelectedClasspathEntries(List<Path> selectedClasspathEntries) {
this.selectedClasspathEntries = selectedClasspathEntries;
}

}
Expand Up @@ -10,7 +10,6 @@

package org.junit.platform.console.tasks;

import static java.util.stream.Collectors.toCollection;
import static org.junit.platform.engine.discovery.ClassNameFilter.includeClassNamePatterns;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClasspathRoots;
import static org.junit.platform.launcher.EngineFilter.excludeEngines;
Expand All @@ -20,7 +19,6 @@
import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -62,12 +60,12 @@ private List<ClasspathRootSelector> createClasspathRootSelectors(CommandLineOpti
}

private Set<Path> determineClasspathRootDirectories(CommandLineOptions options) {
if (options.getArguments().isEmpty()) {
if (options.getSelectedClasspathEntries().isEmpty()) {
Set<Path> rootDirs = new LinkedHashSet<>(ReflectionUtils.getAllClasspathRootDirectories());
rootDirs.addAll(options.getAdditionalClasspathEntries());
return rootDirs;
}
return options.getArguments().stream().map(Paths::get).collect(toCollection(LinkedHashSet::new));
return new LinkedHashSet<>(options.getSelectedClasspathEntries());
}

private List<DiscoverySelector> createExplicitDiscoverySelectors(CommandLineOptions options) {
Expand Down
Expand Up @@ -136,18 +136,13 @@ class JUnitPlatformPlugin implements Plugin<Project> {

private void addSelectors(project, selectors, args) {
if (selectors.empty) {
args.add('--scan-class-path')

def rootDirs = []
project.sourceSets.each { sourceSet ->
rootDirs.add(sourceSet.output.classesDir)
rootDirs.add(sourceSet.output.resourcesDir)
rootDirs.addAll(sourceSet.output.dirs.files)
}

rootDirs.each { File root ->
args.add(root.getAbsolutePath())
}
args.addAll(['--scan-class-path', rootDirs.join(File.pathSeparator)])
} else {
selectors.uris.each { uri ->
args.addAll(['-u', uri])
Expand Down
Expand Up @@ -101,17 +101,16 @@ class JUnitPlatformPluginSpec extends Specification {
junitTask.main == ConsoleLauncher.class.getName()

junitTask.args.contains('--hide-details')
junitTask.args.contains('--scan-class-path')
junitTask.args.containsAll('-n', '.*Tests?', '-n', 'Foo', '-n', 'Bar')
junitTask.args.containsAll('-t', 'fast')
junitTask.args.containsAll('-T', 'slow')
junitTask.args.containsAll('-e', 'foo')
junitTask.args.containsAll('-E', 'bar')
junitTask.args.containsAll('--reports-dir', new File('/any').getCanonicalFile().toString())
junitTask.args.contains(project.file('build/classes/main').absolutePath)
junitTask.args.contains(project.file('build/resources/main').absolutePath)
junitTask.args.contains(project.file('build/classes/test').absolutePath)
junitTask.args.contains(project.file('build/resources/test').absolutePath)
def classpathToBeScanned = ['build/classes/main', 'build/resources/main', 'build/classes/test', 'build/resources/test']
.collect { path -> project.file(path).absolutePath }
.join(File.pathSeparator)
junitTask.args.containsAll('--scan-class-path', classpathToBeScanned)

Task testTask = project.tasks.findByName('test')
testTask instanceof Test
Expand Down
Expand Up @@ -58,7 +58,7 @@ public void parseNoArguments() {
() -> assertEquals(emptyList(), options.getSelectedUris()),
() -> assertEquals(emptyList(), options.getSelectedFiles()),
() -> assertEquals(emptyList(), options.getSelectedDirectories()),
() -> assertEquals(emptyList(), options.getArguments())
() -> assertEquals(emptyList(), options.getSelectedClasspathEntries())
);
// @formatter:on
}
Expand Down Expand Up @@ -344,12 +344,21 @@ public void parseInvalidClasspathResourceSelectors() {
}

@Test
public void parseExtraArguments() {
public void parseClasspathScanningEntries() {
Path dir = Paths.get(".");
// @formatter:off
assertAll(
() -> assertEquals(asList("foo"), parseArgLine("foo").getArguments()),
() -> assertEquals(asList("foo", "bar"), parseArgLine("-h foo bar").getArguments()),
() -> assertEquals(asList("foo", "bar"), parseArgLine("-h -- foo bar").getArguments())
() -> assertTrue(parseArgLine("--scan-class-path").isScanClasspath()),
() -> assertEquals(emptyList(), parseArgLine("--scan-class-path").getSelectedClasspathEntries()),
() -> assertTrue(parseArgLine("--scan-classpath").isScanClasspath()),
() -> assertEquals(emptyList(), parseArgLine("--scan-classpath").getSelectedClasspathEntries()),
() -> assertTrue(parseArgLine("--scan-class-path .").isScanClasspath()),
() -> assertEquals(singletonList(dir), parseArgLine("--scan-class-path .").getSelectedClasspathEntries()),
() -> assertEquals(singletonList(dir), parseArgLine("--scan-class-path=.").getSelectedClasspathEntries()),
() -> assertEquals(singletonList(dir), parseArgLine("-scan-class-path .").getSelectedClasspathEntries()),
() -> assertEquals(singletonList(dir), parseArgLine("-scan-class-path=.").getSelectedClasspathEntries()),
() -> assertEquals(asList(dir, Paths.get("src/test/java")), parseArgLine("--scan-class-path . --scan-class-path src/test/java").getSelectedClasspathEntries()),
() -> assertEquals(asList(dir, Paths.get("src/test/java")), parseArgLine("--scan-class-path ." + File.pathSeparator + "src/test/java").getSelectedClasspathEntries())
);
// @formatter:on
}
Expand Down
Expand Up @@ -61,7 +61,7 @@ public void convertsScanClasspathOptionWithoutExplicitRootDirectories() {
@Test
public void convertsScanClasspathOptionWithExplicitRootDirectories() {
options.setScanClasspath(true);
options.setArguments(asList(".", ".."));
options.setSelectedClasspathEntries(asList(Paths.get("."), Paths.get("..")));

LauncherDiscoveryRequest request = convert();

Expand Down

0 comments on commit adaf70d

Please sign in to comment.