cmCTestRunTest.cxx 29 KB

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