cmCTestRunTest.cxx 27 KB

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