ctkUtilsCopyDirRecursivelyTest1.cpp 4.3 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 "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 ctkUtilsCopyDirRecursivelyTest1" << 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 ctkUtilsCopyDirRecursivelyTest1(int argc, char * argv [] )
  48. {
  49. Q_UNUSED(argc);
  50. Q_UNUSED(argv);
  51. // --------------------------------------------------------------------------
  52. // Test copyDirRecursively(const QString &srcPath, const QString &dstPath);
  53. // --------------------------------------------------------------------------
  54. QDir tmp = QDir::temp();
  55. QString temporaryDirName =
  56. QString("ctkUtilsCopyDirRecursivelyTest1.%1").arg(QTime::currentTime().toString("hhmmsszzz"));
  57. {
  58. // Attempt to copy nonexistent relative directory
  59. QString nonexistentRelativeDirPath = temporaryDirName;
  60. if (ctk::copyDirRecursively(nonexistentRelativeDirPath, tmp.absolutePath()))
  61. {
  62. std::cerr << "Line " << __LINE__ << " - Problem with ctk::copyDirRecursively() !"
  63. << " - It should fail to copy nonexistent directory: " <<
  64. qPrintable(nonexistentRelativeDirPath)<< std::endl;
  65. return EXIT_FAILURE;
  66. }
  67. }
  68. {
  69. // Attempt to copy nonexistent absolute directory
  70. QString nonexistentAbsoluteDirPath = tmp.absolutePath() + "/" + temporaryDirName;
  71. if (ctk::copyDirRecursively(nonexistentAbsoluteDirPath, tmp.absolutePath()))
  72. {
  73. std::cerr << "Line " << __LINE__ << " - Problem with ctk::copyDirRecursively() !"
  74. << " - It should fail to delete nonexistent directory: " <<
  75. qPrintable(nonexistentAbsoluteDirPath)<< std::endl;
  76. return EXIT_FAILURE;
  77. }
  78. }
  79. // Create a directory structure
  80. tmp.mkdir(temporaryDirName);
  81. tmp.cd(temporaryDirName);
  82. if (!createFile(__LINE__, tmp, "foo", "a.txt"))
  83. {
  84. return EXIT_FAILURE;
  85. }
  86. if (!createFile(__LINE__, tmp, "foo/bar", "b.txt"))
  87. {
  88. return EXIT_FAILURE;
  89. }
  90. if (!createFile(__LINE__, tmp, "foo/zoo", "c.txt"))
  91. {
  92. return EXIT_FAILURE;
  93. }
  94. {
  95. QString srcPath(tmp.path() + "/foo");
  96. QString destPath(tmp.path() + "/dest");
  97. if (!ctk::copyDirRecursively(srcPath, destPath))
  98. {
  99. std::cerr << "Line " << __LINE__ << " - Problem with ctk::copyDirRecursively()"
  100. << " - Failed to copy directory: " << qPrintable(srcPath)
  101. << "into" << qPrintable(destPath) << std::endl;
  102. return EXIT_FAILURE;
  103. }
  104. }
  105. {
  106. // Atempt to copy a directory into itself
  107. QString srcPath(tmp.path());
  108. QString destPath(tmp.path() + "/dest");
  109. if (ctk::copyDirRecursively(srcPath, destPath))
  110. {
  111. std::cerr << "Line " << __LINE__ << " - Problem with ctk::copyDirRecursively()"
  112. << " - Copy of directory: " << qPrintable(srcPath)
  113. << "into itself " << qPrintable(destPath)
  114. << " should fail !" << std::endl;
  115. return EXIT_FAILURE;
  116. }
  117. }
  118. return EXIT_SUCCESS;
  119. }