cmCTestRunTest.cxx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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 = CM_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 = CM_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. switch (this->TestProcess->GetExitException()) {
  222. case cmsysProcess_Exception_Fault:
  223. cmCTestLog(this->CTest, HANDLER_OUTPUT, "SegFault");
  224. this->TestResult.Status = cmCTestTestHandler::SEGFAULT;
  225. break;
  226. case cmsysProcess_Exception_Illegal:
  227. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Illegal");
  228. this->TestResult.Status = cmCTestTestHandler::ILLEGAL;
  229. break;
  230. case cmsysProcess_Exception_Interrupt:
  231. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Interrupt");
  232. this->TestResult.Status = cmCTestTestHandler::INTERRUPT;
  233. break;
  234. case cmsysProcess_Exception_Numerical:
  235. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Numerical");
  236. this->TestResult.Status = cmCTestTestHandler::NUMERICAL;
  237. break;
  238. default:
  239. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Other");
  240. this->TestResult.Status = cmCTestTestHandler::OTHER_FAULT;
  241. }
  242. } else if ("Disabled" == this->TestResult.CompletionStatus) {
  243. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Not Run (Disabled) ");
  244. } else // cmsysProcess_State_Error
  245. {
  246. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Not Run ");
  247. }
  248. passed = this->TestResult.Status == cmCTestTestHandler::COMPLETED;
  249. char buf[1024];
  250. sprintf(buf, "%6.2f sec", this->TestProcess->GetTotalTime());
  251. cmCTestLog(this->CTest, HANDLER_OUTPUT, buf << "\n");
  252. if (outputTestErrorsToConsole) {
  253. cmCTestLog(this->CTest, HANDLER_OUTPUT, this->ProcessOutput << std::endl);
  254. }
  255. if (this->TestHandler->LogFile) {
  256. *this->TestHandler->LogFile << "Test time = " << buf << std::endl;
  257. }
  258. // Set the working directory to the tests directory to process Dart files.
  259. {
  260. cmWorkingDirectory workdir(this->TestProperties->Directory);
  261. this->DartProcessing();
  262. }
  263. // if this is doing MemCheck then all the output needs to be put into
  264. // Output since that is what is parsed by cmCTestMemCheckHandler
  265. if (!this->TestHandler->MemCheck && started) {
  266. this->TestHandler->CleanTestOutput(
  267. this->ProcessOutput,
  268. static_cast<size_t>(
  269. this->TestResult.Status == cmCTestTestHandler::COMPLETED
  270. ? this->TestHandler->CustomMaximumPassedTestOutputSize
  271. : this->TestHandler->CustomMaximumFailedTestOutputSize));
  272. }
  273. this->TestResult.Reason = reason;
  274. if (this->TestHandler->LogFile) {
  275. bool pass = true;
  276. const char* reasonType = "Test Pass Reason";
  277. if (this->TestResult.Status != cmCTestTestHandler::COMPLETED &&
  278. this->TestResult.Status != cmCTestTestHandler::NOT_RUN) {
  279. reasonType = "Test Fail Reason";
  280. pass = false;
  281. }
  282. double ttime = this->TestProcess->GetTotalTime();
  283. int hours = static_cast<int>(ttime / (60 * 60));
  284. int minutes = static_cast<int>(ttime / 60) % 60;
  285. int seconds = static_cast<int>(ttime) % 60;
  286. char buffer[100];
  287. sprintf(buffer, "%02d:%02d:%02d", hours, minutes, seconds);
  288. *this->TestHandler->LogFile
  289. << "----------------------------------------------------------"
  290. << std::endl;
  291. if (!this->TestResult.Reason.empty()) {
  292. *this->TestHandler->LogFile << reasonType << ":\n"
  293. << this->TestResult.Reason << "\n";
  294. } else {
  295. if (pass) {
  296. *this->TestHandler->LogFile << "Test Passed.\n";
  297. } else {
  298. *this->TestHandler->LogFile << "Test Failed.\n";
  299. }
  300. }
  301. *this->TestHandler->LogFile
  302. << "\"" << this->TestProperties->Name
  303. << "\" end time: " << this->CTest->CurrentTime() << std::endl
  304. << "\"" << this->TestProperties->Name << "\" time elapsed: " << buffer
  305. << std::endl
  306. << "----------------------------------------------------------"
  307. << std::endl
  308. << std::endl;
  309. }
  310. // if the test actually started and ran
  311. // record the results in TestResult
  312. if (started) {
  313. bool compress = !this->TestHandler->MemCheck &&
  314. this->CompressionRatio < 1 && this->CTest->ShouldCompressTestOutput();
  315. this->TestResult.Output =
  316. compress ? this->CompressedOutput : this->ProcessOutput;
  317. this->TestResult.CompressOutput = compress;
  318. this->TestResult.ReturnValue = this->TestProcess->GetExitValue();
  319. if (!skipped) {
  320. this->TestResult.CompletionStatus = "Completed";
  321. }
  322. this->TestResult.ExecutionTime = this->TestProcess->GetTotalTime();
  323. this->MemCheckPostProcess();
  324. this->ComputeWeightedCost();
  325. }
  326. // If the test does not need to rerun push the current TestResult onto the
  327. // TestHandler vector
  328. if (!this->NeedsToRerun()) {
  329. this->TestHandler->TestResults.push_back(this->TestResult);
  330. }
  331. delete this->TestProcess;
  332. return passed || skipped;
  333. }
  334. bool cmCTestRunTest::StartAgain()
  335. {
  336. if (!this->RunAgain) {
  337. return false;
  338. }
  339. this->RunAgain = false; // reset
  340. // change to tests directory
  341. cmWorkingDirectory workdir(this->TestProperties->Directory);
  342. this->StartTest(this->TotalNumberOfTests);
  343. return true;
  344. }
  345. bool cmCTestRunTest::NeedsToRerun()
  346. {
  347. this->NumberOfRunsLeft--;
  348. if (this->NumberOfRunsLeft == 0) {
  349. return false;
  350. }
  351. // if number of runs left is not 0, and we are running until
  352. // we find a failed test, then return true so the test can be
  353. // restarted
  354. if (this->RunUntilFail &&
  355. this->TestResult.Status == cmCTestTestHandler::COMPLETED) {
  356. this->RunAgain = true;
  357. return true;
  358. }
  359. return false;
  360. }
  361. void cmCTestRunTest::ComputeWeightedCost()
  362. {
  363. double prev = static_cast<double>(this->TestProperties->PreviousRuns);
  364. double avgcost = static_cast<double>(this->TestProperties->Cost);
  365. double current = this->TestResult.ExecutionTime;
  366. if (this->TestResult.Status == cmCTestTestHandler::COMPLETED) {
  367. this->TestProperties->Cost =
  368. static_cast<float>(((prev * avgcost) + current) / (prev + 1.0));
  369. this->TestProperties->PreviousRuns++;
  370. }
  371. }
  372. void cmCTestRunTest::MemCheckPostProcess()
  373. {
  374. if (!this->TestHandler->MemCheck) {
  375. return;
  376. }
  377. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, this->Index
  378. << ": process test output now: "
  379. << this->TestProperties->Name << " "
  380. << this->TestResult.Name << std::endl,
  381. this->TestHandler->GetQuiet());
  382. cmCTestMemCheckHandler* handler =
  383. static_cast<cmCTestMemCheckHandler*>(this->TestHandler);
  384. handler->PostProcessTest(this->TestResult, this->Index);
  385. }
  386. // Starts the execution of a test. Returns once it has started
  387. bool cmCTestRunTest::StartTest(size_t total)
  388. {
  389. this->TotalNumberOfTests = total; // save for rerun case
  390. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(2 * getNumWidth(total) + 8)
  391. << "Start "
  392. << std::setw(getNumWidth(this->TestHandler->GetMaxIndex()))
  393. << this->TestProperties->Index << ": "
  394. << this->TestProperties->Name << std::endl);
  395. this->ProcessOutput.clear();
  396. // Return immediately if test is disabled
  397. if (this->TestProperties->Disabled) {
  398. this->TestResult.Properties = this->TestProperties;
  399. this->TestResult.ExecutionTime = 0;
  400. this->TestResult.CompressOutput = false;
  401. this->TestResult.ReturnValue = -1;
  402. this->TestResult.CompletionStatus = "Disabled";
  403. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  404. this->TestResult.TestCount = this->TestProperties->Index;
  405. this->TestResult.Name = this->TestProperties->Name;
  406. this->TestResult.Path = this->TestProperties->Directory;
  407. this->TestProcess = new cmProcess;
  408. this->TestResult.Output = "Disabled";
  409. this->TestResult.FullCommandLine = "";
  410. return false;
  411. }
  412. this->ComputeArguments();
  413. std::vector<std::string>& args = this->TestProperties->Args;
  414. this->TestResult.Properties = this->TestProperties;
  415. this->TestResult.ExecutionTime = 0;
  416. this->TestResult.CompressOutput = false;
  417. this->TestResult.ReturnValue = -1;
  418. this->TestResult.CompletionStatus = "Failed to start";
  419. this->TestResult.Status = cmCTestTestHandler::BAD_COMMAND;
  420. this->TestResult.TestCount = this->TestProperties->Index;
  421. this->TestResult.Name = this->TestProperties->Name;
  422. this->TestResult.Path = this->TestProperties->Directory;
  423. if (!this->FailedDependencies.empty()) {
  424. this->TestProcess = new cmProcess;
  425. std::string msg = "Failed test dependencies:";
  426. for (std::set<std::string>::const_iterator it =
  427. this->FailedDependencies.begin();
  428. it != this->FailedDependencies.end(); ++it) {
  429. msg += " " + *it;
  430. }
  431. *this->TestHandler->LogFile << msg << std::endl;
  432. cmCTestLog(this->CTest, HANDLER_OUTPUT, msg << std::endl);
  433. this->TestResult.Output = msg;
  434. this->TestResult.FullCommandLine = "";
  435. this->TestResult.CompletionStatus = "Fixture dependency failed";
  436. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  437. return false;
  438. }
  439. if (args.size() >= 2 && args[1] == "NOT_AVAILABLE") {
  440. this->TestProcess = new cmProcess;
  441. std::string msg;
  442. if (this->CTest->GetConfigType().empty()) {
  443. msg = "Test not available without configuration.";
  444. msg += " (Missing \"-C <config>\"?)";
  445. } else {
  446. msg = "Test not available in configuration \"";
  447. msg += this->CTest->GetConfigType();
  448. msg += "\".";
  449. }
  450. *this->TestHandler->LogFile << msg << std::endl;
  451. cmCTestLog(this->CTest, ERROR_MESSAGE, msg << std::endl);
  452. this->TestResult.Output = msg;
  453. this->TestResult.FullCommandLine = "";
  454. this->TestResult.CompletionStatus = "Missing Configuration";
  455. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  456. return false;
  457. }
  458. // Check if all required files exist
  459. for (std::vector<std::string>::iterator i =
  460. this->TestProperties->RequiredFiles.begin();
  461. i != this->TestProperties->RequiredFiles.end(); ++i) {
  462. std::string file = *i;
  463. if (!cmSystemTools::FileExists(file.c_str())) {
  464. // Required file was not found
  465. this->TestProcess = new cmProcess;
  466. *this->TestHandler->LogFile << "Unable to find required file: " << file
  467. << std::endl;
  468. cmCTestLog(this->CTest, ERROR_MESSAGE,
  469. "Unable to find required file: " << file << std::endl);
  470. this->TestResult.Output = "Unable to find required file: " + file;
  471. this->TestResult.FullCommandLine = "";
  472. this->TestResult.CompletionStatus = "Required Files Missing";
  473. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  474. return false;
  475. }
  476. }
  477. // log and return if we did not find the executable
  478. if (this->ActualCommand == "") {
  479. // if the command was not found create a TestResult object
  480. // that has that information
  481. this->TestProcess = new cmProcess;
  482. *this->TestHandler->LogFile << "Unable to find executable: " << args[1]
  483. << std::endl;
  484. cmCTestLog(this->CTest, ERROR_MESSAGE,
  485. "Unable to find executable: " << args[1] << std::endl);
  486. this->TestResult.Output = "Unable to find executable: " + args[1];
  487. this->TestResult.FullCommandLine = "";
  488. this->TestResult.CompletionStatus = "Unable to find executable";
  489. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  490. return false;
  491. }
  492. this->StartTime = this->CTest->CurrentTime();
  493. double timeout = this->ResolveTimeout();
  494. if (this->StopTimePassed) {
  495. return false;
  496. }
  497. return this->ForkProcess(timeout, this->TestProperties->ExplicitTimeout,
  498. &this->TestProperties->Environment);
  499. }
  500. void cmCTestRunTest::ComputeArguments()
  501. {
  502. this->Arguments.clear(); // reset becaue this might be a rerun
  503. std::vector<std::string>::const_iterator j =
  504. this->TestProperties->Args.begin();
  505. ++j; // skip test name
  506. // find the test executable
  507. if (this->TestHandler->MemCheck) {
  508. cmCTestMemCheckHandler* handler =
  509. static_cast<cmCTestMemCheckHandler*>(this->TestHandler);
  510. this->ActualCommand = handler->MemoryTester;
  511. this->TestProperties->Args[1] = this->TestHandler->FindTheExecutable(
  512. this->TestProperties->Args[1].c_str());
  513. } else {
  514. this->ActualCommand = this->TestHandler->FindTheExecutable(
  515. this->TestProperties->Args[1].c_str());
  516. ++j; // skip the executable (it will be actualCommand)
  517. }
  518. std::string testCommand =
  519. cmSystemTools::ConvertToOutputPath(this->ActualCommand.c_str());
  520. // Prepends memcheck args to our command string
  521. this->TestHandler->GenerateTestCommand(this->Arguments, this->Index);
  522. for (std::vector<std::string>::iterator i = this->Arguments.begin();
  523. i != this->Arguments.end(); ++i) {
  524. testCommand += " \"";
  525. testCommand += *i;
  526. testCommand += "\"";
  527. }
  528. for (; j != this->TestProperties->Args.end(); ++j) {
  529. testCommand += " \"";
  530. testCommand += *j;
  531. testCommand += "\"";
  532. this->Arguments.push_back(*j);
  533. }
  534. this->TestResult.FullCommandLine = testCommand;
  535. // Print the test command in verbose mode
  536. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl
  537. << this->Index << ": "
  538. << (this->TestHandler->MemCheck ? "MemCheck" : "Test")
  539. << " command: " << testCommand << std::endl);
  540. // Print any test-specific env vars in verbose mode
  541. if (!this->TestProperties->Environment.empty()) {
  542. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, this->Index
  543. << ": "
  544. << "Environment variables: " << std::endl);
  545. }
  546. for (std::vector<std::string>::const_iterator e =
  547. this->TestProperties->Environment.begin();
  548. e != this->TestProperties->Environment.end(); ++e) {
  549. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, this->Index << ": " << *e
  550. << std::endl);
  551. }
  552. }
  553. void cmCTestRunTest::DartProcessing()
  554. {
  555. if (!this->ProcessOutput.empty() &&
  556. this->ProcessOutput.find("<DartMeasurement") !=
  557. this->ProcessOutput.npos) {
  558. if (this->TestHandler->DartStuff.find(this->ProcessOutput.c_str())) {
  559. this->TestResult.DartString = this->TestHandler->DartStuff.match(1);
  560. // keep searching and replacing until none are left
  561. while (this->TestHandler->DartStuff1.find(this->ProcessOutput.c_str())) {
  562. // replace the exact match for the string
  563. cmSystemTools::ReplaceString(
  564. this->ProcessOutput, this->TestHandler->DartStuff1.match(1).c_str(),
  565. "");
  566. }
  567. }
  568. }
  569. }
  570. double cmCTestRunTest::ResolveTimeout()
  571. {
  572. double timeout = this->TestProperties->Timeout;
  573. if (this->CTest->GetStopTime() == "") {
  574. return timeout;
  575. }
  576. struct tm* lctime;
  577. time_t current_time = time(CM_NULLPTR);
  578. lctime = gmtime(&current_time);
  579. int gm_hour = lctime->tm_hour;
  580. time_t gm_time = mktime(lctime);
  581. lctime = localtime(&current_time);
  582. int local_hour = lctime->tm_hour;
  583. int tzone_offset = local_hour - gm_hour;
  584. if (gm_time > current_time && gm_hour < local_hour) {
  585. // this means gm_time is on the next day
  586. tzone_offset -= 24;
  587. } else if (gm_time < current_time && gm_hour > local_hour) {
  588. // this means gm_time is on the previous day
  589. tzone_offset += 24;
  590. }
  591. tzone_offset *= 100;
  592. char buf[1024];
  593. // add todays year day and month to the time in str because
  594. // curl_getdate no longer assumes the day is today
  595. sprintf(buf, "%d%02d%02d %s %+05i", lctime->tm_year + 1900,
  596. lctime->tm_mon + 1, lctime->tm_mday,
  597. this->CTest->GetStopTime().c_str(), tzone_offset);
  598. time_t stop_time = curl_getdate(buf, &current_time);
  599. if (stop_time == -1) {
  600. return timeout;
  601. }
  602. // the stop time refers to the next day
  603. if (this->CTest->NextDayStopTime) {
  604. stop_time += 24 * 60 * 60;
  605. }
  606. int stop_timeout =
  607. static_cast<int>(stop_time - current_time) % (24 * 60 * 60);
  608. this->CTest->LastStopTimeout = stop_timeout;
  609. if (stop_timeout <= 0 || stop_timeout > this->CTest->LastStopTimeout) {
  610. cmCTestLog(this->CTest, ERROR_MESSAGE, "The stop time has been passed. "
  611. "Stopping all tests."
  612. << std::endl);
  613. this->StopTimePassed = true;
  614. return 0;
  615. }
  616. return timeout == 0 ? stop_timeout
  617. : (timeout < stop_timeout ? timeout : stop_timeout);
  618. }
  619. bool cmCTestRunTest::ForkProcess(double testTimeOut, bool explicitTimeout,
  620. std::vector<std::string>* environment)
  621. {
  622. this->TestProcess = new cmProcess;
  623. this->TestProcess->SetId(this->Index);
  624. this->TestProcess->SetWorkingDirectory(
  625. this->TestProperties->Directory.c_str());
  626. this->TestProcess->SetCommand(this->ActualCommand.c_str());
  627. this->TestProcess->SetCommandArguments(this->Arguments);
  628. // determine how much time we have
  629. double timeout = this->CTest->GetRemainingTimeAllowed() - 120;
  630. if (this->CTest->GetTimeOut() > 0 && this->CTest->GetTimeOut() < timeout) {
  631. timeout = this->CTest->GetTimeOut();
  632. }
  633. if (testTimeOut > 0 &&
  634. testTimeOut < this->CTest->GetRemainingTimeAllowed()) {
  635. timeout = testTimeOut;
  636. }
  637. // always have at least 1 second if we got to here
  638. if (timeout <= 0) {
  639. timeout = 1;
  640. }
  641. // handle timeout explicitly set to 0
  642. if (testTimeOut == 0 && explicitTimeout) {
  643. timeout = 0;
  644. }
  645. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, this->Index
  646. << ": "
  647. << "Test timeout computed to be: " << timeout << "\n",
  648. this->TestHandler->GetQuiet());
  649. this->TestProcess->SetTimeout(timeout);
  650. #ifdef CMAKE_BUILD_WITH_CMAKE
  651. cmSystemTools::SaveRestoreEnvironment sre;
  652. #endif
  653. if (environment && !environment->empty()) {
  654. cmSystemTools::AppendEnv(*environment);
  655. }
  656. return this->TestProcess->StartProcess();
  657. }
  658. void cmCTestRunTest::WriteLogOutputTop(size_t completed, size_t total)
  659. {
  660. // if this is the last or only run of this test
  661. // then print out completed / total
  662. // Only issue is if a test fails and we are running until fail
  663. // then it will never print out the completed / total, same would
  664. // got for run until pass. Trick is when this is called we don't
  665. // yet know if we are passing or failing.
  666. if (this->NumberOfRunsLeft == 1) {
  667. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(getNumWidth(total))
  668. << completed << "/");
  669. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(getNumWidth(total))
  670. << total << " ");
  671. }
  672. // if this is one of several runs of a test just print blank space
  673. // to keep things neat
  674. else {
  675. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(getNumWidth(total))
  676. << " "
  677. << " ");
  678. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(getNumWidth(total))
  679. << " "
  680. << " ");
  681. }
  682. if (this->TestHandler->MemCheck) {
  683. cmCTestLog(this->CTest, HANDLER_OUTPUT, "MemCheck");
  684. } else {
  685. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Test");
  686. }
  687. std::ostringstream indexStr;
  688. indexStr << " #" << this->Index << ":";
  689. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  690. std::setw(3 + getNumWidth(this->TestHandler->GetMaxIndex()))
  691. << indexStr.str());
  692. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  693. const int maxTestNameWidth = this->CTest->GetMaxTestNameWidth();
  694. std::string outname = this->TestProperties->Name + " ";
  695. outname.resize(maxTestNameWidth + 4, '.');
  696. *this->TestHandler->LogFile << this->TestProperties->Index << "/"
  697. << this->TestHandler->TotalNumberOfTests
  698. << " Testing: " << this->TestProperties->Name
  699. << std::endl;
  700. *this->TestHandler->LogFile << this->TestProperties->Index << "/"
  701. << this->TestHandler->TotalNumberOfTests
  702. << " Test: " << this->TestProperties->Name
  703. << std::endl;
  704. *this->TestHandler->LogFile << "Command: \"" << this->ActualCommand << "\"";
  705. for (std::vector<std::string>::iterator i = this->Arguments.begin();
  706. i != this->Arguments.end(); ++i) {
  707. *this->TestHandler->LogFile << " \"" << *i << "\"";
  708. }
  709. *this->TestHandler->LogFile
  710. << std::endl
  711. << "Directory: " << this->TestProperties->Directory << std::endl
  712. << "\"" << this->TestProperties->Name
  713. << "\" start time: " << this->StartTime << std::endl;
  714. *this->TestHandler->LogFile
  715. << "Output:" << std::endl
  716. << "----------------------------------------------------------"
  717. << std::endl;
  718. *this->TestHandler->LogFile << this->ProcessOutput << "<end of output>"
  719. << std::endl;
  720. cmCTestLog(this->CTest, HANDLER_OUTPUT, outname.c_str());
  721. cmCTestLog(this->CTest, DEBUG, "Testing " << this->TestProperties->Name
  722. << " ... ");
  723. }