cmCTestRunTest.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html 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 "cmCTestRunTest.h"
  14. #include "cmCTestMemCheckHandler.h"
  15. #include "cmCTest.h"
  16. #include "cmSystemTools.h"
  17. cmCTestRunTest::cmCTestRunTest()
  18. {
  19. }
  20. cmCTestRunTest::~cmCTestRunTest()
  21. {
  22. }
  23. bool cmCTestRunTest::IsRunning()
  24. {
  25. return this->TestProcess->IsRunning();
  26. }
  27. //---------------------------------------------------------
  28. //waits .1 sec for output from this process.
  29. void cmCTestRunTest::CheckOutput()
  30. {
  31. std::string out, err;
  32. int pipe = this->TestProcess->CheckOutput(.1, out, err);
  33. if(pipe == cmsysProcess_Pipe_STDOUT)
  34. {
  35. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  36. this->GetIndex() << ": " << out << std::endl);
  37. this->ProcessOutput += out;
  38. this->ProcessOutput += "\n";
  39. }
  40. else if(pipe == cmsysProcess_Pipe_STDERR)
  41. {
  42. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  43. this->GetIndex() << ": " << err << std::endl);
  44. this->ProcessOutput += err;
  45. this->ProcessOutput += "\n";
  46. }
  47. }
  48. //---------------------------------------------------------
  49. bool cmCTestRunTest::EndTest(int completed, int total)
  50. {
  51. //restore the old environment
  52. if (this->ModifyEnv)
  53. {
  54. cmSystemTools::RestoreEnv(this->OrigEnv);
  55. }
  56. this->WriteLogOutputTop(completed, total);
  57. std::string reason;
  58. bool passed = true;
  59. int res = this->TestProcess->GetProcessStatus();
  60. int retVal = this->TestProcess->GetExitValue();
  61. std::vector<std::pair<cmsys::RegularExpression,
  62. std::string> >::iterator passIt;
  63. bool forceFail = false;
  64. if ( this->TestProperties->RequiredRegularExpressions.size() > 0 )
  65. {
  66. bool found = false;
  67. for ( passIt = this->TestProperties->RequiredRegularExpressions.begin();
  68. passIt != this->TestProperties->RequiredRegularExpressions.end();
  69. ++ passIt )
  70. {
  71. if ( passIt->first.find(this->ProcessOutput.c_str()) )
  72. {
  73. found = true;
  74. reason = "Required regular expression found.";
  75. }
  76. }
  77. if ( !found )
  78. {
  79. reason = "Required regular expression not found.";
  80. forceFail = true;
  81. }
  82. reason += "Regex=[";
  83. for ( passIt = this->TestProperties->RequiredRegularExpressions.begin();
  84. passIt != this->TestProperties->RequiredRegularExpressions.end();
  85. ++ passIt )
  86. {
  87. reason += passIt->second;
  88. reason += "\n";
  89. }
  90. reason += "]";
  91. }
  92. if ( this->TestProperties->ErrorRegularExpressions.size() > 0 )
  93. {
  94. for ( passIt = this->TestProperties->ErrorRegularExpressions.begin();
  95. passIt != this->TestProperties->ErrorRegularExpressions.end();
  96. ++ passIt )
  97. {
  98. if ( passIt->first.find(this->ProcessOutput.c_str()) )
  99. {
  100. reason = "Error regular expression found in output.";
  101. reason += " Regex=[";
  102. reason += passIt->second;
  103. reason += "]";
  104. forceFail = true;
  105. }
  106. }
  107. }
  108. if (res == cmsysProcess_State_Exited)
  109. {
  110. bool success =
  111. !forceFail && (retVal == 0 ||
  112. this->TestProperties->RequiredRegularExpressions.size());
  113. if((success && !this->TestProperties->WillFail)
  114. || (!success && this->TestProperties->WillFail))
  115. {
  116. this->TestResult.Status = cmCTestTestHandler::COMPLETED;
  117. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Passed " );
  118. }
  119. else
  120. {
  121. this->TestResult.Status = cmCTestTestHandler::FAILED;
  122. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Failed " << reason );
  123. }
  124. }
  125. else if ( res == cmsysProcess_State_Expired )
  126. {
  127. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Timeout");
  128. this->TestResult.Status = cmCTestTestHandler::TIMEOUT;
  129. }
  130. else if ( res == cmsysProcess_State_Exception )
  131. {
  132. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Exception: ");
  133. switch ( retVal )
  134. {
  135. case cmsysProcess_Exception_Fault:
  136. cmCTestLog(this->CTest, HANDLER_OUTPUT, "SegFault");
  137. this->TestResult.Status = cmCTestTestHandler::SEGFAULT;
  138. break;
  139. case cmsysProcess_Exception_Illegal:
  140. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Illegal");
  141. this->TestResult.Status = cmCTestTestHandler::ILLEGAL;
  142. break;
  143. case cmsysProcess_Exception_Interrupt:
  144. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Interrupt");
  145. this->TestResult.Status = cmCTestTestHandler::INTERRUPT;
  146. break;
  147. case cmsysProcess_Exception_Numerical:
  148. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Numerical");
  149. this->TestResult.Status = cmCTestTestHandler::NUMERICAL;
  150. break;
  151. default:
  152. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Other");
  153. this->TestResult.Status = cmCTestTestHandler::OTHER_FAULT;
  154. }
  155. }
  156. else // if ( res == cmsysProcess_State_Error )
  157. {
  158. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Bad command " << res );
  159. this->TestResult.Status = cmCTestTestHandler::BAD_COMMAND;
  160. }
  161. passed = this->TestResult.Status == cmCTestTestHandler::COMPLETED;
  162. char buf[1024];
  163. sprintf(buf, "%6.2f sec", this->TestProcess->GetTotalTime());
  164. cmCTestLog(this->CTest, HANDLER_OUTPUT, buf << "\n" );
  165. if ( this->TestHandler->LogFile )
  166. {
  167. *this->TestHandler->LogFile << "Test time = " << buf << std::endl;
  168. }
  169. this->DartProcessing();
  170. // if this is doing MemCheck then all the output needs to be put into
  171. // Output since that is what is parsed by cmCTestMemCheckHandler
  172. if(!this->TestHandler->MemCheck)
  173. {
  174. if ( this->TestResult.Status == cmCTestTestHandler::COMPLETED )
  175. {
  176. this->TestHandler->CleanTestOutput(this->ProcessOutput,
  177. static_cast<size_t>
  178. (this->TestHandler->CustomMaximumPassedTestOutputSize));
  179. }
  180. else
  181. {
  182. this->TestHandler->CleanTestOutput(this->ProcessOutput,
  183. static_cast<size_t>
  184. (this->TestHandler->CustomMaximumFailedTestOutputSize));
  185. }
  186. }
  187. this->TestResult.Reason = reason;
  188. if ( this->TestHandler->LogFile )
  189. {
  190. bool pass = true;
  191. const char* reasonType = "Test Pass Reason";
  192. if(this->TestResult.Status != cmCTestTestHandler::COMPLETED &&
  193. this->TestResult.Status != cmCTestTestHandler::NOT_RUN)
  194. {
  195. reasonType = "Test Fail Reason";
  196. pass = false;
  197. }
  198. double ttime = this->TestProcess->GetTotalTime();
  199. int hours = static_cast<int>(ttime / (60 * 60));
  200. int minutes = static_cast<int>(ttime / 60) % 60;
  201. int seconds = static_cast<int>(ttime) % 60;
  202. char buffer[100];
  203. sprintf(buffer, "%02d:%02d:%02d", hours, minutes, seconds);
  204. *this->TestHandler->LogFile
  205. << "----------------------------------------------------------"
  206. << std::endl;
  207. if(this->TestResult.Reason.size())
  208. {
  209. *this->TestHandler->LogFile << reasonType << ":\n"
  210. << this->TestResult.Reason << "\n";
  211. }
  212. else
  213. {
  214. if(pass)
  215. {
  216. *this->TestHandler->LogFile << "Test Passed.\n";
  217. }
  218. else
  219. {
  220. *this->TestHandler->LogFile << "Test Failed.\n";
  221. }
  222. }
  223. *this->TestHandler->LogFile << "\"" << this->TestProperties->Name.c_str()
  224. << "\" end time: " << this->CTest->CurrentTime() << std::endl
  225. << "\"" << this->TestProperties->Name.c_str() << "\" time elapsed: "
  226. << buffer << std::endl
  227. << "----------------------------------------------------------"
  228. << std::endl << std::endl;
  229. }
  230. this->TestResult.Output = this->ProcessOutput;
  231. this->TestResult.ReturnValue = this->TestProcess->GetExitValue();
  232. this->TestResult.CompletionStatus = "Completed";
  233. this->TestResult.ExecutionTime = this->TestProcess->GetTotalTime();
  234. this->TestHandler->TestResults.push_back( this->TestResult );
  235. this->MemCheckPostProcess();
  236. delete this->TestProcess;
  237. return passed;
  238. }
  239. //--------------------------------------------------------------
  240. void cmCTestRunTest::MemCheckPostProcess()
  241. {
  242. if(!this->TestHandler->MemCheck)
  243. {
  244. return;
  245. }
  246. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, this->Index
  247. << ": process test output now: "
  248. << this->TestProperties->Name.c_str() << " "
  249. << this->TestResult.Name.c_str() << std::endl);
  250. cmCTestMemCheckHandler * handler = static_cast<cmCTestMemCheckHandler*>
  251. (this->TestHandler);
  252. if(handler->MemoryTesterStyle == cmCTestMemCheckHandler::BOUNDS_CHECKER)
  253. {
  254. handler->PostProcessBoundsCheckerTest(this->TestResult);
  255. }
  256. else if(handler->MemoryTesterStyle == cmCTestMemCheckHandler::PURIFY)
  257. {
  258. handler->PostProcessPurifyTest(this->TestResult);
  259. }
  260. }
  261. void cmCTestRunTest::SetTestHandler(cmCTestTestHandler * handler)
  262. {
  263. this->TestHandler = handler;
  264. this->CTest = handler->CTest;
  265. }
  266. //----------------------------------------------------------------------
  267. // Starts the execution of a test. Returns once it has started
  268. bool cmCTestRunTest::StartTest()
  269. {
  270. this->ComputeArguments();
  271. std::vector<std::string>& args = this->TestProperties->Args;
  272. this->TestResult.Properties = this->TestProperties;
  273. this->TestResult.ExecutionTime = 0;
  274. this->TestResult.ReturnValue = -1;
  275. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  276. this->TestResult.TestCount = this->TestProperties->Index;
  277. this->TestResult.Name = this->TestProperties->Name;
  278. this->TestResult.Path = this->TestProperties->Directory.c_str();
  279. // continue if we did not find the executable
  280. if (this->TestCommand == "")
  281. {
  282. *this->TestHandler->LogFile << "Unable to find executable: "
  283. << args[1].c_str() << std::endl;
  284. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unable to find executable: "
  285. << args[1].c_str() << std::endl);
  286. this->TestResult.Output = "Unable to find executable: " + args[1];
  287. if ( !this->CTest->GetShowOnly() )
  288. {
  289. this->TestResult.FullCommandLine = this->ActualCommand;
  290. this->TestHandler->TestResults.push_back( this->TestResult );
  291. return false;
  292. }
  293. }
  294. this->StartTime = this->CTest->CurrentTime();
  295. return this->CreateProcess(this->TestProperties->Timeout,
  296. &this->TestProperties->Environment);
  297. }
  298. void cmCTestRunTest::ComputeArguments()
  299. {
  300. std::vector<std::string>::const_iterator j =
  301. this->TestProperties->Args.begin();
  302. ++j; // skip test name
  303. // find the test executable
  304. if(this->TestHandler->MemCheck)
  305. {
  306. cmCTestMemCheckHandler * handler = static_cast<cmCTestMemCheckHandler*>
  307. (this->TestHandler);
  308. this->ActualCommand = handler->MemoryTester.c_str();
  309. }
  310. else
  311. {
  312. this->ActualCommand =
  313. this->TestHandler->FindTheExecutable(
  314. this->TestProperties->Args[1].c_str());
  315. ++j; //skip the executable (it will be actualCommand)
  316. }
  317. this->TestCommand
  318. = cmSystemTools::ConvertToOutputPath(this->ActualCommand.c_str());
  319. //Prepends memcheck args to our command string
  320. this->TestHandler->GenerateTestCommand(this->Arguments);
  321. for(std::vector<std::string>::iterator i = this->Arguments.begin();
  322. i != this->Arguments.end(); ++i)
  323. {
  324. this->TestCommand += " ";
  325. this->TestCommand += cmSystemTools::EscapeSpaces(j->c_str());
  326. }
  327. for(;j != this->TestProperties->Args.end(); ++j)
  328. {
  329. this->TestCommand += " ";
  330. this->TestCommand += cmSystemTools::EscapeSpaces(j->c_str());
  331. this->Arguments.push_back(*j);
  332. }
  333. this->TestResult.FullCommandLine = this->TestCommand;
  334. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl
  335. << this->Index << ": "
  336. << (this->TestHandler->MemCheck?"MemCheck":"Test")
  337. << " command: " << this->TestCommand
  338. << std::endl);
  339. }
  340. //----------------------------------------------------------------------
  341. void cmCTestRunTest::DartProcessing()
  342. {
  343. if (!this->ProcessOutput.empty() &&
  344. this->ProcessOutput.find("<DartMeasurement") != this->ProcessOutput.npos)
  345. {
  346. if (this->TestHandler->DartStuff.find(this->ProcessOutput.c_str()))
  347. {
  348. std::string dartString = this->TestHandler->DartStuff.match(1);
  349. // keep searching and replacing until none are left
  350. while (this->TestHandler->DartStuff1.find(this->ProcessOutput.c_str()))
  351. {
  352. // replace the exact match for the string
  353. cmSystemTools::ReplaceString(this->ProcessOutput,
  354. this->TestHandler->DartStuff1.match(1).c_str(), "");
  355. }
  356. this->TestResult.RegressionImages
  357. = this->TestHandler->GenerateRegressionImages(dartString);
  358. }
  359. }
  360. }
  361. //----------------------------------------------------------------------
  362. bool cmCTestRunTest::CreateProcess(double testTimeOut,
  363. std::vector<std::string>* environment)
  364. {
  365. this->TestProcess = new cmProcess;
  366. this->TestProcess->SetId(this->Index);
  367. this->TestProcess->SetWorkingDirectory(this->TestProperties->Directory.c_str());
  368. this->TestProcess->SetCommand(this->ActualCommand.c_str());
  369. this->TestProcess->SetCommandArguments(this->Arguments);
  370. std::vector<std::string> origEnv;
  371. this->ModifyEnv = (environment && environment->size()>0);
  372. // determine how much time we have
  373. double timeout = this->CTest->GetRemainingTimeAllowed() - 120;
  374. if (this->CTest->GetTimeOut() && this->CTest->GetTimeOut() < timeout)
  375. {
  376. timeout = this->CTest->GetTimeOut();
  377. }
  378. if (testTimeOut
  379. && testTimeOut < this->CTest->GetRemainingTimeAllowed())
  380. {
  381. timeout = testTimeOut;
  382. }
  383. // always have at least 1 second if we got to here
  384. if (timeout <= 0)
  385. {
  386. timeout = 1;
  387. }
  388. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, this->Index << ": "
  389. << "Test timeout computed to be: " << timeout << "\n");
  390. if (this->ModifyEnv)
  391. {
  392. this->OrigEnv = cmSystemTools::AppendEnv(environment);
  393. }
  394. return this->TestProcess->StartProcess();
  395. }
  396. void cmCTestRunTest::WriteLogOutputTop(int completed, int total)
  397. {
  398. /* Not sure whether we want to prepend the test index anymore
  399. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(3)
  400. << this->Index << ": ");*/
  401. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(3)
  402. << completed << "/");
  403. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(3)
  404. << total << " ");
  405. if ( this->TestHandler->MemCheck )
  406. {
  407. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Memory Check");
  408. }
  409. else
  410. {
  411. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Testing");
  412. }
  413. cmOStringStream indexStr;
  414. indexStr << " (" << this->Index << ")";
  415. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(6)
  416. << indexStr.str().c_str());
  417. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  418. const int maxTestNameWidth = this->CTest->GetMaxTestNameWidth();
  419. std::string outname = this->TestProperties->Name + " ";
  420. outname.resize(maxTestNameWidth, '.');
  421. *this->TestHandler->LogFile << this->TestProperties->Index << "/"
  422. << this->TestHandler->TotalNumberOfTests << " Testing: "
  423. << this->TestProperties->Name << std::endl;
  424. *this->TestHandler->LogFile << this->TestProperties->Index << "/"
  425. << this->TestHandler->TotalNumberOfTests
  426. << " Test: " << this->TestProperties->Name.c_str() << std::endl;
  427. *this->TestHandler->LogFile << "Command: \"" << this->ActualCommand << "\"";
  428. for (std::vector<std::string>::iterator i = this->Arguments.begin();
  429. i != this->Arguments.end(); ++i)
  430. {
  431. *this->TestHandler->LogFile
  432. << " \"" << i->c_str() << "\"";
  433. }
  434. *this->TestHandler->LogFile << std::endl
  435. << "Directory: " << this->TestProperties->Directory << std::endl
  436. << "\"" << this->TestProperties->Name.c_str() << "\" start time: "
  437. << this->StartTime << std::endl;
  438. *this->TestHandler->LogFile
  439. << "Output:" << std::endl
  440. << "----------------------------------------------------------"
  441. << std::endl;
  442. *this->TestHandler->LogFile
  443. << this->ProcessOutput.c_str() << "<end of output>" << std::endl;
  444. cmCTestLog(this->CTest, HANDLER_OUTPUT, outname.c_str());
  445. cmCTestLog(this->CTest, DEBUG, "Testing "
  446. << this->TestProperties->Name.c_str() << " ... ");
  447. }