ctest.cxx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"."))
  52. {
  53. return fullPath;
  54. }
  55. if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,""))
  56. {
  57. return fullPath;
  58. }
  59. if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Release"))
  60. {
  61. return fullPath;
  62. }
  63. if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Debug"))
  64. {
  65. return fullPath;
  66. }
  67. if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"MinSizeRel"))
  68. {
  69. return fullPath;
  70. }
  71. if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"RelWithDebInfo"))
  72. {
  73. return fullPath;
  74. }
  75. // if everything else failed, check the users path
  76. if (dir != "")
  77. {
  78. std::string path = cmSystemTools::FindProgram(file.c_str());
  79. if (path != "")
  80. {
  81. return path;
  82. }
  83. }
  84. return fullPath;
  85. }
  86. void ctest::ProcessDirectory(int &passed, std::vector<std::string> &failed)
  87. {
  88. // does the DartTestfile.txt exist ?
  89. if(!cmSystemTools::FileExists("DartTestfile.txt"))
  90. {
  91. return;
  92. }
  93. // parse the file
  94. std::ifstream fin("DartTestfile.txt");
  95. if(!fin)
  96. {
  97. return;
  98. }
  99. std::string name;
  100. std::vector<std::string> args;
  101. cmRegularExpression var(this->m_RegExp.c_str());
  102. cmRegularExpression dartStuff("([\t\n ]*<DartMeasurement.*/DartMeasurement[a-zA-Z]*>[\t ]*[\n]*)");
  103. bool parseError;
  104. while ( fin )
  105. {
  106. if(cmSystemTools::ParseFunction(fin, name, args, "DartTestfile.txt",
  107. parseError))
  108. {
  109. if (name == "SUBDIRS")
  110. {
  111. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  112. for(std::vector<std::string>::iterator j = args.begin();
  113. j != args.end(); ++j)
  114. {
  115. std::string nwd = cwd + "/";
  116. nwd += *j;
  117. if (cmSystemTools::FileIsDirectory(nwd.c_str()))
  118. {
  119. std::cerr << "Changing directory into " << nwd.c_str() << "\n";
  120. cmSystemTools::ChangeDirectory(nwd.c_str());
  121. this->ProcessDirectory(passed, failed);
  122. }
  123. }
  124. // return to the original directory
  125. cmSystemTools::ChangeDirectory(cwd.c_str());
  126. }
  127. if (name == "ADD_TEST")
  128. {
  129. if (this->m_UseRegExp && !var.find(args[0].c_str()))
  130. {
  131. continue;
  132. }
  133. fprintf(stderr,"Testing %-30s ",args[0].c_str());
  134. fflush(stderr);
  135. //std::cerr << "Testing " << args[0] << " ... ";
  136. // find the test executable
  137. std::string testCommand =
  138. cmSystemTools::EscapeSpaces(this->FindExecutable(args[1].c_str()).c_str());
  139. // continue if we did not find the executable
  140. if (testCommand == "")
  141. {
  142. std::cerr << "Unable to find executable: " <<
  143. args[1].c_str() << "\n";
  144. continue;
  145. }
  146. // add the arguments
  147. std::vector<std::string>::iterator j = args.begin();
  148. ++j;
  149. ++j;
  150. for(;j != args.end(); ++j)
  151. {
  152. testCommand += " ";
  153. testCommand += cmSystemTools::EscapeSpaces(j->c_str());
  154. }
  155. /**
  156. * Run an executable command and put the stdout in output.
  157. */
  158. std::string output;
  159. int retVal;
  160. if (!cmSystemTools::RunCommand(testCommand.c_str(), output,
  161. retVal, false) || retVal != 0)
  162. {
  163. fprintf(stderr,"***Failed\n");
  164. if (output != "")
  165. {
  166. if (dartStuff.find(output.c_str()))
  167. {
  168. cmSystemTools::ReplaceString(output,
  169. dartStuff.match(1).c_str(),"");
  170. }
  171. if (output != "")
  172. {
  173. std::cerr << output.c_str() << "\n";
  174. }
  175. }
  176. failed.push_back(args[0]);
  177. }
  178. else
  179. {
  180. fprintf(stderr," Passed\n");
  181. if (output != "")
  182. {
  183. if (dartStuff.find(output.c_str()))
  184. {
  185. cmSystemTools::ReplaceString(output,
  186. dartStuff.match(1).c_str(),"");
  187. }
  188. if (output != "")
  189. {
  190. std::cerr << output.c_str() << "\n";
  191. }
  192. }
  193. passed++;
  194. }
  195. }
  196. }
  197. }
  198. }
  199. // this is a test driver program for cmake.
  200. int main (int argc, char *argv[])
  201. {
  202. int passed = 0;
  203. std::vector<std::string> failed;
  204. int total;
  205. ctest inst;
  206. // look at the args
  207. std::vector<std::string> args;
  208. for(int i =0; i < argc; ++i)
  209. {
  210. args.push_back(argv[i]);
  211. }
  212. for(unsigned int i=1; i < args.size(); ++i)
  213. {
  214. std::string arg = args[i];
  215. if(arg.find("-R",0) == 0 && i < args.size() - 1)
  216. {
  217. inst.m_UseRegExp = true;
  218. inst.m_RegExp = args[i+1];
  219. }
  220. }
  221. // call process directory
  222. inst.ProcessDirectory(passed, failed);
  223. total = passed + failed.size();
  224. if (total == 0)
  225. {
  226. std::cerr << "No tests were found!!!\n";
  227. }
  228. else
  229. {
  230. float percent = passed * 100.0 / total;
  231. fprintf(stderr,"%.0f%% tests passed, %i tests failed out of %i\n",
  232. percent, failed.size(), total);
  233. if (failed.size())
  234. {
  235. std::cerr << "The following tests failed:\n";
  236. for(std::vector<std::string>::iterator j = failed.begin();
  237. j != failed.end(); ++j)
  238. {
  239. std::cerr << "\t" << *j << "\n";
  240. }
  241. }
  242. }
  243. return failed.size();
  244. }