Browse Source

Merge pull request #754 from jcfr/fix-ctkutils-removeDirRecursively

Fix ctkutils remove dir recursively
Jean-Christophe Fillion-Robin 7 years ago
parent
commit
4d758c6fc2
2 changed files with 52 additions and 28 deletions
  1. 49 26
      Libs/Core/Testing/Cpp/ctkUtilsTest4.cpp
  2. 3 2
      Libs/Core/ctkUtils.cpp

+ 49 - 26
Libs/Core/Testing/Cpp/ctkUtilsTest4.cpp

@@ -25,6 +25,8 @@
 #include <QTime>
 
 // CTK includes
+#include "ctkCoreTestingMacros.h"
+#include "ctkScopedCurrentDir.h"
 #include "ctkUtils.h"
 
 // STD includes
@@ -34,7 +36,7 @@
 namespace
 {
 //-----------------------------------------------------------------------------
-bool createFile(int line, const QDir& dir, const QString& relativePath, const QString& fileName)
+int createFile(int line, const QDir& dir, const QString& relativePath, const QString& fileName)
 {
   QDir newDir(dir);
   newDir.mkpath(relativePath);
@@ -49,10 +51,10 @@ bool createFile(int line, const QDir& dir, const QString& relativePath, const QS
   if (!QFile::exists(filePath))
     {
     std::cerr << "Line " << line << " - Failed to create file" << qPrintable(filePath) << std::endl;
-    return false;
+    return EXIT_FAILURE;
     }
 
-  return true;
+  return EXIT_SUCCESS;
 }
 
 } // end of anonymous namespace
@@ -74,6 +76,7 @@ int ctkUtilsTest4(int argc, char * argv [] )
 
   // Attempt to delete nonexistent relative directory
   QString nonexistentRelativeDirPath = temporaryDirName;
+  CHECK_BOOL(QDir(nonexistentRelativeDirPath).exists(), false);
   if (ctk::removeDirRecursively(nonexistentRelativeDirPath))
     {
     std::cerr << "Line " << __LINE__ << " - Problem with ctk::removeDirRecursively() !"
@@ -83,7 +86,8 @@ int ctkUtilsTest4(int argc, char * argv [] )
     }
 
   // Attempt to delete nonexistent absolute directory
-  QString nonexistentAbsoluteDirPath = QFileInfo(tmp, temporaryDirName).dir().absolutePath();
+  QString nonexistentAbsoluteDirPath = QFileInfo(tmp, temporaryDirName).absoluteFilePath();
+  CHECK_BOOL(QDir(nonexistentAbsoluteDirPath).exists(), false);
   if (ctk::removeDirRecursively(nonexistentAbsoluteDirPath))
     {
     std::cerr << "Line " << __LINE__ << " - Problem with ctk::removeDirRecursively() !"
@@ -92,28 +96,47 @@ int ctkUtilsTest4(int argc, char * argv [] )
     return EXIT_FAILURE;
     }
 
-  // Create a directory structure
-  tmp.mkdir(temporaryDirName);
-  tmp.cd(temporaryDirName);
-  if (!createFile(__LINE__, tmp, "foo", "a.txt"))
-    {
-    return EXIT_FAILURE;
-    }
-  if (!createFile(__LINE__, tmp, "foo/bar", "b.txt"))
-    {
-    return EXIT_FAILURE;
-    }
-  if (!createFile(__LINE__, tmp, "foo/zoo", "c.txt"))
-    {
-    return EXIT_FAILURE;
-    }
-
-  if (!ctk::removeDirRecursively(tmp.absolutePath()))
-    {
-    std::cerr << "Line " << __LINE__ << " - Problem with ctk::removeDirRecursively()"
-              << " - Failed to delete directory:" << qPrintable(tmp.absolutePath()) << std::endl;
-    return EXIT_FAILURE;
-    }
+  // Absolute path
+  {
+    // Create a directory structure
+    CHECK_BOOL(tmp.mkdir(temporaryDirName), true);
+    QDir caseTmp = QDir(tmp);
+    CHECK_BOOL(caseTmp.cd(temporaryDirName), true);
+    CHECK_EXIT_SUCCESS(createFile(__LINE__, caseTmp, "foo", "a.txt"));
+    CHECK_EXIT_SUCCESS(createFile(__LINE__, caseTmp, "foo/bar", "b.txt"));
+    CHECK_EXIT_SUCCESS(createFile(__LINE__, caseTmp, "foo/zoo", "c.txt"));
+
+    if (!ctk::removeDirRecursively(caseTmp.absolutePath()))
+      {
+      std::cerr << "Line " << __LINE__ << " - Problem with ctk::removeDirRecursively()"
+                << " - Failed to delete directory:" << qPrintable(caseTmp.absolutePath()) << std::endl;
+      return EXIT_FAILURE;
+      }
+    CHECK_BOOL(caseTmp.exists(), false);
+  }
+
+  // Relative path
+  {
+    // Create a directory structure
+    CHECK_BOOL(tmp.mkdir(temporaryDirName), true);
+    QDir caseTmp = QDir(tmp);
+    CHECK_BOOL(caseTmp.cd(temporaryDirName), true);
+    CHECK_EXIT_SUCCESS(createFile(__LINE__, caseTmp, "foo", "a.txt"));
+    CHECK_EXIT_SUCCESS(createFile(__LINE__, caseTmp, "foo/bar", "b.txt"));
+    CHECK_EXIT_SUCCESS(createFile(__LINE__, caseTmp, "foo/zoo", "c.txt"));
+
+    ctkScopedCurrentDir currentDir(tmp.path());
+
+    if (!ctk::removeDirRecursively(temporaryDirName))
+      {
+      std::cerr << "Line " << __LINE__ << " - Problem with ctk::removeDirRecursively()"
+                << " - Failed to delete directory:" << qPrintable(temporaryDirName)
+                << " - Current directory:" << qPrintable(QDir::currentPath())
+                << std::endl;
+      return EXIT_FAILURE;
+      }
+    CHECK_BOOL(caseTmp.exists(), false);
+  }
 
   return EXIT_SUCCESS;
 }

+ 3 - 2
Libs/Core/ctkUtils.cpp

@@ -308,7 +308,7 @@ bool ctk::removeDirRecursively(const QString & dirName)
   bool result = false;
   QDir dir(dirName);
 
-  if (dir.exists(dirName))
+  if (dir.exists())
     {
     foreach (QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden  | QDir::AllDirs | QDir::Files, QDir::DirsFirst))
       {
@@ -326,7 +326,8 @@ bool ctk::removeDirRecursively(const QString & dirName)
         return result;
         }
       }
-    result = dir.rmdir(dirName);
+    QDir parentDir(QFileInfo(dirName).absolutePath());
+    result = parentDir.rmdir(dirName);
     }
 
   return result;