cmAddTestCommand.cxx 4.5 KB

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