cmCTestRunTest.cxx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. (!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. std::vector<std::string>::const_iterator j =
  537. this->TestProperties->Args.begin();
  538. ++j; // skip test name
  539. // find the test executable
  540. if (this->TestHandler->MemCheck) {
  541. cmCTestMemCheckHandler* handler =
  542. static_cast<cmCTestMemCheckHandler*>(this->TestHandler);
  543. this->ActualCommand = handler->MemoryTester;
  544. this->TestProperties->Args[1] = this->TestHandler->FindTheExecutable(
  545. this->TestProperties->Args[1].c_str());
  546. } else {
  547. this->ActualCommand = this->TestHandler->FindTheExecutable(
  548. this->TestProperties->Args[1].c_str());
  549. ++j; // skip the executable (it will be actualCommand)
  550. }
  551. std::string testCommand =
  552. cmSystemTools::ConvertToOutputPath(this->ActualCommand);
  553. // Prepends memcheck args to our command string
  554. this->TestHandler->GenerateTestCommand(this->Arguments, this->Index);
  555. for (std::string const& arg : this->Arguments) {
  556. testCommand += " \"";
  557. testCommand += arg;
  558. testCommand += "\"";
  559. }
  560. for (; j != this->TestProperties->Args.end(); ++j) {
  561. testCommand += " \"";
  562. testCommand += *j;
  563. testCommand += "\"";
  564. this->Arguments.push_back(*j);
  565. }
  566. this->TestResult.FullCommandLine = testCommand;
  567. // Print the test command in verbose mode
  568. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  569. std::endl
  570. << this->Index << ": "
  571. << (this->TestHandler->MemCheck ? "MemCheck" : "Test")
  572. << " command: " << testCommand << std::endl);
  573. // Print any test-specific env vars in verbose mode
  574. if (!this->TestProperties->Environment.empty()) {
  575. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  576. this->Index << ": "
  577. << "Environment variables: " << std::endl);
  578. }
  579. for (std::string const& env : this->TestProperties->Environment) {
  580. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  581. this->Index << ": " << env << std::endl);
  582. }
  583. }
  584. void cmCTestRunTest::DartProcessing()
  585. {
  586. if (!this->ProcessOutput.empty() &&
  587. this->ProcessOutput.find("<DartMeasurement") != std::string::npos) {
  588. if (this->TestHandler->DartStuff.find(this->ProcessOutput)) {
  589. this->TestResult.DartString = this->TestHandler->DartStuff.match(1);
  590. // keep searching and replacing until none are left
  591. while (this->TestHandler->DartStuff1.find(this->ProcessOutput)) {
  592. // replace the exact match for the string
  593. cmSystemTools::ReplaceString(
  594. this->ProcessOutput, this->TestHandler->DartStuff1.match(1).c_str(),
  595. "");
  596. }
  597. }
  598. }
  599. }
  600. bool cmCTestRunTest::ForkProcess(cmDuration testTimeOut, bool explicitTimeout,
  601. std::vector<std::string>* environment,
  602. std::vector<size_t>* affinity)
  603. {
  604. this->TestProcess = cm::make_unique<cmProcess>(*this);
  605. this->TestProcess->SetId(this->Index);
  606. this->TestProcess->SetWorkingDirectory(this->TestProperties->Directory);
  607. this->TestProcess->SetCommand(this->ActualCommand);
  608. this->TestProcess->SetCommandArguments(this->Arguments);
  609. // determine how much time we have
  610. cmDuration timeout = this->CTest->GetRemainingTimeAllowed();
  611. if (timeout != cmCTest::MaxDuration()) {
  612. timeout -= std::chrono::minutes(2);
  613. }
  614. if (this->CTest->GetTimeOut() > cmDuration::zero() &&
  615. this->CTest->GetTimeOut() < timeout) {
  616. timeout = this->CTest->GetTimeOut();
  617. }
  618. if (testTimeOut > cmDuration::zero() &&
  619. testTimeOut < this->CTest->GetRemainingTimeAllowed()) {
  620. timeout = testTimeOut;
  621. }
  622. // always have at least 1 second if we got to here
  623. if (timeout <= cmDuration::zero()) {
  624. timeout = std::chrono::seconds(1);
  625. }
  626. // handle timeout explicitly set to 0
  627. if (testTimeOut == cmDuration::zero() && explicitTimeout) {
  628. timeout = cmDuration::zero();
  629. }
  630. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  631. this->Index << ": "
  632. << "Test timeout computed to be: "
  633. << cmDurationTo<unsigned int>(timeout)
  634. << "\n",
  635. this->TestHandler->GetQuiet());
  636. this->TestProcess->SetTimeout(timeout);
  637. #ifndef CMAKE_BOOTSTRAP
  638. cmSystemTools::SaveRestoreEnvironment sre;
  639. #endif
  640. if (environment && !environment->empty()) {
  641. cmSystemTools::AppendEnv(*environment);
  642. }
  643. return this->TestProcess->StartProcess(this->MultiTestHandler.Loop,
  644. affinity);
  645. }
  646. void cmCTestRunTest::WriteLogOutputTop(size_t completed, size_t total)
  647. {
  648. std::ostringstream outputStream;
  649. // If this is the last or only run of this test, or progress output is
  650. // requested, then print out completed / total.
  651. // Only issue is if a test fails and we are running until fail
  652. // then it will never print out the completed / total, same would
  653. // got for run until pass. Trick is when this is called we don't
  654. // yet know if we are passing or failing.
  655. if (this->NumberOfRunsLeft == 1 || this->CTest->GetTestProgressOutput()) {
  656. outputStream << std::setw(getNumWidth(total)) << completed << "/";
  657. outputStream << std::setw(getNumWidth(total)) << total << " ";
  658. }
  659. // if this is one of several runs of a test just print blank space
  660. // to keep things neat
  661. else {
  662. outputStream << std::setw(getNumWidth(total)) << " ";
  663. outputStream << std::setw(getNumWidth(total)) << " ";
  664. }
  665. if (this->TestHandler->MemCheck) {
  666. outputStream << "MemCheck";
  667. } else {
  668. outputStream << "Test";
  669. }
  670. std::ostringstream indexStr;
  671. indexStr << " #" << this->Index << ":";
  672. outputStream << std::setw(3 + getNumWidth(this->TestHandler->GetMaxIndex()))
  673. << indexStr.str();
  674. outputStream << " ";
  675. const int maxTestNameWidth = this->CTest->GetMaxTestNameWidth();
  676. std::string outname = this->TestProperties->Name + " ";
  677. outname.resize(maxTestNameWidth + 4, '.');
  678. outputStream << outname;
  679. *this->TestHandler->LogFile << this->TestProperties->Index << "/"
  680. << this->TestHandler->TotalNumberOfTests
  681. << " Testing: " << this->TestProperties->Name
  682. << std::endl;
  683. *this->TestHandler->LogFile << this->TestProperties->Index << "/"
  684. << this->TestHandler->TotalNumberOfTests
  685. << " Test: " << this->TestProperties->Name
  686. << std::endl;
  687. *this->TestHandler->LogFile << "Command: \"" << this->ActualCommand << "\"";
  688. for (std::string const& arg : this->Arguments) {
  689. *this->TestHandler->LogFile << " \"" << arg << "\"";
  690. }
  691. *this->TestHandler->LogFile
  692. << std::endl
  693. << "Directory: " << this->TestProperties->Directory << std::endl
  694. << "\"" << this->TestProperties->Name
  695. << "\" start time: " << this->StartTime << std::endl;
  696. *this->TestHandler->LogFile
  697. << "Output:" << std::endl
  698. << "----------------------------------------------------------"
  699. << std::endl;
  700. *this->TestHandler->LogFile << this->ProcessOutput << "<end of output>"
  701. << std::endl;
  702. if (!this->CTest->GetTestProgressOutput()) {
  703. cmCTestLog(this->CTest, HANDLER_OUTPUT, outputStream.str());
  704. }
  705. cmCTestLog(this->CTest, DEBUG,
  706. "Testing " << this->TestProperties->Name << " ... ");
  707. }
  708. void cmCTestRunTest::FinalizeTest()
  709. {
  710. this->MultiTestHandler.FinishTestProcess(this, true);
  711. }