diff --git a/platform/util-rt/src/com/intellij/openapi/util/io/FileUtilRt.java b/platform/util-rt/src/com/intellij/openapi/util/io/FileUtilRt.java index 21c4776a1260..e0e37aa82fa5 100644 --- a/platform/util-rt/src/com/intellij/openapi/util/io/FileUtilRt.java +++ b/platform/util-rt/src/com/intellij/openapi/util/io/FileUtilRt.java @@ -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}. + *

+ * 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 "."; diff --git a/platform/util/src/com/intellij/openapi/util/io/FileUtil.java b/platform/util/src/com/intellij/openapi/util/io/FileUtil.java index 69ed816b5a5e..3287178e0bfc 100644 --- a/platform/util/src/com/intellij/openapi/util/io/FileUtil.java +++ b/platform/util/src/com/intellij/openapi/util/io/FileUtil.java @@ -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}. + *

+ * 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); diff --git a/platform/util/testSrc/com/intellij/openapi/util/io/FileUtilHeavyTest.java b/platform/util/testSrc/com/intellij/openapi/util/io/FileUtilHeavyTest.java index d4b851ff5ca8..58bf38f95aac 100644 --- a/platform/util/testSrc/com/intellij/openapi/util/io/FileUtilHeavyTest.java +++ b/platform/util/testSrc/com/intellij/openapi/util/io/FileUtilHeavyTest.java @@ -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))); + } } \ No newline at end of file diff --git a/platform/util/testSrc/com/intellij/openapi/util/io/FileUtilLightTest.java b/platform/util/testSrc/com/intellij/openapi/util/io/FileUtilLightTest.java index 2c3849249a3a..820d1f328669 100644 --- a/platform/util/testSrc/com/intellij/openapi/util/io/FileUtilLightTest.java +++ b/platform/util/testSrc/com/intellij/openapi/util/io/FileUtilLightTest.java @@ -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));