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. testCommand = cmSystemTools::ConvertToOutputPath(testCommand.c_str());
  183. // add the arguments
  184. std::vector<std::string>::iterator j = args.begin();
  185. ++j;
  186. ++j;
  187. for(;j != args.end(); ++j)
  188. {
  189. testCommand += " ";
  190. testCommand += cmSystemTools::EscapeSpaces(j->c_str());
  191. }
  192. /**
  193. * Run an executable command and put the stdout in output.
  194. */
  195. std::string output;
  196. int retVal;
  197. if (!cmSystemTools::RunCommand(testCommand.c_str(), output,
  198. retVal, 0, false) || retVal != 0)
  199. {
  200. fprintf(stderr,"***Failed\n");
  201. if (output != "")
  202. {
  203. if (dartStuff.find(output.c_str()))
  204. {
  205. cmSystemTools::ReplaceString(output,
  206. dartStuff.match(1).c_str(),"");
  207. }
  208. if (output != "")
  209. {
  210. std::cerr << output.c_str() << "\n";
  211. }
  212. }
  213. failed.push_back(args[0]);
  214. }
  215. else
  216. {
  217. fprintf(stderr," Passed\n");
  218. if (output != "")
  219. {
  220. if (dartStuff.find(output.c_str()))
  221. {
  222. cmSystemTools::ReplaceString(output,
  223. dartStuff.match(1).c_str(),"");
  224. }
  225. if (output != "")
  226. {
  227. std::cerr << output.c_str() << "\n";
  228. }
  229. }
  230. passed.push_back(args[0]);
  231. }
  232. }
  233. }
  234. }
  235. }
  236. // this is a test driver program for cmake.
  237. int main (int argc, char *argv[])
  238. {
  239. std::vector<std::string> passed;
  240. std::vector<std::string> failed;
  241. int total;
  242. ctest inst;
  243. // look at the args
  244. std::vector<std::string> args;
  245. for(int i =0; i < argc; ++i)
  246. {
  247. args.push_back(argv[i]);
  248. }
  249. for(unsigned int i=1; i < args.size(); ++i)
  250. {
  251. std::string arg = args[i];
  252. if(arg.find("-D",0) == 0 && i < args.size() - 1)
  253. {
  254. inst.m_ConfigType = args[i+1];
  255. }
  256. if(arg.find("-R",0) == 0 && i < args.size() - 1)
  257. {
  258. inst.m_UseIncludeRegExp = true;
  259. inst.m_IncludeRegExp = args[i+1];
  260. }
  261. if(arg.find("-E",0) == 0 && i < args.size() - 1)
  262. {
  263. inst.m_UseExcludeRegExp = true;
  264. inst.m_ExcludeRegExp = args[i+1];
  265. inst.m_UseExcludeRegExpFirst = inst.m_UseIncludeRegExp ? false : true;
  266. }
  267. }
  268. // call process directory
  269. inst.ProcessDirectory(passed, failed);
  270. total = int(passed.size()) + int(failed.size());
  271. if (total == 0)
  272. {
  273. std::cerr << "No tests were found!!!\n";
  274. }
  275. else
  276. {
  277. if (passed.size() && (inst.m_UseIncludeRegExp || inst.m_UseExcludeRegExp))
  278. {
  279. std::cerr << "\nThe following tests passed:\n";
  280. for(std::vector<std::string>::iterator j = passed.begin();
  281. j != passed.end(); ++j)
  282. {
  283. std::cerr << "\t" << *j << "\n";
  284. }
  285. }
  286. float percent = float(passed.size()) * 100.0f / total;
  287. fprintf(stderr,"\n%.0f%% tests passed, %i tests failed out of %i\n",
  288. percent, int(failed.size()), total);
  289. if (failed.size())
  290. {
  291. std::cerr << "\nThe following tests FAILED:\n";
  292. for(std::vector<std::string>::iterator j = failed.begin();
  293. j != failed.end(); ++j)
  294. {
  295. std::cerr << "\t" << *j << "\n";
  296. }
  297. }
  298. }
  299. return int(failed.size());
  300. }