ctkCmdLineModuleDefaultPathBuilderTest.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) University College London
  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.apache.org/licenses/LICENSE-2.0.txt
  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 <QList>
  16. #include <QString>
  17. #include <QDir>
  18. #include <QCoreApplication>
  19. #include <QProcessEnvironment>
  20. // CTK includes
  21. #include "ctkTest.h"
  22. #include "ctkCmdLineModuleDefaultPathBuilder.h"
  23. // Others
  24. #include <iostream>
  25. /**
  26. * \fn Basic tests for ctkCmdLineModuleDefaultPathBuilderTest.
  27. */
  28. int ctkCmdLineModuleDefaultPathBuilderTest(int argc, char* argv[])
  29. {
  30. QCoreApplication myApp(argc, argv);
  31. if (argc != 2)
  32. {
  33. qDebug() << "Usage: ctkCmdLineModuleDefaultPathBuilderTest directoryThatTheApplicationIsIn";
  34. }
  35. QString runtimeDirectoryName = argv[1];
  36. QDir runtimeDirectory(runtimeDirectoryName);
  37. ctkCmdLineModuleDefaultPathBuilder builder;
  38. QStringList defaultList = builder.getDirectoryList();
  39. if (defaultList.size() != 0)
  40. {
  41. qDebug() << "The default list should be empty";
  42. return EXIT_FAILURE;
  43. }
  44. builder.addCurrentDir();
  45. QStringList result = builder.getDirectoryList();
  46. qDebug() << "1. Built:" << result;
  47. if (result.size() != 1)
  48. {
  49. qDebug() << "Calling addCurrentDir should add one directory to the list of directories.";
  50. return EXIT_FAILURE;
  51. }
  52. builder.addCurrentDir("cli-modules");
  53. result = builder.getDirectoryList();
  54. qDebug() << "2. Built:" << result;
  55. if (result.size() != 2)
  56. {
  57. qDebug() << "Calling addCurrentDir(cli-modules) should add one directory to the list of directories.";
  58. return EXIT_FAILURE;
  59. }
  60. builder.clear();
  61. builder.addApplicationDir();
  62. result = builder.getDirectoryList();
  63. qDebug() << "3. Built:" << result;
  64. if (result.size() != 1)
  65. {
  66. qDebug() << "Calling addApplicationDir should return the application installation dir.";
  67. return EXIT_FAILURE;
  68. }
  69. builder.addApplicationDir("cli-modules");
  70. result = builder.getDirectoryList();
  71. qDebug() << "4. Built:" << result;
  72. if (result.size() != 2)
  73. {
  74. qDebug() << "Calling addApplicationDir(cli-modules) should return 2 directories, one the sub-directory of the other.";
  75. return EXIT_FAILURE;
  76. }
  77. QString tmp1 = result[0];
  78. QString tmp2 = result[1];
  79. QString expected = tmp1 + QDir::separator() + "cli-modules";
  80. if (tmp2 != expected)
  81. {
  82. qDebug() << "Expected " << expected << ", but got " << tmp2;
  83. return EXIT_FAILURE;
  84. }
  85. builder.clear();
  86. builder.addCtkModuleLoadPath();
  87. result = builder.getDirectoryList();
  88. qDebug() << "5. Built:" << result;
  89. // If the environment variable CTK_MODULE_LOAD_PATH exists, it should point to a valid list of directories.
  90. QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
  91. qDebug() << "Environment is:" << env.toStringList();
  92. #ifdef Q_OS_WIN32
  93. QString pathSeparator(";");
  94. #else
  95. QString pathSeparator(":");
  96. #endif
  97. if (env.contains("CTK_MODULE_LOAD_PATH"))
  98. {
  99. QString loadPath = env.value("CTK_MODULE_LOAD_PATH");
  100. QStringList loadPaths = loadPath.split(pathSeparator, QString::SkipEmptyParts);
  101. foreach (QString path, loadPaths)
  102. {
  103. if (!loadPaths.contains(path))
  104. {
  105. qDebug() << "Expecte loadPaths to contain path=" << path;
  106. return EXIT_FAILURE;
  107. }
  108. }
  109. }
  110. builder.clear();
  111. builder.addApplicationDir("blah-blah-blah");
  112. builder.setStrictMode(true);
  113. result = builder.getDirectoryList();
  114. qDebug() << "6. Built:" << result;
  115. if (result.size() != 0)
  116. {
  117. qDebug() << "Calling addApplicationDir(blah-blah-blah) should return nothing unless we do have a sub-folder called blah-blah-blah.";
  118. return EXIT_FAILURE;
  119. }
  120. return EXIT_SUCCESS;
  121. }