cmAddTestCommand.cxx 4.34 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
3 4
#include "cmAddTestCommand.h"

5
#include <sstream>
6

7
#include "cmMakefile.h"
8
#include "cmTest.h"
9 10 11
#include "cmTestGenerator.h"

class cmExecutionStatus;
12

13
// cmExecutableCommand
14 15
bool cmAddTestCommand::InitialPass(std::vector<std::string> const& args,
                                   cmExecutionStatus&)
16
{
17
  if (!args.empty() && args[0] == "NAME") {
18
    return this->HandleNameMode(args);
19
  }
20

Ken Martin's avatar
Ken Martin committed
21 22 23
  // First argument is the name of the test Second argument is the name of
  // the executable to run (a target or external program) Remaining arguments
  // are the arguments to pass to the executable
24
  if (args.size() < 2) {
25 26
    this->SetError("called with incorrect number of arguments");
    return false;
27
  }
28

29
  // Collect the command with arguments.
30
  std::vector<std::string> command(args.begin() + 1, args.end());
31

32 33
  // Create the test but add a generator only the first time it is
  // seen.  This preserves behavior from before test generators.
34
  cmTest* test = this->Makefile->GetTest(args[0]);
35
  if (test) {
36 37
    // If the test was already added by a new-style signature do not
    // allow it to be duplicated.
38
    if (!test->GetOldStyle()) {
39
      std::ostringstream e;
40 41
      e << " given test name \"" << args[0]
        << "\" which already exists in this directory.";
42
      this->SetError(e.str());
43 44
      return false;
    }
45
  } else {
46
    test = this->Makefile->CreateTest(args[0]);
47
    test->SetOldStyle(true);
48
    this->Makefile->AddTestGenerator(new cmTestGenerator(test));
49
  }
50
  test->SetCommand(command);
51

52
  return true;
53
}
54 55 56 57 58

bool cmAddTestCommand::HandleNameMode(std::vector<std::string> const& args)
{
  std::string name;
  std::vector<std::string> configurations;
59
  std::string working_directory;
60
  std::vector<std::string> command;
61
  bool command_expand_lists = false;
62 63

  // Read the arguments.
64 65
  enum Doing
  {
66 67 68
    DoingName,
    DoingCommand,
    DoingConfigs,
69
    DoingWorkingDirectory,
70 71 72
    DoingNone
  };
  Doing doing = DoingName;
73 74 75
  for (unsigned int i = 1; i < args.size(); ++i) {
    if (args[i] == "COMMAND") {
      if (!command.empty()) {
76 77 78
        this->SetError(" may be given at most one COMMAND.");
        return false;
      }
79 80 81
      doing = DoingCommand;
    } else if (args[i] == "CONFIGURATIONS") {
      if (!configurations.empty()) {
82 83 84
        this->SetError(" may be given at most one set of CONFIGURATIONS.");
        return false;
      }
85 86 87
      doing = DoingConfigs;
    } else if (args[i] == "WORKING_DIRECTORY") {
      if (!working_directory.empty()) {
88 89 90
        this->SetError(" may be given at most one WORKING_DIRECTORY.");
        return false;
      }
91
      doing = DoingWorkingDirectory;
92 93 94 95 96 97 98
    } else if (args[i] == "COMMAND_EXPAND_LISTS") {
      if (command_expand_lists) {
        this->SetError(" may be given at most one COMMAND_EXPAND_LISTS.");
        return false;
      }
      command_expand_lists = true;
      doing = DoingNone;
99
    } else if (doing == DoingName) {
100 101
      name = args[i];
      doing = DoingNone;
102
    } else if (doing == DoingCommand) {
103
      command.push_back(args[i]);
104
    } else if (doing == DoingConfigs) {
105
      configurations.push_back(args[i]);
106
    } else if (doing == DoingWorkingDirectory) {
107 108
      working_directory = args[i];
      doing = DoingNone;
109
    } else {
110
      std::ostringstream e;
111
      e << " given unknown argument:\n  " << args[i] << "\n";
112
      this->SetError(e.str());
113 114
      return false;
    }
115
  }
116 117

  // Require a test name.
118
  if (name.empty()) {
119 120
    this->SetError(" must be given non-empty NAME.");
    return false;
121
  }
122 123

  // Require a command.
124
  if (command.empty()) {
125 126
    this->SetError(" must be given non-empty COMMAND.");
    return false;
127
  }
128 129

  // Require a unique test name within the directory.
130
  if (this->Makefile->GetTest(name)) {
131
    std::ostringstream e;
132 133
    e << " given test NAME \"" << name
      << "\" which already exists in this directory.";
134
    this->SetError(e.str());
135
    return false;
136
  }
137 138

  // Add the test.
139
  cmTest* test = this->Makefile->CreateTest(name);
140 141
  test->SetOldStyle(false);
  test->SetCommand(command);
142
  if (!working_directory.empty()) {
143
    test->SetProperty("WORKING_DIRECTORY", working_directory.c_str());
144
  }
145
  test->SetCommandExpandLists(command_expand_lists);
146 147 148 149
  this->Makefile->AddTestGenerator(new cmTestGenerator(test, configurations));

  return true;
}