cmCTestRunTest.cxx 28 KB

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