cmCTestRunTest.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCTestRunTest.h"
  4. #include "cmCTest.h"
  5. #include "cmCTestMemCheckHandler.h"
  6. #include "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 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. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Skipped ");
  200. } else if ((success && !this->TestProperties->WillFail) ||
  201. (!success && this->TestProperties->WillFail)) {
  202. this->TestResult.Status = cmCTestTestHandler::COMPLETED;
  203. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Passed ");
  204. } else {
  205. this->TestResult.Status = cmCTestTestHandler::FAILED;
  206. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Failed " << reason);
  207. outputTestErrorsToConsole = this->CTest->OutputTestOutputOnTestFailure;
  208. }
  209. } else if (res == cmsysProcess_State_Expired) {
  210. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Timeout ");
  211. this->TestResult.Status = cmCTestTestHandler::TIMEOUT;
  212. outputTestErrorsToConsole = this->CTest->OutputTestOutputOnTestFailure;
  213. } else if (res == cmsysProcess_State_Exception) {
  214. outputTestErrorsToConsole = this->CTest->OutputTestOutputOnTestFailure;
  215. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Exception: ");
  216. switch (this->TestProcess->GetExitException()) {
  217. case cmsysProcess_Exception_Fault:
  218. cmCTestLog(this->CTest, HANDLER_OUTPUT, "SegFault");
  219. this->TestResult.Status = cmCTestTestHandler::SEGFAULT;
  220. break;
  221. case cmsysProcess_Exception_Illegal:
  222. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Illegal");
  223. this->TestResult.Status = cmCTestTestHandler::ILLEGAL;
  224. break;
  225. case cmsysProcess_Exception_Interrupt:
  226. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Interrupt");
  227. this->TestResult.Status = cmCTestTestHandler::INTERRUPT;
  228. break;
  229. case cmsysProcess_Exception_Numerical:
  230. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Numerical");
  231. this->TestResult.Status = cmCTestTestHandler::NUMERICAL;
  232. break;
  233. default:
  234. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Other");
  235. this->TestResult.Status = cmCTestTestHandler::OTHER_FAULT;
  236. }
  237. } else // cmsysProcess_State_Error
  238. {
  239. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Not Run ");
  240. }
  241. passed = this->TestResult.Status == cmCTestTestHandler::COMPLETED;
  242. char buf[1024];
  243. sprintf(buf, "%6.2f sec", this->TestProcess->GetTotalTime());
  244. cmCTestLog(this->CTest, HANDLER_OUTPUT, buf << "\n");
  245. if (outputTestErrorsToConsole) {
  246. cmCTestLog(this->CTest, HANDLER_OUTPUT, this->ProcessOutput << std::endl);
  247. }
  248. if (this->TestHandler->LogFile) {
  249. *this->TestHandler->LogFile << "Test time = " << buf << std::endl;
  250. }
  251. // Set the working directory to the tests directory to process Dart files.
  252. {
  253. cmWorkingDirectory workdir(this->TestProperties->Directory);
  254. this->DartProcessing();
  255. }
  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. cmWorkingDirectory workdir(this->TestProperties->Directory);
  333. this->StartTest(this->TotalNumberOfTests);
  334. return true;
  335. }
  336. bool cmCTestRunTest::NeedsToRerun()
  337. {
  338. this->NumberOfRunsLeft--;
  339. if (this->NumberOfRunsLeft == 0) {
  340. return false;
  341. }
  342. // if number of runs left is not 0, and we are running until
  343. // we find a failed test, then return true so the test can be
  344. // restarted
  345. if (this->RunUntilFail &&
  346. this->TestResult.Status == cmCTestTestHandler::COMPLETED) {
  347. this->RunAgain = true;
  348. return true;
  349. }
  350. return false;
  351. }
  352. void cmCTestRunTest::ComputeWeightedCost()
  353. {
  354. double prev = static_cast<double>(this->TestProperties->PreviousRuns);
  355. double avgcost = static_cast<double>(this->TestProperties->Cost);
  356. double current = this->TestResult.ExecutionTime;
  357. if (this->TestResult.Status == cmCTestTestHandler::COMPLETED) {
  358. this->TestProperties->Cost =
  359. static_cast<float>(((prev * avgcost) + current) / (prev + 1.0));
  360. this->TestProperties->PreviousRuns++;
  361. }
  362. }
  363. void cmCTestRunTest::MemCheckPostProcess()
  364. {
  365. if (!this->TestHandler->MemCheck) {
  366. return;
  367. }
  368. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, this->Index
  369. << ": process test output now: "
  370. << this->TestProperties->Name << " "
  371. << this->TestResult.Name << std::endl,
  372. this->TestHandler->GetQuiet());
  373. cmCTestMemCheckHandler* handler =
  374. static_cast<cmCTestMemCheckHandler*>(this->TestHandler);
  375. handler->PostProcessTest(this->TestResult, this->Index);
  376. }
  377. // Starts the execution of a test. Returns once it has started
  378. bool cmCTestRunTest::StartTest(size_t total)
  379. {
  380. this->TotalNumberOfTests = total; // save for rerun case
  381. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(2 * getNumWidth(total) + 8)
  382. << "Start "
  383. << std::setw(getNumWidth(this->TestHandler->GetMaxIndex()))
  384. << this->TestProperties->Index << ": "
  385. << this->TestProperties->Name << std::endl);
  386. this->ProcessOutput.clear();
  387. this->ComputeArguments();
  388. std::vector<std::string>& args = this->TestProperties->Args;
  389. this->TestResult.Properties = this->TestProperties;
  390. this->TestResult.ExecutionTime = 0;
  391. this->TestResult.CompressOutput = false;
  392. this->TestResult.ReturnValue = -1;
  393. this->TestResult.CompletionStatus = "Failed to start";
  394. this->TestResult.Status = cmCTestTestHandler::BAD_COMMAND;
  395. this->TestResult.TestCount = this->TestProperties->Index;
  396. this->TestResult.Name = this->TestProperties->Name;
  397. this->TestResult.Path = this->TestProperties->Directory;
  398. if (!this->FailedDependencies.empty()) {
  399. this->TestProcess = new cmProcess;
  400. std::string msg = "Failed test dependencies:";
  401. for (std::set<std::string>::const_iterator it =
  402. this->FailedDependencies.begin();
  403. it != this->FailedDependencies.end(); ++it) {
  404. msg += " " + *it;
  405. }
  406. *this->TestHandler->LogFile << msg << std::endl;
  407. cmCTestLog(this->CTest, HANDLER_OUTPUT, msg << std::endl);
  408. this->TestResult.Output = msg;
  409. this->TestResult.FullCommandLine = "";
  410. this->TestResult.CompletionStatus = "Not Run";
  411. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  412. return false;
  413. }
  414. if (args.size() >= 2 && args[1] == "NOT_AVAILABLE") {
  415. this->TestProcess = new cmProcess;
  416. std::string msg;
  417. if (this->CTest->GetConfigType().empty()) {
  418. msg = "Test not available without configuration.";
  419. msg += " (Missing \"-C <config>\"?)";
  420. } else {
  421. msg = "Test not available in configuration \"";
  422. msg += this->CTest->GetConfigType();
  423. msg += "\".";
  424. }
  425. *this->TestHandler->LogFile << msg << std::endl;
  426. cmCTestLog(this->CTest, ERROR_MESSAGE, msg << std::endl);
  427. this->TestResult.Output = msg;
  428. this->TestResult.FullCommandLine = "";
  429. this->TestResult.CompletionStatus = "Not Run";
  430. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  431. return false;
  432. }
  433. // Check if all required files exist
  434. for (std::vector<std::string>::iterator i =
  435. this->TestProperties->RequiredFiles.begin();
  436. i != this->TestProperties->RequiredFiles.end(); ++i) {
  437. std::string file = *i;
  438. if (!cmSystemTools::FileExists(file.c_str())) {
  439. // Required file was not found
  440. this->TestProcess = new cmProcess;
  441. *this->TestHandler->LogFile << "Unable to find required file: " << file
  442. << std::endl;
  443. cmCTestLog(this->CTest, ERROR_MESSAGE,
  444. "Unable to find required file: " << file << std::endl);
  445. this->TestResult.Output = "Unable to find required file: " + file;
  446. this->TestResult.FullCommandLine = "";
  447. this->TestResult.CompletionStatus = "Not Run";
  448. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  449. return false;
  450. }
  451. }
  452. // log and return if we did not find the executable
  453. if (this->ActualCommand == "") {
  454. // if the command was not found create a TestResult object
  455. // that has that information
  456. this->TestProcess = new cmProcess;
  457. *this->TestHandler->LogFile << "Unable to find executable: " << args[1]
  458. << std::endl;
  459. cmCTestLog(this->CTest, ERROR_MESSAGE,
  460. "Unable to find executable: " << args[1] << std::endl);
  461. this->TestResult.Output = "Unable to find executable: " + args[1];
  462. this->TestResult.FullCommandLine = "";
  463. this->TestResult.CompletionStatus = "Not Run";
  464. this->TestResult.Status = cmCTestTestHandler::NOT_RUN;
  465. return false;
  466. }
  467. this->StartTime = this->CTest->CurrentTime();
  468. double timeout = this->ResolveTimeout();
  469. if (this->StopTimePassed) {
  470. return false;
  471. }
  472. return this->ForkProcess(timeout, this->TestProperties->ExplicitTimeout,
  473. &this->TestProperties->Environment);
  474. }
  475. void cmCTestRunTest::ComputeArguments()
  476. {
  477. this->Arguments.clear(); // reset becaue this might be a rerun
  478. std::vector<std::string>::const_iterator j =
  479. this->TestProperties->Args.begin();
  480. ++j; // skip test name
  481. // find the test executable
  482. if (this->TestHandler->MemCheck) {
  483. cmCTestMemCheckHandler* handler =
  484. static_cast<cmCTestMemCheckHandler*>(this->TestHandler);
  485. this->ActualCommand = handler->MemoryTester;
  486. this->TestProperties->Args[1] = this->TestHandler->FindTheExecutable(
  487. this->TestProperties->Args[1].c_str());
  488. } else {
  489. this->ActualCommand = this->TestHandler->FindTheExecutable(
  490. this->TestProperties->Args[1].c_str());
  491. ++j; // skip the executable (it will be actualCommand)
  492. }
  493. std::string testCommand =
  494. cmSystemTools::ConvertToOutputPath(this->ActualCommand.c_str());
  495. // Prepends memcheck args to our command string
  496. this->TestHandler->GenerateTestCommand(this->Arguments, this->Index);
  497. for (std::vector<std::string>::iterator i = this->Arguments.begin();
  498. i != this->Arguments.end(); ++i) {
  499. testCommand += " \"";
  500. testCommand += *i;
  501. testCommand += "\"";
  502. }
  503. for (; j != this->TestProperties->Args.end(); ++j) {
  504. testCommand += " \"";
  505. testCommand += *j;
  506. testCommand += "\"";
  507. this->Arguments.push_back(*j);
  508. }
  509. this->TestResult.FullCommandLine = testCommand;
  510. // Print the test command in verbose mode
  511. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl
  512. << this->Index << ": "
  513. << (this->TestHandler->MemCheck ? "MemCheck" : "Test")
  514. << " command: " << testCommand << std::endl);
  515. // Print any test-specific env vars in verbose mode
  516. if (!this->TestProperties->Environment.empty()) {
  517. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, this->Index
  518. << ": "
  519. << "Environment variables: " << std::endl);
  520. }
  521. for (std::vector<std::string>::const_iterator e =
  522. this->TestProperties->Environment.begin();
  523. e != this->TestProperties->Environment.end(); ++e) {
  524. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, this->Index << ": " << *e
  525. << std::endl);
  526. }
  527. }
  528. void cmCTestRunTest::DartProcessing()
  529. {
  530. if (!this->ProcessOutput.empty() &&
  531. this->ProcessOutput.find("<DartMeasurement") !=
  532. this->ProcessOutput.npos) {
  533. if (this->TestHandler->DartStuff.find(this->ProcessOutput.c_str())) {
  534. this->TestResult.DartString = this->TestHandler->DartStuff.match(1);
  535. // keep searching and replacing until none are left
  536. while (this->TestHandler->DartStuff1.find(this->ProcessOutput.c_str())) {
  537. // replace the exact match for the string
  538. cmSystemTools::ReplaceString(
  539. this->ProcessOutput, this->TestHandler->DartStuff1.match(1).c_str(),
  540. "");
  541. }
  542. }
  543. }
  544. }
  545. double cmCTestRunTest::ResolveTimeout()
  546. {
  547. double timeout = this->TestProperties->Timeout;
  548. if (this->CTest->GetStopTime() == "") {
  549. return timeout;
  550. }
  551. struct tm* lctime;
  552. time_t current_time = time(CM_NULLPTR);
  553. lctime = gmtime(&current_time);
  554. int gm_hour = lctime->tm_hour;
  555. time_t gm_time = mktime(lctime);
  556. lctime = localtime(&current_time);
  557. int local_hour = lctime->tm_hour;
  558. int tzone_offset = local_hour - gm_hour;
  559. if (gm_time > current_time && gm_hour < local_hour) {
  560. // this means gm_time is on the next day
  561. tzone_offset -= 24;
  562. } else if (gm_time < current_time && gm_hour > local_hour) {
  563. // this means gm_time is on the previous day
  564. tzone_offset += 24;
  565. }
  566. tzone_offset *= 100;
  567. char buf[1024];
  568. // add todays year day and month to the time in str because
  569. // curl_getdate no longer assumes the day is today
  570. sprintf(buf, "%d%02d%02d %s %+05i", lctime->tm_year + 1900,
  571. lctime->tm_mon + 1, lctime->tm_mday,
  572. this->CTest->GetStopTime().c_str(), tzone_offset);
  573. time_t stop_time = curl_getdate(buf, &current_time);
  574. if (stop_time == -1) {
  575. return timeout;
  576. }
  577. // the stop time refers to the next day
  578. if (this->CTest->NextDayStopTime) {
  579. stop_time += 24 * 60 * 60;
  580. }
  581. int stop_timeout =
  582. static_cast<int>(stop_time - current_time) % (24 * 60 * 60);
  583. this->CTest->LastStopTimeout = stop_timeout;
  584. if (stop_timeout <= 0 || stop_timeout > this->CTest->LastStopTimeout) {
  585. cmCTestLog(this->CTest, ERROR_MESSAGE, "The stop time has been passed. "
  586. "Stopping all tests."
  587. << std::endl);
  588. this->StopTimePassed = true;
  589. return 0;
  590. }
  591. return timeout == 0 ? stop_timeout
  592. : (timeout < stop_timeout ? timeout : stop_timeout);
  593. }
  594. bool cmCTestRunTest::ForkProcess(double testTimeOut, bool explicitTimeout,
  595. std::vector<std::string>* environment)
  596. {
  597. this->TestProcess = new cmProcess;
  598. this->TestProcess->SetId(this->Index);
  599. this->TestProcess->SetWorkingDirectory(
  600. this->TestProperties->Directory.c_str());
  601. this->TestProcess->SetCommand(this->ActualCommand.c_str());
  602. this->TestProcess->SetCommandArguments(this->Arguments);
  603. // determine how much time we have
  604. double timeout = this->CTest->GetRemainingTimeAllowed() - 120;
  605. if (this->CTest->GetTimeOut() > 0 && this->CTest->GetTimeOut() < timeout) {
  606. timeout = this->CTest->GetTimeOut();
  607. }
  608. if (testTimeOut > 0 &&
  609. testTimeOut < this->CTest->GetRemainingTimeAllowed()) {
  610. timeout = testTimeOut;
  611. }
  612. // always have at least 1 second if we got to here
  613. if (timeout <= 0) {
  614. timeout = 1;
  615. }
  616. // handle timeout explicitly set to 0
  617. if (testTimeOut == 0 && explicitTimeout) {
  618. timeout = 0;
  619. }
  620. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, this->Index
  621. << ": "
  622. << "Test timeout computed to be: " << timeout << "\n",
  623. this->TestHandler->GetQuiet());
  624. this->TestProcess->SetTimeout(timeout);
  625. #ifdef CMAKE_BUILD_WITH_CMAKE
  626. cmSystemTools::SaveRestoreEnvironment sre;
  627. #endif
  628. if (environment && !environment->empty()) {
  629. cmSystemTools::AppendEnv(*environment);
  630. }
  631. return this->TestProcess->StartProcess();
  632. }
  633. void cmCTestRunTest::WriteLogOutputTop(size_t completed, size_t total)
  634. {
  635. // if this is the last or only run of this test
  636. // then print out completed / total
  637. // Only issue is if a test fails and we are running until fail
  638. // then it will never print out the completed / total, same would
  639. // got for run until pass. Trick is when this is called we don't
  640. // yet know if we are passing or failing.
  641. if (this->NumberOfRunsLeft == 1) {
  642. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(getNumWidth(total))
  643. << completed << "/");
  644. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(getNumWidth(total))
  645. << total << " ");
  646. }
  647. // if this is one of several runs of a test just print blank space
  648. // to keep things neat
  649. else {
  650. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(getNumWidth(total))
  651. << " "
  652. << " ");
  653. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(getNumWidth(total))
  654. << " "
  655. << " ");
  656. }
  657. if (this->TestHandler->MemCheck) {
  658. cmCTestLog(this->CTest, HANDLER_OUTPUT, "MemCheck");
  659. } else {
  660. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Test");
  661. }
  662. std::ostringstream indexStr;
  663. indexStr << " #" << this->Index << ":";
  664. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  665. std::setw(3 + getNumWidth(this->TestHandler->GetMaxIndex()))
  666. << indexStr.str());
  667. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  668. const int maxTestNameWidth = this->CTest->GetMaxTestNameWidth();
  669. std::string outname = this->TestProperties->Name + " ";
  670. outname.resize(maxTestNameWidth + 4, '.');
  671. *this->TestHandler->LogFile << this->TestProperties->Index << "/"
  672. << this->TestHandler->TotalNumberOfTests
  673. << " Testing: " << this->TestProperties->Name
  674. << std::endl;
  675. *this->TestHandler->LogFile << this->TestProperties->Index << "/"
  676. << this->TestHandler->TotalNumberOfTests
  677. << " Test: " << this->TestProperties->Name
  678. << std::endl;
  679. *this->TestHandler->LogFile << "Command: \"" << this->ActualCommand << "\"";
  680. for (std::vector<std::string>::iterator i = this->Arguments.begin();
  681. i != this->Arguments.end(); ++i) {
  682. *this->TestHandler->LogFile << " \"" << *i << "\"";
  683. }
  684. *this->TestHandler->LogFile
  685. << std::endl
  686. << "Directory: " << this->TestProperties->Directory << std::endl
  687. << "\"" << this->TestProperties->Name
  688. << "\" start time: " << this->StartTime << std::endl;
  689. *this->TestHandler->LogFile
  690. << "Output:" << std::endl
  691. << "----------------------------------------------------------"
  692. << std::endl;
  693. *this->TestHandler->LogFile << this->ProcessOutput << "<end of output>"
  694. << std::endl;
  695. cmCTestLog(this->CTest, HANDLER_OUTPUT, outname.c_str());
  696. cmCTestLog(this->CTest, DEBUG, "Testing " << this->TestProperties->Name
  697. << " ... ");
  698. }