cmCTestRunTest.cxx 17 KB

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