From ce951b29ec4e71f6bb09b8e2f46b789d5ac0780e Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Tue, 26 Sep 2017 22:25:53 +0200 Subject: [PATCH] Made tl::parent_path require existing parent - otherwise the previous implementation returned true always. --- src/tl/tl/tlFileUtils.cc | 7 ++++++- src/tl/unit_tests/tlFileUtils.cc | 18 +++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/tl/tl/tlFileUtils.cc b/src/tl/tl/tlFileUtils.cc index 9ae545118..59514ec03 100644 --- a/src/tl/tl/tlFileUtils.cc +++ b/src/tl/tl/tlFileUtils.cc @@ -34,13 +34,18 @@ bool is_parent_path (const QString &parent, const QString &path) { QFileInfo parent_info (parent); + if (! parent_info.exists ()) { + // If the parent path does not exist, we always return false. This cannot be a parent. + return false; + } + QFileInfo path_info (path); while (parent_info != path_info) { - path_info = path_info.path (); if (path_info.isRoot ()) { return false; } + path_info = path_info.path (); } return true; diff --git a/src/tl/unit_tests/tlFileUtils.cc b/src/tl/unit_tests/tlFileUtils.cc index 2991c0605..c73b17195 100644 --- a/src/tl/unit_tests/tlFileUtils.cc +++ b/src/tl/unit_tests/tlFileUtils.cc @@ -30,11 +30,19 @@ TEST (1) { - EXPECT_EQ (tl::is_parent_path (std::string ("/home"), "/home/matthias"), true); - EXPECT_EQ (tl::is_parent_path (std::string ("/home"), "/home"), true); - EXPECT_EQ (tl::is_parent_path (std::string (""), ""), true); - EXPECT_EQ (tl::is_parent_path (std::string ("/opt/klayout"), "/home/matthias"), false); - EXPECT_EQ (tl::is_parent_path (std::string ("/home/klayout"), "/home/matthias"), false); + EXPECT_EQ (tl::is_parent_path (std::string ("."), "./doesnotexist"), true); + EXPECT_EQ (tl::is_parent_path (std::string ("./doesnotexist"), "./alsodoesnotexist"), false); + EXPECT_EQ (tl::is_parent_path (std::string ("."), "."), true); + + QString p = QFileInfo (tl::to_qstring (tmp_file ())).path (); + QDir (p).mkdir (QString::fromUtf8 ("x")); + QDir (p).mkdir (QString::fromUtf8 ("y")); + EXPECT_EQ (tl::is_parent_path (tl::to_string (p), tl::to_string (p)), true); + EXPECT_EQ (tl::is_parent_path (tl::to_string (p), tl::to_string (p) + "/x"), true); + EXPECT_EQ (tl::is_parent_path (tl::to_string (p) + "/x", tl::to_string (p) + "/x"), true); + EXPECT_EQ (tl::is_parent_path (tl::to_string (p) + "/x", tl::to_string (p) + "/y"), false); + EXPECT_EQ (tl::is_parent_path (tl::to_string (QDir::root ().absolutePath ()), tl::to_string (p) + "/y"), true); + EXPECT_EQ (tl::is_parent_path (tl::to_string (QDir::root ().absolutePath ()), tl::to_string (p)), true); } TEST (2)