cmCTestRunTest.cxx 28 KB

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