ctkCmdLineModuleDefaultPathBuilderTest.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 <QApplication>
  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. QApplication 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. builder.clear();
  78. builder.addCtkModuleLoadPathDir();
  79. result = builder.getDirectoryList();
  80. qDebug() << "5. Built:" << result;
  81. // If the environment variable CTK_MODULE_LOAD_PATH exists, it should point to a valid directory.
  82. // If it does not exist, then the list should not change.
  83. QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
  84. qDebug() << "Environment is:" << env.toStringList();
  85. if (env.contains("CTK_MODULE_LOAD_PATH"))
  86. {
  87. QDir loadDir(env.value("CTK_MODULE_LOAD_PATH"));
  88. qDebug() << "CTK_MODULE_LOAD_PATH does exist, and is set to:" << env.value("CTK_MODULE_LOAD_PATH") << ", and isExists() returns " << loadDir.exists();
  89. if (loadDir.exists() && result.size() != 1)
  90. {
  91. qDebug() << "Environment variable CTK_MODULE_LOAD_PATH did exist and is valid, so there should be 1 entries";
  92. return EXIT_FAILURE;
  93. }
  94. else if (!loadDir.exists() && result.size() != 0)
  95. {
  96. qDebug() << "Environment variable CTK_MODULE_LOAD_PATH did exist but is invalid, so there should be 0 entries";
  97. return EXIT_FAILURE;
  98. }
  99. }
  100. else if (result.size() != 0)
  101. {
  102. qDebug() << "Environment variable CTK_MODULE_LOAD_PATH did not exist, so there should still be 0 entries.";
  103. return EXIT_FAILURE;
  104. }
  105. return EXIT_SUCCESS;
  106. }