cmCTestRunTest.cxx 28 KB

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