[errorprone] FS#searchPath: handle surprising behavior of String#split
See https://errorprone.info/bugpattern/StringSplitter Change-Id: Ic80f6c53ea96537ed4d046621e774288fced7ce1
This commit is contained in:
parent
a94e54ce87
commit
e53c1864e6
|
@ -13,6 +13,7 @@
|
||||||
import static java.time.Instant.EPOCH;
|
import static java.time.Instant.EPOCH;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assume.assumeNoException;
|
import static org.junit.Assume.assumeNoException;
|
||||||
import static org.junit.Assume.assumeTrue;
|
import static org.junit.Assume.assumeTrue;
|
||||||
|
@ -233,4 +234,26 @@ public void testRepoCacheRelativePathUnbornRepo() {
|
||||||
assertFalse(RepositoryCache.FileKey
|
assertFalse(RepositoryCache.FileKey
|
||||||
.isGitRepository(new File("repo.git"), FS.DETECTED));
|
.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"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -97,6 +97,9 @@ public abstract class FS {
|
||||||
private static final Pattern VERSION = Pattern
|
private static final Pattern VERSION = Pattern
|
||||||
.compile("\\s(\\d+)\\.(\\d+)\\.(\\d+)"); //$NON-NLS-1$
|
.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;
|
private volatile Boolean supportSymlinks;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1310,8 +1313,10 @@ private File defaultUserHomeImpl() {
|
||||||
* @return the first match found, or null
|
* @return the first match found, or null
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("StringSplitter")
|
||||||
protected static File searchPath(String path, String... lookFor) {
|
protected static File searchPath(String path, String... lookFor) {
|
||||||
if (path == null) {
|
if (StringUtils.isEmptyOrNull(path)
|
||||||
|
|| EMPTY_PATH.matcher(path).find()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue