cmCTestRunTest.cxx 30 KB

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