cmCTestRunTest.cxx 28 KB

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