cmCTestRunTest.cxx 28 KB

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