Skip to content

Commit 976a110

Browse files
committed
Protect against potential problems when converting file-based selectors
(cherry picked from commit 23405b8)
1 parent e94f728 commit 976a110

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

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

Lines changed: 22 additions & 12 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

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

80-
static TestSource toSource(DiscoverySelector selector) {
81+
private static TestSource toSource(DiscoverySelector selector) {
8182
if (selector instanceof ClassSelector) {
8283
return ClassSource.from(((ClassSelector) selector).getClassName());
8384
}
@@ -97,18 +98,27 @@ static TestSource toSource(DiscoverySelector selector) {
9798
if (selector instanceof PackageSelector) {
9899
return PackageSource.from(((PackageSelector) selector).getPackageName());
99100
}
100-
if (selector instanceof FileSelector) {
101-
FileSelector fileSelector = (FileSelector) selector;
102-
return fileSelector.getPosition() //
103-
.map(DiscoveryIssueCollector::convert) //
104-
.map(position -> FileSource.from(fileSelector.getFile(), position)) //
105-
.orElseGet(() -> FileSource.from(fileSelector.getFile()));
106-
}
107-
if (selector instanceof DirectorySelector) {
108-
return DirectorySource.from(((DirectorySelector) selector).getDirectory());
101+
try {
102+
// Both FileSource and DirectorySource call File.getCanonicalFile() to normalize the reported file which
103+
// can throw an exception for certain file names on certain file systems. UriSource.from(...) is affected
104+
// as well because it may return a FileSource or DirectorySource
105+
if (selector instanceof FileSelector) {
106+
FileSelector fileSelector = (FileSelector) selector;
107+
return fileSelector.getPosition() //
108+
.map(DiscoveryIssueCollector::convert) //
109+
.map(position -> FileSource.from(fileSelector.getFile(), position)) //
110+
.orElseGet(() -> FileSource.from(fileSelector.getFile()));
111+
}
112+
if (selector instanceof DirectorySelector) {
113+
return DirectorySource.from(((DirectorySelector) selector).getDirectory());
114+
}
115+
if (selector instanceof UriSelector) {
116+
return UriSource.from(((UriSelector) selector).getUri());
117+
}
109118
}
110-
if (selector instanceof UriSelector) {
111-
return UriSource.from(((UriSelector) selector).getUri());
119+
catch (Exception ex) {
120+
rethrowIfUnrecoverable(ex);
121+
logger.warn(ex, () -> String.format("Failed to convert DiscoverySelector [%s] into TestSource", selector));
112122
}
113123
return null;
114124
}

0 commit comments

Comments
 (0)