Made tl::parent_path require existing parent - otherwise the previous implementation returned true always.

This commit is contained in:
Matthias Koefferlein 2017-09-26 22:25:53 +02:00
parent 7504293c28
commit ce951b29ec
2 changed files with 19 additions and 6 deletions

View File

@ -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;

View File

@ -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)