cmCTestRunTest.cxx 30 KB

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