FileUtil: getRelativePath should return relative the path from the base to the file regardless existence or the type of the base file.

This commit is contained in:
Vladislav.Soroka
2017-08-22 16:00:16 +03:00
parent 156b3bc112
commit 27ba077018
4 changed files with 38 additions and 21 deletions
@@ -234,15 +234,19 @@ public class FileUtilRt {
return fileName.replace('\\', '/');
}
/**
* Gets the relative path from the {@code base} to the {@code file} regardless existence or the type of the {@code base}.
* <p>
* NOTE: if the file(not directory) passed as the {@code base} the result can not be used as a relative path from the {@code base} parent directory to the {@code file}
*
* @param base the base
* @param file the file
* @return the relative path from the {@code base} to the {@code file} or {@code null}
*/
@Nullable
public static String getRelativePath(File base, File file) {
if (base == null || file == null) return null;
if (base.exists() && !base.isDirectory()) {
base = base.getParentFile();
if (base == null) return null;
}
//noinspection FileEqualsUsage
if (base.equals(file)) return ".";
@@ -78,6 +78,15 @@ public class FileUtil extends FileUtilRt {
return StringUtil.join(parts, File.separator);
}
/**
* Gets the relative path from the {@code base} to the {@code file} regardless existence or the type of the {@code base}.
* <p>
* NOTE: if the file(not directory) passed as the {@code base} the result can not be used as a relative path from the {@code base} parent directory to the {@code file}
*
* @param base the base
* @param file the file
* @return the relative path from the {@code base} to the {@code file} or {@code null}
*/
@Nullable
public static String getRelativePath(File base, File file) {
return FileUtilRt.getRelativePath(base, file);
@@ -324,4 +324,20 @@ public class FileUtilHeavyTest {
String path = myFindTestFirstFile.getPath();
assertEquals(SystemInfo.isFileSystemCaseSensitive, FileUtil.isFileSystemCaseSensitive(path));
}
@Test
public void testFileRelativePath() {
String relativePath = FileUtil.toSystemDependentName("relative/path.file");
File existingDir = myTempDirectory;
assertEquals(relativePath, FileUtil.getRelativePath(existingDir, new File(existingDir, relativePath)));
File notExistingDirOrFile = new File("not/existing/path");
assertEquals(relativePath, FileUtil.getRelativePath(notExistingDirOrFile, new File(notExistingDirOrFile, relativePath)));
// FileUtil.getRelativePath(File, File) should have the same behavior then FileUtil.getRelativePath(String, String, char)
File existingFile = IoTestUtil.createTestFile(existingDir, "foo.file");
assertEquals(".." + File.separatorChar + relativePath,
FileUtil.getRelativePath(existingFile, new File(existingFile.getParent(), relativePath)));
}
}
@@ -24,10 +24,12 @@ import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.Convertor;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.*;
@@ -190,20 +192,6 @@ public class FileUtilLightTest {
}
}
@Test
public void testFileRelativePath() {
String relativePath = FileUtil.toSystemDependentName("relative/path.file");
File existingDir = IoTestUtil.createTestDir(UUID.randomUUID().toString());
assertEquals(relativePath, FileUtil.getRelativePath(existingDir, new File(existingDir, relativePath)));
File notExistingDir = new File("not/existing/path");
assertEquals(relativePath, FileUtil.getRelativePath(notExistingDir, new File(notExistingDir, relativePath)));
File existingFile = IoTestUtil.createTestFile(existingDir, "foo.file");
assertEquals(relativePath, FileUtil.getRelativePath(existingFile, new File(existingFile.getParent(), relativePath)));
}
@Test
public void testRelativeToUserHome() {
assertEquals(SystemProperties.getUserHome(), FileUtil.getLocationRelativeToUserHome(SystemProperties.getUserHome(), false));