cmCTestRunTest.cxx 23 KB

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