cmCTestRunTest.cxx 29 KB

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