FIX: stupid pb fixed (close to being medieval'ed by The Ken)
[cmake.git] / Source / ctest.cxx
blobef37aad8424c38394068d33e369a9ee096370a87
1 /*=========================================================================
3 Program: Insight Segmentation & Registration Toolkit
4 Module: $RCSfile: ctest.cxx,v $
5 Language: C++
6 Date: $Date: 2002-06-24 18:07:46 $
7 Version: $Revision: 1.23 $
9 Copyright (c) 2002 Insight Consortium. All rights reserved.
10 See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include <stdio.h>
18 #include "ctest.h"
19 #include "cmRegularExpression.h"
20 #include "cmSystemTools.h"
21 bool TryExecutable(const char *dir, const char *file,
22 std::string *fullPath, const char *subdir)
24 // try current directory
25 std::string tryPath;
26 if (dir && strcmp(dir,""))
28 tryPath = dir;
29 tryPath += "/";
32 if (subdir && strcmp(subdir,""))
34 tryPath += subdir;
35 tryPath += "/";
38 tryPath += file;
39 if(cmSystemTools::FileExists(tryPath.c_str()))
41 *fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
42 return true;
44 tryPath += cmSystemTools::GetExecutableExtension();
45 if(cmSystemTools::FileExists(tryPath.c_str()))
47 *fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
48 return true;
50 return false;
54 std::string ctest::FindExecutable(const char *exe)
56 std::string fullPath = "";
57 std::string dir;
58 std::string file;
60 cmSystemTools::SplitProgramPath(exe, dir, file);
61 if(m_ConfigType != "")
63 if(TryExecutable(dir.c_str(), file.c_str(), &fullPath, m_ConfigType.c_str()))
65 return fullPath;
67 std::string tried = dir;
68 dir += "/";
69 dir += m_ConfigType;
70 dir += "/";
71 dir += file;
72 cmSystemTools::Error("config type specified on the command line, but test executable not found.",
73 dir.c_str());
74 return "";
76 if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"."))
78 return fullPath;
81 if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,""))
83 return fullPath;
86 if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Release"))
88 return fullPath;
91 if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Debug"))
93 return fullPath;
96 if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"MinSizeRel"))
98 return fullPath;
101 if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"RelWithDebInfo"))
103 return fullPath;
106 // if everything else failed, check the users path
107 if (dir != "")
109 std::string path = cmSystemTools::FindProgram(file.c_str());
110 if (path != "")
112 return path;
116 return fullPath;
120 void ctest::ProcessDirectory(std::vector<std::string> &passed,
121 std::vector<std::string> &failed)
123 // does the DartTestfile.txt exist ?
124 if(!cmSystemTools::FileExists("DartTestfile.txt"))
126 return;
129 // parse the file
130 std::ifstream fin("DartTestfile.txt");
131 if(!fin)
133 return;
136 int firstTest = 1;
138 std::string name;
139 std::vector<std::string> args;
140 cmRegularExpression ireg(this->m_IncludeRegExp.c_str());
141 cmRegularExpression ereg(this->m_ExcludeRegExp.c_str());
142 cmRegularExpression dartStuff("([\t\n ]*<DartMeasurement.*/DartMeasurement[a-zA-Z]*>[\t ]*[\n]*)");
144 bool parseError;
145 while ( fin )
147 if(cmSystemTools::ParseFunction(fin, name, args, "DartTestfile.txt",
148 parseError))
150 if (name == "SUBDIRS")
152 std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
153 for(std::vector<std::string>::iterator j = args.begin();
154 j != args.end(); ++j)
156 std::string nwd = cwd + "/";
157 nwd += *j;
158 if (cmSystemTools::FileIsDirectory(nwd.c_str()))
160 cmSystemTools::ChangeDirectory(nwd.c_str());
161 this->ProcessDirectory(passed, failed);
164 // return to the original directory
165 cmSystemTools::ChangeDirectory(cwd.c_str());
168 if (name == "ADD_TEST")
170 if (this->m_UseExcludeRegExp &&
171 this->m_UseExcludeRegExpFirst &&
172 ereg.find(args[0].c_str()))
174 continue;
176 if (this->m_UseIncludeRegExp && !ireg.find(args[0].c_str()))
178 continue;
180 if (this->m_UseExcludeRegExp &&
181 !this->m_UseExcludeRegExpFirst &&
182 ereg.find(args[0].c_str()))
184 continue;
186 if (firstTest)
188 std::string nwd = cmSystemTools::GetCurrentWorkingDirectory();
189 std::cerr << "Changing directory into " << nwd.c_str() << "\n";
190 firstTest = 0;
192 fprintf(stderr,"Testing %-30s ",args[0].c_str());
193 fflush(stderr);
194 //std::cerr << "Testing " << args[0] << " ... ";
195 // find the test executable
196 std::string testCommand =
197 cmSystemTools::EscapeSpaces(this->FindExecutable(args[1].c_str()).c_str());
198 // continue if we did not find the executable
199 if (testCommand == "")
201 std::cerr << "Unable to find executable: " <<
202 args[1].c_str() << "\n";
203 continue;
206 testCommand = cmSystemTools::ConvertToOutputPath(testCommand.c_str());
207 // add the arguments
208 std::vector<std::string>::iterator j = args.begin();
209 ++j;
210 ++j;
211 for(;j != args.end(); ++j)
213 testCommand += " ";
214 testCommand += cmSystemTools::EscapeSpaces(j->c_str());
217 * Run an executable command and put the stdout in output.
219 std::string output;
220 int retVal;
221 if (!cmSystemTools::RunCommand(testCommand.c_str(), output,
222 retVal, 0, false) || retVal != 0)
224 fprintf(stderr,"***Failed\n");
225 if (output != "")
227 if (dartStuff.find(output.c_str()))
229 cmSystemTools::ReplaceString(output,
230 dartStuff.match(1).c_str(),"");
232 if (output != "")
234 std::cerr << output.c_str() << "\n";
237 failed.push_back(args[0]);
239 else
241 fprintf(stderr," Passed\n");
242 if (output != "")
244 if (dartStuff.find(output.c_str()))
246 cmSystemTools::ReplaceString(output,
247 dartStuff.match(1).c_str(),"");
249 if (output != "")
251 std::cerr << output.c_str() << "\n";
254 passed.push_back(args[0]);
263 // this is a test driver program for cmake.
264 int main (int argc, char *argv[])
266 std::vector<std::string> passed;
267 std::vector<std::string> failed;
268 int total;
270 ctest inst;
272 // look at the args
273 std::vector<std::string> args;
274 for(int i =0; i < argc; ++i)
276 args.push_back(argv[i]);
279 for(unsigned int i=1; i < args.size(); ++i)
281 std::string arg = args[i];
282 if(arg.find("-D",0) == 0 && i < args.size() - 1)
284 inst.m_ConfigType = args[i+1];
287 if(arg.find("-R",0) == 0 && i < args.size() - 1)
289 inst.m_UseIncludeRegExp = true;
290 inst.m_IncludeRegExp = args[i+1];
293 if(arg.find("-E",0) == 0 && i < args.size() - 1)
295 inst.m_UseExcludeRegExp = true;
296 inst.m_ExcludeRegExp = args[i+1];
297 inst.m_UseExcludeRegExpFirst = inst.m_UseIncludeRegExp ? false : true;
301 // call process directory
302 inst.ProcessDirectory(passed, failed);
303 total = int(passed.size()) + int(failed.size());
305 if (total == 0)
307 std::cerr << "No tests were found!!!\n";
309 else
311 if (passed.size() && (inst.m_UseIncludeRegExp || inst.m_UseExcludeRegExp))
313 std::cerr << "\nThe following tests passed:\n";
314 for(std::vector<std::string>::iterator j = passed.begin();
315 j != passed.end(); ++j)
317 std::cerr << "\t" << *j << "\n";
321 float percent = float(passed.size()) * 100.0f / total;
322 fprintf(stderr,"\n%.0f%% tests passed, %i tests failed out of %i\n",
323 percent, int(failed.size()), total);
325 if (failed.size())
327 std::cerr << "\nThe following tests FAILED:\n";
328 for(std::vector<std::string>::iterator j = failed.begin();
329 j != failed.end(); ++j)
331 std::cerr << "\t" << *j << "\n";
336 return int(failed.size());