From 6125140c3ea968b79423882d1191a897af9d76be Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Wed, 12 Apr 2023 14:24:21 +0100 Subject: [PATCH] JAL-629 Add substituteHomeDir to getDirname(). Tighten dirname tests --- src/jalview/util/FileUtils.java | 4 +++- test/jalview/util/FileUtilsTest.java | 20 ++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/jalview/util/FileUtils.java b/src/jalview/util/FileUtils.java index f5b3701..9b56c64 100644 --- a/src/jalview/util/FileUtils.java +++ b/src/jalview/util/FileUtils.java @@ -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( diff --git a/test/jalview/util/FileUtilsTest.java b/test/jalview/util/FileUtilsTest.java index 8b52577..d49fd4c 100644 --- a/test/jalview/util/FileUtilsTest.java +++ b/test/jalview/util/FileUtilsTest.java @@ -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 }, // }; } } -- 1.7.10.2