ctest.cxx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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, "DartTestfile.txt"))
  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 =
  123. cmSystemTools::EscapeSpaces(this->FindExecutable(args[1].c_str()).c_str());
  124. // add the arguments
  125. std::vector<std::string>::iterator j = args.begin();
  126. ++j;
  127. ++j;
  128. for(;j != args.end(); ++j)
  129. {
  130. testCommand += " ";
  131. testCommand += cmSystemTools::EscapeSpaces(j->c_str());
  132. }
  133. /**
  134. * Run an executable command and put the stdout in output.
  135. */
  136. std::string output;
  137. int retVal;
  138. if (!cmSystemTools::RunCommand(testCommand.c_str(), output,
  139. retVal, false) || retVal != 0)
  140. {
  141. fprintf(stderr,"***Failed\n");
  142. if (output != "")
  143. {
  144. if (dartStuff.find(output.c_str()))
  145. {
  146. cmSystemTools::ReplaceString(output,
  147. dartStuff.match(1).c_str(),"");
  148. }
  149. if (output != "")
  150. {
  151. std::cerr << output.c_str() << "\n";
  152. }
  153. }
  154. failed++;
  155. }
  156. else
  157. {
  158. fprintf(stderr," Passed\n");
  159. if (output != "")
  160. {
  161. if (dartStuff.find(output.c_str()))
  162. {
  163. cmSystemTools::ReplaceString(output,
  164. dartStuff.match(1).c_str(),"");
  165. }
  166. if (output != "")
  167. {
  168. std::cerr << output.c_str() << "\n";
  169. }
  170. }
  171. passed++;
  172. }
  173. }
  174. }
  175. }
  176. }
  177. // this is a test driver program for cmake.
  178. int main (int argc, char *argv[])
  179. {
  180. int passed = 0;
  181. int failed = 0;
  182. int total;
  183. ctest inst;
  184. // look at the args
  185. std::vector<std::string> args;
  186. for(int i =0; i < argc; ++i)
  187. {
  188. args.push_back(argv[i]);
  189. }
  190. for(unsigned int i=1; i < args.size(); ++i)
  191. {
  192. std::string arg = args[i];
  193. if(arg.find("-R",0) == 0 && i < args.size() - 1)
  194. {
  195. inst.m_UseRegExp = true;
  196. inst.m_RegExp = args[i+1];
  197. }
  198. }
  199. // call process directory
  200. inst.ProcessDirectory(passed, failed);
  201. total = passed + failed;
  202. if (total == 0)
  203. {
  204. std::cerr << "No tests were found!!!\n";
  205. }
  206. else
  207. {
  208. float percent = passed * 100.0 / total;
  209. fprintf(stderr,"%.0f%% tests passed, %i tests failed out of %i\n",
  210. percent,failed, total);
  211. }
  212. return failed;
  213. }