ctest.cxx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. std::string tried = dir;
  59. dir += "/";
  60. dir += m_ConfigType;
  61. dir += "/";
  62. dir += file;
  63. cmSystemTools::Error("config type specified on the command line, but test executable not found.",
  64. dir.c_str());
  65. return "";
  66. }
  67. if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"."))
  68. {
  69. return fullPath;
  70. }
  71. if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,""))
  72. {
  73. return fullPath;
  74. }
  75. if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Release"))
  76. {
  77. return fullPath;
  78. }
  79. if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Debug"))
  80. {
  81. return fullPath;
  82. }
  83. if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"MinSizeRel"))
  84. {
  85. return fullPath;
  86. }
  87. if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"RelWithDebInfo"))
  88. {
  89. return fullPath;
  90. }
  91. // if everything else failed, check the users path
  92. if (dir != "")
  93. {
  94. std::string path = cmSystemTools::FindProgram(file.c_str());
  95. if (path != "")
  96. {
  97. return path;
  98. }
  99. }
  100. return fullPath;
  101. }
  102. void ctest::ProcessDirectory(std::vector<std::string> &passed,
  103. std::vector<std::string> &failed)
  104. {
  105. // does the DartTestfile.txt exist ?
  106. if(!cmSystemTools::FileExists("DartTestfile.txt"))
  107. {
  108. return;
  109. }
  110. // parse the file
  111. std::ifstream fin("DartTestfile.txt");
  112. if(!fin)
  113. {
  114. return;
  115. }
  116. int firstTest = 1;
  117. std::string name;
  118. std::vector<std::string> args;
  119. cmRegularExpression ireg(this->m_IncludeRegExp.c_str());
  120. cmRegularExpression ereg(this->m_ExcludeRegExp.c_str());
  121. cmRegularExpression dartStuff("([\t\n ]*<DartMeasurement.*/DartMeasurement[a-zA-Z]*>[\t ]*[\n]*)");
  122. bool parseError;
  123. while ( fin )
  124. {
  125. if(cmSystemTools::ParseFunction(fin, name, args, "DartTestfile.txt",
  126. parseError))
  127. {
  128. if (name == "SUBDIRS")
  129. {
  130. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  131. for(std::vector<std::string>::iterator j = args.begin();
  132. j != args.end(); ++j)
  133. {
  134. std::string nwd = cwd + "/";
  135. nwd += *j;
  136. if (cmSystemTools::FileIsDirectory(nwd.c_str()))
  137. {
  138. cmSystemTools::ChangeDirectory(nwd.c_str());
  139. this->ProcessDirectory(passed, failed);
  140. }
  141. }
  142. // return to the original directory
  143. cmSystemTools::ChangeDirectory(cwd.c_str());
  144. }
  145. if (name == "ADD_TEST")
  146. {
  147. if (this->m_UseExcludeRegExp &&
  148. this->m_UseExcludeRegExpFirst &&
  149. ereg.find(args[0].c_str()))
  150. {
  151. continue;
  152. }
  153. if (this->m_UseIncludeRegExp && !ireg.find(args[0].c_str()))
  154. {
  155. continue;
  156. }
  157. if (this->m_UseExcludeRegExp &&
  158. !this->m_UseExcludeRegExpFirst &&
  159. ereg.find(args[0].c_str()))
  160. {
  161. continue;
  162. }
  163. if (firstTest)
  164. {
  165. std::string nwd = cmSystemTools::GetCurrentWorkingDirectory();
  166. std::cerr << "Changing directory into " << nwd.c_str() << "\n";
  167. firstTest = 0;
  168. }
  169. fprintf(stderr,"Testing %-30s ",args[0].c_str());
  170. fflush(stderr);
  171. //std::cerr << "Testing " << args[0] << " ... ";
  172. // find the test executable
  173. std::string testCommand =
  174. cmSystemTools::EscapeSpaces(this->FindExecutable(args[1].c_str()).c_str());
  175. // continue if we did not find the executable
  176. if (testCommand == "")
  177. {
  178. std::cerr << "Unable to find executable: " <<
  179. args[1].c_str() << "\n";
  180. continue;
  181. }
  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, 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. }