cmCTestRunTest.cxx 27 KB

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