cmCTestTestHandler.cxx 49 KB

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