cmCTestRunTest.cxx 28 KB

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