ctkUtilsTest4.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "ctkUtils.h"
  21. // STD includes
  22. #include <cstdlib>
  23. #include <iostream>
  24. namespace
  25. {
  26. //-----------------------------------------------------------------------------
  27. bool createFile(int line, const QDir& dir, const QString& relativePath, const QString& fileName)
  28. {
  29. QDir newDir(dir);
  30. newDir.mkpath(relativePath);
  31. newDir.cd(relativePath);
  32. QString filePath = QFileInfo(newDir, fileName).filePath();
  33. QFile file(filePath);
  34. file.open(QIODevice::Text | QIODevice::WriteOnly);
  35. QTextStream out(&file);
  36. out << "Generated by ctkUtilsTest4" << endl;
  37. file.close();
  38. if (!QFile::exists(filePath))
  39. {
  40. std::cerr << "Line " << line << " - Failed to create file" << qPrintable(filePath) << std::endl;
  41. return false;
  42. }
  43. return true;
  44. }
  45. } // end of anonymous namespace
  46. //-----------------------------------------------------------------------------
  47. int ctkUtilsTest4(int argc, char * argv [] )
  48. {
  49. Q_UNUSED(argc);
  50. Q_UNUSED(argv);
  51. // --------------------------------------------------------------------------
  52. // Test removeDirRecursively(const QString & dirName);
  53. // --------------------------------------------------------------------------
  54. QDir tmp = QDir::temp();
  55. QString temporaryDirName =
  56. QString("ctkUtilsTest4.%1").arg(QTime::currentTime().toString("hhmmsszzz"));
  57. // Attempt to delete nonexistent relative directory
  58. QString nonexistentRelativeDirPath = temporaryDirName;
  59. if (ctk::removeDirRecursively(nonexistentRelativeDirPath))
  60. {
  61. std::cerr << "Line " << __LINE__ << " - Problem with ctk::removeDirRecursively() !"
  62. << " - It should fail to delete nonexistent directory: " <<
  63. qPrintable(nonexistentRelativeDirPath)<< std::endl;
  64. return EXIT_FAILURE;
  65. }
  66. // Attempt to delete nonexistent absolute directory
  67. QString nonexistentAbsoluteDirPath = QFileInfo(tmp, temporaryDirName).dir().absolutePath();
  68. if (ctk::removeDirRecursively(nonexistentAbsoluteDirPath))
  69. {
  70. std::cerr << "Line " << __LINE__ << " - Problem with ctk::removeDirRecursively() !"
  71. << " - It should fail to delete nonexistent directory: " <<
  72. qPrintable(nonexistentAbsoluteDirPath)<< std::endl;
  73. return EXIT_FAILURE;
  74. }
  75. // Create a directory structure
  76. tmp.mkdir(temporaryDirName);
  77. tmp.cd(temporaryDirName);
  78. if (!createFile(__LINE__, tmp, "foo", "a.txt"))
  79. {
  80. return EXIT_FAILURE;
  81. }
  82. if (!createFile(__LINE__, tmp, "foo/bar", "b.txt"))
  83. {
  84. return EXIT_FAILURE;
  85. }
  86. if (!createFile(__LINE__, tmp, "foo/zoo", "c.txt"))
  87. {
  88. return EXIT_FAILURE;
  89. }
  90. if (!ctk::removeDirRecursively(tmp.absolutePath()))
  91. {
  92. std::cerr << "Line " << __LINE__ << " - Problem with ctk::removeDirRecursively()"
  93. << " - Failed to delete directory:" << qPrintable(tmp.absolutePath()) << std::endl;
  94. return EXIT_FAILURE;
  95. }
  96. return EXIT_SUCCESS;
  97. }