Skip to content

Commit 23405b8

Browse files
committed
Protect against potential problems when converting file-based selectors
1 parent 63362e9 commit 23405b8

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

junit-platform-launcher/src/main/java/org/junit/platform/launcher/core/DiscoveryIssueCollector.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
package org.junit.platform.launcher.core;
1212

13+
import static org.junit.platform.commons.util.UnrecoverableExceptions.rethrowIfUnrecoverable;
1314
import static org.junit.platform.engine.SelectorResolutionResult.Status.FAILED;
1415
import static org.junit.platform.engine.SelectorResolutionResult.Status.UNRESOLVED;
1516

@@ -78,7 +79,7 @@ else if (result.getStatus() == UNRESOLVED && selector instanceof UniqueIdSelecto
7879
}
7980
}
8081

81-
static @Nullable TestSource toSource(DiscoverySelector selector) {
82+
private static @Nullable TestSource toSource(DiscoverySelector selector) {
8283
if (selector instanceof ClassSelector classSelector) {
8384
return ClassSource.from(classSelector.getClassName());
8485
}
@@ -96,17 +97,26 @@ else if (result.getStatus() == UNRESOLVED && selector instanceof UniqueIdSelecto
9697
if (selector instanceof PackageSelector packageSelector) {
9798
return PackageSource.from(packageSelector.getPackageName());
9899
}
99-
if (selector instanceof FileSelector fileSelector) {
100-
return fileSelector.getPosition() //
101-
.map(DiscoveryIssueCollector::convert) //
102-
.map(position -> FileSource.from(fileSelector.getFile(), position)) //
103-
.orElseGet(() -> FileSource.from(fileSelector.getFile()));
104-
}
105-
if (selector instanceof DirectorySelector directorySelector) {
106-
return DirectorySource.from(directorySelector.getDirectory());
100+
try {
101+
// Both FileSource and DirectorySource call File.getCanonicalFile() to normalize the reported file which
102+
// can throw an exception for certain file names on certain file systems. UriSource.from(...) is affected
103+
// as well because it may return a FileSource or DirectorySource
104+
if (selector instanceof FileSelector fileSelector) {
105+
return fileSelector.getPosition() //
106+
.map(DiscoveryIssueCollector::convert) //
107+
.map(position -> FileSource.from(fileSelector.getFile(), position)) //
108+
.orElseGet(() -> FileSource.from(fileSelector.getFile()));
109+
}
110+
if (selector instanceof DirectorySelector directorySelector) {
111+
return DirectorySource.from(directorySelector.getDirectory());
112+
}
113+
if (selector instanceof UriSelector uriSelector) {
114+
return UriSource.from(uriSelector.getUri());
115+
}
107116
}
108-
if (selector instanceof UriSelector uriSelector) {
109-
return UriSource.from(uriSelector.getUri());
117+
catch (Exception ex) {
118+
rethrowIfUnrecoverable(ex);
119+
logger.warn(ex, () -> "Failed to convert DiscoverySelector [%s] into TestSource".formatted(selector));
110120
}
111121
return null;
112122
}

0 commit comments

Comments
 (0)