cmAddTestCommand.cxx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmAddTestCommand.h"
  4. #include <cm/memory>
  5. #include "cmExecutionStatus.h"
  6. #include "cmMakefile.h"
  7. #include "cmStringAlgorithms.h"
  8. #include "cmTest.h"
  9. #include "cmTestGenerator.h"
  10. static bool cmAddTestCommandHandleNameMode(
  11. std::vector<std::string> const& args, cmExecutionStatus& status);
  12. bool cmAddTestCommand(std::vector<std::string> const& args,
  13. cmExecutionStatus& status)
  14. {
  15. if (!args.empty() && args[0] == "NAME") {
  16. return cmAddTestCommandHandleNameMode(args, status);
  17. }
  18. // First argument is the name of the test Second argument is the name of
  19. // the executable to run (a target or external program) Remaining arguments
  20. // are the arguments to pass to the executable
  21. if (args.size() < 2) {
  22. status.SetError("called with incorrect number of arguments");
  23. return false;
  24. }
  25. cmMakefile& mf = status.GetMakefile();
  26. // Collect the command with arguments.
  27. std::vector<std::string> command(args.begin() + 1, args.end());
  28. // Create the test but add a generator only the first time it is
  29. // seen. This preserves behavior from before test generators.
  30. cmTest* test = mf.GetTest(args[0]);
  31. if (test) {
  32. // If the test was already added by a new-style signature do not
  33. // allow it to be duplicated.
  34. if (!test->GetOldStyle()) {
  35. status.SetError(cmStrCat(" given test name \"", args[0],
  36. "\" which already exists in this directory."));
  37. return false;
  38. }
  39. } else {
  40. test = mf.CreateTest(args[0]);
  41. test->SetOldStyle(true);
  42. mf.AddTestGenerator(cm::make_unique<cmTestGenerator>(test));
  43. }
  44. test->SetCommand(command);
  45. return true;
  46. }
  47. bool cmAddTestCommandHandleNameMode(std::vector<std::string> const& args,
  48. cmExecutionStatus& status)
  49. {
  50. std::string name;
  51. std::vector<std::string> configurations;
  52. std::string working_directory;
  53. std::vector<std::string> command;
  54. bool command_expand_lists = false;
  55. // Read the arguments.
  56. enum Doing
  57. {
  58. DoingName,
  59. DoingCommand,
  60. DoingConfigs,
  61. DoingWorkingDirectory,
  62. DoingNone
  63. };
  64. Doing doing = DoingName;
  65. for (unsigned int i = 1; i < args.size(); ++i) {
  66. if (args[i] == "COMMAND") {
  67. if (!command.empty()) {
  68. status.SetError(" may be given at most one COMMAND.");
  69. return false;
  70. }
  71. doing = DoingCommand;
  72. } else if (args[i] == "CONFIGURATIONS") {
  73. if (!configurations.empty()) {
  74. status.SetError(" may be given at most one set of CONFIGURATIONS.");
  75. return false;
  76. }
  77. doing = DoingConfigs;
  78. } else if (args[i] == "WORKING_DIRECTORY") {
  79. if (!working_directory.empty()) {
  80. status.SetError(" may be given at most one WORKING_DIRECTORY.");
  81. return false;
  82. }
  83. doing = DoingWorkingDirectory;
  84. } else if (args[i] == "COMMAND_EXPAND_LISTS") {
  85. if (command_expand_lists) {
  86. status.SetError(" may be given at most one COMMAND_EXPAND_LISTS.");
  87. return false;
  88. }
  89. command_expand_lists = true;
  90. doing = DoingNone;
  91. } else if (doing == DoingName) {
  92. name = args[i];
  93. doing = DoingNone;
  94. } else if (doing == DoingCommand) {
  95. command.push_back(args[i]);
  96. } else if (doing == DoingConfigs) {
  97. configurations.push_back(args[i]);
  98. } else if (doing == DoingWorkingDirectory) {
  99. working_directory = args[i];
  100. doing = DoingNone;
  101. } else {
  102. status.SetError(cmStrCat(" given unknown argument:\n ", args[i], "\n"));
  103. return false;
  104. }
  105. }
  106. // Require a test name.
  107. if (name.empty()) {
  108. status.SetError(" must be given non-empty NAME.");
  109. return false;
  110. }
  111. // Require a command.
  112. if (command.empty()) {
  113. status.SetError(" must be given non-empty COMMAND.");
  114. return false;
  115. }
  116. cmMakefile& mf = status.GetMakefile();
  117. // Require a unique test name within the directory.
  118. if (mf.GetTest(name)) {
  119. status.SetError(cmStrCat(" given test NAME \"", name,
  120. "\" which already exists in this directory."));
  121. return false;
  122. }
  123. // Add the test.
  124. cmTest* test = mf.CreateTest(name);
  125. test->SetOldStyle(false);
  126. test->SetCommand(command);
  127. if (!working_directory.empty()) {
  128. test->SetProperty("WORKING_DIRECTORY", working_directory.c_str());
  129. }
  130. test->SetCommandExpandLists(command_expand_lists);
  131. mf.AddTestGenerator(cm::make_unique<cmTestGenerator>(test, configurations));
  132. return true;
  133. }