cmCTestTestHandler.cxx 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  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" << std::endl);
  396. if ( ! this->PreProcessHandler() )
  397. {
  398. return -1;
  399. }
  400. cmGeneratedFileStream mLogFile;
  401. this->StartLogFile((this->MemCheck ? "DynamicAnalysis" : "Test"), mLogFile);
  402. this->LogFile = &mLogFile;
  403. std::vector<cmStdString> passed;
  404. std::vector<cmStdString> failed;
  405. int total;
  406. this->ProcessDirectory(passed, failed);
  407. total = int(passed.size()) + int(failed.size());
  408. if (total == 0)
  409. {
  410. if ( !this->CTest->GetShowOnly() )
  411. {
  412. cmCTestLog(this->CTest, ERROR_MESSAGE, "No tests were found!!!"
  413. << std::endl);
  414. }
  415. }
  416. else
  417. {
  418. if (this->HandlerVerbose && passed.size() &&
  419. (this->UseIncludeRegExpFlag || this->UseExcludeRegExpFlag))
  420. {
  421. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl
  422. << "The following tests passed:" << std::endl);
  423. for(std::vector<cmStdString>::iterator j = passed.begin();
  424. j != passed.end(); ++j)
  425. {
  426. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "\t" << *j
  427. << std::endl);
  428. }
  429. }
  430. float percent = float(passed.size()) * 100.0f / total;
  431. if ( failed.size() > 0 && percent > 99)
  432. {
  433. percent = 99;
  434. }
  435. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl
  436. << static_cast<int>(percent + .5) << "% tests passed, "
  437. << failed.size() << " tests failed out of " << total << std::endl);
  438. //fprintf(stderr,"\n%.0f%% tests passed, %i tests failed out of %i\n",
  439. // percent, int(failed.size()), total);
  440. if (failed.size())
  441. {
  442. cmGeneratedFileStream ofs;
  443. cmCTestLog(this->CTest, ERROR_MESSAGE, std::endl
  444. << "The following tests FAILED:" << std::endl);
  445. this->StartLogFile("TestsFailed", ofs);
  446. std::vector<cmCTestTestHandler::cmCTestTestResult>::iterator ftit;
  447. for(ftit = this->TestResults.begin();
  448. ftit != this->TestResults.end(); ++ftit)
  449. {
  450. if ( ftit->Status != cmCTestTestHandler::COMPLETED )
  451. {
  452. ofs << ftit->TestCount << ":" << ftit->Name << std::endl;
  453. cmCTestLog(this->CTest, HANDLER_OUTPUT, "\t" << std::setw(3)
  454. << ftit->TestCount << " - " << ftit->Name.c_str() << " ("
  455. << this->GetTestStatus(ftit->Status) << ")" << std::endl);
  456. }
  457. }
  458. }
  459. }
  460. if ( this->CTest->GetProduceXML() )
  461. {
  462. cmGeneratedFileStream xmlfile;
  463. if( !this->StartResultingXML(
  464. (this->MemCheck ? "DynamicAnalysis" : "Test"), xmlfile) )
  465. {
  466. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot create "
  467. << (this->MemCheck ? "memory check" : "testing")
  468. << " XML file" << std::endl);
  469. this->LogFile = 0;
  470. return 1;
  471. }
  472. this->GenerateDartOutput(xmlfile);
  473. }
  474. if ( ! this->PostProcessHandler() )
  475. {
  476. this->LogFile = 0;
  477. return -1;
  478. }
  479. if ( !failed.empty() )
  480. {
  481. this->LogFile = 0;
  482. return -1;
  483. }
  484. this->LogFile = 0;
  485. return 0;
  486. }
  487. //----------------------------------------------------------------------
  488. void cmCTestTestHandler::ProcessDirectory(std::vector<cmStdString> &passed,
  489. std::vector<cmStdString> &failed)
  490. {
  491. std::string current_dir = cmSystemTools::GetCurrentWorkingDirectory();
  492. this->TestList.clear();
  493. this->GetListOfTests();
  494. cmCTestTestHandler::ListOfTests::size_type tmsize = this->TestList.size();
  495. this->StartTest = this->CTest->CurrentTime();
  496. double elapsed_time_start = cmSystemTools::GetTime();
  497. *this->LogFile << "Start testing: " << this->StartTest << std::endl
  498. << "----------------------------------------------------------"
  499. << std::endl;
  500. // how many tests are in based on RegExp?
  501. int inREcnt = 0;
  502. cmCTestTestHandler::ListOfTests::iterator it;
  503. for ( it = this->TestList.begin(); it != this->TestList.end(); it ++ )
  504. {
  505. if (it->IsInBasedOnREOptions)
  506. {
  507. inREcnt ++;
  508. }
  509. }
  510. // expand the test list based on the union flag
  511. if (this->UseUnion)
  512. {
  513. this->ExpandTestsToRunInformation((int)tmsize);
  514. }
  515. else
  516. {
  517. this->ExpandTestsToRunInformation(inREcnt);
  518. }
  519. int cnt = 0;
  520. inREcnt = 0;
  521. std::string last_directory = "";
  522. for ( it = this->TestList.begin(); it != this->TestList.end(); it ++ )
  523. {
  524. cnt ++;
  525. if (it->IsInBasedOnREOptions)
  526. {
  527. inREcnt++;
  528. }
  529. const std::string& testname = it->Name;
  530. std::vector<std::string>& args = it->Args;
  531. cmCTestTestResult cres;
  532. cres.ExecutionTime = 0;
  533. cres.ReturnValue = -1;
  534. cres.Status = cmCTestTestHandler::NOT_RUN;
  535. cres.TestCount = cnt;
  536. if (!(last_directory == it->Directory))
  537. {
  538. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  539. "Changing directory into " << it->Directory.c_str() << "\n");
  540. *this->LogFile << "Changing directory into: " << it->Directory.c_str()
  541. << std::endl;
  542. last_directory = it->Directory;
  543. cmSystemTools::ChangeDirectory(it->Directory.c_str());
  544. }
  545. cres.Name = testname;
  546. cres.Path = it->Directory.c_str();
  547. if (this->UseUnion)
  548. {
  549. // if it is not in the list and not in the regexp then skip
  550. if ((this->TestsToRun.size() &&
  551. std::find(this->TestsToRun.begin(), this->TestsToRun.end(), cnt)
  552. == this->TestsToRun.end()) && !it->IsInBasedOnREOptions)
  553. {
  554. continue;
  555. }
  556. }
  557. else
  558. {
  559. // is this test in the list of tests to run? If not then skip it
  560. if ((this->TestsToRun.size() &&
  561. std::find(this->TestsToRun.begin(),
  562. this->TestsToRun.end(), inREcnt)
  563. == this->TestsToRun.end()) || !it->IsInBasedOnREOptions)
  564. {
  565. continue;
  566. }
  567. }
  568. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(3) << cnt << "/");
  569. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(3) << tmsize << " ");
  570. if ( this->MemCheck )
  571. {
  572. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Memory Check");
  573. }
  574. else
  575. {
  576. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Testing");
  577. }
  578. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  579. std::string outname = testname;
  580. outname.resize(30, ' ');
  581. *this->LogFile << cnt << "/" << tmsize << " Testing: " << testname
  582. << std::endl;
  583. if ( this->CTest->GetShowOnly() )
  584. {
  585. cmCTestLog(this->CTest, HANDLER_OUTPUT, outname.c_str() << std::endl);
  586. }
  587. else
  588. {
  589. cmCTestLog(this->CTest, HANDLER_OUTPUT, outname.c_str());
  590. }
  591. cmCTestLog(this->CTest, DEBUG, "Testing " << args[0].c_str() << " ... ");
  592. // find the test executable
  593. std::string actualCommand = this->FindTheExecutable(args[1].c_str());
  594. std::string testCommand
  595. = cmSystemTools::ConvertToOutputPath(actualCommand.c_str());
  596. // continue if we did not find the executable
  597. if (testCommand == "")
  598. {
  599. *this->LogFile << "Unable to find executable: " << args[1].c_str()
  600. << std::endl;
  601. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unable to find executable: "
  602. << args[1].c_str() << std::endl);
  603. if ( !this->CTest->GetShowOnly() )
  604. {
  605. cres.FullCommandLine = actualCommand;
  606. this->TestResults.push_back( cres );
  607. failed.push_back(testname);
  608. continue;
  609. }
  610. }
  611. // add the arguments
  612. std::vector<std::string>::const_iterator j = args.begin();
  613. ++j;
  614. ++j;
  615. std::vector<const char*> arguments;
  616. this->GenerateTestCommand(arguments);
  617. arguments.push_back(actualCommand.c_str());
  618. for(;j != args.end(); ++j)
  619. {
  620. testCommand += " ";
  621. testCommand += cmSystemTools::EscapeSpaces(j->c_str());
  622. arguments.push_back(j->c_str());
  623. }
  624. arguments.push_back(0);
  625. /**
  626. * Run an executable command and put the stdout in output.
  627. */
  628. std::string output;
  629. int retVal = 0;
  630. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl
  631. << (this->MemCheck?"MemCheck":"Test") << " command: " << testCommand
  632. << std::endl);
  633. *this->LogFile << cnt << "/" << tmsize
  634. << " Test: " << testname.c_str() << std::endl;
  635. *this->LogFile << "Command: ";
  636. std::vector<cmStdString>::size_type ll;
  637. for ( ll = 0; ll < arguments.size()-1; ll ++ )
  638. {
  639. *this->LogFile << "\"" << arguments[ll] << "\" ";
  640. }
  641. *this->LogFile
  642. << std::endl
  643. << "Directory: " << it->Directory << std::endl
  644. << "\"" << testname.c_str() << "\" start time: "
  645. << this->CTest->CurrentTime() << std::endl
  646. << "Output:" << std::endl
  647. << "----------------------------------------------------------"
  648. << std::endl;
  649. int res = 0;
  650. double clock_start, clock_finish;
  651. clock_start = cmSystemTools::GetTime();
  652. if ( !this->CTest->GetShowOnly() )
  653. {
  654. res = this->CTest->RunTest(arguments, &output, &retVal, this->LogFile);
  655. }
  656. clock_finish = cmSystemTools::GetTime();
  657. if ( this->LogFile )
  658. {
  659. double ttime = clock_finish - clock_start;
  660. int hours = static_cast<int>(ttime / (60 * 60));
  661. int minutes = static_cast<int>(ttime / 60) % 60;
  662. int seconds = static_cast<int>(ttime) % 60;
  663. char buffer[100];
  664. sprintf(buffer, "%02d:%02d:%02d", hours, minutes, seconds);
  665. *this->LogFile
  666. << "----------------------------------------------------------"
  667. << std::endl
  668. << "\"" << testname.c_str() << "\" end time: "
  669. << this->CTest->CurrentTime() << std::endl
  670. << "\"" << testname.c_str() << "\" time elapsed: "
  671. << buffer << std::endl
  672. << "----------------------------------------------------------"
  673. << std::endl << std::endl;
  674. }
  675. cres.ExecutionTime = (double)(clock_finish - clock_start);
  676. cres.FullCommandLine = testCommand;
  677. if ( !this->CTest->GetShowOnly() )
  678. {
  679. bool testFailed = false;
  680. std::vector<cmsys::RegularExpression>::iterator passIt;
  681. bool forceFail = false;
  682. if ( it->RequiredRegularExpressions.size() > 0 )
  683. {
  684. bool found = false;
  685. for ( passIt = it->RequiredRegularExpressions.begin();
  686. passIt != it->RequiredRegularExpressions.end();
  687. ++ passIt )
  688. {
  689. if ( passIt->find(output.c_str()) )
  690. {
  691. found = true;
  692. }
  693. }
  694. if ( !found )
  695. {
  696. forceFail = true;
  697. }
  698. }
  699. if ( it->ErrorRegularExpressions.size() > 0 )
  700. {
  701. for ( passIt = it->ErrorRegularExpressions.begin();
  702. passIt != it->ErrorRegularExpressions.end();
  703. ++ passIt )
  704. {
  705. if ( passIt->find(output.c_str()) )
  706. {
  707. forceFail = true;
  708. }
  709. }
  710. }
  711. if (res == cmsysProcess_State_Exited &&
  712. (retVal == 0 || it->RequiredRegularExpressions.size()) &&
  713. !forceFail)
  714. {
  715. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Passed");
  716. if ( it->WillFail )
  717. {
  718. cmCTestLog(this->CTest, HANDLER_OUTPUT, " - But it should fail!");
  719. cres.Status = cmCTestTestHandler::FAILED;
  720. testFailed = true;
  721. }
  722. else
  723. {
  724. cres.Status = cmCTestTestHandler::COMPLETED;
  725. }
  726. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl);
  727. }
  728. else
  729. {
  730. testFailed = true;
  731. cres.Status = cmCTestTestHandler::FAILED;
  732. if ( res == cmsysProcess_State_Expired )
  733. {
  734. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Timeout" << std::endl);
  735. cres.Status = cmCTestTestHandler::TIMEOUT;
  736. }
  737. else if ( res == cmsysProcess_State_Exception )
  738. {
  739. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Exception: ");
  740. switch ( retVal )
  741. {
  742. case cmsysProcess_Exception_Fault:
  743. cmCTestLog(this->CTest, HANDLER_OUTPUT, "SegFault");
  744. cres.Status = cmCTestTestHandler::SEGFAULT;
  745. break;
  746. case cmsysProcess_Exception_Illegal:
  747. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Illegal");
  748. cres.Status = cmCTestTestHandler::ILLEGAL;
  749. break;
  750. case cmsysProcess_Exception_Interrupt:
  751. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Interrupt");
  752. cres.Status = cmCTestTestHandler::INTERRUPT;
  753. break;
  754. case cmsysProcess_Exception_Numerical:
  755. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Numerical");
  756. cres.Status = cmCTestTestHandler::NUMERICAL;
  757. break;
  758. default:
  759. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Other");
  760. cres.Status = cmCTestTestHandler::OTHER_FAULT;
  761. }
  762. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl);
  763. }
  764. else if ( res == cmsysProcess_State_Error )
  765. {
  766. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Bad command " << res
  767. << std::endl);
  768. cres.Status = cmCTestTestHandler::BAD_COMMAND;
  769. }
  770. else
  771. {
  772. // Force fail will also be here?
  773. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Failed");
  774. if ( it->WillFail )
  775. {
  776. cres.Status = cmCTestTestHandler::COMPLETED;
  777. cmCTestLog(this->CTest, HANDLER_OUTPUT, " - supposed to fail");
  778. testFailed = false;
  779. }
  780. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl);
  781. }
  782. }
  783. if ( testFailed )
  784. {
  785. failed.push_back(testname);
  786. }
  787. else
  788. {
  789. passed.push_back(testname);
  790. }
  791. if (!output.empty() && output.find("<DartMeasurement") != output.npos)
  792. {
  793. if (this->DartStuff.find(output.c_str()))
  794. {
  795. std::string dartString = this->DartStuff.match(1);
  796. cmSystemTools::ReplaceString(output, dartString.c_str(),"");
  797. cres.RegressionImages
  798. = this->GenerateRegressionImages(dartString);
  799. }
  800. }
  801. }
  802. if ( cres.Status == cmCTestTestHandler::COMPLETED )
  803. {
  804. this->CleanTestOutput(output, static_cast<size_t>(
  805. this->CustomMaximumPassedTestOutputSize));
  806. }
  807. else
  808. {
  809. this->CleanTestOutput(output, static_cast<size_t>(
  810. this->CustomMaximumFailedTestOutputSize));
  811. }
  812. cres.Output = output;
  813. cres.ReturnValue = retVal;
  814. cres.CompletionStatus = "Completed";
  815. this->TestResults.push_back( cres );
  816. }
  817. this->EndTest = this->CTest->CurrentTime();
  818. this->ElapsedTestingTime = cmSystemTools::GetTime() - elapsed_time_start;
  819. if ( this->LogFile )
  820. {
  821. *this->LogFile << "End testing: " << this->EndTest << std::endl;
  822. }
  823. cmSystemTools::ChangeDirectory(current_dir.c_str());
  824. }
  825. //----------------------------------------------------------------------
  826. void cmCTestTestHandler::GenerateTestCommand(std::vector<const char*>&)
  827. {
  828. }
  829. //----------------------------------------------------------------------
  830. void cmCTestTestHandler::GenerateDartOutput(std::ostream& os)
  831. {
  832. if ( !this->CTest->GetProduceXML() )
  833. {
  834. return;
  835. }
  836. this->CTest->StartXML(os);
  837. os << "<Testing>\n"
  838. << "\t<StartDateTime>" << this->StartTest << "</StartDateTime>\n"
  839. << "\t<TestList>\n";
  840. cmCTestTestHandler::TestResultsVector::size_type cc;
  841. for ( cc = 0; cc < this->TestResults.size(); cc ++ )
  842. {
  843. cmCTestTestResult *result = &this->TestResults[cc];
  844. std::string testPath = result->Path + "/" + result->Name;
  845. os << "\t\t<Test>" << cmCTest::MakeXMLSafe(
  846. this->CTest->GetShortPathToFile(testPath.c_str()))
  847. << "</Test>" << std::endl;
  848. }
  849. os << "\t</TestList>\n";
  850. for ( cc = 0; cc < this->TestResults.size(); cc ++ )
  851. {
  852. cmCTestTestResult *result = &this->TestResults[cc];
  853. os << "\t<Test Status=\"";
  854. if ( result->Status == cmCTestTestHandler::COMPLETED )
  855. {
  856. os << "passed";
  857. }
  858. else if ( result->Status == cmCTestTestHandler::NOT_RUN )
  859. {
  860. os << "notrun";
  861. }
  862. else
  863. {
  864. os << "failed";
  865. }
  866. std::string testPath = result->Path + "/" + result->Name;
  867. os << "\">\n"
  868. << "\t\t<Name>" << cmCTest::MakeXMLSafe(result->Name) << "</Name>\n"
  869. << "\t\t<Path>" << cmCTest::MakeXMLSafe(
  870. this->CTest->GetShortPathToFile(result->Path.c_str())) << "</Path>\n"
  871. << "\t\t<FullName>" << cmCTest::MakeXMLSafe(
  872. this->CTest->GetShortPathToFile(testPath.c_str())) << "</FullName>\n"
  873. << "\t\t<FullCommandLine>"
  874. << cmCTest::MakeXMLSafe(result->FullCommandLine)
  875. << "</FullCommandLine>\n"
  876. << "\t\t<Results>" << std::endl;
  877. if ( result->Status != cmCTestTestHandler::NOT_RUN )
  878. {
  879. if ( result->Status != cmCTestTestHandler::COMPLETED ||
  880. result->ReturnValue )
  881. {
  882. os << "\t\t\t<NamedMeasurement type=\"text/string\" "
  883. "name=\"Exit Code\"><Value>"
  884. << this->GetTestStatus(result->Status) << "</Value>"
  885. "</NamedMeasurement>\n"
  886. << "\t\t\t<NamedMeasurement type=\"text/string\" "
  887. "name=\"Exit Value\"><Value>"
  888. << result->ReturnValue << "</Value></NamedMeasurement>"
  889. << std::endl;
  890. }
  891. os << result->RegressionImages;
  892. os << "\t\t\t<NamedMeasurement type=\"numeric/double\" "
  893. << "name=\"Execution Time\"><Value>"
  894. << result->ExecutionTime << "</Value></NamedMeasurement>\n";
  895. os
  896. << "\t\t\t<NamedMeasurement type=\"text/string\" "
  897. << "name=\"Completion Status\"><Value>"
  898. << result->CompletionStatus << "</Value></NamedMeasurement>\n";
  899. }
  900. os
  901. << "\t\t\t<Measurement>\n"
  902. << "\t\t\t\t<Value>";
  903. os << cmCTest::MakeXMLSafe(result->Output);
  904. os
  905. << "</Value>\n"
  906. << "\t\t\t</Measurement>\n"
  907. << "\t\t</Results>\n"
  908. << "\t</Test>" << std::endl;
  909. }
  910. os << "\t<EndDateTime>" << this->EndTest << "</EndDateTime>\n"
  911. << "<ElapsedMinutes>"
  912. << static_cast<int>(this->ElapsedTestingTime/6)/10.0
  913. << "</ElapsedMinutes>"
  914. << "</Testing>" << std::endl;
  915. this->CTest->EndXML(os);
  916. }
  917. //----------------------------------------------------------------------
  918. int cmCTestTestHandler::ExecuteCommands(std::vector<cmStdString>& vec)
  919. {
  920. std::vector<cmStdString>::iterator it;
  921. for ( it = vec.begin(); it != vec.end(); ++it )
  922. {
  923. int retVal = 0;
  924. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run command: " << *it
  925. << std::endl);
  926. if ( !cmSystemTools::RunSingleCommand(it->c_str(), 0, &retVal, 0, true
  927. /*this->Verbose*/) || retVal != 0 )
  928. {
  929. cmCTestLog(this->CTest, ERROR_MESSAGE, "Problem running command: "
  930. << *it << std::endl);
  931. return 0;
  932. }
  933. }
  934. return 1;
  935. }
  936. //----------------------------------------------------------------------
  937. // Find the appropriate executable to run for a test
  938. std::string cmCTestTestHandler::FindTheExecutable(const char *exe)
  939. {
  940. std::string fullPath = "";
  941. std::string dir;
  942. std::string file;
  943. cmSystemTools::SplitProgramPath(exe, dir, file);
  944. // first try to find the executable given a config type subdir if there is
  945. // one
  946. if(this->CTest->GetConfigType() != "" &&
  947. ::TryExecutable(dir.c_str(), file.c_str(), &fullPath,
  948. this->CTest->GetConfigType().c_str()))
  949. {
  950. return fullPath;
  951. }
  952. // next try the current directory as the subdir
  953. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"."))
  954. {
  955. return fullPath;
  956. }
  957. // try without the config subdir
  958. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,""))
  959. {
  960. return fullPath;
  961. }
  962. if ( this->CTest->GetConfigType() == "" )
  963. {
  964. // No config type, so try to guess it
  965. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Deployment"))
  966. {
  967. return fullPath;
  968. }
  969. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Development"))
  970. {
  971. return fullPath;
  972. }
  973. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Release"))
  974. {
  975. return fullPath;
  976. }
  977. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Debug"))
  978. {
  979. return fullPath;
  980. }
  981. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"MinSizeRel"))
  982. {
  983. return fullPath;
  984. }
  985. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"RelWithDebInfo"))
  986. {
  987. return fullPath;
  988. }
  989. }
  990. // if everything else failed, check the users path, but only if a full path
  991. // wasn;t specified
  992. if (dir.size() == 0)
  993. {
  994. std::string path = cmSystemTools::FindProgram(file.c_str());
  995. if (path != "")
  996. {
  997. return path;
  998. }
  999. }
  1000. if ( this->CTest->GetConfigType() != "" )
  1001. {
  1002. dir += "/";
  1003. dir += this->CTest->GetConfigType();
  1004. dir += "/";
  1005. dir += file;
  1006. cmSystemTools::Error("config type specified on the command line, but "
  1007. "test executable not found.",
  1008. dir.c_str());
  1009. return "";
  1010. }
  1011. return fullPath;
  1012. }
  1013. //----------------------------------------------------------------------
  1014. void cmCTestTestHandler::GetListOfTests()
  1015. {
  1016. if ( !this->IncludeRegExp.empty() )
  1017. {
  1018. this->IncludeTestsRegularExpression.compile(this->IncludeRegExp.c_str());
  1019. }
  1020. if ( !this->ExcludeRegExp.empty() )
  1021. {
  1022. this->ExcludeTestsRegularExpression.compile(this->ExcludeRegExp.c_str());
  1023. }
  1024. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1025. "Constructing a list of tests" << std::endl);
  1026. cmake cm;
  1027. cmGlobalGenerator gg;
  1028. gg.SetCMakeInstance(&cm);
  1029. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  1030. lg->SetGlobalGenerator(&gg);
  1031. cmMakefile *mf = lg->GetMakefile();
  1032. mf->AddDefinition("CTEST_CONFIGURATION_TYPE",
  1033. this->CTest->GetConfigType().c_str());
  1034. // Add handler for ADD_TEST
  1035. cmCTestAddTestCommand* newCom1 = new cmCTestAddTestCommand;
  1036. newCom1->TestHandler = this;
  1037. cm.AddCommand(newCom1);
  1038. // Add handler for SUBDIR
  1039. cmCTestSubdirCommand* newCom2 = new cmCTestSubdirCommand;
  1040. newCom2->TestHandler = this;
  1041. cm.AddCommand(newCom2);
  1042. // Add handler for SET_SOURCE_FILES_PROPERTIES
  1043. cmCTestSetTestsPropertiesCommand* newCom3
  1044. = new cmCTestSetTestsPropertiesCommand;
  1045. newCom3->TestHandler = this;
  1046. cm.AddCommand(newCom3);
  1047. const char* testFilename;
  1048. if( cmSystemTools::FileExists("CTestTestfile.cmake") )
  1049. {
  1050. // does the CTestTestfile.cmake exist ?
  1051. testFilename = "CTestTestfile.cmake";
  1052. }
  1053. else if( cmSystemTools::FileExists("DartTestfile.txt") )
  1054. {
  1055. // does the DartTestfile.txt exist ?
  1056. testFilename = "DartTestfile.txt";
  1057. }
  1058. else
  1059. {
  1060. return;
  1061. }
  1062. if ( !mf->ReadListFile(0, testFilename) )
  1063. {
  1064. return;
  1065. }
  1066. if ( cmSystemTools::GetErrorOccuredFlag() )
  1067. {
  1068. return;
  1069. }
  1070. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1071. "Done constructing a list of tests" << std::endl);
  1072. }
  1073. //----------------------------------------------------------------------
  1074. void cmCTestTestHandler::UseIncludeRegExp()
  1075. {
  1076. this->UseIncludeRegExpFlag = true;
  1077. }
  1078. //----------------------------------------------------------------------
  1079. void cmCTestTestHandler::UseExcludeRegExp()
  1080. {
  1081. this->UseExcludeRegExpFlag = true;
  1082. this->UseExcludeRegExpFirst = this->UseIncludeRegExpFlag ? false : true;
  1083. }
  1084. //----------------------------------------------------------------------
  1085. const char* cmCTestTestHandler::GetTestStatus(int status)
  1086. {
  1087. static const char statuses[][100] = {
  1088. "Not Run",
  1089. "Timeout",
  1090. "SEGFAULT",
  1091. "ILLEGAL",
  1092. "INTERRUPT",
  1093. "NUMERICAL",
  1094. "OTHER_FAULT",
  1095. "Failed",
  1096. "BAD_COMMAND",
  1097. "Completed"
  1098. };
  1099. if ( status < cmCTestTestHandler::NOT_RUN ||
  1100. status > cmCTestTestHandler::COMPLETED )
  1101. {
  1102. return "No Status";
  1103. }
  1104. return statuses[status];
  1105. }
  1106. //----------------------------------------------------------------------
  1107. void cmCTestTestHandler::ExpandTestsToRunInformation(int numTests)
  1108. {
  1109. if (this->TestsToRunString.empty())
  1110. {
  1111. return;
  1112. }
  1113. int start;
  1114. int end = -1;
  1115. double stride = -1;
  1116. std::string::size_type pos = 0;
  1117. std::string::size_type pos2;
  1118. // read start
  1119. if(GetNextNumber(this->TestsToRunString, start, pos, pos2))
  1120. {
  1121. // read end
  1122. if(GetNextNumber(this->TestsToRunString, end, pos, pos2))
  1123. {
  1124. // read stride
  1125. if(GetNextRealNumber(this->TestsToRunString, stride, pos, pos2))
  1126. {
  1127. int val =0;
  1128. // now read specific numbers
  1129. while(GetNextNumber(this->TestsToRunString, val, pos, pos2))
  1130. {
  1131. this->TestsToRun.push_back(val);
  1132. }
  1133. this->TestsToRun.push_back(val);
  1134. }
  1135. }
  1136. }
  1137. // if start is not specified then we assume we start at 1
  1138. if(start == -1)
  1139. {
  1140. start = 1;
  1141. }
  1142. // if end isnot specified then we assume we end with the last test
  1143. if(end == -1)
  1144. {
  1145. end = numTests;
  1146. }
  1147. // if the stride wasn't specified then it defaults to 1
  1148. if(stride == -1)
  1149. {
  1150. stride = 1;
  1151. }
  1152. // if we have a range then add it
  1153. if(end != -1 && start != -1 && stride > 0)
  1154. {
  1155. int i = 0;
  1156. while (i*stride + start <= end)
  1157. {
  1158. this->TestsToRun.push_back(static_cast<int>(i*stride+start));
  1159. ++i;
  1160. }
  1161. }
  1162. // sort the array
  1163. std::sort(this->TestsToRun.begin(), this->TestsToRun.end(),
  1164. std::less<int>());
  1165. // remove duplicates
  1166. std::vector<int>::iterator new_end =
  1167. std::unique(this->TestsToRun.begin(), this->TestsToRun.end());
  1168. this->TestsToRun.erase(new_end, this->TestsToRun.end());
  1169. }
  1170. //----------------------------------------------------------------------
  1171. // Just for convenience
  1172. #define SPACE_REGEX "[ \t\r\n]"
  1173. //----------------------------------------------------------------------
  1174. std::string cmCTestTestHandler::GenerateRegressionImages(
  1175. const std::string& xml)
  1176. {
  1177. cmsys::RegularExpression twoattributes(
  1178. "<DartMeasurement"
  1179. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1180. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1181. SPACE_REGEX "*>([^<]*)</DartMeasurement>");
  1182. cmsys::RegularExpression threeattributes(
  1183. "<DartMeasurement"
  1184. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1185. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1186. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1187. SPACE_REGEX "*>([^<]*)</DartMeasurement>");
  1188. cmsys::RegularExpression fourattributes(
  1189. "<DartMeasurement"
  1190. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1191. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1192. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1193. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1194. SPACE_REGEX "*>([^<]*)</DartMeasurement>");
  1195. cmsys::RegularExpression measurementfile(
  1196. "<DartMeasurementFile"
  1197. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1198. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1199. SPACE_REGEX "*>([^<]*)</DartMeasurementFile>");
  1200. cmOStringStream ostr;
  1201. bool done = false;
  1202. std::string cxml = xml;
  1203. while ( ! done )
  1204. {
  1205. if ( twoattributes.find(cxml) )
  1206. {
  1207. ostr
  1208. << "\t\t\t<NamedMeasurement"
  1209. << " " << twoattributes.match(1) << "=\""
  1210. << twoattributes.match(2) << "\""
  1211. << " " << twoattributes.match(3) << "=\""
  1212. << twoattributes.match(4) << "\""
  1213. << "><Value>" << twoattributes.match(5)
  1214. << "</Value></NamedMeasurement>"
  1215. << std::endl;
  1216. cxml.erase(twoattributes.start(),
  1217. twoattributes.end() - twoattributes.start());
  1218. }
  1219. else if ( threeattributes.find(cxml) )
  1220. {
  1221. ostr
  1222. << "\t\t\t<NamedMeasurement"
  1223. << " " << threeattributes.match(1) << "=\""
  1224. << threeattributes.match(2) << "\""
  1225. << " " << threeattributes.match(3) << "=\""
  1226. << threeattributes.match(4) << "\""
  1227. << " " << threeattributes.match(5) << "=\""
  1228. << threeattributes.match(6) << "\""
  1229. << "><Value>" << threeattributes.match(7)
  1230. << "</Value></NamedMeasurement>"
  1231. << std::endl;
  1232. cxml.erase(threeattributes.start(),
  1233. threeattributes.end() - threeattributes.start());
  1234. }
  1235. else if ( fourattributes.find(cxml) )
  1236. {
  1237. ostr
  1238. << "\t\t\t<NamedMeasurement"
  1239. << " " << fourattributes.match(1) << "=\""
  1240. << fourattributes.match(2) << "\""
  1241. << " " << fourattributes.match(3) << "=\""
  1242. << fourattributes.match(4) << "\""
  1243. << " " << fourattributes.match(5) << "=\""
  1244. << fourattributes.match(6) << "\""
  1245. << " " << fourattributes.match(7) << "=\""
  1246. << fourattributes.match(8) << "\""
  1247. << "><Value>" << fourattributes.match(9)
  1248. << "</Value></NamedMeasurement>"
  1249. << std::endl;
  1250. cxml.erase(fourattributes.start(),
  1251. fourattributes.end() - fourattributes.start());
  1252. }
  1253. else if ( measurementfile.find(cxml) )
  1254. {
  1255. const std::string& filename =
  1256. cmCTest::CleanString(measurementfile.match(5));
  1257. if ( cmSystemTools::FileExists(filename.c_str()) )
  1258. {
  1259. long len = cmSystemTools::FileLength(filename.c_str());
  1260. if ( len == 0 )
  1261. {
  1262. std::string k1 = measurementfile.match(1);
  1263. std::string v1 = measurementfile.match(2);
  1264. std::string k2 = measurementfile.match(3);
  1265. std::string v2 = measurementfile.match(4);
  1266. if ( cmSystemTools::LowerCase(k1) == "type" )
  1267. {
  1268. v1 = "text/string";
  1269. }
  1270. if ( cmSystemTools::LowerCase(k2) == "type" )
  1271. {
  1272. v2 = "text/string";
  1273. }
  1274. ostr
  1275. << "\t\t\t<NamedMeasurement"
  1276. << " " << k1 << "=\"" << v1 << "\""
  1277. << " " << k2 << "=\"" << v2 << "\""
  1278. << " encoding=\"none\""
  1279. << "><Value>Image " << filename.c_str()
  1280. << " is empty</Value></NamedMeasurement>";
  1281. }
  1282. else
  1283. {
  1284. std::ifstream ifs(filename.c_str(), std::ios::in
  1285. #ifdef _WIN32
  1286. | std::ios::binary
  1287. #endif
  1288. );
  1289. unsigned char *file_buffer = new unsigned char [ len + 1 ];
  1290. ifs.read(reinterpret_cast<char*>(file_buffer), len);
  1291. unsigned char *encoded_buffer
  1292. = new unsigned char [ static_cast<int>(len * 1.5 + 5) ];
  1293. unsigned long rlen
  1294. = cmsysBase64_Encode(file_buffer, len, encoded_buffer, 1);
  1295. unsigned long cc;
  1296. ostr
  1297. << "\t\t\t<NamedMeasurement"
  1298. << " " << measurementfile.match(1) << "=\""
  1299. << measurementfile.match(2) << "\""
  1300. << " " << measurementfile.match(3) << "=\""
  1301. << measurementfile.match(4) << "\""
  1302. << " encoding=\"base64\""
  1303. << ">" << std::endl << "\t\t\t\t<Value>";
  1304. for ( cc = 0; cc < rlen; cc ++ )
  1305. {
  1306. ostr << encoded_buffer[cc];
  1307. if ( cc % 60 == 0 && cc )
  1308. {
  1309. ostr << std::endl;
  1310. }
  1311. }
  1312. ostr
  1313. << "</Value>" << std::endl << "\t\t\t</NamedMeasurement>"
  1314. << std::endl;
  1315. delete [] file_buffer;
  1316. delete [] encoded_buffer;
  1317. }
  1318. }
  1319. else
  1320. {
  1321. int idx = 4;
  1322. if ( measurementfile.match(1) == "name" )
  1323. {
  1324. idx = 2;
  1325. }
  1326. ostr
  1327. << "\t\t\t<NamedMeasurement"
  1328. << " name=\"" << measurementfile.match(idx) << "\""
  1329. << " text=\"text/string\""
  1330. << "><Value>File " << filename.c_str()
  1331. << " not found</Value></NamedMeasurement>"
  1332. << std::endl;
  1333. cmCTestLog(this->CTest, HANDLER_OUTPUT, "File \"" << filename.c_str()
  1334. << "\" not found." << std::endl);
  1335. }
  1336. cxml.erase(measurementfile.start(),
  1337. measurementfile.end() - measurementfile.start());
  1338. }
  1339. else
  1340. {
  1341. done = true;
  1342. }
  1343. }
  1344. return ostr.str();
  1345. }
  1346. //----------------------------------------------------------------------
  1347. void cmCTestTestHandler::SetIncludeRegExp(const char *arg)
  1348. {
  1349. this->IncludeRegExp = arg;
  1350. }
  1351. //----------------------------------------------------------------------
  1352. void cmCTestTestHandler::SetExcludeRegExp(const char *arg)
  1353. {
  1354. this->ExcludeRegExp = arg;
  1355. }
  1356. //----------------------------------------------------------------------
  1357. void cmCTestTestHandler::SetTestsToRunInformation(const char* in)
  1358. {
  1359. if ( !in )
  1360. {
  1361. return;
  1362. }
  1363. this->TestsToRunString = in;
  1364. // if the argument is a file, then read it and use the contents as the
  1365. // string
  1366. if(cmSystemTools::FileExists(in))
  1367. {
  1368. std::ifstream fin(in);
  1369. unsigned long filelen = cmSystemTools::FileLength(in);
  1370. char* buff = new char[filelen+1];
  1371. fin.getline(buff, filelen);
  1372. buff[fin.gcount()] = 0;
  1373. this->TestsToRunString = buff;
  1374. }
  1375. }
  1376. //----------------------------------------------------------------------
  1377. bool cmCTestTestHandler::CleanTestOutput(std::string& output,
  1378. size_t remove_threshold)
  1379. {
  1380. if ( remove_threshold == 0 )
  1381. {
  1382. return true;
  1383. }
  1384. if ( output.find("CTEST_FULL_OUTPUT") != output.npos )
  1385. {
  1386. return true;
  1387. }
  1388. cmOStringStream ostr;
  1389. std::string::size_type cc;
  1390. std::string::size_type skipsize = 0;
  1391. int inTag = 0;
  1392. int skipped = 0;
  1393. for ( cc = 0; cc < output.size(); cc ++ )
  1394. {
  1395. int ch = output[cc];
  1396. if ( ch < 0 || ch > 255 )
  1397. {
  1398. break;
  1399. }
  1400. if ( ch == '<' )
  1401. {
  1402. inTag = 1;
  1403. }
  1404. if ( !inTag )
  1405. {
  1406. int notskip = 0;
  1407. // Skip
  1408. if ( skipsize < remove_threshold )
  1409. {
  1410. ostr << static_cast<char>(ch);
  1411. notskip = 1;
  1412. }
  1413. skipsize ++;
  1414. if ( notskip && skipsize >= remove_threshold )
  1415. {
  1416. skipped = 1;
  1417. }
  1418. }
  1419. else
  1420. {
  1421. ostr << static_cast<char>(ch);
  1422. }
  1423. if ( ch == '>' )
  1424. {
  1425. inTag = 0;
  1426. }
  1427. }
  1428. if ( skipped )
  1429. {
  1430. ostr << "..." << std::endl << "The rest of the test output was removed "
  1431. "since it exceeds the threshold of "
  1432. << remove_threshold << " characters." << std::endl;
  1433. }
  1434. output = ostr.str();
  1435. return true;
  1436. }
  1437. //----------------------------------------------------------------------
  1438. bool cmCTestTestHandler::SetTestsProperties(
  1439. const std::vector<std::string>& args)
  1440. {
  1441. std::vector<std::string>::const_iterator it;
  1442. std::vector<cmStdString> tests;
  1443. bool found = false;
  1444. for ( it = args.begin(); it != args.end(); ++ it )
  1445. {
  1446. if ( *it == "PROPERTIES" )
  1447. {
  1448. found = true;
  1449. break;
  1450. }
  1451. tests.push_back(*it);
  1452. }
  1453. if ( !found )
  1454. {
  1455. return false;
  1456. }
  1457. ++ it; // skip PROPERTIES
  1458. for ( ; it != args.end(); ++ it )
  1459. {
  1460. std::string key = *it;
  1461. ++ it;
  1462. if ( it == args.end() )
  1463. {
  1464. break;
  1465. }
  1466. std::string val = *it;
  1467. std::vector<cmStdString>::const_iterator tit;
  1468. for ( tit = tests.begin(); tit != tests.end(); ++ tit )
  1469. {
  1470. cmCTestTestHandler::ListOfTests::iterator rtit;
  1471. for ( rtit = this->TestList.begin();
  1472. rtit != this->TestList.end();
  1473. ++ rtit )
  1474. {
  1475. if ( *tit == rtit->Name )
  1476. {
  1477. if ( key == "WILL_FAIL" )
  1478. {
  1479. rtit->WillFail = cmSystemTools::IsOn(val.c_str());
  1480. }
  1481. if ( key == "FAIL_REGULAR_EXPRESSION" )
  1482. {
  1483. std::vector<std::string> lval;
  1484. cmSystemTools::ExpandListArgument(val.c_str(), lval);
  1485. std::vector<std::string>::iterator crit;
  1486. for ( crit = lval.begin(); crit != lval.end(); ++ crit )
  1487. {
  1488. rtit->ErrorRegularExpressions.push_back(
  1489. cmsys::RegularExpression(crit->c_str()));
  1490. }
  1491. }
  1492. if ( key == "PASS_REGULAR_EXPRESSION" )
  1493. {
  1494. std::vector<std::string> lval;
  1495. cmSystemTools::ExpandListArgument(val.c_str(), lval);
  1496. std::vector<std::string>::iterator crit;
  1497. for ( crit = lval.begin(); crit != lval.end(); ++ crit )
  1498. {
  1499. rtit->RequiredRegularExpressions.push_back(
  1500. cmsys::RegularExpression(crit->c_str()));
  1501. }
  1502. }
  1503. }
  1504. }
  1505. }
  1506. }
  1507. return true;
  1508. }
  1509. //----------------------------------------------------------------------
  1510. bool cmCTestTestHandler::AddTest(const std::vector<std::string>& args)
  1511. {
  1512. const std::string& testname = args[0];
  1513. if (this->UseExcludeRegExpFlag &&
  1514. this->UseExcludeRegExpFirst &&
  1515. this->ExcludeTestsRegularExpression.find(testname.c_str()))
  1516. {
  1517. return true;
  1518. }
  1519. if ( this->MemCheck )
  1520. {
  1521. std::vector<cmStdString>::iterator it;
  1522. bool found = false;
  1523. for ( it = this->CustomTestsIgnore.begin();
  1524. it != this->CustomTestsIgnore.end(); ++ it )
  1525. {
  1526. if ( *it == testname )
  1527. {
  1528. found = true;
  1529. break;
  1530. }
  1531. }
  1532. if ( found )
  1533. {
  1534. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Ignore memcheck: "
  1535. << *it << std::endl);
  1536. return true;
  1537. }
  1538. }
  1539. else
  1540. {
  1541. std::vector<cmStdString>::iterator it;
  1542. bool found = false;
  1543. for ( it = this->CustomTestsIgnore.begin();
  1544. it != this->CustomTestsIgnore.end(); ++ it )
  1545. {
  1546. if ( *it == testname )
  1547. {
  1548. found = true;
  1549. break;
  1550. }
  1551. }
  1552. if ( found )
  1553. {
  1554. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Ignore test: "
  1555. << *it << std::endl);
  1556. return true;
  1557. }
  1558. }
  1559. cmCTestTestProperties test;
  1560. test.Name = testname;
  1561. test.Args = args;
  1562. test.Directory = cmSystemTools::GetCurrentWorkingDirectory();
  1563. test.IsInBasedOnREOptions = true;
  1564. test.WillFail = false;
  1565. if (this->UseIncludeRegExpFlag &&
  1566. !this->IncludeTestsRegularExpression.find(testname.c_str()))
  1567. {
  1568. test.IsInBasedOnREOptions = false;
  1569. }
  1570. else if (this->UseExcludeRegExpFlag &&
  1571. !this->UseExcludeRegExpFirst &&
  1572. this->ExcludeTestsRegularExpression.find(testname.c_str()))
  1573. {
  1574. test.IsInBasedOnREOptions = false;
  1575. }
  1576. this->TestList.push_back(test);
  1577. return true;
  1578. }