cmCTestRunTest.cxx 17 KB

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