cmCTestRunTest.cxx 16 KB

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