ctkCmdLineModuleDefaultPathBuilderTest.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.build();
  39. if (defaultList.size() != 0)
  40. {
  41. qDebug() << "The default list should be empty";
  42. return EXIT_FAILURE;
  43. }
  44. builder.setLoadFromCurrentDir(true);
  45. QStringList result = builder.build();
  46. qDebug() << "1. Built:" << result;
  47. if (result.size() != 2)
  48. {
  49. qDebug() << "The flag setLoadFromCurrentDir enables scanning of the current working directory plus the subfolder cli-modules";
  50. return EXIT_FAILURE;
  51. }
  52. builder.setLoadFromApplicationDir(true);
  53. result = builder.build();
  54. qDebug() << "2. Built:" << result;
  55. if (result.size() != 4)
  56. {
  57. qDebug() << "The flag setLoadFromApplicationDir enables scanning of the current installation directory plus the subfolder cli-modules";
  58. return EXIT_FAILURE;
  59. }
  60. builder.setLoadFromCurrentDir(false);
  61. result = builder.build();
  62. qDebug() << "3. Built:" << result;
  63. if (!result.contains(runtimeDirectory.absolutePath()))
  64. {
  65. qDebug() << "Loading from the application diretory (where THIS application is located), should produce the same path as passed in via the command line argument ${CTK_CMAKE_RUNTIME_OUTPUT_DIRECTORY}";
  66. }
  67. builder.setLoadFromHomeDir(true);
  68. result = builder.build();
  69. qDebug() << "4. Built:" << result;
  70. if (result.size() != 4)
  71. {
  72. qDebug() << "Should now be loading from applicationDir, applicationDir/cli-modules, homeDir, homeDir/cli-modules";
  73. return EXIT_FAILURE;
  74. }
  75. builder.setLoadFromCtkModuleLoadPath(true);
  76. result = builder.build();
  77. qDebug() << "5. Built:" << result;
  78. // If the environment variable CTK_MODULE_LOAD_PATH exists, it should point to a valid directory.
  79. // If it does not exist, then the list should not change.
  80. QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
  81. qDebug() << "Environment is:" << env.toStringList();
  82. if (env.contains("CTK_MODULE_LOAD_PATH"))
  83. {
  84. QDir loadDir(env.value("CTK_MODULE_LOAD_PATH"));
  85. qDebug() << "CTK_MODULE_LOAD_PATH does exist, and is set to:" << env.value("CTK_MODULE_LOAD_PATH") << ", and isExists() returns " << loadDir.exists();
  86. if (loadDir.exists() && result.size() != 5)
  87. {
  88. qDebug() << "Environment variable CTK_MODULE_LOAD_PATH did exist and is valid, so there should be 5 entries";
  89. return EXIT_FAILURE;
  90. }
  91. else if (!loadDir.exists() && result.size() != 4)
  92. {
  93. qDebug() << "Environment variable CTK_MODULE_LOAD_PATH did exist but is invalid, so there should be 4 entries";
  94. return EXIT_FAILURE;
  95. }
  96. }
  97. else if (result.size() != 4)
  98. {
  99. qDebug() << "Environment variable CTK_MODULE_LOAD_PATH did not exist, so there should still be 4 entries as previous test";
  100. return EXIT_FAILURE;
  101. }
  102. return EXIT_SUCCESS;
  103. }