소스 검색

BUG: ctkUtils: Improve removeDirRecursively test to consider relative path

The test illustrates the problem and is failing.
Jean-Christophe Fillion-Robin 7 년 전
부모
커밋
e2fe98a44c
1개의 변경된 파일49개의 추가작업 그리고 26개의 파일을 삭제
  1. 49 26
      Libs/Core/Testing/Cpp/ctkUtilsTest4.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;
 }