cmCTestRunTest.cxx 26 KB

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