From 638bf05a23e1ef7dddd3b5d42d9521d8849a4375 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 2 Nov 2022 16:56:37 +1100 Subject: Fix BLI_path_parent_dir returning success with a single period as input While relatively harmless, BLI_path_parent_dir wasn't returning failure when passed in "./" which wouldn't succeed. Instead of adding a ".." and normalizing, normalize the path and remove the last directly. This is simpler than inspecting the resulting path to see if normalize removed it or not. Add additional tests which failed previously. --- source/blender/blenlib/tests/BLI_path_util_test.cc | 25 +++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'source/blender/blenlib/tests') diff --git a/source/blender/blenlib/tests/BLI_path_util_test.cc b/source/blender/blenlib/tests/BLI_path_util_test.cc index 379ff432988..293d353efcc 100644 --- a/source/blender/blenlib/tests/BLI_path_util_test.cc +++ b/source/blender/blenlib/tests/BLI_path_util_test.cc @@ -102,8 +102,6 @@ TEST(path_util, Clean_Parent) /** \name Tests for: #BLI_path_parent_dir * \{ */ -TEST(path_util, ParentDir) -{ #define PARENT_DIR(input, output) \ { \ char path[FILE_MAX] = input; \ @@ -118,12 +116,25 @@ TEST(path_util, ParentDir) } \ ((void)0) +TEST(path_util, ParentDir_Simple) +{ PARENT_DIR("/a/b/", "/a/"); PARENT_DIR("/a/b", "/a/"); PARENT_DIR("/a", "/"); +} + +TEST(path_util, ParentDir_NOP) +{ PARENT_DIR("/", "/"); PARENT_DIR("", ""); + PARENT_DIR(".", "."); + PARENT_DIR("./", "./"); + PARENT_DIR(".//", ".//"); + PARENT_DIR("./.", "./."); +} +TEST(path_util, ParentDir_TrailingPeriod) +{ /* Ensure trailing dots aren't confused with parent path. */ PARENT_DIR("/.../.../.../", "/.../.../"); PARENT_DIR("/.../.../...", "/.../.../"); @@ -133,10 +144,18 @@ TEST(path_util, ParentDir) PARENT_DIR("/a./b./c./", "/a./b./"); PARENT_DIR("/a./b./c.", "/a./b./"); +} -#undef PARENT_DIR +TEST(path_util, ParentDir_Complex) +{ + PARENT_DIR("./a/", "./"); + PARENT_DIR("./a", "./"); + PARENT_DIR("../a/", "../"); + PARENT_DIR("../a", "../"); } +#undef PARENT_DIR + /** \} */ /* -------------------------------------------------------------------- */ -- cgit v1.2.3