JAL-629 Add substituteHomeDir to getDirname(). Tighten dirname tests
authorBen Soares <b.soares@dundee.ac.uk>
Wed, 12 Apr 2023 13:24:21 +0000 (14:24 +0100)
committerBen Soares <b.soares@dundee.ac.uk>
Wed, 12 Apr 2023 13:24:21 +0000 (14:24 +0100)
src/jalview/util/FileUtils.java
test/jalview/util/FileUtilsTest.java

index f5b3701..9b56c64 100644 (file)
@@ -164,7 +164,9 @@ public class FileUtils
     String dirname = null;
     try
     {
-      dirname = file.getParentFile().getCanonicalPath();
+      File p = file.getParentFile();
+      File d = new File(substituteHomeDir(p.getPath()));
+      dirname = d.getCanonicalPath();
     } catch (IOException e)
     {
       Console.debug(
index 8b52577..d49fd4c 100644 (file)
@@ -56,7 +56,7 @@ public class FileUtilsTest
 
   @Test(groups = "Functional", dataProvider = "dirnamesAndBasenames")
   public void testDirnamesAndBasenames(String filename, int where,
-          String dirname, String basename)
+          String dirname, String basename, String notInDirname)
   {
     File file = new File(filename);
     String d = FileUtils.getDirname(file);
@@ -71,6 +71,13 @@ public class FileUtilsTest
     else if (where > 0)
       Assert.assertTrue(d.endsWith(dirname), "getDirname(" + file.getPath()
               + ")=" + d + " didn't end with '" + d + "'");
+
+    // ensure dirname doesn't end with basename (which means you can't use same
+    // filename as dir in tests!)
+    Assert.assertFalse(d.endsWith(b));
+
+    if (notInDirname != null)
+      Assert.assertFalse(d.contains(notInDirname));
   }
 
   @DataProvider(name = "patternsAndMinNumFiles")
@@ -94,11 +101,12 @@ public class FileUtilsTest
   public Object[][] dirnamesAndBasenames()
   {
     return new Object[][] { // -1=startsWith, 0=equals, 1=endsWith
-        { "~/hello/sailor", -1, System.getProperty("user.home"), "sailor" }, //
-        { "~/hello/sailor", 1, "/hello", "sailor" }, //
-        { "./examples/uniref50.fa", -1, "/", "uniref50" }, //
-        { "./examples/uniref50.fa", 1, "/examples", "uniref50" }, //
-        { "examples/uniref50.fa", 1, "/examples", "uniref50" }, //
+        { "~/hello/sailor", -1, System.getProperty("user.home"), "sailor",
+            "~" }, //
+        { "~/hello/sailor", 1, "/hello", "sailor", "~" }, //
+        { "./examples/uniref50.fa", -1, "/", "uniref50", null }, //
+        { "./examples/uniref50.fa", 1, "/examples", "uniref50", null }, //
+        { "examples/uniref50.fa", 1, "/examples", "uniref50", null }, //
     };
   }
 }