cmCTestRunTest.cxx 27 KB

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