ctkUtilsTest4.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.commontk.org/LICENSE
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QDebug>
  16. #include <QDir>
  17. #include <QTextStream>
  18. #include <QTime>
  19. // CTK includes
  20. #include "ctkCoreTestingMacros.h"
  21. #include "ctkScopedCurrentDir.h"
  22. #include "ctkUtils.h"
  23. // STD includes
  24. #include <cstdlib>
  25. #include <iostream>
  26. namespace
  27. {
  28. //-----------------------------------------------------------------------------
  29. int createFile(int line, const QDir& dir, const QString& relativePath, const QString& fileName)
  30. {
  31. QDir newDir(dir);
  32. newDir.mkpath(relativePath);
  33. newDir.cd(relativePath);
  34. QString filePath = QFileInfo(newDir, fileName).filePath();
  35. QFile file(filePath);
  36. file.open(QIODevice::Text | QIODevice::WriteOnly);
  37. QTextStream out(&file);
  38. out << "Generated by ctkUtilsTest4" << endl;
  39. file.close();
  40. if (!QFile::exists(filePath))
  41. {
  42. std::cerr << "Line " << line << " - Failed to create file" << qPrintable(filePath) << std::endl;
  43. return EXIT_FAILURE;
  44. }
  45. return EXIT_SUCCESS;
  46. }
  47. } // end of anonymous namespace
  48. //-----------------------------------------------------------------------------
  49. int ctkUtilsTest4(int argc, char * argv [] )
  50. {
  51. Q_UNUSED(argc);
  52. Q_UNUSED(argv);
  53. // --------------------------------------------------------------------------
  54. // Test removeDirRecursively(const QString & dirName);
  55. // --------------------------------------------------------------------------
  56. QDir tmp = QDir::temp();
  57. QString temporaryDirName =
  58. QString("ctkUtilsTest4.%1").arg(QTime::currentTime().toString("hhmmsszzz"));
  59. // Attempt to delete nonexistent relative directory
  60. QString nonexistentRelativeDirPath = temporaryDirName;
  61. CHECK_BOOL(QDir(nonexistentRelativeDirPath).exists(), false);
  62. if (ctk::removeDirRecursively(nonexistentRelativeDirPath))
  63. {
  64. std::cerr << "Line " << __LINE__ << " - Problem with ctk::removeDirRecursively() !"
  65. << " - It should fail to delete nonexistent directory: " <<
  66. qPrintable(nonexistentRelativeDirPath)<< std::endl;
  67. return EXIT_FAILURE;
  68. }
  69. // Attempt to delete nonexistent absolute directory
  70. QString nonexistentAbsoluteDirPath = QFileInfo(tmp, temporaryDirName).absoluteFilePath();
  71. CHECK_BOOL(QDir(nonexistentAbsoluteDirPath).exists(), false);
  72. if (ctk::removeDirRecursively(nonexistentAbsoluteDirPath))
  73. {
  74. std::cerr << "Line " << __LINE__ << " - Problem with ctk::removeDirRecursively() !"
  75. << " - It should fail to delete nonexistent directory: " <<
  76. qPrintable(nonexistentAbsoluteDirPath)<< std::endl;
  77. return EXIT_FAILURE;
  78. }
  79. // Absolute path
  80. {
  81. // Create a directory structure
  82. CHECK_BOOL(tmp.mkdir(temporaryDirName), true);
  83. QDir caseTmp = QDir(tmp);
  84. CHECK_BOOL(caseTmp.cd(temporaryDirName), true);
  85. CHECK_EXIT_SUCCESS(createFile(__LINE__, caseTmp, "foo", "a.txt"));
  86. CHECK_EXIT_SUCCESS(createFile(__LINE__, caseTmp, "foo/bar", "b.txt"));
  87. CHECK_EXIT_SUCCESS(createFile(__LINE__, caseTmp, "foo/zoo", "c.txt"));
  88. if (!ctk::removeDirRecursively(caseTmp.absolutePath()))
  89. {
  90. std::cerr << "Line " << __LINE__ << " - Problem with ctk::removeDirRecursively()"
  91. << " - Failed to delete directory:" << qPrintable(caseTmp.absolutePath()) << std::endl;
  92. return EXIT_FAILURE;
  93. }
  94. CHECK_BOOL(caseTmp.exists(), false);
  95. }
  96. // Relative path
  97. {
  98. // Create a directory structure
  99. CHECK_BOOL(tmp.mkdir(temporaryDirName), true);
  100. QDir caseTmp = QDir(tmp);
  101. CHECK_BOOL(caseTmp.cd(temporaryDirName), true);
  102. CHECK_EXIT_SUCCESS(createFile(__LINE__, caseTmp, "foo", "a.txt"));
  103. CHECK_EXIT_SUCCESS(createFile(__LINE__, caseTmp, "foo/bar", "b.txt"));
  104. CHECK_EXIT_SUCCESS(createFile(__LINE__, caseTmp, "foo/zoo", "c.txt"));
  105. ctkScopedCurrentDir currentDir(tmp.path());
  106. if (!ctk::removeDirRecursively(temporaryDirName))
  107. {
  108. std::cerr << "Line " << __LINE__ << " - Problem with ctk::removeDirRecursively()"
  109. << " - Failed to delete directory:" << qPrintable(temporaryDirName)
  110. << " - Current directory:" << qPrintable(QDir::currentPath())
  111. << std::endl;
  112. return EXIT_FAILURE;
  113. }
  114. CHECK_BOOL(caseTmp.exists(), false);
  115. }
  116. return EXIT_SUCCESS;
  117. }