cmCTestRunTest.cxx 28 KB

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