packaging: Initial packaging
[platform/upstream/cmake.git] / Source / cmAddTestCommand.cxx
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the License for more information.
11 ============================================================================*/
12 #include "cmAddTestCommand.h"
13
14 #include "cmTestGenerator.h"
15
16 #include "cmTest.h"
17
18
19 // cmExecutableCommand
20 bool cmAddTestCommand
21 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
22 {
23   if(!args.empty() && args[0] == "NAME")
24     {
25     return this->HandleNameMode(args);
26     }
27
28   // First argument is the name of the test Second argument is the name of
29   // the executable to run (a target or external program) Remaining arguments
30   // are the arguments to pass to the executable
31   if(args.size() < 2 )
32     {
33     this->SetError("called with incorrect number of arguments");
34     return false;
35     }
36
37   // Collect the command with arguments.
38   std::vector<std::string> command;
39   for(std::vector<std::string>::const_iterator it = args.begin() + 1;
40       it != args.end(); ++it)
41     {
42     command.push_back(*it);
43     }
44
45   // Create the test but add a generator only the first time it is
46   // seen.  This preserves behavior from before test generators.
47   cmTest* test = this->Makefile->GetTest(args[0].c_str());
48   if(test)
49     {
50     // If the test was already added by a new-style signature do not
51     // allow it to be duplicated.
52     if(!test->GetOldStyle())
53       {
54       cmOStringStream e;
55       e << " given test name \"" << args[0]
56         << "\" which already exists in this directory.";
57       this->SetError(e.str().c_str());
58       return false;
59       }
60     }
61   else
62     {
63     test = this->Makefile->CreateTest(args[0].c_str());
64     test->SetOldStyle(true);
65     this->Makefile->AddTestGenerator(new cmTestGenerator(test));
66     }
67   test->SetCommand(command);
68
69   return true;
70 }
71
72 //----------------------------------------------------------------------------
73 bool cmAddTestCommand::HandleNameMode(std::vector<std::string> const& args)
74 {
75   std::string name;
76   std::vector<std::string> configurations;
77   std::string working_directory;
78   std::vector<std::string> command;
79
80   // Read the arguments.
81   enum Doing {
82     DoingName,
83     DoingCommand,
84     DoingConfigs,
85     DoingWorkingDirectory,
86     DoingNone
87   };
88   Doing doing = DoingName;
89   for(unsigned int i=1; i < args.size(); ++i)
90     {
91     if(args[i] == "COMMAND")
92       {
93       if(!command.empty())
94         {
95         this->SetError(" may be given at most one COMMAND.");
96         return false;
97         }
98       doing = DoingCommand;
99       }
100     else if(args[i] == "CONFIGURATIONS")
101       {
102       if(!configurations.empty())
103         {
104         this->SetError(" may be given at most one set of CONFIGURATIONS.");
105         return false;
106         }
107       doing = DoingConfigs;
108       }
109     else if(args[i] == "WORKING_DIRECTORY")
110       {
111       if(!working_directory.empty())
112         {
113         this->SetError(" may be given at most one WORKING_DIRECTORY.");
114         return false;
115         }
116       doing = DoingWorkingDirectory;
117       }
118     else if(doing == DoingName)
119       {
120       name = args[i];
121       doing = DoingNone;
122       }
123     else if(doing == DoingCommand)
124       {
125       command.push_back(args[i]);
126       }
127     else if(doing == DoingConfigs)
128       {
129       configurations.push_back(args[i]);
130       }
131     else if(doing == DoingWorkingDirectory)
132       {
133       working_directory = args[i];
134       doing = DoingNone;
135       }
136     else
137       {
138       cmOStringStream e;
139       e << " given unknown argument:\n  " << args[i] << "\n";
140       this->SetError(e.str().c_str());
141       return false;
142       }
143     }
144
145   // Require a test name.
146   if(name.empty())
147     {
148     this->SetError(" must be given non-empty NAME.");
149     return false;
150     }
151
152   // Require a command.
153   if(command.empty())
154     {
155     this->SetError(" must be given non-empty COMMAND.");
156     return false;
157     }
158
159   // Require a unique test name within the directory.
160   if(this->Makefile->GetTest(name.c_str()))
161     {
162     cmOStringStream e;
163     e << " given test NAME \"" << name
164       << "\" which already exists in this directory.";
165     this->SetError(e.str().c_str());
166     return false;
167     }
168
169   // Add the test.
170   cmTest* test = this->Makefile->CreateTest(name.c_str());
171   test->SetOldStyle(false);
172   test->SetCommand(command);
173   if(!working_directory.empty())
174     {
175     test->SetProperty("WORKING_DIRECTORY", working_directory.c_str());
176     }
177   this->Makefile->AddTestGenerator(new cmTestGenerator(test, configurations));
178
179   return true;
180 }