cmCTestRunTest.cxx 25 KB

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