cmCTestRunTest.cxx 28 KB

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