cmAddTestCommand.cxx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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;
  25. command.insert(command.end(), args.begin() + 1, args.end());
  26. // Create the test but add a generator only the first time it is
  27. // seen. This preserves behavior from before test generators.
  28. cmTest* test = this->Makefile->GetTest(args[0]);
  29. if (test) {
  30. // If the test was already added by a new-style signature do not
  31. // allow it to be duplicated.
  32. if (!test->GetOldStyle()) {
  33. std::ostringstream e;
  34. e << " given test name \"" << args[0]
  35. << "\" which already exists in this directory.";
  36. this->SetError(e.str());
  37. return false;
  38. }
  39. } else {
  40. test = this->Makefile->CreateTest(args[0]);
  41. test->SetOldStyle(true);
  42. this->Makefile->AddTestGenerator(new cmTestGenerator(test));
  43. }
  44. test->SetCommand(command);
  45. return true;
  46. }
  47. bool cmAddTestCommand::HandleNameMode(std::vector<std::string> const& args)
  48. {
  49. std::string name;
  50. std::vector<std::string> configurations;
  51. std::string working_directory;
  52. std::vector<std::string> command;
  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 (doing == DoingName) {
  83. name = args[i];
  84. doing = DoingNone;
  85. } else if (doing == DoingCommand) {
  86. command.push_back(args[i]);
  87. } else if (doing == DoingConfigs) {
  88. configurations.push_back(args[i]);
  89. } else if (doing == DoingWorkingDirectory) {
  90. working_directory = args[i];
  91. doing = DoingNone;
  92. } else {
  93. std::ostringstream e;
  94. e << " given unknown argument:\n " << args[i] << "\n";
  95. this->SetError(e.str());
  96. return false;
  97. }
  98. }
  99. // Require a test name.
  100. if (name.empty()) {
  101. this->SetError(" must be given non-empty NAME.");
  102. return false;
  103. }
  104. // Require a command.
  105. if (command.empty()) {
  106. this->SetError(" must be given non-empty COMMAND.");
  107. return false;
  108. }
  109. // Require a unique test name within the directory.
  110. if (this->Makefile->GetTest(name)) {
  111. std::ostringstream e;
  112. e << " given test NAME \"" << name
  113. << "\" which already exists in this directory.";
  114. this->SetError(e.str());
  115. return false;
  116. }
  117. // Add the test.
  118. cmTest* test = this->Makefile->CreateTest(name);
  119. test->SetOldStyle(false);
  120. test->SetCommand(command);
  121. if (!working_directory.empty()) {
  122. test->SetProperty("WORKING_DIRECTORY", working_directory.c_str());
  123. }
  124. this->Makefile->AddTestGenerator(new cmTestGenerator(test, configurations));
  125. return true;
  126. }