cmCTestTestHandler.cxx 50 KB

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