ctest.cxx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include <stdio.h>
  2. #include "ctest.h"
  3. #include "cmRegularExpression.h"
  4. bool TryExecutable(const char *dir, const char *file,
  5. std::string *fullPath, const char *subdir)
  6. {
  7. // try current directory
  8. std::string tryPath;
  9. if (dir && strcmp(dir,""))
  10. {
  11. tryPath = dir;
  12. tryPath += "/";
  13. }
  14. if (subdir && strcmp(subdir,""))
  15. {
  16. tryPath += subdir;
  17. tryPath += "/";
  18. }
  19. tryPath += file;
  20. if(cmSystemTools::FileExists(tryPath.c_str()))
  21. {
  22. *fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  23. return true;
  24. }
  25. tryPath += cmSystemTools::GetExecutableExtension();
  26. if(cmSystemTools::FileExists(tryPath.c_str()))
  27. {
  28. *fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  29. return true;
  30. }
  31. return false;
  32. }
  33. std::string ctest::FindExecutable(const char *exe)
  34. {
  35. std::string fullPath = "";
  36. std::string dir;
  37. std::string file;
  38. cmSystemTools::SplitProgramPath(exe, dir, file);
  39. if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"."))
  40. {
  41. return fullPath;
  42. }
  43. if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,""))
  44. {
  45. return fullPath;
  46. }
  47. if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Release"))
  48. {
  49. return fullPath;
  50. }
  51. if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Debug"))
  52. {
  53. return fullPath;
  54. }
  55. if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"MinSizeRel"))
  56. {
  57. return fullPath;
  58. }
  59. if (TryExecutable(dir.c_str(),file.c_str(),&fullPath,"RelWithDebInfo"))
  60. {
  61. return fullPath;
  62. }
  63. // if everything else failed, check the users path
  64. if (dir != "")
  65. {
  66. std::string path = cmSystemTools::FindProgram(file.c_str());
  67. if (path != "")
  68. {
  69. return path;
  70. }
  71. }
  72. return fullPath;
  73. }
  74. void ctest::ProcessDirectory(int &passed, int &failed)
  75. {
  76. // does the DartTestfile.txt exist ?
  77. if(!cmSystemTools::FileExists("DartTestfile.txt"))
  78. {
  79. return;
  80. }
  81. // parse the file
  82. std::ifstream fin("DartTestfile.txt");
  83. if(!fin)
  84. {
  85. return;
  86. }
  87. std::string name;
  88. std::vector<std::string> args;
  89. cmRegularExpression var(this->m_RegExp.c_str());
  90. cmRegularExpression dartStuff("([\t\n ]*<DartMeasurement.*/DartMeasurement[a-zA-Z]*>[\t ]*[\n]*)");
  91. while ( fin )
  92. {
  93. if(cmSystemTools::ParseFunction(fin, name, args))
  94. {
  95. if (name == "SUBDIRS")
  96. {
  97. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  98. for(std::vector<std::string>::iterator j = args.begin();
  99. j != args.end(); ++j)
  100. {
  101. std::string nwd = cwd + "/";
  102. nwd += *j;
  103. if (cmSystemTools::FileIsDirectory(nwd.c_str()))
  104. {
  105. std::cerr << "Changing directory into " << nwd.c_str() << "\n";
  106. cmSystemTools::ChangeDirectory(nwd.c_str());
  107. this->ProcessDirectory(passed,failed);
  108. }
  109. }
  110. // return to the original directory
  111. cmSystemTools::ChangeDirectory(cwd.c_str());
  112. }
  113. if (name == "ADD_TEST")
  114. {
  115. if (this->m_UseRegExp && !var.find(args[0].c_str()))
  116. {
  117. continue;
  118. }
  119. fprintf(stderr,"Testing %-30s ",args[0].c_str());
  120. //std::cerr << "Testing " << args[0] << " ... ";
  121. // find the test executable
  122. std::string testCommand = this->FindExecutable(args[1].c_str());
  123. // add the arguments
  124. std::vector<std::string>::iterator j = args.begin();
  125. ++j;
  126. ++j;
  127. for(;j != args.end(); ++j)
  128. {
  129. testCommand += " ";
  130. testCommand += *j;
  131. }
  132. /**
  133. * Run an executable command and put the stdout in output.
  134. */
  135. std::string output;
  136. int retVal;
  137. if (!cmSystemTools::RunCommand(testCommand.c_str(), output,
  138. retVal, false) || retVal != 0)
  139. {
  140. fprintf(stderr,"***Failed\n");
  141. if (output != "")
  142. {
  143. if (dartStuff.find(output.c_str()))
  144. {
  145. cmSystemTools::ReplaceString(output,
  146. dartStuff.match(1).c_str(),"");
  147. }
  148. if (output != "")
  149. {
  150. std::cerr << output.c_str() << "\n";
  151. }
  152. }
  153. failed++;
  154. }
  155. else
  156. {
  157. fprintf(stderr," Passed\n");
  158. if (output != "")
  159. {
  160. if (dartStuff.find(output.c_str()))
  161. {
  162. cmSystemTools::ReplaceString(output,
  163. dartStuff.match(1).c_str(),"");
  164. }
  165. if (output != "")
  166. {
  167. std::cerr << output.c_str() << "\n";
  168. }
  169. }
  170. passed++;
  171. }
  172. }
  173. }
  174. }
  175. }
  176. // this is a test driver program for cmake.
  177. int main (int argc, char *argv[])
  178. {
  179. int passed = 0;
  180. int failed = 0;
  181. int total;
  182. ctest inst;
  183. // look at the args
  184. std::vector<std::string> args;
  185. for(int i =0; i < argc; ++i)
  186. {
  187. args.push_back(argv[i]);
  188. }
  189. for(unsigned int i=1; i < args.size(); ++i)
  190. {
  191. std::string arg = args[i];
  192. if(arg.find("-R",0) == 0 && i < args.size() - 1)
  193. {
  194. inst.m_UseRegExp = true;
  195. inst.m_RegExp = args[i+1];
  196. }
  197. }
  198. // call process directory
  199. inst.ProcessDirectory(passed, failed);
  200. total = passed + failed;
  201. if (total == 0)
  202. {
  203. std::cerr << "No tests were found!!!\n";
  204. }
  205. else
  206. {
  207. float percent = passed * 100.0 / total;
  208. fprintf(stderr,"%.0f%% tests passed, %i tests failed out of %i\n",
  209. percent,failed, total);
  210. }
  211. return failed;
  212. }