cmCTestRunTest.cxx 17 KB

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