cmCTestRunTest.cxx 17 KB

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