ctest.cxx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. #include "cmSystemTools.h"
  17. bool TryExecutable(const char *dir, const char *file,
  18. std::string *fullPath, const char *subdir)
  19. {
  20. // try current directory
  21. std::string tryPath;
  22. if (dir && strcmp(dir,""))
  23. {
  24. tryPath = dir;
  25. tryPath += "/";
  26. }
  27. if (subdir && strcmp(subdir,""))
  28. {
  29. tryPath += subdir;
  30. tryPath += "/";
  31. }
  32. tryPath += file;
  33. if(cmSystemTools::FileExists(tryPath.c_str()))
  34. {
  35. *fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  36. return true;
  37. }
  38. tryPath += cmSystemTools::GetExecutableExtension();
  39. if(cmSystemTools::FileExists(tryPath.c_str()))
  40. {
  41. *fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  42. return true;
  43. }
  44. return false;
  45. }
  46. std::string ctest::FindExecutable(const char *exe)
  47. {
  48. std::string fullPath = "";
  49. std::string dir;
  50. std::string file;
  51. cmSystemTools::SplitProgramPath(exe, dir, file);
  52. if(m_ConfigType != "")
  53. {
  54. if(TryExecutable(dir.c_str(), file.c_str(), &fullPath, m_ConfigType.c_str()))
  55. {
  56. return fullPath;
  57. }
  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(std::vector<std::string> &passed,
  102. std::vector<std::string> &failed)
  103. {
  104. // does the DartTestfile.txt exist ?
  105. if(!cmSystemTools::FileExists("DartTestfile.txt"))
  106. {
  107. return;
  108. }
  109. // parse the file
  110. std::ifstream fin("DartTestfile.txt");
  111. if(!fin)
  112. {
  113. return;
  114. }
  115. int firstTest = 1;
  116. std::string name;
  117. std::vector<std::string> args;
  118. cmRegularExpression ireg(this->m_IncludeRegExp.c_str());
  119. cmRegularExpression ereg(this->m_ExcludeRegExp.c_str());
  120. cmRegularExpression dartStuff("([\t\n ]*<DartMeasurement.*/DartMeasurement[a-zA-Z]*>[\t ]*[\n]*)");
  121. bool parseError;
  122. while ( fin )
  123. {
  124. if(cmSystemTools::ParseFunction(fin, name, args, "DartTestfile.txt",
  125. parseError))
  126. {
  127. if (name == "SUBDIRS")
  128. {
  129. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  130. for(std::vector<std::string>::iterator j = args.begin();
  131. j != args.end(); ++j)
  132. {
  133. std::string nwd = cwd + "/";
  134. nwd += *j;
  135. if (cmSystemTools::FileIsDirectory(nwd.c_str()))
  136. {
  137. cmSystemTools::ChangeDirectory(nwd.c_str());
  138. this->ProcessDirectory(passed, failed);
  139. }
  140. }
  141. // return to the original directory
  142. cmSystemTools::ChangeDirectory(cwd.c_str());
  143. }
  144. if (name == "ADD_TEST")
  145. {
  146. if (this->m_UseExcludeRegExp &&
  147. this->m_UseExcludeRegExpFirst &&
  148. ereg.find(args[0].c_str()))
  149. {
  150. continue;
  151. }
  152. if (this->m_UseIncludeRegExp && !ireg.find(args[0].c_str()))
  153. {
  154. continue;
  155. }
  156. if (this->m_UseExcludeRegExp &&
  157. !this->m_UseExcludeRegExpFirst &&
  158. ereg.find(args[0].c_str()))
  159. {
  160. continue;
  161. }
  162. if (firstTest)
  163. {
  164. std::string nwd = cmSystemTools::GetCurrentWorkingDirectory();
  165. std::cerr << "Changing directory into " << nwd.c_str() << "\n";
  166. firstTest = 0;
  167. }
  168. fprintf(stderr,"Testing %-30s ",args[0].c_str());
  169. fflush(stderr);
  170. //std::cerr << "Testing " << args[0] << " ... ";
  171. // find the test executable
  172. std::string testCommand =
  173. cmSystemTools::EscapeSpaces(this->FindExecutable(args[1].c_str()).c_str());
  174. // continue if we did not find the executable
  175. if (testCommand == "")
  176. {
  177. std::cerr << "Unable to find executable: " <<
  178. args[1].c_str() << "\n";
  179. continue;
  180. }
  181. testCommand = cmSystemTools::ConvertToOutputPath(testCommand.c_str());
  182. // add the arguments
  183. std::vector<std::string>::iterator j = args.begin();
  184. ++j;
  185. ++j;
  186. for(;j != args.end(); ++j)
  187. {
  188. testCommand += " ";
  189. testCommand += cmSystemTools::EscapeSpaces(j->c_str());
  190. }
  191. /**
  192. * Run an executable command and put the stdout in output.
  193. */
  194. std::string output;
  195. int retVal;
  196. if (!cmSystemTools::RunCommand(testCommand.c_str(), output,
  197. retVal, 0, false) || retVal != 0)
  198. {
  199. fprintf(stderr,"***Failed\n");
  200. if (output != "")
  201. {
  202. if (dartStuff.find(output.c_str()))
  203. {
  204. cmSystemTools::ReplaceString(output,
  205. dartStuff.match(1).c_str(),"");
  206. }
  207. if (output != "")
  208. {
  209. std::cerr << output.c_str() << "\n";
  210. }
  211. }
  212. failed.push_back(args[0]);
  213. }
  214. else
  215. {
  216. fprintf(stderr," Passed\n");
  217. if (output != "")
  218. {
  219. if (dartStuff.find(output.c_str()))
  220. {
  221. cmSystemTools::ReplaceString(output,
  222. dartStuff.match(1).c_str(),"");
  223. }
  224. if (output != "")
  225. {
  226. std::cerr << output.c_str() << "\n";
  227. }
  228. }
  229. passed.push_back(args[0]);
  230. }
  231. }
  232. }
  233. }
  234. }
  235. // this is a test driver program for cmake.
  236. int main (int argc, char *argv[])
  237. {
  238. std::vector<std::string> passed;
  239. std::vector<std::string> failed;
  240. int total;
  241. ctest inst;
  242. // look at the args
  243. std::vector<std::string> args;
  244. for(int i =0; i < argc; ++i)
  245. {
  246. args.push_back(argv[i]);
  247. }
  248. for(unsigned int i=1; i < args.size(); ++i)
  249. {
  250. std::string arg = args[i];
  251. if(arg.find("-D",0) == 0 && i < args.size() - 1)
  252. {
  253. inst.m_ConfigType = args[i+1];
  254. }
  255. if(arg.find("-R",0) == 0 && i < args.size() - 1)
  256. {
  257. inst.m_UseIncludeRegExp = true;
  258. inst.m_IncludeRegExp = args[i+1];
  259. }
  260. if(arg.find("-E",0) == 0 && i < args.size() - 1)
  261. {
  262. inst.m_UseExcludeRegExp = true;
  263. inst.m_ExcludeRegExp = args[i+1];
  264. inst.m_UseExcludeRegExpFirst = inst.m_UseIncludeRegExp ? false : true;
  265. }
  266. }
  267. // call process directory
  268. inst.ProcessDirectory(passed, failed);
  269. total = int(passed.size()) + int(failed.size());
  270. if (total == 0)
  271. {
  272. std::cerr << "No tests were found!!!\n";
  273. }
  274. else
  275. {
  276. if (passed.size() && (inst.m_UseIncludeRegExp || inst.m_UseExcludeRegExp))
  277. {
  278. std::cerr << "\nThe following tests passed:\n";
  279. for(std::vector<std::string>::iterator j = passed.begin();
  280. j != passed.end(); ++j)
  281. {
  282. std::cerr << "\t" << *j << "\n";
  283. }
  284. }
  285. float percent = float(passed.size()) * 100.0f / total;
  286. fprintf(stderr,"\n%.0f%% tests passed, %i tests failed out of %i\n",
  287. percent, int(failed.size()), total);
  288. if (failed.size())
  289. {
  290. std::cerr << "\nThe following tests FAILED:\n";
  291. for(std::vector<std::string>::iterator j = failed.begin();
  292. j != failed.end(); ++j)
  293. {
  294. std::cerr << "\t" << *j << "\n";
  295. }
  296. }
  297. }
  298. return int(failed.size());
  299. }