ctest.cxx 4.4 KB

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