cmCTestRunTest.cxx 27 KB

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