cmCTestRunTest.cxx 27 KB

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