Skip to content

Log invalid search path roots #4823

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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 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 @@ -24,14 +24,18 @@
import static org.junit.platform.launcher.TagFilter.includeTags;
import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;
import org.junit.platform.commons.util.ModuleUtils;
import org.junit.platform.commons.util.Preconditions;
import org.junit.platform.commons.util.ReflectionUtils;
Expand All @@ -49,6 +53,8 @@
*/
class DiscoveryRequestCreator {

private static final Logger logger = LoggerFactory.getLogger(DiscoveryRequestCreator.class);

static LauncherDiscoveryRequestBuilder toDiscoveryRequestBuilder(TestDiscoveryOptions options) {
LauncherDiscoveryRequestBuilder requestBuilder = request();
List<? extends DiscoverySelector> selectors = createDiscoverySelectors(options);
Expand Down Expand Up @@ -77,7 +83,7 @@ private static List<? extends DiscoverySelector> createDiscoverySelectors(TestDi
}

private static List<ClasspathRootSelector> createClasspathRootSelectors(TestDiscoveryOptions options) {
Set<Path> classpathRoots = determineClasspathRoots(options);
Set<Path> classpathRoots = validateAndLogInvalidRoots(determineClasspathRoots(options));
return selectClasspathRoots(classpathRoots);
}

Expand All @@ -86,12 +92,30 @@ private static Set<Path> determineClasspathRoots(TestDiscoveryOptions options) {
() -> "No classpath entries selected");
if (selectedClasspathEntries.isEmpty()) {
Set<Path> rootDirs = new LinkedHashSet<>(ReflectionUtils.getAllClasspathRootDirectories());
rootDirs.addAll(options.getExistingAdditionalClasspathEntries());
rootDirs.addAll(options.getAdditionalClasspathEntries());
return rootDirs;
}
return new LinkedHashSet<>(selectedClasspathEntries);
}

private static Set<Path> validateAndLogInvalidRoots(Set<Path> roots) {
LinkedHashSet<Path> valid = new LinkedHashSet<>();
HashSet<Path> seen = new HashSet<>();

for (Path root : roots) {
if (!seen.add(root)) {
continue;
}
if (Files.exists(root)) {
valid.add(root);
} else {
logger.warn(() -> "Ignoring non-existing classpath root: %s".formatted(root));
}
}

return valid;
}

private static void addFilters(LauncherDiscoveryRequestBuilder requestBuilder, TestDiscoveryOptions options,
List<? extends DiscoverySelector> selectors) {
requestBuilder.filters(includedClassNamePatterns(options, selectors));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@
import java.io.File;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.LogRecord;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.fixtures.TrackLogRecords;
import org.junit.platform.commons.PreconditionViolationException;
import org.junit.platform.commons.logging.LogRecordListener;
import org.junit.platform.console.options.TestDiscoveryOptions;
import org.junit.platform.engine.Filter;
import org.junit.platform.engine.UniqueId;
Expand Down Expand Up @@ -372,6 +376,34 @@ void convertsConfigurationParametersResources() {
assertThat(configurationParameters.get("com.example.prop.second")).contains("second value");
}

@Test
void logsInvalidSearchPathRoots(@TrackLogRecords LogRecordListener listener) {
var opts = new TestDiscoveryOptions();
opts.setScanClasspath(true);
opts.setSelectedClasspathEntries(List.of(Paths.get("/does/not/exist")));

DiscoveryRequestCreator.toDiscoveryRequestBuilder(opts);

assertThat(listener.stream(DiscoveryRequestCreator.class)) //
.map(LogRecord::getMessage) //
.filteredOn(message -> message.contains("/does/not/exist")) //
.hasSize(1);
}

@Test
void logsInvalidAdditionalClasspathRoots(@TrackLogRecords LogRecordListener listener) {
var opts = new TestDiscoveryOptions();
opts.setScanClasspath(true);
opts.setAdditionalClasspathEntries(List.of(Paths.get("/also/does/not/exist")));

DiscoveryRequestCreator.toDiscoveryRequestBuilder(opts);

assertThat(listener.stream(DiscoveryRequestCreator.class)) //
.map(LogRecord::getMessage) //
.filteredOn(message -> message.contains("/also/does/not/exist")) //
.hasSize(1);
}

private LauncherDiscoveryRequest convert() {
return DiscoveryRequestCreator.toDiscoveryRequestBuilder(options).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ void execute(@FilePrefix("console-launcher") OutputFiles outputFiles) throws Exc
.addArguments("-enableassertions") //
.addArguments("-Djava.util.logging.config.file=logging.properties") //
.addArguments("-Djunit.platform.launcher.interceptors.enabled=true") //
.addArguments("-Duser.language=en", "-Duser.country=US") //
.addArguments("-jar", MavenRepo.jar("junit-platform-console-standalone").toString()) //
.addArguments("execute") //
.addArguments("--scan-class-path") //
Expand Down Expand Up @@ -517,6 +518,7 @@ void executeWithJarredTestClasses(@FilePrefix("jar") OutputFiles jarOutputFiles,
.addArguments("-enableassertions") //
.addArguments("-Djava.util.logging.config.file=logging.properties") //
.addArguments("-Djunit.platform.launcher.interceptors.enabled=true") //
.addArguments("-Duser.language=en", "-Duser.country=US") //
.addArguments("-jar", MavenRepo.jar("junit-platform-console-standalone").toString()) //
.addArguments("execute") //
.addArguments("--scan-class-path") //
Expand Down