cmCTestRunTest.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. 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) )
  40. != cmsysProcess_Pipe_Timeout)
  41. {
  42. if(pipe == cmsysProcess_Pipe_STDOUT ||
  43. pipe == cmsysProcess_Pipe_STDERR)
  44. {
  45. if(gotStdErr)
  46. {
  47. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  48. this->GetIndex() << ": " << err << std::endl);
  49. this->ProcessOutput += err;
  50. this->ProcessOutput += "\n";
  51. }
  52. if(gotStdOut)
  53. {
  54. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  55. this->GetIndex() << ": " << out << std::endl);
  56. this->ProcessOutput += out;
  57. this->ProcessOutput += "\n";
  58. }
  59. }
  60. gotStdOut = false;
  61. gotStdErr = false;
  62. //timeout while reading process output (could denote infinite output)
  63. if(cmSystemTools::GetTime() - clock_start > .1)
  64. {
  65. break;
  66. }
  67. }
  68. }
  69. //---------------------------------------------------------
  70. bool cmCTestRunTest::EndTest(size_t completed, size_t total, bool started)
  71. {
  72. //restore the old environment
  73. if (this->ModifyEnv)
  74. {
  75. cmSystemTools::RestoreEnv(this->OrigEnv);
  76. }
  77. this->WriteLogOutputTop(completed, total);
  78. std::string reason;
  79. bool passed = true;
  80. int res = this->TestProcess->GetProcessStatus();
  81. int retVal = this->TestProcess->GetExitValue();
  82. std::vector<std::pair<cmsys::RegularExpression,
  83. std::string> >::iterator passIt;
  84. bool forceFail = false;
  85. if ( this->TestProperties->RequiredRegularExpressions.size() > 0 )
  86. {
  87. bool found = false;
  88. for ( passIt = this->TestProperties->RequiredRegularExpressions.begin();
  89. passIt != this->TestProperties->RequiredRegularExpressions.end();
  90. ++ passIt )
  91. {
  92. if ( passIt->first.find(this->ProcessOutput.c_str()) )
  93. {
  94. found = true;
  95. reason = "Required regular expression found.";
  96. }
  97. }
  98. if ( !found )
  99. {
  100. reason = "Required regular expression not found.";
  101. forceFail = true;
  102. }
  103. reason += "Regex=[";
  104. for ( passIt = this->TestProperties->RequiredRegularExpressions.begin();
  105. passIt != this->TestProperties->RequiredRegularExpressions.end();
  106. ++ passIt )
  107. {
  108. reason += passIt->second;
  109. reason += "\n";
  110. }
  111. reason += "]";
  112. }
  113. if ( this->TestProperties->ErrorRegularExpressions.size() > 0 )
  114. {
  115. for ( passIt = this->TestProperties->ErrorRegularExpressions.begin();
  116. passIt != this->TestProperties->ErrorRegularExpressions.end();
  117. ++ passIt )
  118. {
  119. if ( passIt->first.find(this->ProcessOutput.c_str()) )
  120. {
  121. reason = "Error regular expression found in output.";
  122. reason += " Regex=[";
  123. reason += passIt->second;
  124. reason += "]";
  125. forceFail = true;
  126. }
  127. }
  128. }
  129. if (res == cmsysProcess_State_Exited)
  130. {
  131. bool success =
  132. !forceFail && (retVal == 0 ||
  133. this->TestProperties->RequiredRegularExpressions.size());
  134. if((success && !this->TestProperties->WillFail)
  135. || (!success && this->TestProperties->WillFail))
  136. {
  137. this->TestResult.Status = cmCTestTestHandler::COMPLETED;
  138. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Passed " );
  139. }
  140. else
  141. {
  142. this->TestResult.Status = cmCTestTestHandler::FAILED;
  143. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Failed " << reason );
  144. }
  145. }
  146. else if ( res == cmsysProcess_State_Expired )
  147. {
  148. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Timeout");
  149. this->TestResult.Status = cmCTestTestHandler::TIMEOUT;
  150. }
  151. else if ( res == cmsysProcess_State_Exception )
  152. {
  153. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Exception: ");
  154. switch ( retVal )
  155. {
  156. case cmsysProcess_Exception_Fault:
  157. cmCTestLog(this->CTest, HANDLER_OUTPUT, "SegFault");
  158. this->TestResult.Status = cmCTestTestHandler::SEGFAULT;
  159. break;
  160. case cmsysProcess_Exception_Illegal:
  161. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Illegal");
  162. this->TestResult.Status = cmCTestTestHandler::ILLEGAL;
  163. break;
  164. case cmsysProcess_Exception_Interrupt:
  165. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Interrupt");
  166. this->TestResult.Status = cmCTestTestHandler::INTERRUPT;
  167. break;
  168. case cmsysProcess_Exception_Numerical:
  169. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Numerical");
  170. this->TestResult.Status = cmCTestTestHandler::NUMERICAL;
  171. break;
  172. default:
  173. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Other");
  174. this->TestResult.Status = cmCTestTestHandler::OTHER_FAULT;
  175. }
  176. }
  177. else // if ( res == cmsysProcess_State_Error )
  178. {
  179. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Bad command " << res );
  180. this->TestResult.Status = cmCTestTestHandler::BAD_COMMAND;
  181. }
  182. passed = this->TestResult.Status == cmCTestTestHandler::COMPLETED;
  183. char buf[1024];
  184. sprintf(buf, "%6.2f sec", this->TestProcess->GetTotalTime());
  185. cmCTestLog(this->CTest, HANDLER_OUTPUT, buf << "\n" );
  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)
  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. }
  259. this->MemCheckPostProcess();
  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. void cmCTestRunTest::SetTestHandler(cmCTestTestHandler * handler)
  286. {
  287. this->TestHandler = handler;
  288. this->CTest = handler->CTest;
  289. }
  290. //----------------------------------------------------------------------
  291. // Starts the execution of a test. Returns once it has started
  292. bool cmCTestRunTest::StartTest()
  293. {
  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. // continue if we did not find the executable
  305. if (this->TestCommand == "")
  306. {
  307. *this->TestHandler->LogFile << "Unable to find executable: "
  308. << args[1].c_str() << std::endl;
  309. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unable to find executable: "
  310. << args[1].c_str() << std::endl);
  311. this->TestResult.Output = "Unable to find executable: " + args[1];
  312. if ( !this->CTest->GetShowOnly() )
  313. {
  314. this->TestResult.FullCommandLine = this->ActualCommand;
  315. this->TestHandler->TestResults.push_back( this->TestResult );
  316. return false;
  317. }
  318. }
  319. this->StartTime = this->CTest->CurrentTime();
  320. return this->CreateProcess(this->TestProperties->Timeout,
  321. &this->TestProperties->Environment);
  322. }
  323. void cmCTestRunTest::ComputeArguments()
  324. {
  325. std::vector<std::string>::const_iterator j =
  326. this->TestProperties->Args.begin();
  327. ++j; // skip test name
  328. // find the test executable
  329. if(this->TestHandler->MemCheck)
  330. {
  331. cmCTestMemCheckHandler * handler = static_cast<cmCTestMemCheckHandler*>
  332. (this->TestHandler);
  333. this->ActualCommand = handler->MemoryTester.c_str();
  334. }
  335. else
  336. {
  337. this->ActualCommand =
  338. this->TestHandler->FindTheExecutable(
  339. this->TestProperties->Args[1].c_str());
  340. ++j; //skip the executable (it will be actualCommand)
  341. }
  342. this->TestCommand
  343. = cmSystemTools::ConvertToOutputPath(this->ActualCommand.c_str());
  344. //Prepends memcheck args to our command string
  345. this->TestHandler->GenerateTestCommand(this->Arguments);
  346. for(std::vector<std::string>::iterator i = this->Arguments.begin();
  347. i != this->Arguments.end(); ++i)
  348. {
  349. this->TestCommand += " ";
  350. this->TestCommand += cmSystemTools::EscapeSpaces(j->c_str());
  351. }
  352. for(;j != this->TestProperties->Args.end(); ++j)
  353. {
  354. this->TestCommand += " ";
  355. this->TestCommand += cmSystemTools::EscapeSpaces(j->c_str());
  356. this->Arguments.push_back(*j);
  357. }
  358. this->TestResult.FullCommandLine = this->TestCommand;
  359. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl
  360. << this->Index << ": "
  361. << (this->TestHandler->MemCheck?"MemCheck":"Test")
  362. << " command: " << this->TestCommand
  363. << std::endl);
  364. }
  365. //----------------------------------------------------------------------
  366. void cmCTestRunTest::DartProcessing()
  367. {
  368. if (!this->ProcessOutput.empty() &&
  369. this->ProcessOutput.find("<DartMeasurement") != this->ProcessOutput.npos)
  370. {
  371. if (this->TestHandler->DartStuff.find(this->ProcessOutput.c_str()))
  372. {
  373. std::string dartString = this->TestHandler->DartStuff.match(1);
  374. // keep searching and replacing until none are left
  375. while (this->TestHandler->DartStuff1.find(this->ProcessOutput.c_str()))
  376. {
  377. // replace the exact match for the string
  378. cmSystemTools::ReplaceString(this->ProcessOutput,
  379. this->TestHandler->DartStuff1.match(1).c_str(), "");
  380. }
  381. this->TestResult.RegressionImages
  382. = this->TestHandler->GenerateRegressionImages(dartString);
  383. }
  384. }
  385. }
  386. //----------------------------------------------------------------------
  387. bool cmCTestRunTest::CreateProcess(double testTimeOut,
  388. std::vector<std::string>* environment)
  389. {
  390. this->TestProcess = new cmProcess;
  391. this->TestProcess->SetId(this->Index);
  392. this->TestProcess->SetWorkingDirectory(
  393. this->TestProperties->Directory.c_str());
  394. this->TestProcess->SetCommand(this->ActualCommand.c_str());
  395. this->TestProcess->SetCommandArguments(this->Arguments);
  396. std::vector<std::string> origEnv;
  397. this->ModifyEnv = (environment && environment->size()>0);
  398. // determine how much time we have
  399. double timeout = this->CTest->GetRemainingTimeAllowed() - 120;
  400. if (this->CTest->GetTimeOut() && this->CTest->GetTimeOut() < timeout)
  401. {
  402. timeout = this->CTest->GetTimeOut();
  403. }
  404. if (testTimeOut
  405. && testTimeOut < this->CTest->GetRemainingTimeAllowed())
  406. {
  407. timeout = testTimeOut;
  408. }
  409. // always have at least 1 second if we got to here
  410. if (timeout <= 0)
  411. {
  412. timeout = 1;
  413. }
  414. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, this->Index << ": "
  415. << "Test timeout computed to be: " << timeout << "\n");
  416. if (this->ModifyEnv)
  417. {
  418. this->OrigEnv = cmSystemTools::AppendEnv(environment);
  419. }
  420. return this->TestProcess->StartProcess();
  421. }
  422. void cmCTestRunTest::WriteLogOutputTop(size_t completed, size_t total)
  423. {
  424. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(getNumWidth(total))
  425. << completed << "/");
  426. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(getNumWidth(total))
  427. << total << " ");
  428. if ( this->TestHandler->MemCheck )
  429. {
  430. cmCTestLog(this->CTest, HANDLER_OUTPUT, "MemCheck");
  431. }
  432. else
  433. {
  434. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Test");
  435. }
  436. cmOStringStream indexStr;
  437. indexStr << " #" << this->Index << ":";
  438. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  439. std::setw(3 + getNumWidth(this->TestHandler->GetMaxIndex()))
  440. << indexStr.str().c_str());
  441. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  442. const int maxTestNameWidth = this->CTest->GetMaxTestNameWidth();
  443. std::string outname = this->TestProperties->Name + " ";
  444. outname.resize(maxTestNameWidth, '.');
  445. *this->TestHandler->LogFile << this->TestProperties->Index << "/"
  446. << this->TestHandler->TotalNumberOfTests << " Testing: "
  447. << this->TestProperties->Name << std::endl;
  448. *this->TestHandler->LogFile << this->TestProperties->Index << "/"
  449. << this->TestHandler->TotalNumberOfTests
  450. << " Test: " << this->TestProperties->Name.c_str() << std::endl;
  451. *this->TestHandler->LogFile << "Command: \"" << this->ActualCommand << "\"";
  452. for (std::vector<std::string>::iterator i = this->Arguments.begin();
  453. i != this->Arguments.end(); ++i)
  454. {
  455. *this->TestHandler->LogFile
  456. << " \"" << i->c_str() << "\"";
  457. }
  458. *this->TestHandler->LogFile << std::endl
  459. << "Directory: " << this->TestProperties->Directory << std::endl
  460. << "\"" << this->TestProperties->Name.c_str() << "\" start time: "
  461. << this->StartTime << std::endl;
  462. *this->TestHandler->LogFile
  463. << "Output:" << std::endl
  464. << "----------------------------------------------------------"
  465. << std::endl;
  466. *this->TestHandler->LogFile
  467. << this->ProcessOutput.c_str() << "<end of output>" << std::endl;
  468. cmCTestLog(this->CTest, HANDLER_OUTPUT, outname.c_str());
  469. cmCTestLog(this->CTest, DEBUG, "Testing "
  470. << this->TestProperties->Name.c_str() << " ... ");
  471. }