ctest.cxx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include <stdio.h>
  14. #include "ctest.h"
  15. #include "cmRegularExpression.h"
  16. bool TryExecutable(const char *dir, const char *file,
  17. std::string *fullPath, const char *subdir)
  18. {
  19. // try current directory
  20. std::string tryPath;
  21. if (dir && strcmp(dir,""))
  22. {
  23. tryPath = dir;
  24. tryPath += "/";
  25. }
  26. if (subdir && strcmp(subdir,""))
  27. {
  28. tryPath += subdir;
  29. tryPath += "/";
  30. }
  31. tryPath += file;
  32. if(cmSystemTools::FileExists(tryPath.c_str()))
  33. {
  34. *fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  35. return true;
  36. }
  37. tryPath += cmSystemTools::GetExecutableExtension();
  38. if(cmSystemTools::FileExists(tryPath.c_str()))
  39. {
  40. *fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  41. return true;
  42. }
  43. return false;
  44. }
  45. std::string ctest::FindExecutable(const char *exe)
  46. {
  47. std::string fullPath = "";
  48. std::string dir;
  49. std::string file;
  50. cmSystemTools::SplitProgramPath(exe, dir, file);
  51. if(m_ConfigType != "")
  52. {
  53. if(TryExecutable(dir.c_str(), file.c_str(), &fullPath, m_ConfigType.c_str()))
  54. {
  55. return fullPath;
  56. }
  57. std::string tried = dir;
  58. dir += "/";
  59. dir += m_ConfigType;
  60. dir += "/";
  61. dir += file;
  62. cmSystemTools::Error("config type specified on the command line, but test executable not found.",
  63. dir.c_str());
  64. return "";
  65. }
  66. if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"."))
  67. {
  68. return fullPath;
  69. }
  70. if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,""))
  71. {
  72. return fullPath;
  73. }
  74. if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Release"))
  75. {
  76. return fullPath;
  77. }
  78. if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Debug"))
  79. {
  80. return fullPath;
  81. }
  82. if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"MinSizeRel"))
  83. {
  84. return fullPath;
  85. }
  86. if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"RelWithDebInfo"))
  87. {
  88. return fullPath;
  89. }
  90. // if everything else failed, check the users path
  91. if (dir != "")
  92. {
  93. std::string path = cmSystemTools::FindProgram(file.c_str());
  94. if (path != "")
  95. {
  96. return path;
  97. }
  98. }
  99. return fullPath;
  100. }
  101. void ctest::ProcessDirectory(int &passed, std::vector<std::string> &failed)
  102. {
  103. // does the DartTestfile.txt exist ?
  104. if(!cmSystemTools::FileExists("DartTestfile.txt"))
  105. {
  106. return;
  107. }
  108. // parse the file
  109. std::ifstream fin("DartTestfile.txt");
  110. if(!fin)
  111. {
  112. return;
  113. }
  114. int firstTest = 1;
  115. std::string name;
  116. std::vector<std::string> args;
  117. cmRegularExpression ireg(this->m_IncludeRegExp.c_str());
  118. cmRegularExpression ereg(this->m_ExcludeRegExp.c_str());
  119. cmRegularExpression dartStuff("([\t\n ]*<DartMeasurement.*/DartMeasurement[a-zA-Z]*>[\t ]*[\n]*)");
  120. bool parseError;
  121. while ( fin )
  122. {
  123. if(cmSystemTools::ParseFunction(fin, name, args, "DartTestfile.txt",
  124. parseError))
  125. {
  126. if (name == "SUBDIRS")
  127. {
  128. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  129. for(std::vector<std::string>::iterator j = args.begin();
  130. j != args.end(); ++j)
  131. {
  132. std::string nwd = cwd + "/";
  133. nwd += *j;
  134. if (cmSystemTools::FileIsDirectory(nwd.c_str()))
  135. {
  136. cmSystemTools::ChangeDirectory(nwd.c_str());
  137. this->ProcessDirectory(passed, failed);
  138. }
  139. }
  140. // return to the original directory
  141. cmSystemTools::ChangeDirectory(cwd.c_str());
  142. }
  143. if (name == "ADD_TEST")
  144. {
  145. if (this->m_UseExcludeRegExp &&
  146. this->m_UseExcludeRegExpFirst &&
  147. ereg.find(args[0].c_str()))
  148. {
  149. continue;
  150. }
  151. if (this->m_UseIncludeRegExp && !ireg.find(args[0].c_str()))
  152. {
  153. continue;
  154. }
  155. if (this->m_UseExcludeRegExp &&
  156. !this->m_UseExcludeRegExpFirst &&
  157. ereg.find(args[0].c_str()))
  158. {
  159. continue;
  160. }
  161. if (firstTest)
  162. {
  163. std::string nwd = cmSystemTools::GetCurrentWorkingDirectory();
  164. std::cerr << "Changing directory into " << nwd.c_str() << "\n";
  165. firstTest = 0;
  166. }
  167. fprintf(stderr,"Testing %-30s ",args[0].c_str());
  168. fflush(stderr);
  169. //std::cerr << "Testing " << args[0] << " ... ";
  170. // find the test executable
  171. std::string testCommand =
  172. cmSystemTools::EscapeSpaces(this->FindExecutable(args[1].c_str()).c_str());
  173. // continue if we did not find the executable
  174. if (testCommand == "")
  175. {
  176. std::cerr << "Unable to find executable: " <<
  177. args[1].c_str() << "\n";
  178. continue;
  179. }
  180. // add the arguments
  181. std::vector<std::string>::iterator j = args.begin();
  182. ++j;
  183. ++j;
  184. for(;j != args.end(); ++j)
  185. {
  186. testCommand += " ";
  187. testCommand += cmSystemTools::EscapeSpaces(j->c_str());
  188. }
  189. /**
  190. * Run an executable command and put the stdout in output.
  191. */
  192. std::string output;
  193. int retVal;
  194. if (!cmSystemTools::RunCommand(testCommand.c_str(), output,
  195. retVal, 0, false) || retVal != 0)
  196. {
  197. fprintf(stderr,"***Failed\n");
  198. if (output != "")
  199. {
  200. if (dartStuff.find(output.c_str()))
  201. {
  202. cmSystemTools::ReplaceString(output,
  203. dartStuff.match(1).c_str(),"");
  204. }
  205. if (output != "")
  206. {
  207. std::cerr << output.c_str() << "\n";
  208. }
  209. }
  210. failed.push_back(args[0]);
  211. }
  212. else
  213. {
  214. fprintf(stderr," Passed\n");
  215. if (output != "")
  216. {
  217. if (dartStuff.find(output.c_str()))
  218. {
  219. cmSystemTools::ReplaceString(output,
  220. dartStuff.match(1).c_str(),"");
  221. }
  222. if (output != "")
  223. {
  224. std::cerr << output.c_str() << "\n";
  225. }
  226. }
  227. passed++;
  228. }
  229. }
  230. }
  231. }
  232. }
  233. // this is a test driver program for cmake.
  234. int main (int argc, char *argv[])
  235. {
  236. int passed = 0;
  237. std::vector<std::string> failed;
  238. int total;
  239. ctest inst;
  240. // look at the args
  241. std::vector<std::string> args;
  242. for(int i =0; i < argc; ++i)
  243. {
  244. args.push_back(argv[i]);
  245. }
  246. for(unsigned int i=1; i < args.size(); ++i)
  247. {
  248. std::string arg = args[i];
  249. if(arg.find("-D",0) == 0 && i < args.size() - 1)
  250. {
  251. inst.m_ConfigType = args[i+1];
  252. }
  253. if(arg.find("-R",0) == 0 && i < args.size() - 1)
  254. {
  255. inst.m_UseIncludeRegExp = true;
  256. inst.m_IncludeRegExp = args[i+1];
  257. }
  258. if(arg.find("-E",0) == 0 && i < args.size() - 1)
  259. {
  260. inst.m_UseExcludeRegExp = true;
  261. inst.m_ExcludeRegExp = args[i+1];
  262. inst.m_UseExcludeRegExpFirst = inst.m_UseIncludeRegExp ? false : true;
  263. }
  264. }
  265. // call process directory
  266. inst.ProcessDirectory(passed, failed);
  267. total = passed + int(failed.size());
  268. if (total == 0)
  269. {
  270. std::cerr << "No tests were found!!!\n";
  271. }
  272. else
  273. {
  274. float percent = passed * 100.0f / total;
  275. fprintf(stderr,"%.0f%% tests passed, %i tests failed out of %i\n",
  276. percent, failed.size(), total);
  277. if (failed.size())
  278. {
  279. std::cerr << "The following tests failed:\n";
  280. for(std::vector<std::string>::iterator j = failed.begin();
  281. j != failed.end(); ++j)
  282. {
  283. std::cerr << "\t" << *j << "\n";
  284. }
  285. }
  286. }
  287. return int(failed.size());
  288. }