ctest.cxx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. std::string name;
  115. std::vector<std::string> args;
  116. cmRegularExpression var(this->m_RegExp.c_str());
  117. cmRegularExpression dartStuff("([\t\n ]*<DartMeasurement.*/DartMeasurement[a-zA-Z]*>[\t ]*[\n]*)");
  118. bool parseError;
  119. while ( fin )
  120. {
  121. if(cmSystemTools::ParseFunction(fin, name, args, "DartTestfile.txt",
  122. parseError))
  123. {
  124. if (name == "SUBDIRS")
  125. {
  126. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  127. for(std::vector<std::string>::iterator j = args.begin();
  128. j != args.end(); ++j)
  129. {
  130. std::string nwd = cwd + "/";
  131. nwd += *j;
  132. if (cmSystemTools::FileIsDirectory(nwd.c_str()))
  133. {
  134. std::cerr << "Changing directory into " << nwd.c_str() << "\n";
  135. cmSystemTools::ChangeDirectory(nwd.c_str());
  136. this->ProcessDirectory(passed, failed);
  137. }
  138. }
  139. // return to the original directory
  140. cmSystemTools::ChangeDirectory(cwd.c_str());
  141. }
  142. if (name == "ADD_TEST")
  143. {
  144. if (this->m_UseRegExp && !var.find(args[0].c_str()))
  145. {
  146. continue;
  147. }
  148. fprintf(stderr,"Testing %-30s ",args[0].c_str());
  149. fflush(stderr);
  150. //std::cerr << "Testing " << args[0] << " ... ";
  151. // find the test executable
  152. std::string testCommand =
  153. cmSystemTools::EscapeSpaces(this->FindExecutable(args[1].c_str()).c_str());
  154. // continue if we did not find the executable
  155. if (testCommand == "")
  156. {
  157. std::cerr << "Unable to find executable: " <<
  158. args[1].c_str() << "\n";
  159. continue;
  160. }
  161. // add the arguments
  162. std::vector<std::string>::iterator j = args.begin();
  163. ++j;
  164. ++j;
  165. for(;j != args.end(); ++j)
  166. {
  167. testCommand += " ";
  168. testCommand += cmSystemTools::EscapeSpaces(j->c_str());
  169. }
  170. /**
  171. * Run an executable command and put the stdout in output.
  172. */
  173. std::string output;
  174. int retVal;
  175. if (!cmSystemTools::RunCommand(testCommand.c_str(), output,
  176. retVal, false) || retVal != 0)
  177. {
  178. fprintf(stderr,"***Failed\n");
  179. if (output != "")
  180. {
  181. if (dartStuff.find(output.c_str()))
  182. {
  183. cmSystemTools::ReplaceString(output,
  184. dartStuff.match(1).c_str(),"");
  185. }
  186. if (output != "")
  187. {
  188. std::cerr << output.c_str() << "\n";
  189. }
  190. }
  191. failed.push_back(args[0]);
  192. }
  193. else
  194. {
  195. fprintf(stderr," Passed\n");
  196. if (output != "")
  197. {
  198. if (dartStuff.find(output.c_str()))
  199. {
  200. cmSystemTools::ReplaceString(output,
  201. dartStuff.match(1).c_str(),"");
  202. }
  203. if (output != "")
  204. {
  205. std::cerr << output.c_str() << "\n";
  206. }
  207. }
  208. passed++;
  209. }
  210. }
  211. }
  212. }
  213. }
  214. // this is a test driver program for cmake.
  215. int main (int argc, char *argv[])
  216. {
  217. int passed = 0;
  218. std::vector<std::string> failed;
  219. int total;
  220. ctest inst;
  221. // look at the args
  222. std::vector<std::string> args;
  223. for(int i =0; i < argc; ++i)
  224. {
  225. args.push_back(argv[i]);
  226. }
  227. for(unsigned int i=1; i < args.size(); ++i)
  228. {
  229. std::string arg = args[i];
  230. if(arg.find("-D",0) == 0 && i < args.size() - 1)
  231. {
  232. inst.m_ConfigType = args[i+1];
  233. }
  234. if(arg.find("-R",0) == 0 && i < args.size() - 1)
  235. {
  236. inst.m_UseRegExp = true;
  237. inst.m_RegExp = args[i+1];
  238. }
  239. }
  240. // call process directory
  241. inst.ProcessDirectory(passed, failed);
  242. total = passed + failed.size();
  243. if (total == 0)
  244. {
  245. std::cerr << "No tests were found!!!\n";
  246. }
  247. else
  248. {
  249. float percent = passed * 100.0 / total;
  250. fprintf(stderr,"%.0f%% tests passed, %i tests failed out of %i\n",
  251. percent, failed.size(), total);
  252. if (failed.size())
  253. {
  254. std::cerr << "The following tests failed:\n";
  255. for(std::vector<std::string>::iterator j = failed.begin();
  256. j != failed.end(); ++j)
  257. {
  258. std::cerr << "\t" << *j << "\n";
  259. }
  260. }
  261. }
  262. return failed.size();
  263. }