cmCTestRunTest.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. #include "cm_curl.h"
  15. #include <cm_zlib.h>
  16. #include <cmsys/Base64.h>
  17. cmCTestRunTest::cmCTestRunTest(cmCTestTestHandler* handler)
  18. {
  19. this->CTest = handler->CTest;
  20. this->TestHandler = handler;
  21. this->TestProcess = 0;
  22. this->TestResult.ExecutionTime =0;
  23. this->TestResult.ReturnValue = 0;
  24. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  25. this->TestResult.TestCount = 0;
  26. this->TestResult.Properties = 0;
  27. this->ProcessOutput = "";
  28. this->CompressedOutput = "";
  29. this->CompressionRatio = 2;
  30. this->StopTimePassed = false;
  31. this->NumberOfRunsLeft = 1; // default to 1 run of the test
  32. this->RunUntilFail = false; // default to run the test once
  33. this->RunAgain = false; // default to not having to run again
  34. }
  35. cmCTestRunTest::~cmCTestRunTest()
  36. {
  37. }
  38. //----------------------------------------------------------------------------
  39. bool cmCTestRunTest::CheckOutput()
  40. {
  41. // Read lines for up to 0.1 seconds of total time.
  42. double timeout = 0.1;
  43. double timeEnd = cmSystemTools::GetTime() + timeout;
  44. std::string line;
  45. while((timeout = timeEnd - cmSystemTools::GetTime(), timeout > 0))
  46. {
  47. int p = this->TestProcess->GetNextOutputLine(line, timeout);
  48. if(p == cmsysProcess_Pipe_None)
  49. {
  50. // Process has terminated and all output read.
  51. return false;
  52. }
  53. else if(p == cmsysProcess_Pipe_STDOUT)
  54. {
  55. // Store this line of output.
  56. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  57. this->GetIndex() << ": " << line << std::endl);
  58. this->ProcessOutput += line;
  59. this->ProcessOutput += "\n";
  60. // Check for TIMEOUT_AFTER_MATCH property.
  61. if (!this->TestProperties->TimeoutRegularExpressions.empty())
  62. {
  63. std::vector<std::pair<cmsys::RegularExpression,
  64. std::string> >::iterator regIt;
  65. for ( regIt = this->TestProperties->TimeoutRegularExpressions.begin();
  66. regIt != this->TestProperties->TimeoutRegularExpressions.end();
  67. ++ regIt )
  68. {
  69. if ( regIt->first.find(this->ProcessOutput.c_str()) )
  70. {
  71. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  72. this->GetIndex() << ": " <<
  73. "Test timeout changed to " <<
  74. this->TestProperties->AlternateTimeout << std::endl);
  75. this->TestProcess->ResetStartTime();
  76. this->TestProcess->ChangeTimeout(
  77. this->TestProperties->AlternateTimeout);
  78. this->TestProperties->TimeoutRegularExpressions.clear();
  79. break;
  80. }
  81. }
  82. }
  83. }
  84. else // if(p == cmsysProcess_Pipe_Timeout)
  85. {
  86. break;
  87. }
  88. }
  89. return true;
  90. }
  91. //---------------------------------------------------------
  92. // Streamed compression of test output. The compressed data
  93. // is appended to this->CompressedOutput
  94. void cmCTestRunTest::CompressOutput()
  95. {
  96. int ret;
  97. z_stream strm;
  98. unsigned char* in =
  99. reinterpret_cast<unsigned char*>(
  100. const_cast<char*>(this->ProcessOutput.c_str()));
  101. //zlib makes the guarantee that this is the maximum output size
  102. int outSize = static_cast<int>(
  103. static_cast<double>(this->ProcessOutput.size()) * 1.001 + 13.0);
  104. unsigned char* out = new unsigned char[outSize];
  105. strm.zalloc = Z_NULL;
  106. strm.zfree = Z_NULL;
  107. strm.opaque = Z_NULL;
  108. ret = deflateInit(&strm, -1); //default compression level
  109. if (ret != Z_OK)
  110. {
  111. delete[] out;
  112. return;
  113. }
  114. strm.avail_in = static_cast<uInt>(this->ProcessOutput.size());
  115. strm.next_in = in;
  116. strm.avail_out = outSize;
  117. strm.next_out = out;
  118. ret = deflate(&strm, Z_FINISH);
  119. if(ret == Z_STREAM_ERROR || ret != Z_STREAM_END)
  120. {
  121. cmCTestLog(this->CTest, ERROR_MESSAGE, "Error during output "
  122. "compression. Sending uncompressed output." << std::endl);
  123. delete[] out;
  124. return;
  125. }
  126. (void)deflateEnd(&strm);
  127. unsigned char *encoded_buffer
  128. = new unsigned char[static_cast<int>(outSize * 1.5)];
  129. size_t rlen
  130. = cmsysBase64_Encode(out, strm.total_out, encoded_buffer, 1);
  131. for(size_t i = 0; i < rlen; i++)
  132. {
  133. this->CompressedOutput += encoded_buffer[i];
  134. }
  135. if(strm.total_in)
  136. {
  137. this->CompressionRatio = static_cast<double>(strm.total_out) /
  138. static_cast<double>(strm.total_in);
  139. }
  140. delete [] encoded_buffer;
  141. delete [] out;
  142. }
  143. //---------------------------------------------------------
  144. bool cmCTestRunTest::EndTest(size_t completed, size_t total, bool started)
  145. {
  146. if ((!this->TestHandler->MemCheck &&
  147. this->CTest->ShouldCompressTestOutput()) ||
  148. (this->TestHandler->MemCheck &&
  149. this->CTest->ShouldCompressMemCheckOutput()))
  150. {
  151. this->CompressOutput();
  152. }
  153. this->WriteLogOutputTop(completed, total);
  154. std::string reason;
  155. bool passed = true;
  156. int res = started ? this->TestProcess->GetProcessStatus()
  157. : cmsysProcess_State_Error;
  158. int retVal = this->TestProcess->GetExitValue();
  159. std::vector<std::pair<cmsys::RegularExpression,
  160. std::string> >::iterator passIt;
  161. bool forceFail = false;
  162. bool outputTestErrorsToConsole = false;
  163. if (!this->TestProperties->RequiredRegularExpressions.empty())
  164. {
  165. bool found = false;
  166. for ( passIt = this->TestProperties->RequiredRegularExpressions.begin();
  167. passIt != this->TestProperties->RequiredRegularExpressions.end();
  168. ++ passIt )
  169. {
  170. if ( passIt->first.find(this->ProcessOutput.c_str()) )
  171. {
  172. found = true;
  173. reason = "Required regular expression found.";
  174. break;
  175. }
  176. }
  177. if ( !found )
  178. {
  179. reason = "Required regular expression not found.";
  180. forceFail = true;
  181. }
  182. reason += "Regex=[";
  183. for ( passIt = this->TestProperties->RequiredRegularExpressions.begin();
  184. passIt != this->TestProperties->RequiredRegularExpressions.end();
  185. ++ passIt )
  186. {
  187. reason += passIt->second;
  188. reason += "\n";
  189. }
  190. reason += "]";
  191. }
  192. if (!this->TestProperties->ErrorRegularExpressions.empty())
  193. {
  194. for ( passIt = this->TestProperties->ErrorRegularExpressions.begin();
  195. passIt != this->TestProperties->ErrorRegularExpressions.end();
  196. ++ passIt )
  197. {
  198. if ( passIt->first.find(this->ProcessOutput.c_str()) )
  199. {
  200. reason = "Error regular expression found in output.";
  201. reason += " Regex=[";
  202. reason += passIt->second;
  203. reason += "]";
  204. forceFail = true;
  205. break;
  206. }
  207. }
  208. }
  209. if (res == cmsysProcess_State_Exited)
  210. {
  211. bool success =
  212. !forceFail && (retVal == 0 ||
  213. this->TestProperties->RequiredRegularExpressions.size());
  214. if(this->TestProperties->SkipReturnCode >= 0
  215. && this->TestProperties->SkipReturnCode == retVal)
  216. {
  217. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  218. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Skipped ");
  219. }
  220. else if((success && !this->TestProperties->WillFail)
  221. || (!success && this->TestProperties->WillFail))
  222. {
  223. this->TestResult.Status = cmCTestTestHandler::COMPLETED;
  224. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Passed " );
  225. }
  226. else
  227. {
  228. this->TestResult.Status = cmCTestTestHandler::FAILED;
  229. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Failed " << reason );
  230. outputTestErrorsToConsole = this->CTest->OutputTestOutputOnTestFailure;
  231. }
  232. }
  233. else if ( res == cmsysProcess_State_Expired )
  234. {
  235. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Timeout ");
  236. this->TestResult.Status = cmCTestTestHandler::TIMEOUT;
  237. outputTestErrorsToConsole = this->CTest->OutputTestOutputOnTestFailure;
  238. }
  239. else if ( res == cmsysProcess_State_Exception )
  240. {
  241. outputTestErrorsToConsole = this->CTest->OutputTestOutputOnTestFailure;
  242. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Exception: ");
  243. switch(this->TestProcess->GetExitException())
  244. {
  245. case cmsysProcess_Exception_Fault:
  246. cmCTestLog(this->CTest, HANDLER_OUTPUT, "SegFault");
  247. this->TestResult.Status = cmCTestTestHandler::SEGFAULT;
  248. break;
  249. case cmsysProcess_Exception_Illegal:
  250. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Illegal");
  251. this->TestResult.Status = cmCTestTestHandler::ILLEGAL;
  252. break;
  253. case cmsysProcess_Exception_Interrupt:
  254. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Interrupt");
  255. this->TestResult.Status = cmCTestTestHandler::INTERRUPT;
  256. break;
  257. case cmsysProcess_Exception_Numerical:
  258. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Numerical");
  259. this->TestResult.Status = cmCTestTestHandler::NUMERICAL;
  260. break;
  261. default:
  262. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Other");
  263. this->TestResult.Status = cmCTestTestHandler::OTHER_FAULT;
  264. }
  265. }
  266. else //cmsysProcess_State_Error
  267. {
  268. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Not Run ");
  269. }
  270. passed = this->TestResult.Status == cmCTestTestHandler::COMPLETED;
  271. char buf[1024];
  272. sprintf(buf, "%6.2f sec", this->TestProcess->GetTotalTime());
  273. cmCTestLog(this->CTest, HANDLER_OUTPUT, buf << "\n" );
  274. if ( outputTestErrorsToConsole )
  275. {
  276. cmCTestLog(this->CTest, HANDLER_OUTPUT, this->ProcessOutput << std::endl );
  277. }
  278. if ( this->TestHandler->LogFile )
  279. {
  280. *this->TestHandler->LogFile << "Test time = " << buf << std::endl;
  281. }
  282. // Set the working directory to the tests directory
  283. std::string oldpath = cmSystemTools::GetCurrentWorkingDirectory();
  284. cmSystemTools::ChangeDirectory(this->TestProperties->Directory);
  285. this->DartProcessing();
  286. // restore working directory
  287. cmSystemTools::ChangeDirectory(oldpath);
  288. // if this is doing MemCheck then all the output needs to be put into
  289. // Output since that is what is parsed by cmCTestMemCheckHandler
  290. if(!this->TestHandler->MemCheck && started)
  291. {
  292. this->TestHandler->CleanTestOutput(this->ProcessOutput,
  293. static_cast<size_t>
  294. (this->TestResult.Status == cmCTestTestHandler::COMPLETED ?
  295. this->TestHandler->CustomMaximumPassedTestOutputSize :
  296. this->TestHandler->CustomMaximumFailedTestOutputSize));
  297. }
  298. this->TestResult.Reason = reason;
  299. if (this->TestHandler->LogFile)
  300. {
  301. bool pass = true;
  302. const char* reasonType = "Test Pass Reason";
  303. if(this->TestResult.Status != cmCTestTestHandler::COMPLETED &&
  304. this->TestResult.Status != cmCTestTestHandler::NOT_RUN)
  305. {
  306. reasonType = "Test Fail Reason";
  307. pass = false;
  308. }
  309. double ttime = this->TestProcess->GetTotalTime();
  310. int hours = static_cast<int>(ttime / (60 * 60));
  311. int minutes = static_cast<int>(ttime / 60) % 60;
  312. int seconds = static_cast<int>(ttime) % 60;
  313. char buffer[100];
  314. sprintf(buffer, "%02d:%02d:%02d", hours, minutes, seconds);
  315. *this->TestHandler->LogFile
  316. << "----------------------------------------------------------"
  317. << std::endl;
  318. if(!this->TestResult.Reason.empty())
  319. {
  320. *this->TestHandler->LogFile << reasonType << ":\n"
  321. << this->TestResult.Reason << "\n";
  322. }
  323. else
  324. {
  325. if(pass)
  326. {
  327. *this->TestHandler->LogFile << "Test Passed.\n";
  328. }
  329. else
  330. {
  331. *this->TestHandler->LogFile << "Test Failed.\n";
  332. }
  333. }
  334. *this->TestHandler->LogFile << "\"" << this->TestProperties->Name
  335. << "\" end time: " << this->CTest->CurrentTime() << std::endl
  336. << "\"" << this->TestProperties->Name << "\" time elapsed: "
  337. << buffer << std::endl
  338. << "----------------------------------------------------------"
  339. << std::endl << std::endl;
  340. }
  341. // if the test actually started and ran
  342. // record the results in TestResult
  343. if(started)
  344. {
  345. bool compress = !this->TestHandler->MemCheck &&
  346. this->CompressionRatio < 1 &&
  347. this->CTest->ShouldCompressTestOutput();
  348. this->TestResult.Output = compress ? this->CompressedOutput
  349. : this->ProcessOutput;
  350. this->TestResult.CompressOutput = compress;
  351. this->TestResult.ReturnValue = this->TestProcess->GetExitValue();
  352. this->TestResult.CompletionStatus = "Completed";
  353. this->TestResult.ExecutionTime = this->TestProcess->GetTotalTime();
  354. this->MemCheckPostProcess();
  355. this->ComputeWeightedCost();
  356. }
  357. // If the test does not need to rerun push the current TestResult onto the
  358. // TestHandler vector
  359. if(!this->NeedsToRerun())
  360. {
  361. this->TestHandler->TestResults.push_back(this->TestResult);
  362. }
  363. delete this->TestProcess;
  364. return passed;
  365. }
  366. bool cmCTestRunTest::StartAgain()
  367. {
  368. if(!this->RunAgain)
  369. {
  370. return false;
  371. }
  372. this->RunAgain = false; // reset
  373. // change to tests directory
  374. std::string current_dir = cmSystemTools::GetCurrentWorkingDirectory();
  375. cmSystemTools::ChangeDirectory(this->TestProperties->Directory);
  376. this->StartTest(this->TotalNumberOfTests);
  377. // change back
  378. cmSystemTools::ChangeDirectory(current_dir);
  379. return true;
  380. }
  381. bool cmCTestRunTest::NeedsToRerun()
  382. {
  383. this->NumberOfRunsLeft--;
  384. if(this->NumberOfRunsLeft == 0)
  385. {
  386. return false;
  387. }
  388. // if number of runs left is not 0, and we are running until
  389. // we find a failed test, then return true so the test can be
  390. // restarted
  391. if(this->RunUntilFail
  392. && this->TestResult.Status == cmCTestTestHandler::COMPLETED)
  393. {
  394. this->RunAgain = true;
  395. return true;
  396. }
  397. return false;
  398. }
  399. //----------------------------------------------------------------------
  400. void cmCTestRunTest::ComputeWeightedCost()
  401. {
  402. double prev = static_cast<double>(this->TestProperties->PreviousRuns);
  403. double avgcost = static_cast<double>(this->TestProperties->Cost);
  404. double current = this->TestResult.ExecutionTime;
  405. if(this->TestResult.Status == cmCTestTestHandler::COMPLETED)
  406. {
  407. this->TestProperties->Cost =
  408. static_cast<float>(((prev * avgcost) + current) / (prev + 1.0));
  409. this->TestProperties->PreviousRuns++;
  410. }
  411. }
  412. //----------------------------------------------------------------------
  413. void cmCTestRunTest::MemCheckPostProcess()
  414. {
  415. if(!this->TestHandler->MemCheck)
  416. {
  417. return;
  418. }
  419. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, this->Index
  420. << ": process test output now: "
  421. << this->TestProperties->Name << " "
  422. << this->TestResult.Name << std::endl,
  423. this->TestHandler->GetQuiet());
  424. cmCTestMemCheckHandler * handler = static_cast<cmCTestMemCheckHandler*>
  425. (this->TestHandler);
  426. handler->PostProcessTest(this->TestResult, this->Index);
  427. }
  428. //----------------------------------------------------------------------
  429. // Starts the execution of a test. Returns once it has started
  430. bool cmCTestRunTest::StartTest(size_t total)
  431. {
  432. this->TotalNumberOfTests = total; // save for rerun case
  433. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(2*getNumWidth(total) + 8)
  434. << "Start "
  435. << std::setw(getNumWidth(this->TestHandler->GetMaxIndex()))
  436. << this->TestProperties->Index << ": "
  437. << this->TestProperties->Name << std::endl);
  438. this->ComputeArguments();
  439. std::vector<std::string>& args = this->TestProperties->Args;
  440. this->TestResult.Properties = this->TestProperties;
  441. this->TestResult.ExecutionTime = 0;
  442. this->TestResult.CompressOutput = false;
  443. this->TestResult.ReturnValue = -1;
  444. this->TestResult.CompletionStatus = "Failed to start";
  445. this->TestResult.Status = cmCTestTestHandler::BAD_COMMAND;
  446. this->TestResult.TestCount = this->TestProperties->Index;
  447. this->TestResult.Name = this->TestProperties->Name;
  448. this->TestResult.Path = this->TestProperties->Directory.c_str();
  449. if(args.size() >= 2 && args[1] == "NOT_AVAILABLE")
  450. {
  451. this->TestProcess = new cmProcess;
  452. std::string msg;
  453. if(this->CTest->GetConfigType().empty())
  454. {
  455. msg = "Test not available without configuration.";
  456. msg += " (Missing \"-C <config>\"?)";
  457. }
  458. else
  459. {
  460. msg = "Test not available in configuration \"";
  461. msg += this->CTest->GetConfigType();
  462. msg += "\".";
  463. }
  464. *this->TestHandler->LogFile << msg << std::endl;
  465. cmCTestLog(this->CTest, ERROR_MESSAGE, msg << std::endl);
  466. this->TestResult.Output = msg;
  467. this->TestResult.FullCommandLine = "";
  468. this->TestResult.CompletionStatus = "Not Run";
  469. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  470. return false;
  471. }
  472. // Check if all required files exist
  473. for(std::vector<std::string>::iterator i =
  474. this->TestProperties->RequiredFiles.begin();
  475. i != this->TestProperties->RequiredFiles.end(); ++i)
  476. {
  477. std::string file = *i;
  478. if(!cmSystemTools::FileExists(file.c_str()))
  479. {
  480. //Required file was not found
  481. this->TestProcess = new cmProcess;
  482. *this->TestHandler->LogFile << "Unable to find required file: "
  483. << file << std::endl;
  484. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unable to find required file: "
  485. << file << std::endl);
  486. this->TestResult.Output = "Unable to find required file: " + file;
  487. this->TestResult.FullCommandLine = "";
  488. this->TestResult.CompletionStatus = "Not Run";
  489. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  490. return false;
  491. }
  492. }
  493. // log and return if we did not find the executable
  494. if (this->ActualCommand == "")
  495. {
  496. // if the command was not found create a TestResult object
  497. // that has that information
  498. this->TestProcess = new cmProcess;
  499. *this->TestHandler->LogFile << "Unable to find executable: "
  500. << args[1] << std::endl;
  501. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unable to find executable: "
  502. << args[1] << std::endl);
  503. this->TestResult.Output = "Unable to find executable: " + args[1];
  504. this->TestResult.FullCommandLine = "";
  505. this->TestResult.CompletionStatus = "Not Run";
  506. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  507. return false;
  508. }
  509. this->StartTime = this->CTest->CurrentTime();
  510. double timeout = this->ResolveTimeout();
  511. if(this->StopTimePassed)
  512. {
  513. return false;
  514. }
  515. return this->ForkProcess(timeout, this->TestProperties->ExplicitTimeout,
  516. &this->TestProperties->Environment);
  517. }
  518. //----------------------------------------------------------------------
  519. void cmCTestRunTest::ComputeArguments()
  520. {
  521. this->Arguments.clear(); // reset becaue this might be a rerun
  522. std::vector<std::string>::const_iterator j =
  523. this->TestProperties->Args.begin();
  524. ++j; // skip test name
  525. // find the test executable
  526. if(this->TestHandler->MemCheck)
  527. {
  528. cmCTestMemCheckHandler * handler = static_cast<cmCTestMemCheckHandler*>
  529. (this->TestHandler);
  530. this->ActualCommand = handler->MemoryTester.c_str();
  531. this->TestProperties->Args[1] = this->TestHandler->FindTheExecutable(
  532. this->TestProperties->Args[1].c_str());
  533. }
  534. else
  535. {
  536. this->ActualCommand =
  537. this->TestHandler->FindTheExecutable(
  538. this->TestProperties->Args[1].c_str());
  539. ++j; //skip the executable (it will be actualCommand)
  540. }
  541. std::string testCommand
  542. = cmSystemTools::ConvertToOutputPath(this->ActualCommand.c_str());
  543. //Prepends memcheck args to our command string
  544. this->TestHandler->GenerateTestCommand(this->Arguments, this->Index);
  545. for(std::vector<std::string>::iterator i = this->Arguments.begin();
  546. i != this->Arguments.end(); ++i)
  547. {
  548. testCommand += " \"";
  549. testCommand += *i;
  550. testCommand += "\"";
  551. }
  552. for(;j != this->TestProperties->Args.end(); ++j)
  553. {
  554. testCommand += " \"";
  555. testCommand += *j;
  556. testCommand += "\"";
  557. this->Arguments.push_back(*j);
  558. }
  559. this->TestResult.FullCommandLine = testCommand;
  560. // Print the test command in verbose mode
  561. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl
  562. << this->Index << ": "
  563. << (this->TestHandler->MemCheck?"MemCheck":"Test")
  564. << " command: " << testCommand
  565. << std::endl);
  566. // Print any test-specific env vars in verbose mode
  567. if (this->TestProperties->Environment.size())
  568. {
  569. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, this->Index << ": "
  570. << "Environment variables: " << std::endl);
  571. }
  572. for(std::vector<std::string>::const_iterator e =
  573. this->TestProperties->Environment.begin();
  574. e != this->TestProperties->Environment.end(); ++e)
  575. {
  576. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, this->Index << ": " << *e
  577. << std::endl);
  578. }
  579. }
  580. //----------------------------------------------------------------------
  581. void cmCTestRunTest::DartProcessing()
  582. {
  583. if (!this->ProcessOutput.empty() &&
  584. this->ProcessOutput.find("<DartMeasurement") != this->ProcessOutput.npos)
  585. {
  586. if (this->TestHandler->DartStuff.find(this->ProcessOutput.c_str()))
  587. {
  588. this->TestResult.DartString = this->TestHandler->DartStuff.match(1);
  589. // keep searching and replacing until none are left
  590. while (this->TestHandler->DartStuff1.find(this->ProcessOutput.c_str()))
  591. {
  592. // replace the exact match for the string
  593. cmSystemTools::ReplaceString(this->ProcessOutput,
  594. this->TestHandler->DartStuff1.match(1).c_str(), "");
  595. }
  596. }
  597. }
  598. }
  599. //----------------------------------------------------------------------
  600. double cmCTestRunTest::ResolveTimeout()
  601. {
  602. double timeout = this->TestProperties->Timeout;
  603. if(this->CTest->GetStopTime() == "")
  604. {
  605. return timeout;
  606. }
  607. struct tm* lctime;
  608. time_t current_time = time(0);
  609. lctime = gmtime(&current_time);
  610. int gm_hour = lctime->tm_hour;
  611. time_t gm_time = mktime(lctime);
  612. lctime = localtime(&current_time);
  613. int local_hour = lctime->tm_hour;
  614. int tzone_offset = local_hour - gm_hour;
  615. if(gm_time > current_time && gm_hour < local_hour)
  616. {
  617. // this means gm_time is on the next day
  618. tzone_offset -= 24;
  619. }
  620. else if(gm_time < current_time && gm_hour > local_hour)
  621. {
  622. // this means gm_time is on the previous day
  623. tzone_offset += 24;
  624. }
  625. tzone_offset *= 100;
  626. char buf[1024];
  627. // add todays year day and month to the time in str because
  628. // curl_getdate no longer assumes the day is today
  629. sprintf(buf, "%d%02d%02d %s %+05i",
  630. lctime->tm_year + 1900,
  631. lctime->tm_mon + 1,
  632. lctime->tm_mday,
  633. this->CTest->GetStopTime().c_str(),
  634. tzone_offset);
  635. time_t stop_time = curl_getdate(buf, &current_time);
  636. if(stop_time == -1)
  637. {
  638. return timeout;
  639. }
  640. //the stop time refers to the next day
  641. if(this->CTest->NextDayStopTime)
  642. {
  643. stop_time += 24*60*60;
  644. }
  645. int stop_timeout = static_cast<int>(stop_time - current_time) % (24*60*60);
  646. this->CTest->LastStopTimeout = stop_timeout;
  647. if(stop_timeout <= 0 || stop_timeout > this->CTest->LastStopTimeout)
  648. {
  649. cmCTestLog(this->CTest, ERROR_MESSAGE, "The stop time has been passed. "
  650. "Stopping all tests." << std::endl);
  651. this->StopTimePassed = true;
  652. return 0;
  653. }
  654. return timeout == 0 ? stop_timeout :
  655. (timeout < stop_timeout ? timeout : stop_timeout);
  656. }
  657. //----------------------------------------------------------------------
  658. bool cmCTestRunTest::ForkProcess(double testTimeOut, bool explicitTimeout,
  659. std::vector<std::string>* environment)
  660. {
  661. this->TestProcess = new cmProcess;
  662. this->TestProcess->SetId(this->Index);
  663. this->TestProcess->SetWorkingDirectory(
  664. this->TestProperties->Directory.c_str());
  665. this->TestProcess->SetCommand(this->ActualCommand.c_str());
  666. this->TestProcess->SetCommandArguments(this->Arguments);
  667. // determine how much time we have
  668. double timeout = this->CTest->GetRemainingTimeAllowed() - 120;
  669. if (this->CTest->GetTimeOut() > 0 && this->CTest->GetTimeOut() < timeout)
  670. {
  671. timeout = this->CTest->GetTimeOut();
  672. }
  673. if (testTimeOut > 0
  674. && testTimeOut < this->CTest->GetRemainingTimeAllowed())
  675. {
  676. timeout = testTimeOut;
  677. }
  678. // always have at least 1 second if we got to here
  679. if (timeout <= 0)
  680. {
  681. timeout = 1;
  682. }
  683. // handle timeout explicitly set to 0
  684. if (testTimeOut == 0 && explicitTimeout)
  685. {
  686. timeout = 0;
  687. }
  688. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, this->Index << ": "
  689. << "Test timeout computed to be: " << timeout << "\n",
  690. this->TestHandler->GetQuiet());
  691. this->TestProcess->SetTimeout(timeout);
  692. #ifdef CMAKE_BUILD_WITH_CMAKE
  693. cmSystemTools::SaveRestoreEnvironment sre;
  694. #endif
  695. if (environment && !environment->empty())
  696. {
  697. cmSystemTools::AppendEnv(*environment);
  698. }
  699. return this->TestProcess->StartProcess();
  700. }
  701. void cmCTestRunTest::WriteLogOutputTop(size_t completed, size_t total)
  702. {
  703. // if this is the last or only run of this test
  704. // then print out completed / total
  705. // Only issue is if a test fails and we are running until fail
  706. // then it will never print out the completed / total, same would
  707. // got for run until pass. Trick is when this is called we don't
  708. // yet know if we are passing or failing.
  709. if(this->NumberOfRunsLeft == 1)
  710. {
  711. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(getNumWidth(total))
  712. << completed << "/");
  713. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(getNumWidth(total))
  714. << total << " ");
  715. }
  716. // if this is one of several runs of a test just print blank space
  717. // to keep things neat
  718. else
  719. {
  720. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(getNumWidth(total))
  721. << " " << " ");
  722. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(getNumWidth(total))
  723. << " " << " ");
  724. }
  725. if ( this->TestHandler->MemCheck )
  726. {
  727. cmCTestLog(this->CTest, HANDLER_OUTPUT, "MemCheck");
  728. }
  729. else
  730. {
  731. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Test");
  732. }
  733. std::ostringstream indexStr;
  734. indexStr << " #" << this->Index << ":";
  735. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  736. std::setw(3 + getNumWidth(this->TestHandler->GetMaxIndex()))
  737. << indexStr.str());
  738. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  739. const int maxTestNameWidth = this->CTest->GetMaxTestNameWidth();
  740. std::string outname = this->TestProperties->Name + " ";
  741. outname.resize(maxTestNameWidth + 4, '.');
  742. *this->TestHandler->LogFile << this->TestProperties->Index << "/"
  743. << this->TestHandler->TotalNumberOfTests << " Testing: "
  744. << this->TestProperties->Name << std::endl;
  745. *this->TestHandler->LogFile << this->TestProperties->Index << "/"
  746. << this->TestHandler->TotalNumberOfTests
  747. << " Test: " << this->TestProperties->Name << std::endl;
  748. *this->TestHandler->LogFile << "Command: \"" << this->ActualCommand << "\"";
  749. for (std::vector<std::string>::iterator i = this->Arguments.begin();
  750. i != this->Arguments.end(); ++i)
  751. {
  752. *this->TestHandler->LogFile
  753. << " \"" << *i << "\"";
  754. }
  755. *this->TestHandler->LogFile << std::endl
  756. << "Directory: " << this->TestProperties->Directory << std::endl
  757. << "\"" << this->TestProperties->Name << "\" start time: "
  758. << this->StartTime << std::endl;
  759. *this->TestHandler->LogFile
  760. << "Output:" << std::endl
  761. << "----------------------------------------------------------"
  762. << std::endl;
  763. *this->TestHandler->LogFile
  764. << this->ProcessOutput << "<end of output>" << std::endl;
  765. cmCTestLog(this->CTest, HANDLER_OUTPUT, outname.c_str());
  766. cmCTestLog(this->CTest, DEBUG, "Testing "
  767. << this->TestProperties->Name << " ... ");
  768. }