cmCTestRunTest.cxx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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. this->TestResult.Properties = this->TestProperties;
  408. this->TestResult.ExecutionTime = cmDuration::zero();
  409. this->TestResult.CompressOutput = false;
  410. this->TestResult.ReturnValue = -1;
  411. this->TestResult.TestCount = this->TestProperties->Index;
  412. this->TestResult.Name = this->TestProperties->Name;
  413. this->TestResult.Path = this->TestProperties->Directory;
  414. // Return immediately if test is disabled
  415. if (this->TestProperties->Disabled) {
  416. this->TestResult.CompletionStatus = "Disabled";
  417. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  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.CompletionStatus = "Failed to start";
  424. this->TestResult.Status = cmCTestTestHandler::BAD_COMMAND;
  425. // Check for failed fixture dependencies before we even look at the command
  426. // arguments because if we are not going to run the test, the command and
  427. // its arguments are irrelevant. This matters for the case where a fixture
  428. // dependency might be creating the executable we want to run.
  429. if (!this->FailedDependencies.empty()) {
  430. this->TestProcess = cm::make_unique<cmProcess>(*this);
  431. std::string msg = "Failed test dependencies:";
  432. for (std::string const& failedDep : this->FailedDependencies) {
  433. msg += " " + failedDep;
  434. }
  435. *this->TestHandler->LogFile << msg << std::endl;
  436. cmCTestLog(this->CTest, HANDLER_OUTPUT, msg << std::endl);
  437. this->TestResult.Output = msg;
  438. this->TestResult.FullCommandLine.clear();
  439. this->TestResult.CompletionStatus = "Fixture dependency failed";
  440. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  441. return false;
  442. }
  443. this->ComputeArguments();
  444. std::vector<std::string>& args = this->TestProperties->Args;
  445. if (args.size() >= 2 && args[1] == "NOT_AVAILABLE") {
  446. this->TestProcess = cm::make_unique<cmProcess>(*this);
  447. std::string msg;
  448. if (this->CTest->GetConfigType().empty()) {
  449. msg = "Test not available without configuration.";
  450. msg += " (Missing \"-C <config>\"?)";
  451. } else {
  452. msg = "Test not available in configuration \"";
  453. msg += this->CTest->GetConfigType();
  454. msg += "\".";
  455. }
  456. *this->TestHandler->LogFile << msg << std::endl;
  457. cmCTestLog(this->CTest, ERROR_MESSAGE, msg << std::endl);
  458. this->TestResult.Output = msg;
  459. this->TestResult.FullCommandLine.clear();
  460. this->TestResult.CompletionStatus = "Missing Configuration";
  461. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  462. return false;
  463. }
  464. // Check if all required files exist
  465. for (std::string const& file : this->TestProperties->RequiredFiles) {
  466. if (!cmSystemTools::FileExists(file)) {
  467. // Required file was not found
  468. this->TestProcess = cm::make_unique<cmProcess>(*this);
  469. *this->TestHandler->LogFile << "Unable to find required file: " << file
  470. << std::endl;
  471. cmCTestLog(this->CTest, ERROR_MESSAGE,
  472. "Unable to find required file: " << file << std::endl);
  473. this->TestResult.Output = "Unable to find required file: " + file;
  474. this->TestResult.FullCommandLine.clear();
  475. this->TestResult.CompletionStatus = "Required Files Missing";
  476. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  477. return false;
  478. }
  479. }
  480. // log and return if we did not find the executable
  481. if (this->ActualCommand.empty()) {
  482. // if the command was not found create a TestResult object
  483. // that has that information
  484. this->TestProcess = cm::make_unique<cmProcess>(*this);
  485. *this->TestHandler->LogFile << "Unable to find executable: " << args[1]
  486. << std::endl;
  487. cmCTestLog(this->CTest, ERROR_MESSAGE,
  488. "Unable to find executable: " << args[1] << std::endl);
  489. this->TestResult.Output = "Unable to find executable: " + args[1];
  490. this->TestResult.FullCommandLine.clear();
  491. this->TestResult.CompletionStatus = "Unable to find executable";
  492. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  493. return false;
  494. }
  495. this->StartTime = this->CTest->CurrentTime();
  496. auto timeout = this->TestProperties->Timeout;
  497. this->TimeoutIsForStopTime = false;
  498. std::chrono::system_clock::time_point stop_time = this->CTest->GetStopTime();
  499. if (stop_time != std::chrono::system_clock::time_point()) {
  500. std::chrono::duration<double> stop_timeout =
  501. (stop_time - std::chrono::system_clock::now()) % std::chrono::hours(24);
  502. if (stop_timeout <= std::chrono::duration<double>::zero()) {
  503. stop_timeout = std::chrono::duration<double>::zero();
  504. }
  505. if (timeout == std::chrono::duration<double>::zero() ||
  506. stop_timeout < timeout) {
  507. this->TimeoutIsForStopTime = true;
  508. timeout = stop_timeout;
  509. }
  510. }
  511. return this->ForkProcess(timeout, this->TestProperties->ExplicitTimeout,
  512. &this->TestProperties->Environment,
  513. &this->TestProperties->Affinity);
  514. }
  515. void cmCTestRunTest::ComputeArguments()
  516. {
  517. this->Arguments.clear(); // reset because this might be a rerun
  518. std::vector<std::string>::const_iterator j =
  519. this->TestProperties->Args.begin();
  520. ++j; // skip test name
  521. // find the test executable
  522. if (this->TestHandler->MemCheck) {
  523. cmCTestMemCheckHandler* handler =
  524. static_cast<cmCTestMemCheckHandler*>(this->TestHandler);
  525. this->ActualCommand = handler->MemoryTester;
  526. this->TestProperties->Args[1] = this->TestHandler->FindTheExecutable(
  527. this->TestProperties->Args[1].c_str());
  528. } else {
  529. this->ActualCommand = this->TestHandler->FindTheExecutable(
  530. this->TestProperties->Args[1].c_str());
  531. ++j; // skip the executable (it will be actualCommand)
  532. }
  533. std::string testCommand =
  534. cmSystemTools::ConvertToOutputPath(this->ActualCommand);
  535. // Prepends memcheck args to our command string
  536. this->TestHandler->GenerateTestCommand(this->Arguments, this->Index);
  537. for (std::string const& arg : this->Arguments) {
  538. testCommand += " \"";
  539. testCommand += arg;
  540. testCommand += "\"";
  541. }
  542. for (; j != this->TestProperties->Args.end(); ++j) {
  543. testCommand += " \"";
  544. testCommand += *j;
  545. testCommand += "\"";
  546. this->Arguments.push_back(*j);
  547. }
  548. this->TestResult.FullCommandLine = testCommand;
  549. // Print the test command in verbose mode
  550. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  551. std::endl
  552. << this->Index << ": "
  553. << (this->TestHandler->MemCheck ? "MemCheck" : "Test")
  554. << " command: " << testCommand << std::endl);
  555. // Print any test-specific env vars in verbose mode
  556. if (!this->TestProperties->Environment.empty()) {
  557. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  558. this->Index << ": "
  559. << "Environment variables: " << std::endl);
  560. }
  561. for (std::string const& env : this->TestProperties->Environment) {
  562. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  563. this->Index << ": " << env << std::endl);
  564. }
  565. }
  566. void cmCTestRunTest::DartProcessing()
  567. {
  568. if (!this->ProcessOutput.empty() &&
  569. this->ProcessOutput.find("<DartMeasurement") != std::string::npos) {
  570. if (this->TestHandler->DartStuff.find(this->ProcessOutput)) {
  571. this->TestResult.DartString = this->TestHandler->DartStuff.match(1);
  572. // keep searching and replacing until none are left
  573. while (this->TestHandler->DartStuff1.find(this->ProcessOutput)) {
  574. // replace the exact match for the string
  575. cmSystemTools::ReplaceString(
  576. this->ProcessOutput, this->TestHandler->DartStuff1.match(1).c_str(),
  577. "");
  578. }
  579. }
  580. }
  581. }
  582. bool cmCTestRunTest::ForkProcess(cmDuration testTimeOut, bool explicitTimeout,
  583. std::vector<std::string>* environment,
  584. std::vector<size_t>* affinity)
  585. {
  586. this->TestProcess = cm::make_unique<cmProcess>(*this);
  587. this->TestProcess->SetId(this->Index);
  588. this->TestProcess->SetWorkingDirectory(this->TestProperties->Directory);
  589. this->TestProcess->SetCommand(this->ActualCommand);
  590. this->TestProcess->SetCommandArguments(this->Arguments);
  591. // determine how much time we have
  592. cmDuration timeout = this->CTest->GetRemainingTimeAllowed();
  593. if (timeout != cmCTest::MaxDuration()) {
  594. timeout -= std::chrono::minutes(2);
  595. }
  596. if (this->CTest->GetTimeOut() > cmDuration::zero() &&
  597. this->CTest->GetTimeOut() < timeout) {
  598. timeout = this->CTest->GetTimeOut();
  599. }
  600. if (testTimeOut > cmDuration::zero() &&
  601. testTimeOut < this->CTest->GetRemainingTimeAllowed()) {
  602. timeout = testTimeOut;
  603. }
  604. // always have at least 1 second if we got to here
  605. if (timeout <= cmDuration::zero()) {
  606. timeout = std::chrono::seconds(1);
  607. }
  608. // handle timeout explicitly set to 0
  609. if (testTimeOut == cmDuration::zero() && explicitTimeout) {
  610. timeout = cmDuration::zero();
  611. }
  612. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  613. this->Index << ": "
  614. << "Test timeout computed to be: "
  615. << cmDurationTo<unsigned int>(timeout)
  616. << "\n",
  617. this->TestHandler->GetQuiet());
  618. this->TestProcess->SetTimeout(timeout);
  619. #ifdef CMAKE_BUILD_WITH_CMAKE
  620. cmSystemTools::SaveRestoreEnvironment sre;
  621. #endif
  622. if (environment && !environment->empty()) {
  623. cmSystemTools::AppendEnv(*environment);
  624. }
  625. return this->TestProcess->StartProcess(this->MultiTestHandler.Loop,
  626. affinity);
  627. }
  628. void cmCTestRunTest::WriteLogOutputTop(size_t completed, size_t total)
  629. {
  630. std::ostringstream outputStream;
  631. // If this is the last or only run of this test, or progress output is
  632. // requested, then print out completed / total.
  633. // Only issue is if a test fails and we are running until fail
  634. // then it will never print out the completed / total, same would
  635. // got for run until pass. Trick is when this is called we don't
  636. // yet know if we are passing or failing.
  637. if (this->NumberOfRunsLeft == 1 || this->CTest->GetTestProgressOutput()) {
  638. outputStream << std::setw(getNumWidth(total)) << completed << "/";
  639. outputStream << std::setw(getNumWidth(total)) << total << " ";
  640. }
  641. // if this is one of several runs of a test just print blank space
  642. // to keep things neat
  643. else {
  644. outputStream << std::setw(getNumWidth(total)) << " ";
  645. outputStream << std::setw(getNumWidth(total)) << " ";
  646. }
  647. if (this->TestHandler->MemCheck) {
  648. outputStream << "MemCheck";
  649. } else {
  650. outputStream << "Test";
  651. }
  652. std::ostringstream indexStr;
  653. indexStr << " #" << this->Index << ":";
  654. outputStream << std::setw(3 + getNumWidth(this->TestHandler->GetMaxIndex()))
  655. << indexStr.str();
  656. outputStream << " ";
  657. const int maxTestNameWidth = this->CTest->GetMaxTestNameWidth();
  658. std::string outname = this->TestProperties->Name + " ";
  659. outname.resize(maxTestNameWidth + 4, '.');
  660. outputStream << outname;
  661. *this->TestHandler->LogFile << this->TestProperties->Index << "/"
  662. << this->TestHandler->TotalNumberOfTests
  663. << " Testing: " << this->TestProperties->Name
  664. << std::endl;
  665. *this->TestHandler->LogFile << this->TestProperties->Index << "/"
  666. << this->TestHandler->TotalNumberOfTests
  667. << " Test: " << this->TestProperties->Name
  668. << std::endl;
  669. *this->TestHandler->LogFile << "Command: \"" << this->ActualCommand << "\"";
  670. for (std::string const& arg : this->Arguments) {
  671. *this->TestHandler->LogFile << " \"" << arg << "\"";
  672. }
  673. *this->TestHandler->LogFile
  674. << std::endl
  675. << "Directory: " << this->TestProperties->Directory << std::endl
  676. << "\"" << this->TestProperties->Name
  677. << "\" start time: " << this->StartTime << std::endl;
  678. *this->TestHandler->LogFile
  679. << "Output:" << std::endl
  680. << "----------------------------------------------------------"
  681. << std::endl;
  682. *this->TestHandler->LogFile << this->ProcessOutput << "<end of output>"
  683. << std::endl;
  684. if (!this->CTest->GetTestProgressOutput()) {
  685. cmCTestLog(this->CTest, HANDLER_OUTPUT, outputStream.str());
  686. }
  687. cmCTestLog(this->CTest, DEBUG,
  688. "Testing " << this->TestProperties->Name << " ... ");
  689. }
  690. void cmCTestRunTest::FinalizeTest()
  691. {
  692. this->MultiTestHandler.FinishTestProcess(this, true);
  693. }