Skip to content

Commit 090e4d9

Browse files
committed
Reduce scope of try-catch block
1 parent 01ff0e7 commit 090e4d9

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

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

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

@@ -67,7 +68,7 @@ public void selectorProcessed(UniqueId engineId, DiscoverySelector selector, Sel
6768
if (result.getStatus() == FAILED) {
6869
this.issues.add(DiscoveryIssue.builder(Severity.ERROR, selector + " resolution failed") //
6970
.cause(result.getThrowable()) //
70-
.source(toSourceSafely(selector)) //
71+
.source(toSource(selector)) //
7172
.build());
7273
}
7374
else if (result.getStatus() == UNRESOLVED && selector instanceof UniqueIdSelector uniqueIdSelector) {
@@ -78,16 +79,6 @@ else if (result.getStatus() == UNRESOLVED && selector instanceof UniqueIdSelecto
7879
}
7980
}
8081

81-
private static @Nullable TestSource toSourceSafely(DiscoverySelector selector) {
82-
try {
83-
return toSource(selector);
84-
}
85-
catch (Exception e) {
86-
logger.error(e, () -> "Failed to convert DiscoverySelector [%s] into TestSource".formatted(selector));
87-
return null;
88-
}
89-
}
90-
9182
private static @Nullable TestSource toSource(DiscoverySelector selector) {
9283
if (selector instanceof ClassSelector classSelector) {
9384
return ClassSource.from(classSelector.getClassName());
@@ -106,17 +97,26 @@ else if (result.getStatus() == UNRESOLVED && selector instanceof UniqueIdSelecto
10697
if (selector instanceof PackageSelector packageSelector) {
10798
return PackageSource.from(packageSelector.getPackageName());
10899
}
109-
if (selector instanceof FileSelector fileSelector) {
110-
return fileSelector.getPosition() //
111-
.map(DiscoveryIssueCollector::convert) //
112-
.map(position -> FileSource.from(fileSelector.getFile(), position)) //
113-
.orElseGet(() -> FileSource.from(fileSelector.getFile()));
114-
}
115-
if (selector instanceof DirectorySelector directorySelector) {
116-
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+
}
117116
}
118-
if (selector instanceof UriSelector uriSelector) {
119-
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));
120120
}
121121
return null;
122122
}

0 commit comments

Comments
 (0)