cmCTestRunTest.cxx 28 KB

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