cmCTestRunTest.cxx 28 KB

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