cmCTestTestHandler.cxx 46 KB

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