cmCTestRunTest.cxx 27 KB

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