From 0ae286be03e59a0ec7f4950ca604a99bbed1b95b Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 17 May 2023 12:57:59 +1000 Subject: [PATCH] Fix trailing slashes causing normalized path comparison to fail --- source/blender/blenlib/intern/path_util.c | 4 ++++ .../blenlib/tests/BLI_path_util_test.cc | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c index eb29f6e3838..82c74462ee3 100644 --- a/source/blender/blenlib/intern/path_util.c +++ b/source/blender/blenlib/intern/path_util.c @@ -1947,6 +1947,10 @@ int BLI_path_cmp_normalized(const char *p1, const char *p2) BLI_path_slash_native(norm_p1); BLI_path_slash_native(norm_p2); + /* One of the paths ending with a slash does not make them different, strip both. */ + BLI_path_slash_rstrip(norm_p1); + BLI_path_slash_rstrip(norm_p2); + BLI_path_normalize(norm_p1); BLI_path_normalize(norm_p2); diff --git a/source/blender/blenlib/tests/BLI_path_util_test.cc b/source/blender/blenlib/tests/BLI_path_util_test.cc index e7940d69be8..3d5b7d2e28d 100644 --- a/source/blender/blenlib/tests/BLI_path_util_test.cc +++ b/source/blender/blenlib/tests/BLI_path_util_test.cc @@ -155,6 +155,26 @@ TEST(path_util, Normalize_UnbalancedRelativeTrailing) /** \} */ +/* -------------------------------------------------------------------- */ +/** \name Tests for: #BLI_path_cmp_normalized + * + * \note #BLI_path_normalize tests handle most of the corner cases. + * \{ */ + +TEST(path_util, CompareNormalized) +{ + /* Trailing slash should not matter. */ + EXPECT_EQ(BLI_path_cmp_normalized("/tmp/", "/tmp"), 0); + /* Slash direction should not matter. */ + EXPECT_EQ(BLI_path_cmp_normalized("\\tmp\\", "/tmp/"), 0); + /* Empty paths should be supported. */ + EXPECT_EQ(BLI_path_cmp_normalized("", ""), 0); + + EXPECT_NE(BLI_path_cmp_normalized("A", "B"), 0); +} + +/** \} */ + /* -------------------------------------------------------------------- */ /** \name Tests for: #BLI_path_parent_dir * \{ */