cmCTestRunTest.cxx 30 KB

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