[errorprone] FS#searchPath: handle surprising behavior of String#split

See https://errorprone.info/bugpattern/StringSplitter

Change-Id: Ic80f6c53ea96537ed4d046621e774288fced7ce1
This commit is contained in:
Matthias Sohn 2023-09-19 14:35:38 +02:00
parent a94e54ce87
commit e53c1864e6
2 changed files with 29 additions and 1 deletions

View File

@ -13,6 +13,7 @@
import static java.time.Instant.EPOCH;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeNoException;
import static org.junit.Assume.assumeTrue;
@ -233,4 +234,26 @@ public void testRepoCacheRelativePathUnbornRepo() {
assertFalse(RepositoryCache.FileKey
.isGitRepository(new File("repo.git"), FS.DETECTED));
}
@Test
public void testSearchPath() throws IOException {
File f1 = new File(trash, "file1");
FileUtils.createNewFile(f1);
f1.setExecutable(true);
File f2 = new File(trash, "file2");
FileUtils.createNewFile(f2);
assertEquals(f1, FS.searchPath(trash.getAbsolutePath(), "file1"));
assertNull(FS.searchPath(trash.getAbsolutePath(), "file2"));
}
@Test
public void testSearchPathEmptyPath() {
assertNull(FS.searchPath("", "file1"));
assertNull(FS.searchPath(File.pathSeparator, "file1"));
assertNull(FS.searchPath(File.pathSeparator + File.pathSeparator,
"file1"));
assertNull(FS.searchPath(
" " + File.pathSeparator + " " + File.pathSeparator + " \t",
"file1"));
}
}

View File

@ -97,6 +97,9 @@ public abstract class FS {
private static final Pattern VERSION = Pattern
.compile("\\s(\\d+)\\.(\\d+)\\.(\\d+)"); //$NON-NLS-1$
private static final Pattern EMPTY_PATH = Pattern
.compile("^[\\p{javaWhitespace}" + File.pathSeparator + "]*$"); //$NON-NLS-1$ //$NON-NLS-2$
private volatile Boolean supportSymlinks;
/**
@ -1310,8 +1313,10 @@ private File defaultUserHomeImpl() {
* @return the first match found, or null
* @since 3.0
*/
@SuppressWarnings("StringSplitter")
protected static File searchPath(String path, String... lookFor) {
if (path == null) {
if (StringUtils.isEmptyOrNull(path)
|| EMPTY_PATH.matcher(path).find()) {
return null;
}