cmCTestRunTest.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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(size_t total)
  288. {
  289. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(2*getNumWidth(total) + 8)
  290. << "Start "
  291. << std::setw(getNumWidth(total))
  292. << this->TestProperties->Index << ": "
  293. << this->TestProperties->Name << std::endl);
  294. this->ComputeArguments();
  295. std::vector<std::string>& args = this->TestProperties->Args;
  296. this->TestResult.Properties = this->TestProperties;
  297. this->TestResult.ExecutionTime = 0;
  298. this->TestResult.ReturnValue = -1;
  299. this->TestResult.CompletionStatus = "Not Run";
  300. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  301. this->TestResult.TestCount = this->TestProperties->Index;
  302. this->TestResult.Name = this->TestProperties->Name;
  303. this->TestResult.Path = this->TestProperties->Directory.c_str();
  304. // log and return if we did not find the executable
  305. if (this->ActualCommand == "")
  306. {
  307. this->TestProcess = new cmProcess;
  308. *this->TestHandler->LogFile << "Unable to find executable: "
  309. << args[1].c_str() << std::endl;
  310. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unable to find executable: "
  311. << args[1].c_str() << std::endl);
  312. this->TestResult.Output = "Unable to find executable: " + args[1];
  313. this->TestResult.FullCommandLine = "";
  314. this->TestHandler->TestResults.push_back(this->TestResult);
  315. return false;
  316. }
  317. this->StartTime = this->CTest->CurrentTime();
  318. return this->CreateProcess(this->TestProperties->Timeout,
  319. &this->TestProperties->Environment);
  320. }
  321. void cmCTestRunTest::ComputeArguments()
  322. {
  323. std::vector<std::string>::const_iterator j =
  324. this->TestProperties->Args.begin();
  325. ++j; // skip test name
  326. // find the test executable
  327. if(this->TestHandler->MemCheck)
  328. {
  329. cmCTestMemCheckHandler * handler = static_cast<cmCTestMemCheckHandler*>
  330. (this->TestHandler);
  331. this->ActualCommand = handler->MemoryTester.c_str();
  332. }
  333. else
  334. {
  335. this->ActualCommand =
  336. this->TestHandler->FindTheExecutable(
  337. this->TestProperties->Args[1].c_str());
  338. ++j; //skip the executable (it will be actualCommand)
  339. }
  340. this->TestCommand
  341. = cmSystemTools::ConvertToOutputPath(this->ActualCommand.c_str());
  342. //Prepends memcheck args to our command string
  343. this->TestHandler->GenerateTestCommand(this->Arguments);
  344. for(std::vector<std::string>::iterator i = this->Arguments.begin();
  345. i != this->Arguments.end(); ++i)
  346. {
  347. this->TestCommand += " ";
  348. this->TestCommand += cmSystemTools::EscapeSpaces(j->c_str());
  349. }
  350. for(;j != this->TestProperties->Args.end(); ++j)
  351. {
  352. this->TestCommand += " ";
  353. this->TestCommand += cmSystemTools::EscapeSpaces(j->c_str());
  354. this->Arguments.push_back(*j);
  355. }
  356. this->TestResult.FullCommandLine = this->TestCommand;
  357. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl
  358. << this->Index << ": "
  359. << (this->TestHandler->MemCheck?"MemCheck":"Test")
  360. << " command: " << this->TestCommand
  361. << std::endl);
  362. }
  363. //----------------------------------------------------------------------
  364. void cmCTestRunTest::DartProcessing()
  365. {
  366. if (!this->ProcessOutput.empty() &&
  367. this->ProcessOutput.find("<DartMeasurement") != this->ProcessOutput.npos)
  368. {
  369. if (this->TestHandler->DartStuff.find(this->ProcessOutput.c_str()))
  370. {
  371. std::string dartString = this->TestHandler->DartStuff.match(1);
  372. // keep searching and replacing until none are left
  373. while (this->TestHandler->DartStuff1.find(this->ProcessOutput.c_str()))
  374. {
  375. // replace the exact match for the string
  376. cmSystemTools::ReplaceString(this->ProcessOutput,
  377. this->TestHandler->DartStuff1.match(1).c_str(), "");
  378. }
  379. this->TestResult.RegressionImages
  380. = this->TestHandler->GenerateRegressionImages(dartString);
  381. }
  382. }
  383. }
  384. //----------------------------------------------------------------------
  385. bool cmCTestRunTest::CreateProcess(double testTimeOut,
  386. std::vector<std::string>* environment)
  387. {
  388. this->TestProcess = new cmProcess;
  389. this->TestProcess->SetId(this->Index);
  390. this->TestProcess->SetWorkingDirectory(
  391. this->TestProperties->Directory.c_str());
  392. this->TestProcess->SetCommand(this->ActualCommand.c_str());
  393. this->TestProcess->SetCommandArguments(this->Arguments);
  394. std::vector<std::string> origEnv;
  395. this->ModifyEnv = (environment && environment->size()>0);
  396. // determine how much time we have
  397. double timeout = this->CTest->GetRemainingTimeAllowed() - 120;
  398. if (this->CTest->GetTimeOut() && this->CTest->GetTimeOut() < timeout)
  399. {
  400. timeout = this->CTest->GetTimeOut();
  401. }
  402. if (testTimeOut
  403. && testTimeOut < this->CTest->GetRemainingTimeAllowed())
  404. {
  405. timeout = testTimeOut;
  406. }
  407. // always have at least 1 second if we got to here
  408. if (timeout <= 0)
  409. {
  410. timeout = 1;
  411. }
  412. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, this->Index << ": "
  413. << "Test timeout computed to be: " << timeout << "\n");
  414. this->TestProcess->SetTimeout(timeout);
  415. if (this->ModifyEnv)
  416. {
  417. this->OrigEnv = cmSystemTools::AppendEnv(environment);
  418. }
  419. return this->TestProcess->StartProcess();
  420. }
  421. void cmCTestRunTest::WriteLogOutputTop(size_t completed, size_t total)
  422. {
  423. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(getNumWidth(total))
  424. << completed << "/");
  425. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(getNumWidth(total))
  426. << total << " ");
  427. if ( this->TestHandler->MemCheck )
  428. {
  429. cmCTestLog(this->CTest, HANDLER_OUTPUT, "MemCheck");
  430. }
  431. else
  432. {
  433. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Test");
  434. }
  435. cmOStringStream indexStr;
  436. indexStr << " #" << this->Index << ":";
  437. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  438. std::setw(3 + getNumWidth(this->TestHandler->GetMaxIndex()))
  439. << indexStr.str().c_str());
  440. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  441. const int maxTestNameWidth = this->CTest->GetMaxTestNameWidth();
  442. std::string outname = this->TestProperties->Name + " ";
  443. outname.resize(maxTestNameWidth + 4, '.');
  444. *this->TestHandler->LogFile << this->TestProperties->Index << "/"
  445. << this->TestHandler->TotalNumberOfTests << " Testing: "
  446. << this->TestProperties->Name << std::endl;
  447. *this->TestHandler->LogFile << this->TestProperties->Index << "/"
  448. << this->TestHandler->TotalNumberOfTests
  449. << " Test: " << this->TestProperties->Name.c_str() << std::endl;
  450. *this->TestHandler->LogFile << "Command: \"" << this->ActualCommand << "\"";
  451. for (std::vector<std::string>::iterator i = this->Arguments.begin();
  452. i != this->Arguments.end(); ++i)
  453. {
  454. *this->TestHandler->LogFile
  455. << " \"" << i->c_str() << "\"";
  456. }
  457. *this->TestHandler->LogFile << std::endl
  458. << "Directory: " << this->TestProperties->Directory << std::endl
  459. << "\"" << this->TestProperties->Name.c_str() << "\" start time: "
  460. << this->StartTime << std::endl;
  461. *this->TestHandler->LogFile
  462. << "Output:" << std::endl
  463. << "----------------------------------------------------------"
  464. << std::endl;
  465. *this->TestHandler->LogFile
  466. << this->ProcessOutput.c_str() << "<end of output>" << std::endl;
  467. cmCTestLog(this->CTest, HANDLER_OUTPUT, outname.c_str());
  468. cmCTestLog(this->CTest, DEBUG, "Testing "
  469. << this->TestProperties->Name.c_str() << " ... ");
  470. }