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