cmAddTestCommand.cxx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "cmExecutionStatus.h"
  5. #include "cmMakefile.h"
  6. #include "cmStringAlgorithms.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. status.SetError(cmStrCat(" given test name \"", args[0],
  35. "\" which already exists in this directory."));
  36. return false;
  37. }
  38. } else {
  39. test = mf.CreateTest(args[0]);
  40. test->SetOldStyle(true);
  41. mf.AddTestGenerator(new cmTestGenerator(test));
  42. }
  43. test->SetCommand(command);
  44. return true;
  45. }
  46. bool cmAddTestCommandHandleNameMode(std::vector<std::string> const& args,
  47. cmExecutionStatus& status)
  48. {
  49. std::string name;
  50. std::vector<std::string> configurations;
  51. std::string working_directory;
  52. std::vector<std::string> command;
  53. bool command_expand_lists = false;
  54. // Read the arguments.
  55. enum Doing
  56. {
  57. DoingName,
  58. DoingCommand,
  59. DoingConfigs,
  60. DoingWorkingDirectory,
  61. DoingNone
  62. };
  63. Doing doing = DoingName;
  64. for (unsigned int i = 1; i < args.size(); ++i) {
  65. if (args[i] == "COMMAND") {
  66. if (!command.empty()) {
  67. status.SetError(" may be given at most one COMMAND.");
  68. return false;
  69. }
  70. doing = DoingCommand;
  71. } else if (args[i] == "CONFIGURATIONS") {
  72. if (!configurations.empty()) {
  73. status.SetError(" may be given at most one set of CONFIGURATIONS.");
  74. return false;
  75. }
  76. doing = DoingConfigs;
  77. } else if (args[i] == "WORKING_DIRECTORY") {
  78. if (!working_directory.empty()) {
  79. status.SetError(" may be given at most one WORKING_DIRECTORY.");
  80. return false;
  81. }
  82. doing = DoingWorkingDirectory;
  83. } else if (args[i] == "COMMAND_EXPAND_LISTS") {
  84. if (command_expand_lists) {
  85. status.SetError(" may be given at most one COMMAND_EXPAND_LISTS.");
  86. return false;
  87. }
  88. command_expand_lists = true;
  89. doing = DoingNone;
  90. } else if (doing == DoingName) {
  91. name = args[i];
  92. doing = DoingNone;
  93. } else if (doing == DoingCommand) {
  94. command.push_back(args[i]);
  95. } else if (doing == DoingConfigs) {
  96. configurations.push_back(args[i]);
  97. } else if (doing == DoingWorkingDirectory) {
  98. working_directory = args[i];
  99. doing = DoingNone;
  100. } else {
  101. status.SetError(cmStrCat(" given unknown argument:\n ", args[i], "\n"));
  102. return false;
  103. }
  104. }
  105. // Require a test name.
  106. if (name.empty()) {
  107. status.SetError(" must be given non-empty NAME.");
  108. return false;
  109. }
  110. // Require a command.
  111. if (command.empty()) {
  112. status.SetError(" must be given non-empty COMMAND.");
  113. return false;
  114. }
  115. cmMakefile& mf = status.GetMakefile();
  116. // Require a unique test name within the directory.
  117. if (mf.GetTest(name)) {
  118. status.SetError(cmStrCat(" given test NAME \"", name,
  119. "\" which already exists in this directory."));
  120. return false;
  121. }
  122. // Add the test.
  123. cmTest* test = mf.CreateTest(name);
  124. test->SetOldStyle(false);
  125. test->SetCommand(command);
  126. if (!working_directory.empty()) {
  127. test->SetProperty("WORKING_DIRECTORY", working_directory.c_str());
  128. }
  129. test->SetCommandExpandLists(command_expand_lists);
  130. mf.AddTestGenerator(new cmTestGenerator(test, configurations));
  131. return true;
  132. }