You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1785 lines
51 KiB

21 years ago
19 years ago
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmCTestTestHandler.h"
  14. #include "cmCTest.h"
  15. #include "cmake.h"
  16. #include "cmGeneratedFileStream.h"
  17. #include <cmsys/Process.h>
  18. #include <cmsys/RegularExpression.hxx>
  19. #include <cmsys/Base64.h>
  20. #include "cmMakefile.h"
  21. #include "cmGlobalGenerator.h"
  22. #include "cmLocalGenerator.h"
  23. #include "cmCommand.h"
  24. #include "cmSystemTools.h"
  25. #include <stdlib.h>
  26. #include <math.h>
  27. #include <float.h>
  28. #include <memory> // auto_ptr
  29. //----------------------------------------------------------------------
  30. class cmCTestSubdirCommand : public cmCommand
  31. {
  32. public:
  33. /**
  34. * This is a virtual constructor for the command.
  35. */
  36. virtual cmCommand* Clone()
  37. {
  38. cmCTestSubdirCommand* c = new cmCTestSubdirCommand;
  39. c->TestHandler = this->TestHandler;
  40. return c;
  41. }
  42. /**
  43. * This is called when the command is first encountered in
  44. * the CMakeLists.txt file.
  45. */
  46. virtual bool InitialPass(std::vector<std::string> const& args);
  47. /**
  48. * The name of the command as specified in CMakeList.txt.
  49. */
  50. virtual const char* GetName() { return "SUBDIRS";}
  51. // Unused methods
  52. virtual const char* GetTerseDocumentation() { return ""; }
  53. virtual const char* GetFullDocumentation() { return ""; }
  54. cmTypeMacro(cmCTestSubdirCommand, cmCommand);
  55. cmCTestTestHandler* TestHandler;
  56. };
  57. //----------------------------------------------------------------------
  58. bool cmCTestSubdirCommand::InitialPass(std::vector<std::string> const& args)
  59. {
  60. if(args.size() < 1 )
  61. {
  62. this->SetError("called with incorrect number of arguments");
  63. return false;
  64. }
  65. std::vector<std::string>::const_iterator it;
  66. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  67. for ( it = args.begin(); it != args.end(); ++ it )
  68. {
  69. cmSystemTools::ChangeDirectory(cwd.c_str());
  70. std::string fname = cwd;
  71. fname += "/";
  72. fname += *it;
  73. if ( !cmSystemTools::FileExists(fname.c_str()) )
  74. {
  75. // No subdirectory? So what...
  76. continue;
  77. }
  78. cmSystemTools::ChangeDirectory(fname.c_str());
  79. const char* testFilename;
  80. if( cmSystemTools::FileExists("CTestTestfile.cmake") )
  81. {
  82. // does the CTestTestfile.cmake exist ?
  83. testFilename = "CTestTestfile.cmake";
  84. }
  85. else if( cmSystemTools::FileExists("DartTestfile.txt") )
  86. {
  87. // does the DartTestfile.txt exist ?
  88. testFilename = "DartTestfile.txt";
  89. }
  90. else
  91. {
  92. // No DartTestfile.txt? Who cares...
  93. cmSystemTools::ChangeDirectory(cwd.c_str());
  94. continue;
  95. }
  96. fname += "/";
  97. fname += testFilename;
  98. bool readit =
  99. this->Makefile->ReadListFile(this->Makefile->GetCurrentListFile(),
  100. fname.c_str());
  101. cmSystemTools::ChangeDirectory(cwd.c_str());
  102. if(!readit)
  103. {
  104. std::string m = "Could not find include file: ";
  105. m += fname;
  106. this->SetError(m.c_str());
  107. return false;
  108. }
  109. }
  110. return true;
  111. }
  112. //----------------------------------------------------------------------
  113. class cmCTestAddTestCommand : public cmCommand
  114. {
  115. public:
  116. /**
  117. * This is a virtual constructor for the command.
  118. */
  119. virtual cmCommand* Clone()
  120. {
  121. cmCTestAddTestCommand* c = new cmCTestAddTestCommand;
  122. c->TestHandler = this->TestHandler;
  123. return c;
  124. }
  125. /**
  126. * This is called when the command is first encountered in
  127. * the CMakeLists.txt file.
  128. */
  129. virtual bool InitialPass(std::vector<std::string> const&);
  130. /**
  131. * The name of the command as specified in CMakeList.txt.
  132. */
  133. virtual const char* GetName() { return "ADD_TEST";}
  134. // Unused methods
  135. virtual const char* GetTerseDocumentation() { return ""; }
  136. virtual const char* GetFullDocumentation() { return ""; }
  137. cmTypeMacro(cmCTestAddTestCommand, cmCommand);
  138. cmCTestTestHandler* TestHandler;
  139. };
  140. //----------------------------------------------------------------------
  141. bool cmCTestAddTestCommand::InitialPass(std::vector<std::string> const& args)
  142. {
  143. if ( args.size() < 2 )
  144. {
  145. this->SetError("called with incorrect number of arguments");
  146. return false;
  147. }
  148. return this->TestHandler->AddTest(args);
  149. }
  150. //----------------------------------------------------------------------
  151. class cmCTestSetTestsPropertiesCommand : public cmCommand
  152. {
  153. public:
  154. /**
  155. * This is a virtual constructor for the command.
  156. */
  157. virtual cmCommand* Clone()
  158. {
  159. cmCTestSetTestsPropertiesCommand* c
  160. = new cmCTestSetTestsPropertiesCommand;
  161. c->TestHandler = this->TestHandler;
  162. return c;
  163. }
  164. /**
  165. * This is called when the command is first encountered in
  166. * the CMakeLists.txt file.
  167. */
  168. virtual bool InitialPass(std::vector<std::string> const&);
  169. /**
  170. * The name of the command as specified in CMakeList.txt.
  171. */
  172. virtual const char* GetName() { return "SET_TESTS_PROPERTIES";}
  173. // Unused methods
  174. virtual const char* GetTerseDocumentation() { return ""; }
  175. virtual const char* GetFullDocumentation() { return ""; }
  176. cmTypeMacro(cmCTestSetTestsPropertiesCommand, cmCommand);
  177. cmCTestTestHandler* TestHandler;
  178. };
  179. //----------------------------------------------------------------------
  180. bool cmCTestSetTestsPropertiesCommand::InitialPass(
  181. std::vector<std::string> const& args)
  182. {
  183. return this->TestHandler->SetTestsProperties(args);
  184. }
  185. //----------------------------------------------------------------------
  186. // Try to find an executable, if found fullPath will be set to the full path
  187. // of where it was found. The directory and filename to search for are passed
  188. // in as well an a subdir (typically used for configuraitons such as
  189. // Release/Debug/etc)
  190. bool TryExecutable(const char *dir, const char *file,
  191. std::string *fullPath, const char *subdir)
  192. {
  193. // try current directory
  194. std::string tryPath;
  195. if (dir && strcmp(dir,""))
  196. {
  197. tryPath = dir;
  198. tryPath += "/";
  199. }
  200. if (subdir && strcmp(subdir,""))
  201. {
  202. tryPath += subdir;
  203. tryPath += "/";
  204. }
  205. tryPath += file;
  206. // find the file without an executable extension
  207. if(cmSystemTools::FileExists(tryPath.c_str()))
  208. {
  209. *fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  210. return true;
  211. }
  212. // if not found try it with the executable extension
  213. tryPath += cmSystemTools::GetExecutableExtension();
  214. if(cmSystemTools::FileExists(tryPath.c_str()))
  215. {
  216. *fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  217. return true;
  218. }
  219. // not found at all, return false
  220. return false;
  221. }
  222. //----------------------------------------------------------------------
  223. // get the next number in a string with numbers separated by ,
  224. // pos is the start of the search and pos2 is the end of the search
  225. // pos becomes pos2 after a call to GetNextNumber.
  226. // -1 is returned at the end of the list.
  227. inline int GetNextNumber(std::string const& in,
  228. int& val,
  229. std::string::size_type& pos,
  230. std::string::size_type& pos2)
  231. {
  232. pos2 = in.find(',', pos);
  233. if(pos2 != in.npos)
  234. {
  235. if(pos2-pos == 0)
  236. {
  237. val = -1;
  238. }
  239. else
  240. {
  241. val = atoi(in.substr(pos, pos2-pos).c_str());
  242. }
  243. pos = pos2+1;
  244. return 1;
  245. }
  246. else
  247. {
  248. if(in.size()-pos == 0)
  249. {
  250. val = -1;
  251. }
  252. else
  253. {
  254. val = atoi(in.substr(pos, in.size()-pos).c_str());
  255. }
  256. return 0;
  257. }
  258. }
  259. //----------------------------------------------------------------------
  260. // get the next number in a string with numbers separated by ,
  261. // pos is the start of the search and pos2 is the end of the search
  262. // pos becomes pos2 after a call to GetNextNumber.
  263. // -1 is returned at the end of the list.
  264. inline int GetNextRealNumber(std::string const& in,
  265. double& val,
  266. std::string::size_type& pos,
  267. std::string::size_type& pos2)
  268. {
  269. pos2 = in.find(',', pos);
  270. if(pos2 != in.npos)
  271. {
  272. if(pos2-pos == 0)
  273. {
  274. val = -1;
  275. }
  276. else
  277. {
  278. val = atof(in.substr(pos, pos2-pos).c_str());
  279. }
  280. pos = pos2+1;
  281. return 1;
  282. }
  283. else
  284. {
  285. if(in.size()-pos == 0)
  286. {
  287. val = -1;
  288. }
  289. else
  290. {
  291. val = atof(in.substr(pos, in.size()-pos).c_str());
  292. }
  293. return 0;
  294. }
  295. }
  296. //----------------------------------------------------------------------
  297. cmCTestTestHandler::cmCTestTestHandler()
  298. {
  299. this->UseUnion = false;
  300. this->UseIncludeRegExpFlag = false;
  301. this->UseExcludeRegExpFlag = false;
  302. this->UseExcludeRegExpFirst = false;
  303. this->CustomMaximumPassedTestOutputSize = 1 * 1024;
  304. this->CustomMaximumFailedTestOutputSize = 300 * 1024;
  305. this->MemCheck = false;
  306. this->LogFile = 0;
  307. this->DartStuff.compile("(<DartMeasurement.*/DartMeasurement[a-zA-Z]*>)");
  308. }
  309. //----------------------------------------------------------------------
  310. void cmCTestTestHandler::Initialize()
  311. {
  312. this->Superclass::Initialize();
  313. this->ElapsedTestingTime = -1;
  314. this->TestResults.clear();
  315. this->CustomTestsIgnore.clear();
  316. this->StartTest = "";
  317. this->EndTest = "";
  318. this->CustomPreTest.clear();
  319. this->CustomPostTest.clear();
  320. this->CustomMaximumPassedTestOutputSize = 1 * 1024;
  321. this->CustomMaximumFailedTestOutputSize = 300 * 1024;
  322. this->TestsToRun.clear();
  323. this->UseIncludeRegExpFlag = false;
  324. this->UseExcludeRegExpFlag = false;
  325. this->UseExcludeRegExpFirst = false;
  326. this->IncludeRegExp = "";
  327. this->ExcludeRegExp = "";
  328. TestsToRunString = "";
  329. this->UseUnion = false;
  330. this->TestList.clear();
  331. }
  332. //----------------------------------------------------------------------
  333. void cmCTestTestHandler::PopulateCustomVectors(cmMakefile *mf)
  334. {
  335. this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_PRE_TEST",
  336. this->CustomPreTest);
  337. this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_POST_TEST",
  338. this->CustomPostTest);
  339. this->CTest->PopulateCustomVector(mf,
  340. "CTEST_CUSTOM_TESTS_IGNORE",
  341. this->CustomTestsIgnore);
  342. this->CTest->PopulateCustomInteger(mf,
  343. "CTEST_CUSTOM_MAXIMUM_PASSED_TEST_OUTPUT_SIZE",
  344. this->CustomMaximumPassedTestOutputSize);
  345. this->CTest->PopulateCustomInteger(mf,
  346. "CTEST_CUSTOM_MAXIMUM_FAILED_TEST_OUTPUT_SIZE",
  347. this->CustomMaximumFailedTestOutputSize);
  348. }
  349. //----------------------------------------------------------------------
  350. int cmCTestTestHandler::PreProcessHandler()
  351. {
  352. if ( !this->ExecuteCommands(this->CustomPreTest) )
  353. {
  354. cmCTestLog(this->CTest, ERROR_MESSAGE,
  355. "Problem executing pre-test command(s)." << std::endl);
  356. return 0;
  357. }
  358. return 1;
  359. }
  360. //----------------------------------------------------------------------
  361. int cmCTestTestHandler::PostProcessHandler()
  362. {
  363. if ( !this->ExecuteCommands(this->CustomPostTest) )
  364. {
  365. cmCTestLog(this->CTest, ERROR_MESSAGE,
  366. "Problem executing post-test command(s)." << std::endl);
  367. return 0;
  368. }
  369. return 1;
  370. }
  371. //----------------------------------------------------------------------
  372. //clearly it would be nice if this were broken up into a few smaller
  373. //functions and commented...
  374. int cmCTestTestHandler::ProcessHandler()
  375. {
  376. // Update internal data structure from generic one
  377. this->SetTestsToRunInformation(this->GetOption("TestsToRunInformation"));
  378. this->SetUseUnion(cmSystemTools::IsOn(this->GetOption("UseUnion")));
  379. const char* val;
  380. val = this->GetOption("IncludeRegularExpression");
  381. if ( val )
  382. {
  383. this->UseIncludeRegExp();
  384. this->SetIncludeRegExp(val);
  385. }
  386. val = this->GetOption("ExcludeRegularExpression");
  387. if ( val )
  388. {
  389. this->UseExcludeRegExp();
  390. this->SetExcludeRegExp(val);
  391. }
  392. this->TestResults.clear();
  393. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  394. (this->MemCheck ? "Memory check" : "Test")
  395. << " project " << cmSystemTools::GetCurrentWorkingDirectory()
  396. << std::endl);
  397. if ( ! this->PreProcessHandler() )
  398. {
  399. return -1;
  400. }
  401. cmGeneratedFileStream mLogFile;
  402. this->StartLogFile((this->MemCheck ? "DynamicAnalysis" : "Test"), mLogFile);
  403. this->LogFile = &mLogFile;
  404. std::vector<cmStdString> passed;
  405. std::vector<cmStdString> failed;
  406. int total;
  407. this->ProcessDirectory(passed, failed);
  408. total = int(passed.size()) + int(failed.size());
  409. if (total == 0)
  410. {
  411. if ( !this->CTest->GetShowOnly() )
  412. {
  413. cmCTestLog(this->CTest, ERROR_MESSAGE, "No tests were found!!!"
  414. << std::endl);
  415. }
  416. }
  417. else
  418. {
  419. if (this->HandlerVerbose && passed.size() &&
  420. (this->UseIncludeRegExpFlag || this->UseExcludeRegExpFlag))
  421. {
  422. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl
  423. << "The following tests passed:" << std::endl);
  424. for(std::vector<cmStdString>::iterator j = passed.begin();
  425. j != passed.end(); ++j)
  426. {
  427. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "\t" << *j
  428. << std::endl);
  429. }
  430. }
  431. float percent = float(passed.size()) * 100.0f / total;
  432. if ( failed.size() > 0 && percent > 99)
  433. {
  434. percent = 99;
  435. }
  436. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl
  437. << static_cast<int>(percent + .5) << "% tests passed, "
  438. << failed.size() << " tests failed out of " << total << std::endl);
  439. //fprintf(stderr,"\n%.0f%% tests passed, %i tests failed out of %i\n",
  440. // percent, int(failed.size()), total);
  441. if (failed.size())
  442. {
  443. cmGeneratedFileStream ofs;
  444. cmCTestLog(this->CTest, ERROR_MESSAGE, std::endl
  445. << "The following tests FAILED:" << std::endl);
  446. this->StartLogFile("TestsFailed", ofs);
  447. std::vector<cmCTestTestHandler::cmCTestTestResult>::iterator ftit;
  448. for(ftit = this->TestResults.begin();
  449. ftit != this->TestResults.end(); ++ftit)
  450. {
  451. if ( ftit->Status != cmCTestTestHandler::COMPLETED )
  452. {
  453. ofs << ftit->TestCount << ":" << ftit->Name << std::endl;
  454. cmCTestLog(this->CTest, HANDLER_OUTPUT, "\t" << std::setw(3)
  455. << ftit->TestCount << " - " << ftit->Name.c_str() << " ("
  456. << this->GetTestStatus(ftit->Status) << ")" << std::endl);
  457. }
  458. }
  459. }
  460. }
  461. if ( this->CTest->GetProduceXML() )
  462. {
  463. cmGeneratedFileStream xmlfile;
  464. if( !this->StartResultingXML(
  465. (this->MemCheck ? "DynamicAnalysis" : "Test"), xmlfile) )
  466. {
  467. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot create "
  468. << (this->MemCheck ? "memory check" : "testing")
  469. << " XML file" << std::endl);
  470. this->LogFile = 0;
  471. return 1;
  472. }
  473. this->GenerateDartOutput(xmlfile);
  474. }
  475. if ( ! this->PostProcessHandler() )
  476. {
  477. this->LogFile = 0;
  478. return -1;
  479. }
  480. if ( !failed.empty() )
  481. {
  482. this->LogFile = 0;
  483. return -1;
  484. }
  485. this->LogFile = 0;
  486. return 0;
  487. }
  488. //----------------------------------------------------------------------
  489. void cmCTestTestHandler::ProcessOneTest(cmCTestTestProperties *it,
  490. std::vector<cmStdString> &passed,
  491. std::vector<cmStdString> &failed,
  492. int cnt, int tmsize)
  493. {
  494. const std::string& testname = it->Name;
  495. std::vector<std::string>& args = it->Args;
  496. cmCTestTestResult cres;
  497. cres.Properties = &*it;
  498. cres.ExecutionTime = 0;
  499. cres.ReturnValue = -1;
  500. cres.Status = cmCTestTestHandler::NOT_RUN;
  501. cres.TestCount = cnt;
  502. cres.Name = testname;
  503. cres.Path = it->Directory.c_str();
  504. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(3) << cnt << "/");
  505. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(3) << tmsize << " ");
  506. if ( this->MemCheck )
  507. {
  508. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Memory Check");
  509. }
  510. else
  511. {
  512. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Testing");
  513. }
  514. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  515. std::string outname = testname;
  516. outname.resize(30, ' ');
  517. *this->LogFile << cnt << "/" << tmsize << " Testing: " << testname
  518. << std::endl;
  519. if ( this->CTest->GetShowOnly() )
  520. {
  521. cmCTestLog(this->CTest, HANDLER_OUTPUT, outname.c_str() << std::endl);
  522. }
  523. else
  524. {
  525. cmCTestLog(this->CTest, HANDLER_OUTPUT, outname.c_str());
  526. }
  527. cmCTestLog(this->CTest, DEBUG, "Testing " << args[0].c_str() << " ... ");
  528. // find the test executable
  529. std::string actualCommand = this->FindTheExecutable(args[1].c_str());
  530. std::string testCommand
  531. = cmSystemTools::ConvertToOutputPath(actualCommand.c_str());
  532. // continue if we did not find the executable
  533. if (testCommand == "")
  534. {
  535. *this->LogFile << "Unable to find executable: " << args[1].c_str()
  536. << std::endl;
  537. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unable to find executable: "
  538. << args[1].c_str() << std::endl);
  539. cres.Output = "Unable to find executable: " + args[1];
  540. if ( !this->CTest->GetShowOnly() )
  541. {
  542. cres.FullCommandLine = actualCommand;
  543. this->TestResults.push_back( cres );
  544. failed.push_back(testname);
  545. return;
  546. }
  547. }
  548. // add the arguments
  549. std::vector<std::string>::const_iterator j = args.begin();
  550. ++j;
  551. ++j;
  552. std::vector<const char*> arguments;
  553. this->GenerateTestCommand(arguments);
  554. arguments.push_back(actualCommand.c_str());
  555. for(;j != args.end(); ++j)
  556. {
  557. testCommand += " ";
  558. testCommand += cmSystemTools::EscapeSpaces(j->c_str());
  559. arguments.push_back(j->c_str());
  560. }
  561. arguments.push_back(0);
  562. /**
  563. * Run an executable command and put the stdout in output.
  564. */
  565. std::string output;
  566. int retVal = 0;
  567. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl
  568. << (this->MemCheck?"MemCheck":"Test")
  569. << " command: " << testCommand
  570. << std::endl);
  571. *this->LogFile << cnt << "/" << tmsize
  572. << " Test: " << testname.c_str() << std::endl;
  573. *this->LogFile << "Command: ";
  574. std::vector<cmStdString>::size_type ll;
  575. for ( ll = 0; ll < arguments.size()-1; ll ++ )
  576. {
  577. *this->LogFile << "\"" << arguments[ll] << "\" ";
  578. }
  579. *this->LogFile
  580. << std::endl
  581. << "Directory: " << it->Directory << std::endl
  582. << "\"" << testname.c_str() << "\" start time: "
  583. << this->CTest->CurrentTime() << std::endl
  584. << "Output:" << std::endl
  585. << "----------------------------------------------------------"
  586. << std::endl;
  587. int res = 0;
  588. double clock_start, clock_finish;
  589. clock_start = cmSystemTools::GetTime();
  590. if ( !this->CTest->GetShowOnly() )
  591. {
  592. res = this->CTest->RunTest(arguments, &output, &retVal, this->LogFile,
  593. it->Timeout);
  594. }
  595. clock_finish = cmSystemTools::GetTime();
  596. if ( this->LogFile )
  597. {
  598. double ttime = clock_finish - clock_start;
  599. int hours = static_cast<int>(ttime / (60 * 60));
  600. int minutes = static_cast<int>(ttime / 60) % 60;
  601. int seconds = static_cast<int>(ttime) % 60;
  602. char buffer[100];
  603. sprintf(buffer, "%02d:%02d:%02d", hours, minutes, seconds);
  604. *this->LogFile
  605. << "----------------------------------------------------------"
  606. << std::endl
  607. << "\"" << testname.c_str() << "\" end time: "
  608. << this->CTest->CurrentTime() << std::endl
  609. << "\"" << testname.c_str() << "\" time elapsed: "
  610. << buffer << std::endl
  611. << "----------------------------------------------------------"
  612. << std::endl << std::endl;
  613. }
  614. cres.ExecutionTime = (double)(clock_finish - clock_start);
  615. cres.FullCommandLine = testCommand;
  616. if ( !this->CTest->GetShowOnly() )
  617. {
  618. bool testFailed = false;
  619. std::vector<cmsys::RegularExpression>::iterator passIt;
  620. bool forceFail = false;
  621. if ( it->RequiredRegularExpressions.size() > 0 )
  622. {
  623. bool found = false;
  624. for ( passIt = it->RequiredRegularExpressions.begin();
  625. passIt != it->RequiredRegularExpressions.end();
  626. ++ passIt )
  627. {
  628. if ( passIt->find(output.c_str()) )
  629. {
  630. found = true;
  631. }
  632. }
  633. if ( !found )
  634. {
  635. forceFail = true;
  636. }
  637. }
  638. if ( it->ErrorRegularExpressions.size() > 0 )
  639. {
  640. for ( passIt = it->ErrorRegularExpressions.begin();
  641. passIt != it->ErrorRegularExpressions.end();
  642. ++ passIt )
  643. {
  644. if ( passIt->find(output.c_str()) )
  645. {
  646. forceFail = true;
  647. }
  648. }
  649. }
  650. if (res == cmsysProcess_State_Exited &&
  651. (retVal == 0 || it->RequiredRegularExpressions.size()) &&
  652. !forceFail)
  653. {
  654. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Passed");
  655. if ( it->WillFail )
  656. {
  657. cmCTestLog(this->CTest, HANDLER_OUTPUT, " - But it should fail!");
  658. cres.Status = cmCTestTestHandler::FAILED;
  659. testFailed = true;
  660. }
  661. else
  662. {
  663. cres.Status = cmCTestTestHandler::COMPLETED;
  664. }
  665. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl);
  666. }
  667. else
  668. {
  669. testFailed = true;
  670. cres.Status = cmCTestTestHandler::FAILED;
  671. if ( res == cmsysProcess_State_Expired )
  672. {
  673. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Timeout" << std::endl);
  674. cres.Status = cmCTestTestHandler::TIMEOUT;
  675. }
  676. else if ( res == cmsysProcess_State_Exception )
  677. {
  678. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Exception: ");
  679. switch ( retVal )
  680. {
  681. case cmsysProcess_Exception_Fault:
  682. cmCTestLog(this->CTest, HANDLER_OUTPUT, "SegFault");
  683. cres.Status = cmCTestTestHandler::SEGFAULT;
  684. break;
  685. case cmsysProcess_Exception_Illegal:
  686. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Illegal");
  687. cres.Status = cmCTestTestHandler::ILLEGAL;
  688. break;
  689. case cmsysProcess_Exception_Interrupt:
  690. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Interrupt");
  691. cres.Status = cmCTestTestHandler::INTERRUPT;
  692. break;
  693. case cmsysProcess_Exception_Numerical:
  694. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Numerical");
  695. cres.Status = cmCTestTestHandler::NUMERICAL;
  696. break;
  697. default:
  698. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Other");
  699. cres.Status = cmCTestTestHandler::OTHER_FAULT;
  700. }
  701. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl);
  702. }
  703. else if ( res == cmsysProcess_State_Error )
  704. {
  705. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Bad command " << res
  706. << std::endl);
  707. cres.Status = cmCTestTestHandler::BAD_COMMAND;
  708. }
  709. else
  710. {
  711. // Force fail will also be here?
  712. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Failed");
  713. if ( it->WillFail )
  714. {
  715. cres.Status = cmCTestTestHandler::COMPLETED;
  716. cmCTestLog(this->CTest, HANDLER_OUTPUT, " - supposed to fail");
  717. testFailed = false;
  718. }
  719. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl);
  720. }
  721. }
  722. if ( testFailed )
  723. {
  724. failed.push_back(testname);
  725. }
  726. else
  727. {
  728. passed.push_back(testname);
  729. }
  730. if (!output.empty() && output.find("<DartMeasurement") != output.npos)
  731. {
  732. if (this->DartStuff.find(output.c_str()))
  733. {
  734. std::string dartString = this->DartStuff.match(1);
  735. cmSystemTools::ReplaceString(output, dartString.c_str(),"");
  736. cres.RegressionImages
  737. = this->GenerateRegressionImages(dartString);
  738. }
  739. }
  740. }
  741. if ( cres.Status == cmCTestTestHandler::COMPLETED )
  742. {
  743. this->CleanTestOutput(output, static_cast<size_t>
  744. (this->CustomMaximumPassedTestOutputSize));
  745. }
  746. else
  747. {
  748. this->CleanTestOutput(output, static_cast<size_t>
  749. (this->CustomMaximumFailedTestOutputSize));
  750. }
  751. cres.Output = output;
  752. cres.ReturnValue = retVal;
  753. cres.CompletionStatus = "Completed";
  754. this->TestResults.push_back( cres );
  755. }
  756. //----------------------------------------------------------------------
  757. void cmCTestTestHandler::ProcessDirectory(std::vector<cmStdString> &passed,
  758. std::vector<cmStdString> &failed)
  759. {
  760. std::string current_dir = cmSystemTools::GetCurrentWorkingDirectory();
  761. this->TestList.clear();
  762. this->GetListOfTests();
  763. cmCTestTestHandler::ListOfTests::size_type tmsize = this->TestList.size();
  764. this->StartTest = this->CTest->CurrentTime();
  765. double elapsed_time_start = cmSystemTools::GetTime();
  766. *this->LogFile << "Start testing: " << this->StartTest << std::endl
  767. << "----------------------------------------------------------"
  768. << std::endl;
  769. // how many tests are in based on RegExp?
  770. int inREcnt = 0;
  771. cmCTestTestHandler::ListOfTests::iterator it;
  772. for ( it = this->TestList.begin(); it != this->TestList.end(); it ++ )
  773. {
  774. if (it->IsInBasedOnREOptions)
  775. {
  776. inREcnt ++;
  777. }
  778. }
  779. // expand the test list based on the union flag
  780. if (this->UseUnion)
  781. {
  782. this->ExpandTestsToRunInformation((int)tmsize);
  783. }
  784. else
  785. {
  786. this->ExpandTestsToRunInformation(inREcnt);
  787. }
  788. int cnt = 0;
  789. inREcnt = 0;
  790. std::string last_directory = "";
  791. for ( it = this->TestList.begin(); it != this->TestList.end(); it ++ )
  792. {
  793. cnt ++;
  794. if (it->IsInBasedOnREOptions)
  795. {
  796. inREcnt++;
  797. }
  798. // if we are out of time then skip this test, we leave two minutes
  799. // to submit results
  800. if (this->CTest->GetRemainingTimeAllowed() - 120 <= 0)
  801. {
  802. continue;
  803. }
  804. if (!(last_directory == it->Directory))
  805. {
  806. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  807. "Changing directory into " << it->Directory.c_str() << "\n");
  808. *this->LogFile << "Changing directory into: " << it->Directory.c_str()
  809. << std::endl;
  810. last_directory = it->Directory;
  811. cmSystemTools::ChangeDirectory(it->Directory.c_str());
  812. }
  813. if (this->UseUnion)
  814. {
  815. // if it is not in the list and not in the regexp then skip
  816. if ((this->TestsToRun.size() &&
  817. std::find(this->TestsToRun.begin(), this->TestsToRun.end(), cnt)
  818. == this->TestsToRun.end()) && !it->IsInBasedOnREOptions)
  819. {
  820. continue;
  821. }
  822. }
  823. else
  824. {
  825. // is this test in the list of tests to run? If not then skip it
  826. if ((this->TestsToRun.size() &&
  827. std::find(this->TestsToRun.begin(),
  828. this->TestsToRun.end(), inREcnt)
  829. == this->TestsToRun.end()) || !it->IsInBasedOnREOptions)
  830. {
  831. continue;
  832. }
  833. }
  834. // process this one test
  835. this->ProcessOneTest(&(*it), passed, failed, cnt, tmsize);
  836. }
  837. this->EndTest = this->CTest->CurrentTime();
  838. this->ElapsedTestingTime = cmSystemTools::GetTime() - elapsed_time_start;
  839. if ( this->LogFile )
  840. {
  841. *this->LogFile << "End testing: " << this->EndTest << std::endl;
  842. }
  843. cmSystemTools::ChangeDirectory(current_dir.c_str());
  844. }
  845. //----------------------------------------------------------------------
  846. void cmCTestTestHandler::GenerateTestCommand(std::vector<const char*>&)
  847. {
  848. }
  849. //----------------------------------------------------------------------
  850. void cmCTestTestHandler::GenerateDartOutput(std::ostream& os)
  851. {
  852. if ( !this->CTest->GetProduceXML() )
  853. {
  854. return;
  855. }
  856. this->CTest->StartXML(os);
  857. os << "<Testing>\n"
  858. << "\t<StartDateTime>" << this->StartTest << "</StartDateTime>\n"
  859. << "\t<TestList>\n";
  860. cmCTestTestHandler::TestResultsVector::size_type cc;
  861. for ( cc = 0; cc < this->TestResults.size(); cc ++ )
  862. {
  863. cmCTestTestResult *result = &this->TestResults[cc];
  864. std::string testPath = result->Path + "/" + result->Name;
  865. os << "\t\t<Test>" << cmCTest::MakeXMLSafe(
  866. this->CTest->GetShortPathToFile(testPath.c_str()))
  867. << "</Test>" << std::endl;
  868. }
  869. os << "\t</TestList>\n";
  870. for ( cc = 0; cc < this->TestResults.size(); cc ++ )
  871. {
  872. cmCTestTestResult *result = &this->TestResults[cc];
  873. os << "\t<Test Status=\"";
  874. if ( result->Status == cmCTestTestHandler::COMPLETED )
  875. {
  876. os << "passed";
  877. }
  878. else if ( result->Status == cmCTestTestHandler::NOT_RUN )
  879. {
  880. os << "notrun";
  881. }
  882. else
  883. {
  884. os << "failed";
  885. }
  886. std::string testPath = result->Path + "/" + result->Name;
  887. os << "\">\n"
  888. << "\t\t<Name>" << cmCTest::MakeXMLSafe(result->Name) << "</Name>\n"
  889. << "\t\t<Path>" << cmCTest::MakeXMLSafe(
  890. this->CTest->GetShortPathToFile(result->Path.c_str())) << "</Path>\n"
  891. << "\t\t<FullName>" << cmCTest::MakeXMLSafe(
  892. this->CTest->GetShortPathToFile(testPath.c_str())) << "</FullName>\n"
  893. << "\t\t<FullCommandLine>"
  894. << cmCTest::MakeXMLSafe(result->FullCommandLine)
  895. << "</FullCommandLine>\n"
  896. << "\t\t<Results>" << std::endl;
  897. if ( result->Status != cmCTestTestHandler::NOT_RUN )
  898. {
  899. if ( result->Status != cmCTestTestHandler::COMPLETED ||
  900. result->ReturnValue )
  901. {
  902. os << "\t\t\t<NamedMeasurement type=\"text/string\" "
  903. "name=\"Exit Code\"><Value>"
  904. << this->GetTestStatus(result->Status) << "</Value>"
  905. "</NamedMeasurement>\n"
  906. << "\t\t\t<NamedMeasurement type=\"text/string\" "
  907. "name=\"Exit Value\"><Value>"
  908. << result->ReturnValue << "</Value></NamedMeasurement>"
  909. << std::endl;
  910. }
  911. os << result->RegressionImages;
  912. os << "\t\t\t<NamedMeasurement type=\"numeric/double\" "
  913. << "name=\"Execution Time\"><Value>"
  914. << result->ExecutionTime << "</Value></NamedMeasurement>\n";
  915. os
  916. << "\t\t\t<NamedMeasurement type=\"text/string\" "
  917. << "name=\"Completion Status\"><Value>"
  918. << result->CompletionStatus << "</Value></NamedMeasurement>\n";
  919. }
  920. os
  921. << "\t\t\t<NamedMeasurement type=\"text/string\" "
  922. << "name=\"Command Line\"><Value>"
  923. << result->FullCommandLine << "</Value></NamedMeasurement>\n";
  924. std::map<cmStdString,cmStdString>::iterator measureIt;
  925. for ( measureIt = result->Properties->Measurements.begin();
  926. measureIt != result->Properties->Measurements.end();
  927. ++ measureIt )
  928. {
  929. os
  930. << "\t\t\t<NamedMeasurement type=\"text/string\" "
  931. << "name=\"" << measureIt->first.c_str() << "\"><Value>"
  932. << measureIt->second.c_str() << "</Value></NamedMeasurement>\n";
  933. }
  934. os
  935. << "\t\t\t<Measurement>\n"
  936. << "\t\t\t\t<Value>";
  937. os << cmCTest::MakeXMLSafe(result->Output);
  938. os
  939. << "</Value>\n"
  940. << "\t\t\t</Measurement>\n"
  941. << "\t\t</Results>\n"
  942. << "\t</Test>" << std::endl;
  943. }
  944. os << "\t<EndDateTime>" << this->EndTest << "</EndDateTime>\n"
  945. << "<ElapsedMinutes>"
  946. << static_cast<int>(this->ElapsedTestingTime/6)/10.0
  947. << "</ElapsedMinutes>"
  948. << "</Testing>" << std::endl;
  949. this->CTest->EndXML(os);
  950. }
  951. //----------------------------------------------------------------------
  952. int cmCTestTestHandler::ExecuteCommands(std::vector<cmStdString>& vec)
  953. {
  954. std::vector<cmStdString>::iterator it;
  955. for ( it = vec.begin(); it != vec.end(); ++it )
  956. {
  957. int retVal = 0;
  958. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run command: " << *it
  959. << std::endl);
  960. if ( !cmSystemTools::RunSingleCommand(it->c_str(), 0, &retVal, 0, true
  961. /*this->Verbose*/) || retVal != 0 )
  962. {
  963. cmCTestLog(this->CTest, ERROR_MESSAGE, "Problem running command: "
  964. << *it << std::endl);
  965. return 0;
  966. }
  967. }
  968. return 1;
  969. }
  970. //----------------------------------------------------------------------
  971. // Find the appropriate executable to run for a test
  972. std::string cmCTestTestHandler::FindTheExecutable(const char *exe)
  973. {
  974. std::string fullPath = "";
  975. std::string dir;
  976. std::string file;
  977. cmSystemTools::SplitProgramPath(exe, dir, file);
  978. // first try to find the executable given a config type subdir if there is
  979. // one
  980. if(this->CTest->GetConfigType() != "" &&
  981. ::TryExecutable(dir.c_str(), file.c_str(), &fullPath,
  982. this->CTest->GetConfigType().c_str()))
  983. {
  984. return fullPath;
  985. }
  986. // next try the current directory as the subdir
  987. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"."))
  988. {
  989. return fullPath;
  990. }
  991. // try without the config subdir
  992. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,""))
  993. {
  994. return fullPath;
  995. }
  996. if ( this->CTest->GetConfigType() == "" )
  997. {
  998. // No config type, so try to guess it
  999. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Deployment"))
  1000. {
  1001. return fullPath;
  1002. }
  1003. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Development"))
  1004. {
  1005. return fullPath;
  1006. }
  1007. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Release"))
  1008. {
  1009. return fullPath;
  1010. }
  1011. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Debug"))
  1012. {
  1013. return fullPath;
  1014. }
  1015. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"MinSizeRel"))
  1016. {
  1017. return fullPath;
  1018. }
  1019. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"RelWithDebInfo"))
  1020. {
  1021. return fullPath;
  1022. }
  1023. }
  1024. // if everything else failed, check the users path, but only if a full path
  1025. // wasn;t specified
  1026. if (dir.size() == 0)
  1027. {
  1028. std::string path = cmSystemTools::FindProgram(file.c_str());
  1029. if (path != "")
  1030. {
  1031. return path;
  1032. }
  1033. }
  1034. if ( this->CTest->GetConfigType() != "" )
  1035. {
  1036. dir += "/";
  1037. dir += this->CTest->GetConfigType();
  1038. dir += "/";
  1039. dir += file;
  1040. cmSystemTools::Error("config type specified on the command line, but "
  1041. "test executable not found.",
  1042. dir.c_str());
  1043. return "";
  1044. }
  1045. return fullPath;
  1046. }
  1047. //----------------------------------------------------------------------
  1048. void cmCTestTestHandler::GetListOfTests()
  1049. {
  1050. if ( !this->IncludeRegExp.empty() )
  1051. {
  1052. this->IncludeTestsRegularExpression.compile(this->IncludeRegExp.c_str());
  1053. }
  1054. if ( !this->ExcludeRegExp.empty() )
  1055. {
  1056. this->ExcludeTestsRegularExpression.compile(this->ExcludeRegExp.c_str());
  1057. }
  1058. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1059. "Constructing a list of tests" << std::endl);
  1060. cmake cm;
  1061. cmGlobalGenerator gg;
  1062. gg.SetCMakeInstance(&cm);
  1063. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  1064. lg->SetGlobalGenerator(&gg);
  1065. cmMakefile *mf = lg->GetMakefile();
  1066. mf->AddDefinition("CTEST_CONFIGURATION_TYPE",
  1067. this->CTest->GetConfigType().c_str());
  1068. // Add handler for ADD_TEST
  1069. cmCTestAddTestCommand* newCom1 = new cmCTestAddTestCommand;
  1070. newCom1->TestHandler = this;
  1071. cm.AddCommand(newCom1);
  1072. // Add handler for SUBDIR
  1073. cmCTestSubdirCommand* newCom2 = new cmCTestSubdirCommand;
  1074. newCom2->TestHandler = this;
  1075. cm.AddCommand(newCom2);
  1076. // Add handler for SET_SOURCE_FILES_PROPERTIES
  1077. cmCTestSetTestsPropertiesCommand* newCom3
  1078. = new cmCTestSetTestsPropertiesCommand;
  1079. newCom3->TestHandler = this;
  1080. cm.AddCommand(newCom3);
  1081. const char* testFilename;
  1082. if( cmSystemTools::FileExists("CTestTestfile.cmake") )
  1083. {
  1084. // does the CTestTestfile.cmake exist ?
  1085. testFilename = "CTestTestfile.cmake";
  1086. }
  1087. else if( cmSystemTools::FileExists("DartTestfile.txt") )
  1088. {
  1089. // does the DartTestfile.txt exist ?
  1090. testFilename = "DartTestfile.txt";
  1091. }
  1092. else
  1093. {
  1094. return;
  1095. }
  1096. if ( !mf->ReadListFile(0, testFilename) )
  1097. {
  1098. return;
  1099. }
  1100. if ( cmSystemTools::GetErrorOccuredFlag() )
  1101. {
  1102. return;
  1103. }
  1104. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1105. "Done constructing a list of tests" << std::endl);
  1106. }
  1107. //----------------------------------------------------------------------
  1108. void cmCTestTestHandler::UseIncludeRegExp()
  1109. {
  1110. this->UseIncludeRegExpFlag = true;
  1111. }
  1112. //----------------------------------------------------------------------
  1113. void cmCTestTestHandler::UseExcludeRegExp()
  1114. {
  1115. this->UseExcludeRegExpFlag = true;
  1116. this->UseExcludeRegExpFirst = this->UseIncludeRegExpFlag ? false : true;
  1117. }
  1118. //----------------------------------------------------------------------
  1119. const char* cmCTestTestHandler::GetTestStatus(int status)
  1120. {
  1121. static const char statuses[][100] = {
  1122. "Not Run",
  1123. "Timeout",
  1124. "SEGFAULT",
  1125. "ILLEGAL",
  1126. "INTERRUPT",
  1127. "NUMERICAL",
  1128. "OTHER_FAULT",
  1129. "Failed",
  1130. "BAD_COMMAND",
  1131. "Completed"
  1132. };
  1133. if ( status < cmCTestTestHandler::NOT_RUN ||
  1134. status > cmCTestTestHandler::COMPLETED )
  1135. {
  1136. return "No Status";
  1137. }
  1138. return statuses[status];
  1139. }
  1140. //----------------------------------------------------------------------
  1141. void cmCTestTestHandler::ExpandTestsToRunInformation(int numTests)
  1142. {
  1143. if (this->TestsToRunString.empty())
  1144. {
  1145. return;
  1146. }
  1147. int start;
  1148. int end = -1;
  1149. double stride = -1;
  1150. std::string::size_type pos = 0;
  1151. std::string::size_type pos2;
  1152. // read start
  1153. if(GetNextNumber(this->TestsToRunString, start, pos, pos2))
  1154. {
  1155. // read end
  1156. if(GetNextNumber(this->TestsToRunString, end, pos, pos2))
  1157. {
  1158. // read stride
  1159. if(GetNextRealNumber(this->TestsToRunString, stride, pos, pos2))
  1160. {
  1161. int val =0;
  1162. // now read specific numbers
  1163. while(GetNextNumber(this->TestsToRunString, val, pos, pos2))
  1164. {
  1165. this->TestsToRun.push_back(val);
  1166. }
  1167. this->TestsToRun.push_back(val);
  1168. }
  1169. }
  1170. }
  1171. // if start is not specified then we assume we start at 1
  1172. if(start == -1)
  1173. {
  1174. start = 1;
  1175. }
  1176. // if end isnot specified then we assume we end with the last test
  1177. if(end == -1)
  1178. {
  1179. end = numTests;
  1180. }
  1181. // if the stride wasn't specified then it defaults to 1
  1182. if(stride == -1)
  1183. {
  1184. stride = 1;
  1185. }
  1186. // if we have a range then add it
  1187. if(end != -1 && start != -1 && stride > 0)
  1188. {
  1189. int i = 0;
  1190. while (i*stride + start <= end)
  1191. {
  1192. this->TestsToRun.push_back(static_cast<int>(i*stride+start));
  1193. ++i;
  1194. }
  1195. }
  1196. // sort the array
  1197. std::sort(this->TestsToRun.begin(), this->TestsToRun.end(),
  1198. std::less<int>());
  1199. // remove duplicates
  1200. std::vector<int>::iterator new_end =
  1201. std::unique(this->TestsToRun.begin(), this->TestsToRun.end());
  1202. this->TestsToRun.erase(new_end, this->TestsToRun.end());
  1203. }
  1204. //----------------------------------------------------------------------
  1205. // Just for convenience
  1206. #define SPACE_REGEX "[ \t\r\n]"
  1207. //----------------------------------------------------------------------
  1208. std::string cmCTestTestHandler::GenerateRegressionImages(
  1209. const std::string& xml)
  1210. {
  1211. cmsys::RegularExpression twoattributes(
  1212. "<DartMeasurement"
  1213. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1214. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1215. SPACE_REGEX "*>([^<]*)</DartMeasurement>");
  1216. cmsys::RegularExpression threeattributes(
  1217. "<DartMeasurement"
  1218. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1219. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1220. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1221. SPACE_REGEX "*>([^<]*)</DartMeasurement>");
  1222. cmsys::RegularExpression fourattributes(
  1223. "<DartMeasurement"
  1224. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1225. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1226. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1227. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1228. SPACE_REGEX "*>([^<]*)</DartMeasurement>");
  1229. cmsys::RegularExpression measurementfile(
  1230. "<DartMeasurementFile"
  1231. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1232. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1233. SPACE_REGEX "*>([^<]*)</DartMeasurementFile>");
  1234. cmOStringStream ostr;
  1235. bool done = false;
  1236. std::string cxml = xml;
  1237. while ( ! done )
  1238. {
  1239. if ( twoattributes.find(cxml) )
  1240. {
  1241. ostr
  1242. << "\t\t\t<NamedMeasurement"
  1243. << " " << twoattributes.match(1) << "=\""
  1244. << twoattributes.match(2) << "\""
  1245. << " " << twoattributes.match(3) << "=\""
  1246. << twoattributes.match(4) << "\""
  1247. << "><Value>" << twoattributes.match(5)
  1248. << "</Value></NamedMeasurement>"
  1249. << std::endl;
  1250. cxml.erase(twoattributes.start(),
  1251. twoattributes.end() - twoattributes.start());
  1252. }
  1253. else if ( threeattributes.find(cxml) )
  1254. {
  1255. ostr
  1256. << "\t\t\t<NamedMeasurement"
  1257. << " " << threeattributes.match(1) << "=\""
  1258. << threeattributes.match(2) << "\""
  1259. << " " << threeattributes.match(3) << "=\""
  1260. << threeattributes.match(4) << "\""
  1261. << " " << threeattributes.match(5) << "=\""
  1262. << threeattributes.match(6) << "\""
  1263. << "><Value>" << threeattributes.match(7)
  1264. << "</Value></NamedMeasurement>"
  1265. << std::endl;
  1266. cxml.erase(threeattributes.start(),
  1267. threeattributes.end() - threeattributes.start());
  1268. }
  1269. else if ( fourattributes.find(cxml) )
  1270. {
  1271. ostr
  1272. << "\t\t\t<NamedMeasurement"
  1273. << " " << fourattributes.match(1) << "=\""
  1274. << fourattributes.match(2) << "\""
  1275. << " " << fourattributes.match(3) << "=\""
  1276. << fourattributes.match(4) << "\""
  1277. << " " << fourattributes.match(5) << "=\""
  1278. << fourattributes.match(6) << "\""
  1279. << " " << fourattributes.match(7) << "=\""
  1280. << fourattributes.match(8) << "\""
  1281. << "><Value>" << fourattributes.match(9)
  1282. << "</Value></NamedMeasurement>"
  1283. << std::endl;
  1284. cxml.erase(fourattributes.start(),
  1285. fourattributes.end() - fourattributes.start());
  1286. }
  1287. else if ( measurementfile.find(cxml) )
  1288. {
  1289. const std::string& filename =
  1290. cmCTest::CleanString(measurementfile.match(5));
  1291. if ( cmSystemTools::FileExists(filename.c_str()) )
  1292. {
  1293. long len = cmSystemTools::FileLength(filename.c_str());
  1294. if ( len == 0 )
  1295. {
  1296. std::string k1 = measurementfile.match(1);
  1297. std::string v1 = measurementfile.match(2);
  1298. std::string k2 = measurementfile.match(3);
  1299. std::string v2 = measurementfile.match(4);
  1300. if ( cmSystemTools::LowerCase(k1) == "type" )
  1301. {
  1302. v1 = "text/string";
  1303. }
  1304. if ( cmSystemTools::LowerCase(k2) == "type" )
  1305. {
  1306. v2 = "text/string";
  1307. }
  1308. ostr
  1309. << "\t\t\t<NamedMeasurement"
  1310. << " " << k1 << "=\"" << v1 << "\""
  1311. << " " << k2 << "=\"" << v2 << "\""
  1312. << " encoding=\"none\""
  1313. << "><Value>Image " << filename.c_str()
  1314. << " is empty</Value></NamedMeasurement>";
  1315. }
  1316. else
  1317. {
  1318. std::ifstream ifs(filename.c_str(), std::ios::in
  1319. #ifdef _WIN32
  1320. | std::ios::binary
  1321. #endif
  1322. );
  1323. unsigned char *file_buffer = new unsigned char [ len + 1 ];
  1324. ifs.read(reinterpret_cast<char*>(file_buffer), len);
  1325. unsigned char *encoded_buffer
  1326. = new unsigned char [ static_cast<int>(len * 1.5 + 5) ];
  1327. unsigned long rlen
  1328. = cmsysBase64_Encode(file_buffer, len, encoded_buffer, 1);
  1329. unsigned long cc;
  1330. ostr
  1331. << "\t\t\t<NamedMeasurement"
  1332. << " " << measurementfile.match(1) << "=\""
  1333. << measurementfile.match(2) << "\""
  1334. << " " << measurementfile.match(3) << "=\""
  1335. << measurementfile.match(4) << "\""
  1336. << " encoding=\"base64\""
  1337. << ">" << std::endl << "\t\t\t\t<Value>";
  1338. for ( cc = 0; cc < rlen; cc ++ )
  1339. {
  1340. ostr << encoded_buffer[cc];
  1341. if ( cc % 60 == 0 && cc )
  1342. {
  1343. ostr << std::endl;
  1344. }
  1345. }
  1346. ostr
  1347. << "</Value>" << std::endl << "\t\t\t</NamedMeasurement>"
  1348. << std::endl;
  1349. delete [] file_buffer;
  1350. delete [] encoded_buffer;
  1351. }
  1352. }
  1353. else
  1354. {
  1355. int idx = 4;
  1356. if ( measurementfile.match(1) == "name" )
  1357. {
  1358. idx = 2;
  1359. }
  1360. ostr
  1361. << "\t\t\t<NamedMeasurement"
  1362. << " name=\"" << measurementfile.match(idx) << "\""
  1363. << " text=\"text/string\""
  1364. << "><Value>File " << filename.c_str()
  1365. << " not found</Value></NamedMeasurement>"
  1366. << std::endl;
  1367. cmCTestLog(this->CTest, HANDLER_OUTPUT, "File \"" << filename.c_str()
  1368. << "\" not found." << std::endl);
  1369. }
  1370. cxml.erase(measurementfile.start(),
  1371. measurementfile.end() - measurementfile.start());
  1372. }
  1373. else
  1374. {
  1375. done = true;
  1376. }
  1377. }
  1378. return ostr.str();
  1379. }
  1380. //----------------------------------------------------------------------
  1381. void cmCTestTestHandler::SetIncludeRegExp(const char *arg)
  1382. {
  1383. this->IncludeRegExp = arg;
  1384. }
  1385. //----------------------------------------------------------------------
  1386. void cmCTestTestHandler::SetExcludeRegExp(const char *arg)
  1387. {
  1388. this->ExcludeRegExp = arg;
  1389. }
  1390. //----------------------------------------------------------------------
  1391. void cmCTestTestHandler::SetTestsToRunInformation(const char* in)
  1392. {
  1393. if ( !in )
  1394. {
  1395. return;
  1396. }
  1397. this->TestsToRunString = in;
  1398. // if the argument is a file, then read it and use the contents as the
  1399. // string
  1400. if(cmSystemTools::FileExists(in))
  1401. {
  1402. std::ifstream fin(in);
  1403. unsigned long filelen = cmSystemTools::FileLength(in);
  1404. char* buff = new char[filelen+1];
  1405. fin.getline(buff, filelen);
  1406. buff[fin.gcount()] = 0;
  1407. this->TestsToRunString = buff;
  1408. }
  1409. }
  1410. //----------------------------------------------------------------------
  1411. bool cmCTestTestHandler::CleanTestOutput(std::string& output,
  1412. size_t remove_threshold)
  1413. {
  1414. if ( remove_threshold == 0 )
  1415. {
  1416. return true;
  1417. }
  1418. if ( output.find("CTEST_FULL_OUTPUT") != output.npos )
  1419. {
  1420. return true;
  1421. }
  1422. cmOStringStream ostr;
  1423. std::string::size_type cc;
  1424. std::string::size_type skipsize = 0;
  1425. int inTag = 0;
  1426. int skipped = 0;
  1427. for ( cc = 0; cc < output.size(); cc ++ )
  1428. {
  1429. int ch = output[cc];
  1430. if ( ch < 0 || ch > 255 )
  1431. {
  1432. break;
  1433. }
  1434. if ( ch == '<' )
  1435. {
  1436. inTag = 1;
  1437. }
  1438. if ( !inTag )
  1439. {
  1440. int notskip = 0;
  1441. // Skip
  1442. if ( skipsize < remove_threshold )
  1443. {
  1444. ostr << static_cast<char>(ch);
  1445. notskip = 1;
  1446. }
  1447. skipsize ++;
  1448. if ( notskip && skipsize >= remove_threshold )
  1449. {
  1450. skipped = 1;
  1451. }
  1452. }
  1453. else
  1454. {
  1455. ostr << static_cast<char>(ch);
  1456. }
  1457. if ( ch == '>' )
  1458. {
  1459. inTag = 0;
  1460. }
  1461. }
  1462. if ( skipped )
  1463. {
  1464. ostr << "..." << std::endl << "The rest of the test output was removed "
  1465. "since it exceeds the threshold of "
  1466. << remove_threshold << " characters." << std::endl;
  1467. }
  1468. output = ostr.str();
  1469. return true;
  1470. }
  1471. //----------------------------------------------------------------------
  1472. bool cmCTestTestHandler::SetTestsProperties(
  1473. const std::vector<std::string>& args)
  1474. {
  1475. std::vector<std::string>::const_iterator it;
  1476. std::vector<cmStdString> tests;
  1477. bool found = false;
  1478. for ( it = args.begin(); it != args.end(); ++ it )
  1479. {
  1480. if ( *it == "PROPERTIES" )
  1481. {
  1482. found = true;
  1483. break;
  1484. }
  1485. tests.push_back(*it);
  1486. }
  1487. if ( !found )
  1488. {
  1489. return false;
  1490. }
  1491. ++ it; // skip PROPERTIES
  1492. for ( ; it != args.end(); ++ it )
  1493. {
  1494. std::string key = *it;
  1495. ++ it;
  1496. if ( it == args.end() )
  1497. {
  1498. break;
  1499. }
  1500. std::string val = *it;
  1501. std::vector<cmStdString>::const_iterator tit;
  1502. for ( tit = tests.begin(); tit != tests.end(); ++ tit )
  1503. {
  1504. cmCTestTestHandler::ListOfTests::iterator rtit;
  1505. for ( rtit = this->TestList.begin();
  1506. rtit != this->TestList.end();
  1507. ++ rtit )
  1508. {
  1509. if ( *tit == rtit->Name )
  1510. {
  1511. if ( key == "WILL_FAIL" )
  1512. {
  1513. rtit->WillFail = cmSystemTools::IsOn(val.c_str());
  1514. }
  1515. if ( key == "TIMEOUT" )
  1516. {
  1517. rtit->Timeout = atof(val.c_str());
  1518. }
  1519. if ( key == "FAIL_REGULAR_EXPRESSION" )
  1520. {
  1521. std::vector<std::string> lval;
  1522. cmSystemTools::ExpandListArgument(val.c_str(), lval);
  1523. std::vector<std::string>::iterator crit;
  1524. for ( crit = lval.begin(); crit != lval.end(); ++ crit )
  1525. {
  1526. rtit->ErrorRegularExpressions.push_back(
  1527. cmsys::RegularExpression(crit->c_str()));
  1528. }
  1529. }
  1530. if ( key == "MEASUREMENT" )
  1531. {
  1532. size_t pos = val.find_first_of("=");
  1533. if ( pos != val.npos )
  1534. {
  1535. std::string mKey = val.substr(0, pos);
  1536. const char* mVal = val.c_str() + pos + 1;
  1537. rtit->Measurements[mKey] = mVal;
  1538. }
  1539. else
  1540. {
  1541. rtit->Measurements[val] = "1";
  1542. }
  1543. }
  1544. if ( key == "PASS_REGULAR_EXPRESSION" )
  1545. {
  1546. std::vector<std::string> lval;
  1547. cmSystemTools::ExpandListArgument(val.c_str(), lval);
  1548. std::vector<std::string>::iterator crit;
  1549. for ( crit = lval.begin(); crit != lval.end(); ++ crit )
  1550. {
  1551. rtit->RequiredRegularExpressions.push_back(
  1552. cmsys::RegularExpression(crit->c_str()));
  1553. }
  1554. }
  1555. }
  1556. }
  1557. }
  1558. }
  1559. return true;
  1560. }
  1561. //----------------------------------------------------------------------
  1562. bool cmCTestTestHandler::AddTest(const std::vector<std::string>& args)
  1563. {
  1564. const std::string& testname = args[0];
  1565. cmCTestLog(this->CTest, DEBUG, "Add test: " << args[0] << std::endl);
  1566. if (this->UseExcludeRegExpFlag &&
  1567. this->UseExcludeRegExpFirst &&
  1568. this->ExcludeTestsRegularExpression.find(testname.c_str()))
  1569. {
  1570. return true;
  1571. }
  1572. if ( this->MemCheck )
  1573. {
  1574. std::vector<cmStdString>::iterator it;
  1575. bool found = false;
  1576. for ( it = this->CustomTestsIgnore.begin();
  1577. it != this->CustomTestsIgnore.end(); ++ it )
  1578. {
  1579. if ( *it == testname )
  1580. {
  1581. found = true;
  1582. break;
  1583. }
  1584. }
  1585. if ( found )
  1586. {
  1587. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Ignore memcheck: "
  1588. << *it << std::endl);
  1589. return true;
  1590. }
  1591. }
  1592. else
  1593. {
  1594. std::vector<cmStdString>::iterator it;
  1595. bool found = false;
  1596. for ( it = this->CustomTestsIgnore.begin();
  1597. it != this->CustomTestsIgnore.end(); ++ it )
  1598. {
  1599. if ( *it == testname )
  1600. {
  1601. found = true;
  1602. break;
  1603. }
  1604. }
  1605. if ( found )
  1606. {
  1607. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Ignore test: "
  1608. << *it << std::endl);
  1609. return true;
  1610. }
  1611. }
  1612. cmCTestTestProperties test;
  1613. test.Name = testname;
  1614. test.Args = args;
  1615. test.Directory = cmSystemTools::GetCurrentWorkingDirectory();
  1616. cmCTestLog(this->CTest, DEBUG, "Set test directory: "
  1617. << test.Directory << std::endl);
  1618. test.IsInBasedOnREOptions = true;
  1619. test.WillFail = false;
  1620. test.Timeout = 0;
  1621. if (this->UseIncludeRegExpFlag &&
  1622. !this->IncludeTestsRegularExpression.find(testname.c_str()))
  1623. {
  1624. test.IsInBasedOnREOptions = false;
  1625. }
  1626. else if (this->UseExcludeRegExpFlag &&
  1627. !this->UseExcludeRegExpFirst &&
  1628. this->ExcludeTestsRegularExpression.find(testname.c_str()))
  1629. {
  1630. test.IsInBasedOnREOptions = false;
  1631. }
  1632. this->TestList.push_back(test);
  1633. return true;
  1634. }