cmCTestRunTest.cxx 27 KB

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