cmCTestRunTest.cxx 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. return true;
  314. }
  315. this->StartTest(completed, this->TotalNumberOfTests);
  316. return true;
  317. }
  318. bool cmCTestRunTest::NeedsToRepeat()
  319. {
  320. this->NumberOfRunsLeft--;
  321. if (this->NumberOfRunsLeft == 0) {
  322. return false;
  323. }
  324. // if number of runs left is not 0, and we are running until
  325. // we find a failed (or passed) test, then return true so the test can be
  326. // restarted
  327. if ((this->RepeatMode == cmCTest::Repeat::UntilFail &&
  328. this->TestResult.Status == cmCTestTestHandler::COMPLETED) ||
  329. (this->RepeatMode == cmCTest::Repeat::UntilPass &&
  330. this->TestResult.Status != cmCTestTestHandler::COMPLETED) ||
  331. (this->RepeatMode == cmCTest::Repeat::AfterTimeout &&
  332. this->TestResult.Status == cmCTestTestHandler::TIMEOUT)) {
  333. this->RunAgain = true;
  334. return true;
  335. }
  336. return false;
  337. }
  338. void cmCTestRunTest::ComputeWeightedCost()
  339. {
  340. double prev = static_cast<double>(this->TestProperties->PreviousRuns);
  341. double avgcost = static_cast<double>(this->TestProperties->Cost);
  342. double current = this->TestResult.ExecutionTime.count();
  343. if (this->TestResult.Status == cmCTestTestHandler::COMPLETED) {
  344. this->TestProperties->Cost =
  345. static_cast<float>(((prev * avgcost) + current) / (prev + 1.0));
  346. this->TestProperties->PreviousRuns++;
  347. }
  348. }
  349. void cmCTestRunTest::MemCheckPostProcess()
  350. {
  351. if (!this->TestHandler->MemCheck) {
  352. return;
  353. }
  354. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  355. this->Index << ": process test output now: "
  356. << this->TestProperties->Name << " "
  357. << this->TestResult.Name << std::endl,
  358. this->TestHandler->GetQuiet());
  359. cmCTestMemCheckHandler* handler =
  360. static_cast<cmCTestMemCheckHandler*>(this->TestHandler);
  361. handler->PostProcessTest(this->TestResult, this->Index);
  362. }
  363. void cmCTestRunTest::StartFailure(std::string const& output)
  364. {
  365. // Still need to log the Start message so the test summary records our
  366. // attempt to start this test
  367. if (!this->CTest->GetTestProgressOutput()) {
  368. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  369. std::setw(2 * getNumWidth(this->TotalNumberOfTests) + 8)
  370. << "Start "
  371. << std::setw(getNumWidth(this->TestHandler->GetMaxIndex()))
  372. << this->TestProperties->Index << ": "
  373. << this->TestProperties->Name << std::endl);
  374. }
  375. this->ProcessOutput.clear();
  376. if (!output.empty()) {
  377. *this->TestHandler->LogFile << output << std::endl;
  378. cmCTestLog(this->CTest, ERROR_MESSAGE, output << std::endl);
  379. }
  380. this->TestResult.Properties = this->TestProperties;
  381. this->TestResult.ExecutionTime = cmDuration::zero();
  382. this->TestResult.CompressOutput = false;
  383. this->TestResult.ReturnValue = -1;
  384. this->TestResult.CompletionStatus = "Failed to start";
  385. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  386. this->TestResult.TestCount = this->TestProperties->Index;
  387. this->TestResult.Name = this->TestProperties->Name;
  388. this->TestResult.Path = this->TestProperties->Directory;
  389. this->TestResult.Output = output;
  390. this->TestResult.FullCommandLine.clear();
  391. this->TestProcess = cm::make_unique<cmProcess>(*this);
  392. }
  393. std::string cmCTestRunTest::GetTestPrefix(size_t completed, size_t total) const
  394. {
  395. std::ostringstream outputStream;
  396. outputStream << std::setw(getNumWidth(total)) << completed << "/";
  397. outputStream << std::setw(getNumWidth(total)) << total << " ";
  398. if (this->TestHandler->MemCheck) {
  399. outputStream << "MemCheck";
  400. } else {
  401. outputStream << "Test";
  402. }
  403. std::ostringstream indexStr;
  404. indexStr << " #" << this->Index << ":";
  405. outputStream << std::setw(3 + getNumWidth(this->TestHandler->GetMaxIndex()))
  406. << indexStr.str();
  407. outputStream << " ";
  408. return outputStream.str();
  409. }
  410. // Starts the execution of a test. Returns once it has started
  411. bool cmCTestRunTest::StartTest(size_t completed, size_t total)
  412. {
  413. this->TotalNumberOfTests = total; // save for rerun case
  414. if (!this->CTest->GetTestProgressOutput()) {
  415. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  416. std::setw(2 * getNumWidth(total) + 8)
  417. << "Start "
  418. << std::setw(getNumWidth(this->TestHandler->GetMaxIndex()))
  419. << this->TestProperties->Index << ": "
  420. << this->TestProperties->Name << std::endl);
  421. } else {
  422. std::string testName =
  423. GetTestPrefix(completed, total) + this->TestProperties->Name + "\n";
  424. cmCTestLog(this->CTest, HANDLER_TEST_PROGRESS_OUTPUT, testName);
  425. }
  426. this->ProcessOutput.clear();
  427. this->TestResult.Properties = this->TestProperties;
  428. this->TestResult.ExecutionTime = cmDuration::zero();
  429. this->TestResult.CompressOutput = false;
  430. this->TestResult.ReturnValue = -1;
  431. this->TestResult.TestCount = this->TestProperties->Index;
  432. this->TestResult.Name = this->TestProperties->Name;
  433. this->TestResult.Path = this->TestProperties->Directory;
  434. // Return immediately if test is disabled
  435. if (this->TestProperties->Disabled) {
  436. this->TestResult.CompletionStatus = "Disabled";
  437. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  438. this->TestProcess = cm::make_unique<cmProcess>(*this);
  439. this->TestResult.Output = "Disabled";
  440. this->TestResult.FullCommandLine.clear();
  441. return false;
  442. }
  443. this->TestResult.CompletionStatus = "Failed to start";
  444. this->TestResult.Status = cmCTestTestHandler::BAD_COMMAND;
  445. // Check for failed fixture dependencies before we even look at the command
  446. // arguments because if we are not going to run the test, the command and
  447. // its arguments are irrelevant. This matters for the case where a fixture
  448. // dependency might be creating the executable we want to run.
  449. if (!this->FailedDependencies.empty()) {
  450. this->TestProcess = cm::make_unique<cmProcess>(*this);
  451. std::string msg = "Failed test dependencies:";
  452. for (std::string const& failedDep : this->FailedDependencies) {
  453. msg += " " + failedDep;
  454. }
  455. *this->TestHandler->LogFile << msg << std::endl;
  456. cmCTestLog(this->CTest, HANDLER_OUTPUT, msg << std::endl);
  457. this->TestResult.Output = msg;
  458. this->TestResult.FullCommandLine.clear();
  459. this->TestResult.CompletionStatus = "Fixture dependency failed";
  460. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  461. return false;
  462. }
  463. this->ComputeArguments();
  464. std::vector<std::string>& args = this->TestProperties->Args;
  465. if (args.size() >= 2 && args[1] == "NOT_AVAILABLE") {
  466. this->TestProcess = cm::make_unique<cmProcess>(*this);
  467. std::string msg;
  468. if (this->CTest->GetConfigType().empty()) {
  469. msg = "Test not available without configuration. (Missing \"-C "
  470. "<config>\"?)";
  471. } else {
  472. msg = cmStrCat("Test not available in configuration \"",
  473. this->CTest->GetConfigType(), "\".");
  474. }
  475. *this->TestHandler->LogFile << msg << std::endl;
  476. cmCTestLog(this->CTest, ERROR_MESSAGE, msg << std::endl);
  477. this->TestResult.Output = msg;
  478. this->TestResult.FullCommandLine.clear();
  479. this->TestResult.CompletionStatus = "Missing Configuration";
  480. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  481. return false;
  482. }
  483. // Check if all required files exist
  484. for (std::string const& file : this->TestProperties->RequiredFiles) {
  485. if (!cmSystemTools::FileExists(file)) {
  486. // Required file was not found
  487. this->TestProcess = cm::make_unique<cmProcess>(*this);
  488. *this->TestHandler->LogFile << "Unable to find required file: " << file
  489. << std::endl;
  490. cmCTestLog(this->CTest, ERROR_MESSAGE,
  491. "Unable to find required file: " << file << std::endl);
  492. this->TestResult.Output = "Unable to find required file: " + file;
  493. this->TestResult.FullCommandLine.clear();
  494. this->TestResult.CompletionStatus = "Required Files Missing";
  495. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  496. return false;
  497. }
  498. }
  499. // log and return if we did not find the executable
  500. if (this->ActualCommand.empty()) {
  501. // if the command was not found create a TestResult object
  502. // that has that information
  503. this->TestProcess = cm::make_unique<cmProcess>(*this);
  504. *this->TestHandler->LogFile << "Unable to find executable: " << args[1]
  505. << std::endl;
  506. cmCTestLog(this->CTest, ERROR_MESSAGE,
  507. "Unable to find executable: " << args[1] << std::endl);
  508. this->TestResult.Output = "Unable to find executable: " + args[1];
  509. this->TestResult.FullCommandLine.clear();
  510. this->TestResult.CompletionStatus = "Unable to find executable";
  511. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  512. return false;
  513. }
  514. this->StartTime = this->CTest->CurrentTime();
  515. auto timeout = this->TestProperties->Timeout;
  516. this->TimeoutIsForStopTime = false;
  517. std::chrono::system_clock::time_point stop_time = this->CTest->GetStopTime();
  518. if (stop_time != std::chrono::system_clock::time_point()) {
  519. std::chrono::duration<double> stop_timeout =
  520. (stop_time - std::chrono::system_clock::now()) % std::chrono::hours(24);
  521. if (stop_timeout <= std::chrono::duration<double>::zero()) {
  522. stop_timeout = std::chrono::duration<double>::zero();
  523. }
  524. if (timeout == std::chrono::duration<double>::zero() ||
  525. stop_timeout < timeout) {
  526. this->TimeoutIsForStopTime = true;
  527. timeout = stop_timeout;
  528. }
  529. }
  530. return this->ForkProcess(timeout, this->TestProperties->ExplicitTimeout,
  531. &this->TestProperties->Environment,
  532. &this->TestProperties->Affinity);
  533. }
  534. void cmCTestRunTest::ComputeArguments()
  535. {
  536. this->Arguments.clear(); // reset because this might be a rerun
  537. auto j = this->TestProperties->Args.begin();
  538. ++j; // skip test name
  539. // find the test executable
  540. if (this->TestHandler->MemCheck) {
  541. cmCTestMemCheckHandler* handler =
  542. static_cast<cmCTestMemCheckHandler*>(this->TestHandler);
  543. this->ActualCommand = handler->MemoryTester;
  544. this->TestProperties->Args[1] = this->TestHandler->FindTheExecutable(
  545. this->TestProperties->Args[1].c_str());
  546. } else {
  547. this->ActualCommand = this->TestHandler->FindTheExecutable(
  548. this->TestProperties->Args[1].c_str());
  549. ++j; // skip the executable (it will be actualCommand)
  550. }
  551. std::string testCommand =
  552. cmSystemTools::ConvertToOutputPath(this->ActualCommand);
  553. // Prepends memcheck args to our command string
  554. this->TestHandler->GenerateTestCommand(this->Arguments, this->Index);
  555. for (std::string const& arg : this->Arguments) {
  556. testCommand += " \"";
  557. testCommand += arg;
  558. testCommand += "\"";
  559. }
  560. for (; j != this->TestProperties->Args.end(); ++j) {
  561. testCommand += " \"";
  562. testCommand += *j;
  563. testCommand += "\"";
  564. this->Arguments.push_back(*j);
  565. }
  566. this->TestResult.FullCommandLine = testCommand;
  567. // Print the test command in verbose mode
  568. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  569. std::endl
  570. << this->Index << ": "
  571. << (this->TestHandler->MemCheck ? "MemCheck" : "Test")
  572. << " command: " << testCommand << std::endl);
  573. // Print any test-specific env vars in verbose mode
  574. if (!this->TestProperties->Environment.empty()) {
  575. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  576. this->Index << ": "
  577. << "Environment variables: " << std::endl);
  578. }
  579. for (std::string const& env : this->TestProperties->Environment) {
  580. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  581. this->Index << ": " << env << std::endl);
  582. }
  583. }
  584. void cmCTestRunTest::DartProcessing()
  585. {
  586. if (!this->ProcessOutput.empty() &&
  587. this->ProcessOutput.find("<DartMeasurement") != std::string::npos) {
  588. if (this->TestHandler->DartStuff.find(this->ProcessOutput)) {
  589. this->TestResult.DartString = this->TestHandler->DartStuff.match(1);
  590. // keep searching and replacing until none are left
  591. while (this->TestHandler->DartStuff1.find(this->ProcessOutput)) {
  592. // replace the exact match for the string
  593. cmSystemTools::ReplaceString(
  594. this->ProcessOutput, this->TestHandler->DartStuff1.match(1).c_str(),
  595. "");
  596. }
  597. }
  598. }
  599. }
  600. bool cmCTestRunTest::ForkProcess(cmDuration testTimeOut, bool explicitTimeout,
  601. std::vector<std::string>* environment,
  602. std::vector<size_t>* affinity)
  603. {
  604. this->TestProcess = cm::make_unique<cmProcess>(*this);
  605. this->TestProcess->SetId(this->Index);
  606. this->TestProcess->SetWorkingDirectory(this->TestProperties->Directory);
  607. this->TestProcess->SetCommand(this->ActualCommand);
  608. this->TestProcess->SetCommandArguments(this->Arguments);
  609. // determine how much time we have
  610. cmDuration timeout = this->CTest->GetRemainingTimeAllowed();
  611. if (timeout != cmCTest::MaxDuration()) {
  612. timeout -= std::chrono::minutes(2);
  613. }
  614. if (this->CTest->GetTimeOut() > cmDuration::zero() &&
  615. this->CTest->GetTimeOut() < timeout) {
  616. timeout = this->CTest->GetTimeOut();
  617. }
  618. if (testTimeOut > cmDuration::zero() &&
  619. testTimeOut < this->CTest->GetRemainingTimeAllowed()) {
  620. timeout = testTimeOut;
  621. }
  622. // always have at least 1 second if we got to here
  623. if (timeout <= cmDuration::zero()) {
  624. timeout = std::chrono::seconds(1);
  625. }
  626. // handle timeout explicitly set to 0
  627. if (testTimeOut == cmDuration::zero() && explicitTimeout) {
  628. timeout = cmDuration::zero();
  629. }
  630. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  631. this->Index << ": "
  632. << "Test timeout computed to be: "
  633. << cmDurationTo<unsigned int>(timeout)
  634. << "\n",
  635. this->TestHandler->GetQuiet());
  636. this->TestProcess->SetTimeout(timeout);
  637. #ifndef CMAKE_BOOTSTRAP
  638. cmSystemTools::SaveRestoreEnvironment sre;
  639. #endif
  640. if (environment && !environment->empty()) {
  641. cmSystemTools::AppendEnv(*environment);
  642. }
  643. if (this->UseAllocatedResources) {
  644. this->SetupResourcesEnvironment();
  645. } else {
  646. cmSystemTools::UnsetEnv("CTEST_RESOURCE_GROUP_COUNT");
  647. }
  648. return this->TestProcess->StartProcess(this->MultiTestHandler.Loop,
  649. affinity);
  650. }
  651. void cmCTestRunTest::SetupResourcesEnvironment()
  652. {
  653. std::string processCount = "CTEST_RESOURCE_GROUP_COUNT=";
  654. processCount += std::to_string(this->AllocatedResources.size());
  655. cmSystemTools::PutEnv(processCount);
  656. std::size_t i = 0;
  657. for (auto const& process : this->AllocatedResources) {
  658. std::string prefix = "CTEST_RESOURCE_GROUP_";
  659. prefix += std::to_string(i);
  660. std::string resourceList = prefix + '=';
  661. prefix += '_';
  662. bool firstType = true;
  663. for (auto const& it : process) {
  664. if (!firstType) {
  665. resourceList += ',';
  666. }
  667. firstType = false;
  668. auto resourceType = it.first;
  669. resourceList += resourceType;
  670. std::string var = prefix + cmSystemTools::UpperCase(resourceType) + '=';
  671. bool firstName = true;
  672. for (auto const& it2 : it.second) {
  673. if (!firstName) {
  674. var += ';';
  675. }
  676. firstName = false;
  677. var += "id:" + it2.Id + ",slots:" + std::to_string(it2.Slots);
  678. }
  679. cmSystemTools::PutEnv(var);
  680. }
  681. cmSystemTools::PutEnv(resourceList);
  682. ++i;
  683. }
  684. }
  685. void cmCTestRunTest::WriteLogOutputTop(size_t completed, size_t total)
  686. {
  687. std::ostringstream outputStream;
  688. // If this is the last or only run of this test, or progress output is
  689. // requested, then print out completed / total.
  690. // Only issue is if a test fails and we are running until fail
  691. // then it will never print out the completed / total, same would
  692. // got for run until pass. Trick is when this is called we don't
  693. // yet know if we are passing or failing.
  694. bool const progressOnLast =
  695. (this->RepeatMode != cmCTest::Repeat::UntilPass &&
  696. this->RepeatMode != cmCTest::Repeat::AfterTimeout);
  697. if ((progressOnLast && this->NumberOfRunsLeft == 1) ||
  698. (!progressOnLast && this->NumberOfRunsLeft == this->NumberOfRunsTotal) ||
  699. this->CTest->GetTestProgressOutput()) {
  700. outputStream << std::setw(getNumWidth(total)) << completed << "/";
  701. outputStream << std::setw(getNumWidth(total)) << total << " ";
  702. }
  703. // if this is one of several runs of a test just print blank space
  704. // to keep things neat
  705. else {
  706. outputStream << std::setw(getNumWidth(total)) << " ";
  707. outputStream << std::setw(getNumWidth(total)) << " ";
  708. }
  709. if (this->TestHandler->MemCheck) {
  710. outputStream << "MemCheck";
  711. } else {
  712. outputStream << "Test";
  713. }
  714. std::ostringstream indexStr;
  715. indexStr << " #" << this->Index << ":";
  716. outputStream << std::setw(3 + getNumWidth(this->TestHandler->GetMaxIndex()))
  717. << indexStr.str();
  718. outputStream << " ";
  719. const int maxTestNameWidth = this->CTest->GetMaxTestNameWidth();
  720. std::string outname = this->TestProperties->Name + " ";
  721. outname.resize(maxTestNameWidth + 4, '.');
  722. outputStream << outname;
  723. *this->TestHandler->LogFile << this->TestProperties->Index << "/"
  724. << this->TestHandler->TotalNumberOfTests
  725. << " Testing: " << this->TestProperties->Name
  726. << std::endl;
  727. *this->TestHandler->LogFile << this->TestProperties->Index << "/"
  728. << this->TestHandler->TotalNumberOfTests
  729. << " Test: " << this->TestProperties->Name
  730. << std::endl;
  731. *this->TestHandler->LogFile << "Command: \"" << this->ActualCommand << "\"";
  732. for (std::string const& arg : this->Arguments) {
  733. *this->TestHandler->LogFile << " \"" << arg << "\"";
  734. }
  735. *this->TestHandler->LogFile
  736. << std::endl
  737. << "Directory: " << this->TestProperties->Directory << std::endl
  738. << "\"" << this->TestProperties->Name
  739. << "\" start time: " << this->StartTime << std::endl;
  740. *this->TestHandler->LogFile
  741. << "Output:" << std::endl
  742. << "----------------------------------------------------------"
  743. << std::endl;
  744. *this->TestHandler->LogFile << this->ProcessOutput << "<end of output>"
  745. << std::endl;
  746. if (!this->CTest->GetTestProgressOutput()) {
  747. cmCTestLog(this->CTest, HANDLER_OUTPUT, outputStream.str());
  748. }
  749. cmCTestLog(this->CTest, DEBUG,
  750. "Testing " << this->TestProperties->Name << " ... ");
  751. }
  752. void cmCTestRunTest::FinalizeTest()
  753. {
  754. this->MultiTestHandler.FinishTestProcess(this, true);
  755. }