cmCTestRunTest.cxx 30 KB

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