1 /*=========================================================================
3 Program: Insight Segmentation & Registration Toolkit
4 Module: $RCSfile: ctest.cxx,v $
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 =========================================================================*/
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
26 if (dir
&& strcmp(dir
,""))
32 if (subdir
&& strcmp(subdir
,""))
39 if(cmSystemTools::FileExists(tryPath
.c_str()))
41 *fullPath
= cmSystemTools::CollapseFullPath(tryPath
.c_str());
44 tryPath
+= cmSystemTools::GetExecutableExtension();
45 if(cmSystemTools::FileExists(tryPath
.c_str()))
47 *fullPath
= cmSystemTools::CollapseFullPath(tryPath
.c_str());
54 std::string
ctest::FindExecutable(const char *exe
)
56 std::string fullPath
= "";
60 cmSystemTools::SplitProgramPath(exe
, dir
, file
);
61 if(m_ConfigType
!= "")
63 if(TryExecutable(dir
.c_str(), file
.c_str(), &fullPath
, m_ConfigType
.c_str()))
67 std::string tried
= dir
;
72 cmSystemTools::Error("config type specified on the command line, but test executable not found.",
76 if (TryExecutable(dir
.c_str(),file
.c_str(),&fullPath
,"."))
81 if (TryExecutable(dir
.c_str(),file
.c_str(),&fullPath
,""))
86 if (TryExecutable(dir
.c_str(),file
.c_str(),&fullPath
,"Release"))
91 if (TryExecutable(dir
.c_str(),file
.c_str(),&fullPath
,"Debug"))
96 if (TryExecutable(dir
.c_str(),file
.c_str(),&fullPath
,"MinSizeRel"))
101 if (TryExecutable(dir
.c_str(),file
.c_str(),&fullPath
,"RelWithDebInfo"))
106 // if everything else failed, check the users path
109 std::string path
= cmSystemTools::FindProgram(file
.c_str());
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"))
130 std::ifstream
fin("DartTestfile.txt");
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]*)");
147 if(cmSystemTools::ParseFunction(fin
, name
, args
, "DartTestfile.txt",
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
+ "/";
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()))
176 if (this->m_UseIncludeRegExp
&& !ireg
.find(args
[0].c_str()))
180 if (this->m_UseExcludeRegExp
&&
181 !this->m_UseExcludeRegExpFirst
&&
182 ereg
.find(args
[0].c_str()))
188 std::string nwd
= cmSystemTools::GetCurrentWorkingDirectory();
189 std::cerr
<< "Changing directory into " << nwd
.c_str() << "\n";
192 fprintf(stderr
,"Testing %-30s ",args
[0].c_str());
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";
206 testCommand
= cmSystemTools::ConvertToOutputPath(testCommand
.c_str());
208 std::vector
<std::string
>::iterator j
= args
.begin();
211 for(;j
!= args
.end(); ++j
)
214 testCommand
+= cmSystemTools::EscapeSpaces(j
->c_str());
217 * Run an executable command and put the stdout in output.
221 if (!cmSystemTools::RunCommand(testCommand
.c_str(), output
,
222 retVal
, 0, false) || retVal
!= 0)
224 fprintf(stderr
,"***Failed\n");
227 if (dartStuff
.find(output
.c_str()))
229 cmSystemTools::ReplaceString(output
,
230 dartStuff
.match(1).c_str(),"");
234 std::cerr
<< output
.c_str() << "\n";
237 failed
.push_back(args
[0]);
241 fprintf(stderr
," Passed\n");
244 if (dartStuff
.find(output
.c_str()))
246 cmSystemTools::ReplaceString(output
,
247 dartStuff
.match(1).c_str(),"");
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
;
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());
307 std::cerr
<< "No tests were found!!!\n";
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
);
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());