cmCTestTestHandler.cxx 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCTestTestHandler.h"
  11. #include "cmCTestMultiProcessHandler.h"
  12. #include "cmCTestBatchTestHandler.h"
  13. #include "cmCTest.h"
  14. #include "cmCTestRunTest.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 <cmsys/Directory.hxx>
  21. #include <cmsys/FStream.hxx>
  22. #include "cmMakefile.h"
  23. #include "cmGlobalGenerator.h"
  24. #include "cmCommand.h"
  25. #include "cmSystemTools.h"
  26. #include "cmXMLWriter.h"
  27. #include "cm_utf8.h"
  28. #include <stdlib.h>
  29. #include <math.h>
  30. #include <float.h>
  31. #include <set>
  32. //----------------------------------------------------------------------
  33. class cmCTestSubdirCommand : public cmCommand
  34. {
  35. public:
  36. /**
  37. * This is a virtual constructor for the command.
  38. */
  39. virtual cmCommand* Clone()
  40. {
  41. cmCTestSubdirCommand* c = new cmCTestSubdirCommand;
  42. c->TestHandler = this->TestHandler;
  43. return c;
  44. }
  45. /**
  46. * This is called when the command is first encountered in
  47. * the CMakeLists.txt file.
  48. */
  49. virtual bool InitialPass(std::vector<std::string> const& args,
  50. cmExecutionStatus &);
  51. /**
  52. * The name of the command as specified in CMakeList.txt.
  53. */
  54. virtual std::string GetName() const { return "subdirs";}
  55. cmTypeMacro(cmCTestSubdirCommand, cmCommand);
  56. cmCTestTestHandler* TestHandler;
  57. };
  58. //----------------------------------------------------------------------
  59. bool cmCTestSubdirCommand
  60. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  61. {
  62. if(args.size() < 1 )
  63. {
  64. this->SetError("called with incorrect number of arguments");
  65. return false;
  66. }
  67. std::vector<std::string>::const_iterator it;
  68. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  69. for ( it = args.begin(); it != args.end(); ++ it )
  70. {
  71. std::string fname;
  72. if(cmSystemTools::FileIsFullPath(it->c_str()))
  73. {
  74. fname = *it;
  75. }
  76. else
  77. {
  78. fname = cwd;
  79. fname += "/";
  80. fname += *it;
  81. }
  82. if ( !cmSystemTools::FileIsDirectory(fname) )
  83. {
  84. // No subdirectory? So what...
  85. continue;
  86. }
  87. cmSystemTools::ChangeDirectory(fname);
  88. const char* testFilename;
  89. if( cmSystemTools::FileExists("CTestTestfile.cmake") )
  90. {
  91. // does the CTestTestfile.cmake exist ?
  92. testFilename = "CTestTestfile.cmake";
  93. }
  94. else if( cmSystemTools::FileExists("DartTestfile.txt") )
  95. {
  96. // does the DartTestfile.txt exist ?
  97. testFilename = "DartTestfile.txt";
  98. }
  99. else
  100. {
  101. // No CTestTestfile? Who cares...
  102. continue;
  103. }
  104. fname += "/";
  105. fname += testFilename;
  106. bool readit = this->Makefile->ReadDependentFile(fname.c_str());
  107. cmSystemTools::ChangeDirectory(cwd);
  108. if(!readit)
  109. {
  110. std::string m = "Could not find include file: ";
  111. m += fname;
  112. this->SetError(m);
  113. return false;
  114. }
  115. }
  116. cmSystemTools::ChangeDirectory(cwd);
  117. return true;
  118. }
  119. //----------------------------------------------------------------------
  120. class cmCTestAddSubdirectoryCommand : public cmCommand
  121. {
  122. public:
  123. /**
  124. * This is a virtual constructor for the command.
  125. */
  126. virtual cmCommand* Clone()
  127. {
  128. cmCTestAddSubdirectoryCommand* c = new cmCTestAddSubdirectoryCommand;
  129. c->TestHandler = this->TestHandler;
  130. return c;
  131. }
  132. /**
  133. * This is called when the command is first encountered in
  134. * the CMakeLists.txt file.
  135. */
  136. virtual bool InitialPass(std::vector<std::string> const& args,
  137. cmExecutionStatus &);
  138. /**
  139. * The name of the command as specified in CMakeList.txt.
  140. */
  141. virtual std::string GetName() const { return "add_subdirectory";}
  142. cmTypeMacro(cmCTestAddSubdirectoryCommand, cmCommand);
  143. cmCTestTestHandler* TestHandler;
  144. };
  145. //----------------------------------------------------------------------
  146. bool cmCTestAddSubdirectoryCommand
  147. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  148. {
  149. if(args.size() < 1 )
  150. {
  151. this->SetError("called with incorrect number of arguments");
  152. return false;
  153. }
  154. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  155. cmSystemTools::ChangeDirectory(cwd);
  156. std::string fname = cwd;
  157. fname += "/";
  158. fname += args[0];
  159. if ( !cmSystemTools::FileExists(fname.c_str()) )
  160. {
  161. // No subdirectory? So what...
  162. return true;
  163. }
  164. cmSystemTools::ChangeDirectory(fname);
  165. const char* testFilename;
  166. if( cmSystemTools::FileExists("CTestTestfile.cmake") )
  167. {
  168. // does the CTestTestfile.cmake exist ?
  169. testFilename = "CTestTestfile.cmake";
  170. }
  171. else if( cmSystemTools::FileExists("DartTestfile.txt") )
  172. {
  173. // does the DartTestfile.txt exist ?
  174. testFilename = "DartTestfile.txt";
  175. }
  176. else
  177. {
  178. // No CTestTestfile? Who cares...
  179. cmSystemTools::ChangeDirectory(cwd);
  180. return true;
  181. }
  182. fname += "/";
  183. fname += testFilename;
  184. bool readit = this->Makefile->ReadDependentFile(fname.c_str());
  185. cmSystemTools::ChangeDirectory(cwd);
  186. if(!readit)
  187. {
  188. std::string m = "Could not find include file: ";
  189. m += fname;
  190. this->SetError(m);
  191. return false;
  192. }
  193. return true;
  194. }
  195. //----------------------------------------------------------------------
  196. class cmCTestAddTestCommand : public cmCommand
  197. {
  198. public:
  199. /**
  200. * This is a virtual constructor for the command.
  201. */
  202. virtual cmCommand* Clone()
  203. {
  204. cmCTestAddTestCommand* c = new cmCTestAddTestCommand;
  205. c->TestHandler = this->TestHandler;
  206. return c;
  207. }
  208. /**
  209. * This is called when the command is first encountered in
  210. * the CMakeLists.txt file.
  211. */
  212. virtual bool InitialPass(std::vector<std::string> const&,
  213. cmExecutionStatus &);
  214. /**
  215. * The name of the command as specified in CMakeList.txt.
  216. */
  217. virtual std::string GetName() const { return "add_test";}
  218. cmTypeMacro(cmCTestAddTestCommand, cmCommand);
  219. cmCTestTestHandler* TestHandler;
  220. };
  221. //----------------------------------------------------------------------
  222. bool cmCTestAddTestCommand
  223. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  224. {
  225. if ( args.size() < 2 )
  226. {
  227. this->SetError("called with incorrect number of arguments");
  228. return false;
  229. }
  230. return this->TestHandler->AddTest(args);
  231. }
  232. //----------------------------------------------------------------------
  233. class cmCTestSetTestsPropertiesCommand : public cmCommand
  234. {
  235. public:
  236. /**
  237. * This is a virtual constructor for the command.
  238. */
  239. virtual cmCommand* Clone()
  240. {
  241. cmCTestSetTestsPropertiesCommand* c
  242. = new cmCTestSetTestsPropertiesCommand;
  243. c->TestHandler = this->TestHandler;
  244. return c;
  245. }
  246. /**
  247. * This is called when the command is first encountered in
  248. * the CMakeLists.txt file.
  249. */
  250. virtual bool InitialPass(std::vector<std::string> const&,
  251. cmExecutionStatus &);
  252. /**
  253. * The name of the command as specified in CMakeList.txt.
  254. */
  255. virtual std::string GetName() const { return "set_tests_properties";}
  256. cmTypeMacro(cmCTestSetTestsPropertiesCommand, cmCommand);
  257. cmCTestTestHandler* TestHandler;
  258. };
  259. //----------------------------------------------------------------------
  260. bool cmCTestSetTestsPropertiesCommand
  261. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  262. {
  263. return this->TestHandler->SetTestsProperties(args);
  264. }
  265. //----------------------------------------------------------------------
  266. // get the next number in a string with numbers separated by ,
  267. // pos is the start of the search and pos2 is the end of the search
  268. // pos becomes pos2 after a call to GetNextNumber.
  269. // -1 is returned at the end of the list.
  270. inline int GetNextNumber(std::string const& in,
  271. int& val,
  272. std::string::size_type& pos,
  273. std::string::size_type& pos2)
  274. {
  275. pos2 = in.find(',', pos);
  276. if(pos2 != in.npos)
  277. {
  278. if(pos2-pos == 0)
  279. {
  280. val = -1;
  281. }
  282. else
  283. {
  284. val = atoi(in.substr(pos, pos2-pos).c_str());
  285. }
  286. pos = pos2+1;
  287. return 1;
  288. }
  289. else
  290. {
  291. if(in.size()-pos == 0)
  292. {
  293. val = -1;
  294. }
  295. else
  296. {
  297. val = atoi(in.substr(pos, in.size()-pos).c_str());
  298. }
  299. return 0;
  300. }
  301. }
  302. //----------------------------------------------------------------------
  303. // get the next number in a string with numbers separated by ,
  304. // pos is the start of the search and pos2 is the end of the search
  305. // pos becomes pos2 after a call to GetNextNumber.
  306. // -1 is returned at the end of the list.
  307. inline int GetNextRealNumber(std::string const& in,
  308. double& val,
  309. std::string::size_type& pos,
  310. std::string::size_type& pos2)
  311. {
  312. pos2 = in.find(',', pos);
  313. if(pos2 != in.npos)
  314. {
  315. if(pos2-pos == 0)
  316. {
  317. val = -1;
  318. }
  319. else
  320. {
  321. val = atof(in.substr(pos, pos2-pos).c_str());
  322. }
  323. pos = pos2+1;
  324. return 1;
  325. }
  326. else
  327. {
  328. if(in.size()-pos == 0)
  329. {
  330. val = -1;
  331. }
  332. else
  333. {
  334. val = atof(in.substr(pos, in.size()-pos).c_str());
  335. }
  336. return 0;
  337. }
  338. }
  339. //----------------------------------------------------------------------
  340. cmCTestTestHandler::cmCTestTestHandler()
  341. {
  342. this->UseUnion = false;
  343. this->UseIncludeLabelRegExpFlag = false;
  344. this->UseExcludeLabelRegExpFlag = false;
  345. this->UseIncludeRegExpFlag = false;
  346. this->UseExcludeRegExpFlag = false;
  347. this->UseExcludeRegExpFirst = false;
  348. this->CustomMaximumPassedTestOutputSize = 1 * 1024;
  349. this->CustomMaximumFailedTestOutputSize = 300 * 1024;
  350. this->MemCheck = false;
  351. this->LogFile = 0;
  352. // regex to detect <DartMeasurement>...</DartMeasurement>
  353. this->DartStuff.compile(
  354. "(<DartMeasurement.*/DartMeasurement[a-zA-Z]*>)");
  355. // regex to detect each individual <DartMeasurement>...</DartMeasurement>
  356. this->DartStuff1.compile(
  357. "(<DartMeasurement[^<]*</DartMeasurement[a-zA-Z]*>)");
  358. }
  359. //----------------------------------------------------------------------
  360. void cmCTestTestHandler::Initialize()
  361. {
  362. this->Superclass::Initialize();
  363. this->ElapsedTestingTime = -1;
  364. this->TestResults.clear();
  365. this->CustomTestsIgnore.clear();
  366. this->StartTest = "";
  367. this->EndTest = "";
  368. this->CustomPreTest.clear();
  369. this->CustomPostTest.clear();
  370. this->CustomMaximumPassedTestOutputSize = 1 * 1024;
  371. this->CustomMaximumFailedTestOutputSize = 300 * 1024;
  372. this->TestsToRun.clear();
  373. this->UseIncludeLabelRegExpFlag = false;
  374. this->UseExcludeLabelRegExpFlag = false;
  375. this->UseIncludeRegExpFlag = false;
  376. this->UseExcludeRegExpFlag = false;
  377. this->UseExcludeRegExpFirst = false;
  378. this->IncludeLabelRegularExpression = "";
  379. this->ExcludeLabelRegularExpression = "";
  380. this->IncludeRegExp = "";
  381. this->ExcludeRegExp = "";
  382. TestsToRunString = "";
  383. this->UseUnion = false;
  384. this->TestList.clear();
  385. }
  386. //----------------------------------------------------------------------
  387. void cmCTestTestHandler::PopulateCustomVectors(cmMakefile *mf)
  388. {
  389. this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_PRE_TEST",
  390. this->CustomPreTest);
  391. this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_POST_TEST",
  392. this->CustomPostTest);
  393. this->CTest->PopulateCustomVector(mf,
  394. "CTEST_CUSTOM_TESTS_IGNORE",
  395. this->CustomTestsIgnore);
  396. this->CTest->PopulateCustomInteger(mf,
  397. "CTEST_CUSTOM_MAXIMUM_PASSED_TEST_OUTPUT_SIZE",
  398. this->CustomMaximumPassedTestOutputSize);
  399. this->CTest->PopulateCustomInteger(mf,
  400. "CTEST_CUSTOM_MAXIMUM_FAILED_TEST_OUTPUT_SIZE",
  401. this->CustomMaximumFailedTestOutputSize);
  402. }
  403. //----------------------------------------------------------------------
  404. int cmCTestTestHandler::PreProcessHandler()
  405. {
  406. if ( !this->ExecuteCommands(this->CustomPreTest) )
  407. {
  408. cmCTestLog(this->CTest, ERROR_MESSAGE,
  409. "Problem executing pre-test command(s)." << std::endl);
  410. return 0;
  411. }
  412. return 1;
  413. }
  414. //----------------------------------------------------------------------
  415. int cmCTestTestHandler::PostProcessHandler()
  416. {
  417. if ( !this->ExecuteCommands(this->CustomPostTest) )
  418. {
  419. cmCTestLog(this->CTest, ERROR_MESSAGE,
  420. "Problem executing post-test command(s)." << std::endl);
  421. return 0;
  422. }
  423. return 1;
  424. }
  425. //----------------------------------------------------------------------
  426. //clearly it would be nice if this were broken up into a few smaller
  427. //functions and commented...
  428. int cmCTestTestHandler::ProcessHandler()
  429. {
  430. // Update internal data structure from generic one
  431. this->SetTestsToRunInformation(this->GetOption("TestsToRunInformation"));
  432. this->SetUseUnion(cmSystemTools::IsOn(this->GetOption("UseUnion")));
  433. if(cmSystemTools::IsOn(this->GetOption("ScheduleRandom")))
  434. {
  435. this->CTest->SetScheduleType("Random");
  436. }
  437. if(this->GetOption("ParallelLevel"))
  438. {
  439. this->CTest->SetParallelLevel(atoi(this->GetOption("ParallelLevel")));
  440. }
  441. const char* val;
  442. val = this->GetOption("LabelRegularExpression");
  443. if ( val )
  444. {
  445. this->UseIncludeLabelRegExpFlag = true;
  446. this->IncludeLabelRegExp = val;
  447. }
  448. val = this->GetOption("ExcludeLabelRegularExpression");
  449. if ( val )
  450. {
  451. this->UseExcludeLabelRegExpFlag = true;
  452. this->ExcludeLabelRegExp = val;
  453. }
  454. val = this->GetOption("IncludeRegularExpression");
  455. if ( val )
  456. {
  457. this->UseIncludeRegExp();
  458. this->SetIncludeRegExp(val);
  459. }
  460. val = this->GetOption("ExcludeRegularExpression");
  461. if ( val )
  462. {
  463. this->UseExcludeRegExp();
  464. this->SetExcludeRegExp(val);
  465. }
  466. this->SetRerunFailed(cmSystemTools::IsOn(this->GetOption("RerunFailed")));
  467. this->TestResults.clear();
  468. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  469. (this->MemCheck ? "Memory check" : "Test")
  470. << " project " << cmSystemTools::GetCurrentWorkingDirectory()
  471. << std::endl, this->Quiet);
  472. if ( ! this->PreProcessHandler() )
  473. {
  474. return -1;
  475. }
  476. cmGeneratedFileStream mLogFile;
  477. this->StartLogFile((this->MemCheck ? "DynamicAnalysis" : "Test"), mLogFile);
  478. this->LogFile = &mLogFile;
  479. std::vector<std::string> passed;
  480. std::vector<std::string> failed;
  481. int total;
  482. //start the real time clock
  483. double clock_start, clock_finish;
  484. clock_start = cmSystemTools::GetTime();
  485. this->ProcessDirectory(passed, failed);
  486. clock_finish = cmSystemTools::GetTime();
  487. total = int(passed.size()) + int(failed.size());
  488. if (total == 0)
  489. {
  490. if ( !this->CTest->GetShowOnly() && !this->CTest->ShouldPrintLabels() )
  491. {
  492. cmCTestLog(this->CTest, ERROR_MESSAGE, "No tests were found!!!"
  493. << std::endl);
  494. }
  495. }
  496. else
  497. {
  498. if (this->HandlerVerbose && !passed.empty() &&
  499. (this->UseIncludeRegExpFlag || this->UseExcludeRegExpFlag))
  500. {
  501. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl
  502. << "The following tests passed:" << std::endl, this->Quiet);
  503. for(std::vector<std::string>::iterator j = passed.begin();
  504. j != passed.end(); ++j)
  505. {
  506. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "\t" << *j
  507. << std::endl, this->Quiet);
  508. }
  509. }
  510. float percent = float(passed.size()) * 100.0f / float(total);
  511. if (!failed.empty() && percent > 99)
  512. {
  513. percent = 99;
  514. }
  515. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl
  516. << static_cast<int>(percent + .5f) << "% tests passed, "
  517. << failed.size() << " tests failed out of "
  518. << total << std::endl);
  519. if(this->CTest->GetLabelSummary())
  520. {
  521. this->PrintLabelSummary();
  522. }
  523. char realBuf[1024];
  524. sprintf(realBuf, "%6.2f sec", (double)(clock_finish - clock_start));
  525. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  526. "\nTotal Test time (real) = " << realBuf << "\n", this->Quiet );
  527. if (!failed.empty())
  528. {
  529. cmGeneratedFileStream ofs;
  530. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl
  531. << "The following tests FAILED:" << std::endl);
  532. this->StartLogFile("TestsFailed", ofs);
  533. typedef std::set<cmCTestTestHandler::cmCTestTestResult,
  534. cmCTestTestResultLess> SetOfTests;
  535. SetOfTests resultsSet(this->TestResults.begin(),
  536. this->TestResults.end());
  537. for(SetOfTests::iterator ftit = resultsSet.begin();
  538. ftit != resultsSet.end(); ++ftit)
  539. {
  540. if ( ftit->Status != cmCTestTestHandler::COMPLETED )
  541. {
  542. ofs << ftit->TestCount << ":" << ftit->Name << std::endl;
  543. cmCTestLog(this->CTest, HANDLER_OUTPUT, "\t" << std::setw(3)
  544. << ftit->TestCount << " - "
  545. << ftit->Name << " ("
  546. << this->GetTestStatus(ftit->Status) << ")"
  547. << std::endl);
  548. }
  549. }
  550. }
  551. }
  552. if ( this->CTest->GetProduceXML() )
  553. {
  554. cmGeneratedFileStream xmlfile;
  555. if( !this->StartResultingXML(
  556. (this->MemCheck ? cmCTest::PartMemCheck : cmCTest::PartTest),
  557. (this->MemCheck ? "DynamicAnalysis" : "Test"), xmlfile) )
  558. {
  559. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot create "
  560. << (this->MemCheck ? "memory check" : "testing")
  561. << " XML file" << std::endl);
  562. this->LogFile = 0;
  563. return 1;
  564. }
  565. cmXMLWriter xml(xmlfile);
  566. this->GenerateDartOutput(xml);
  567. }
  568. if ( ! this->PostProcessHandler() )
  569. {
  570. this->LogFile = 0;
  571. return -1;
  572. }
  573. if ( !failed.empty() )
  574. {
  575. this->LogFile = 0;
  576. return -1;
  577. }
  578. this->LogFile = 0;
  579. return 0;
  580. }
  581. //----------------------------------------------------------------------
  582. void cmCTestTestHandler::PrintLabelSummary()
  583. {
  584. cmCTestTestHandler::ListOfTests::iterator it = this->TestList.begin();
  585. std::map<std::string, double> labelTimes;
  586. std::map<std::string, int> labelCounts;
  587. std::set<std::string> labels;
  588. // initialize maps
  589. std::string::size_type maxlen = 0;
  590. for(; it != this->TestList.end(); ++it)
  591. {
  592. cmCTestTestProperties& p = *it;
  593. if(!p.Labels.empty())
  594. {
  595. for(std::vector<std::string>::iterator l = p.Labels.begin();
  596. l != p.Labels.end(); ++l)
  597. {
  598. if((*l).size() > maxlen)
  599. {
  600. maxlen = (*l).size();
  601. }
  602. labels.insert(*l);
  603. labelTimes[*l] = 0;
  604. labelCounts[*l] = 0;
  605. }
  606. }
  607. }
  608. cmCTestTestHandler::TestResultsVector::iterator ri =
  609. this->TestResults.begin();
  610. // fill maps
  611. for(; ri != this->TestResults.end(); ++ri)
  612. {
  613. cmCTestTestResult &result = *ri;
  614. cmCTestTestProperties& p = *result.Properties;
  615. if(!p.Labels.empty())
  616. {
  617. for(std::vector<std::string>::iterator l = p.Labels.begin();
  618. l != p.Labels.end(); ++l)
  619. {
  620. labelTimes[*l] += result.ExecutionTime;
  621. ++labelCounts[*l];
  622. }
  623. }
  624. }
  625. // now print times
  626. if(!labels.empty())
  627. {
  628. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "\nLabel Time Summary:",
  629. this->Quiet);
  630. }
  631. for(std::set<std::string>::const_iterator i = labels.begin();
  632. i != labels.end(); ++i)
  633. {
  634. std::string label = *i;
  635. label.resize(maxlen +3, ' ');
  636. char buf[1024];
  637. sprintf(buf, "%6.2f sec", labelTimes[*i]);
  638. std::ostringstream labelCountStr;
  639. labelCountStr << "(" << labelCounts[*i] << " test";
  640. if (labelCounts[*i] > 1)
  641. {
  642. labelCountStr << "s";
  643. }
  644. labelCountStr << ")";
  645. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "\n"
  646. << label << " = " << buf << " " << labelCountStr.str(),
  647. this->Quiet );
  648. if ( this->LogFile )
  649. {
  650. *this->LogFile << "\n" << *i << " = "
  651. << buf << "\n";
  652. }
  653. }
  654. if(!labels.empty())
  655. {
  656. if(this->LogFile)
  657. {
  658. *this->LogFile << "\n";
  659. }
  660. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "\n", this->Quiet);
  661. }
  662. }
  663. //----------------------------------------------------------------------
  664. void cmCTestTestHandler::CheckLabelFilterInclude(cmCTestTestProperties& it)
  665. {
  666. // if not using Labels to filter then return
  667. if (!this->UseIncludeLabelRegExpFlag )
  668. {
  669. return;
  670. }
  671. // if there are no labels and we are filtering by labels
  672. // then exclude the test as it does not have the label
  673. if(it.Labels.empty())
  674. {
  675. it.IsInBasedOnREOptions = false;
  676. return;
  677. }
  678. // check to see if the label regular expression matches
  679. bool found = false; // assume it does not match
  680. // loop over all labels and look for match
  681. for(std::vector<std::string>::iterator l = it.Labels.begin();
  682. l != it.Labels.end(); ++l)
  683. {
  684. if(this->IncludeLabelRegularExpression.find(*l))
  685. {
  686. found = true;
  687. }
  688. }
  689. // if no match was found, exclude the test
  690. if(!found)
  691. {
  692. it.IsInBasedOnREOptions = false;
  693. }
  694. }
  695. //----------------------------------------------------------------------
  696. void cmCTestTestHandler::CheckLabelFilterExclude(cmCTestTestProperties& it)
  697. {
  698. // if not using Labels to filter then return
  699. if (!this->UseExcludeLabelRegExpFlag )
  700. {
  701. return;
  702. }
  703. // if there are no labels and we are excluding by labels
  704. // then do nothing as a no label can not be a match
  705. if(it.Labels.empty())
  706. {
  707. return;
  708. }
  709. // check to see if the label regular expression matches
  710. bool found = false; // assume it does not match
  711. // loop over all labels and look for match
  712. for(std::vector<std::string>::iterator l = it.Labels.begin();
  713. l != it.Labels.end(); ++l)
  714. {
  715. if(this->ExcludeLabelRegularExpression.find(*l))
  716. {
  717. found = true;
  718. }
  719. }
  720. // if match was found, exclude the test
  721. if(found)
  722. {
  723. it.IsInBasedOnREOptions = false;
  724. }
  725. }
  726. //----------------------------------------------------------------------
  727. void cmCTestTestHandler::CheckLabelFilter(cmCTestTestProperties& it)
  728. {
  729. this->CheckLabelFilterInclude(it);
  730. this->CheckLabelFilterExclude(it);
  731. }
  732. //----------------------------------------------------------------------
  733. void cmCTestTestHandler::ComputeTestList()
  734. {
  735. this->TestList.clear(); // clear list of test
  736. this->GetListOfTests();
  737. if (this->RerunFailed)
  738. {
  739. this->ComputeTestListForRerunFailed();
  740. return;
  741. }
  742. cmCTestTestHandler::ListOfTests::size_type tmsize = this->TestList.size();
  743. // how many tests are in based on RegExp?
  744. int inREcnt = 0;
  745. cmCTestTestHandler::ListOfTests::iterator it;
  746. for ( it = this->TestList.begin(); it != this->TestList.end(); it ++ )
  747. {
  748. this->CheckLabelFilter(*it);
  749. if (it->IsInBasedOnREOptions)
  750. {
  751. inREcnt ++;
  752. }
  753. }
  754. // expand the test list based on the union flag
  755. if (this->UseUnion)
  756. {
  757. this->ExpandTestsToRunInformation((int)tmsize);
  758. }
  759. else
  760. {
  761. this->ExpandTestsToRunInformation(inREcnt);
  762. }
  763. // Now create a final list of tests to run
  764. int cnt = 0;
  765. inREcnt = 0;
  766. std::string last_directory = "";
  767. ListOfTests finalList;
  768. for ( it = this->TestList.begin(); it != this->TestList.end(); it ++ )
  769. {
  770. cnt ++;
  771. if (it->IsInBasedOnREOptions)
  772. {
  773. inREcnt++;
  774. }
  775. if (this->UseUnion)
  776. {
  777. // if it is not in the list and not in the regexp then skip
  778. if ((!this->TestsToRun.empty() &&
  779. std::find(this->TestsToRun.begin(), this->TestsToRun.end(), cnt)
  780. == this->TestsToRun.end()) && !it->IsInBasedOnREOptions)
  781. {
  782. continue;
  783. }
  784. }
  785. else
  786. {
  787. // is this test in the list of tests to run? If not then skip it
  788. if ((!this->TestsToRun.empty() &&
  789. std::find(this->TestsToRun.begin(),
  790. this->TestsToRun.end(), inREcnt)
  791. == this->TestsToRun.end()) || !it->IsInBasedOnREOptions)
  792. {
  793. continue;
  794. }
  795. }
  796. it->Index = cnt; // save the index into the test list for this test
  797. finalList.push_back(*it);
  798. }
  799. // Save the total number of tests before exclusions
  800. this->TotalNumberOfTests = this->TestList.size();
  801. // Set the TestList to the final list of all test
  802. this->TestList = finalList;
  803. this->UpdateMaxTestNameWidth();
  804. }
  805. void cmCTestTestHandler::ComputeTestListForRerunFailed()
  806. {
  807. this->ExpandTestsToRunInformationForRerunFailed();
  808. cmCTestTestHandler::ListOfTests::iterator it;
  809. ListOfTests finalList;
  810. int cnt = 0;
  811. for ( it = this->TestList.begin(); it != this->TestList.end(); it ++ )
  812. {
  813. cnt ++;
  814. // if this test is not in our list of tests to run, then skip it.
  815. if ((!this->TestsToRun.empty() &&
  816. std::find(this->TestsToRun.begin(), this->TestsToRun.end(), cnt)
  817. == this->TestsToRun.end()))
  818. {
  819. continue;
  820. }
  821. it->Index = cnt;
  822. finalList.push_back(*it);
  823. }
  824. // Save the total number of tests before exclusions
  825. this->TotalNumberOfTests = this->TestList.size();
  826. // Set the TestList to the list of failed tests to rerun
  827. this->TestList = finalList;
  828. this->UpdateMaxTestNameWidth();
  829. }
  830. void cmCTestTestHandler::UpdateMaxTestNameWidth()
  831. {
  832. std::string::size_type max = this->CTest->GetMaxTestNameWidth();
  833. for ( cmCTestTestHandler::ListOfTests::iterator it = this->TestList.begin();
  834. it != this->TestList.end(); it ++ )
  835. {
  836. cmCTestTestProperties& p = *it;
  837. if(max < p.Name.size())
  838. {
  839. max = p.Name.size();
  840. }
  841. }
  842. if(static_cast<std::string::size_type>(this->CTest->GetMaxTestNameWidth())
  843. != max)
  844. {
  845. this->CTest->SetMaxTestNameWidth(static_cast<int>(max));
  846. }
  847. }
  848. bool cmCTestTestHandler::GetValue(const char* tag,
  849. int& value,
  850. std::istream& fin)
  851. {
  852. std::string line;
  853. bool ret = true;
  854. cmSystemTools::GetLineFromStream(fin, line);
  855. if(line == tag)
  856. {
  857. fin >> value;
  858. ret = cmSystemTools::GetLineFromStream(fin, line); // read blank line
  859. }
  860. else
  861. {
  862. cmCTestLog(this->CTest, ERROR_MESSAGE,
  863. "parse error: missing tag: "
  864. << tag << " found [" << line << "]" << std::endl);
  865. ret = false;
  866. }
  867. return ret;
  868. }
  869. bool cmCTestTestHandler::GetValue(const char* tag,
  870. double& value,
  871. std::istream& fin)
  872. {
  873. std::string line;
  874. cmSystemTools::GetLineFromStream(fin, line);
  875. bool ret = true;
  876. if(line == tag)
  877. {
  878. fin >> value;
  879. ret = cmSystemTools::GetLineFromStream(fin, line); // read blank line
  880. }
  881. else
  882. {
  883. cmCTestLog(this->CTest, ERROR_MESSAGE,
  884. "parse error: missing tag: "
  885. << tag << " found [" << line << "]" << std::endl);
  886. ret = false;
  887. }
  888. return ret;
  889. }
  890. bool cmCTestTestHandler::GetValue(const char* tag,
  891. bool& value,
  892. std::istream& fin)
  893. {
  894. std::string line;
  895. cmSystemTools::GetLineFromStream(fin, line);
  896. bool ret = true;
  897. if(line == tag)
  898. {
  899. #ifdef __HAIKU__
  900. int tmp = 0;
  901. fin >> tmp;
  902. value = false;
  903. if(tmp)
  904. {
  905. value = true;
  906. }
  907. #else
  908. fin >> value;
  909. #endif
  910. ret = cmSystemTools::GetLineFromStream(fin, line); // read blank line
  911. }
  912. else
  913. {
  914. cmCTestLog(this->CTest, ERROR_MESSAGE,
  915. "parse error: missing tag: "
  916. << tag << " found [" << line << "]" << std::endl);
  917. ret = false;
  918. }
  919. return ret;
  920. }
  921. bool cmCTestTestHandler::GetValue(const char* tag,
  922. size_t& value,
  923. std::istream& fin)
  924. {
  925. std::string line;
  926. cmSystemTools::GetLineFromStream(fin, line);
  927. bool ret = true;
  928. if(line == tag)
  929. {
  930. fin >> value;
  931. ret = cmSystemTools::GetLineFromStream(fin, line); // read blank line
  932. }
  933. else
  934. {
  935. cmCTestLog(this->CTest, ERROR_MESSAGE,
  936. "parse error: missing tag: "
  937. << tag << " found [" << line << "]" << std::endl);
  938. ret = false;
  939. }
  940. return ret;
  941. }
  942. bool cmCTestTestHandler::GetValue(const char* tag,
  943. std::string& value,
  944. std::istream& fin)
  945. {
  946. std::string line;
  947. cmSystemTools::GetLineFromStream(fin, line);
  948. bool ret = true;
  949. if(line == tag)
  950. {
  951. ret = cmSystemTools::GetLineFromStream(fin, value);
  952. }
  953. else
  954. {
  955. cmCTestLog(this->CTest, ERROR_MESSAGE,
  956. "parse error: missing tag: "
  957. << tag << " found [" << line << "]" << std::endl);
  958. ret = false;
  959. }
  960. return ret;
  961. }
  962. //---------------------------------------------------------------------
  963. void cmCTestTestHandler::ProcessDirectory(std::vector<std::string> &passed,
  964. std::vector<std::string> &failed)
  965. {
  966. this->ComputeTestList();
  967. this->StartTest = this->CTest->CurrentTime();
  968. this->StartTestTime = static_cast<unsigned int>(cmSystemTools::GetTime());
  969. double elapsed_time_start = cmSystemTools::GetTime();
  970. cmCTestMultiProcessHandler* parallel = this->CTest->GetBatchJobs() ?
  971. new cmCTestBatchTestHandler : new cmCTestMultiProcessHandler;
  972. parallel->SetCTest(this->CTest);
  973. parallel->SetParallelLevel(this->CTest->GetParallelLevel());
  974. parallel->SetTestHandler(this);
  975. parallel->SetQuiet(this->Quiet);
  976. if(this->TestLoad > 0)
  977. {
  978. parallel->SetTestLoad(this->TestLoad);
  979. }
  980. else
  981. {
  982. parallel->SetTestLoad(this->CTest->GetTestLoad());
  983. }
  984. *this->LogFile << "Start testing: "
  985. << this->CTest->CurrentTime() << std::endl
  986. << "----------------------------------------------------------"
  987. << std::endl;
  988. cmCTestMultiProcessHandler::TestMap tests;
  989. cmCTestMultiProcessHandler::PropertiesMap properties;
  990. bool randomSchedule = this->CTest->GetScheduleType() == "Random";
  991. if(randomSchedule)
  992. {
  993. srand((unsigned)time(0));
  994. }
  995. for (ListOfTests::iterator it = this->TestList.begin();
  996. it != this->TestList.end(); ++it)
  997. {
  998. cmCTestTestProperties& p = *it;
  999. cmCTestMultiProcessHandler::TestSet depends;
  1000. if(randomSchedule)
  1001. {
  1002. p.Cost = static_cast<float>(rand());
  1003. }
  1004. if(p.Timeout == 0 && this->CTest->GetGlobalTimeout() != 0)
  1005. {
  1006. p.Timeout = this->CTest->GetGlobalTimeout();
  1007. }
  1008. if(!p.Depends.empty())
  1009. {
  1010. for(std::vector<std::string>::iterator i = p.Depends.begin();
  1011. i != p.Depends.end(); ++i)
  1012. {
  1013. for(ListOfTests::iterator it2 = this->TestList.begin();
  1014. it2 != this->TestList.end(); ++it2)
  1015. {
  1016. if(it2->Name == *i)
  1017. {
  1018. depends.insert(it2->Index);
  1019. break; // break out of test loop as name can only match 1
  1020. }
  1021. }
  1022. }
  1023. }
  1024. tests[it->Index] = depends;
  1025. properties[it->Index] = &*it;
  1026. }
  1027. parallel->SetTests(tests, properties);
  1028. parallel->SetPassFailVectors(&passed, &failed);
  1029. this->TestResults.clear();
  1030. parallel->SetTestResults(&this->TestResults);
  1031. if(this->CTest->ShouldPrintLabels())
  1032. {
  1033. parallel->PrintLabels();
  1034. }
  1035. else if(this->CTest->GetShowOnly())
  1036. {
  1037. parallel->PrintTestList();
  1038. }
  1039. else
  1040. {
  1041. parallel->RunTests();
  1042. }
  1043. delete parallel;
  1044. this->EndTest = this->CTest->CurrentTime();
  1045. this->EndTestTime = static_cast<unsigned int>(cmSystemTools::GetTime());
  1046. this->ElapsedTestingTime = cmSystemTools::GetTime() - elapsed_time_start;
  1047. *this->LogFile << "End testing: "
  1048. << this->CTest->CurrentTime() << std::endl;
  1049. }
  1050. //----------------------------------------------------------------------
  1051. void cmCTestTestHandler::GenerateTestCommand(std::vector<std::string>&, int)
  1052. {
  1053. }
  1054. //----------------------------------------------------------------------
  1055. void cmCTestTestHandler::GenerateDartOutput(cmXMLWriter& xml)
  1056. {
  1057. if ( !this->CTest->GetProduceXML() )
  1058. {
  1059. return;
  1060. }
  1061. this->CTest->StartXML(xml, this->AppendXML);
  1062. xml.StartElement("Testing");
  1063. xml.Element("StartDateTime", this->StartTest);
  1064. xml.Element("StartTestTime", this->StartTestTime);
  1065. xml.StartElement("TestList");
  1066. cmCTestTestHandler::TestResultsVector::size_type cc;
  1067. for ( cc = 0; cc < this->TestResults.size(); cc ++ )
  1068. {
  1069. cmCTestTestResult *result = &this->TestResults[cc];
  1070. std::string testPath = result->Path + "/" + result->Name;
  1071. xml.Element("Test", this->CTest->GetShortPathToFile(testPath.c_str()));
  1072. }
  1073. xml.EndElement(); // TestList
  1074. for ( cc = 0; cc < this->TestResults.size(); cc ++ )
  1075. {
  1076. cmCTestTestResult *result = &this->TestResults[cc];
  1077. this->WriteTestResultHeader(xml, result);
  1078. xml.StartElement("Results");
  1079. if ( result->Status != cmCTestTestHandler::NOT_RUN )
  1080. {
  1081. if ( result->Status != cmCTestTestHandler::COMPLETED ||
  1082. result->ReturnValue )
  1083. {
  1084. xml.StartElement("NamedMeasurement");
  1085. xml.Attribute("type", "text/string");
  1086. xml.Attribute("name", "Exit Code");
  1087. xml.Element("Value", this->GetTestStatus(result->Status));
  1088. xml.EndElement(); // NamedMeasurement
  1089. xml.StartElement("NamedMeasurement");
  1090. xml.Attribute("type", "text/string");
  1091. xml.Attribute("name", "Exit Value");
  1092. xml.Element("Value", result->ReturnValue);
  1093. xml.EndElement(); // NamedMeasurement
  1094. }
  1095. this->GenerateRegressionImages(xml, result->DartString);
  1096. xml.StartElement("NamedMeasurement");
  1097. xml.Attribute("type", "numeric/double");
  1098. xml.Attribute("name", "Execution Time");
  1099. xml.Element("Value", result->ExecutionTime);
  1100. xml.EndElement(); // NamedMeasurement
  1101. if(!result->Reason.empty())
  1102. {
  1103. const char* reasonType = "Pass Reason";
  1104. if(result->Status != cmCTestTestHandler::COMPLETED &&
  1105. result->Status != cmCTestTestHandler::NOT_RUN)
  1106. {
  1107. reasonType = "Fail Reason";
  1108. }
  1109. xml.StartElement("NamedMeasurement");
  1110. xml.Attribute("type", "text/string");
  1111. xml.Attribute("name", reasonType);
  1112. xml.Element("Value", result->Reason);
  1113. xml.EndElement(); // NamedMeasurement
  1114. }
  1115. xml.StartElement("NamedMeasurement");
  1116. xml.Attribute("type", "text/string");
  1117. xml.Attribute("name", "Completion Status");
  1118. xml.Element("Value", result->CompletionStatus);
  1119. xml.EndElement(); // NamedMeasurement
  1120. }
  1121. xml.StartElement("NamedMeasurement");
  1122. xml.Attribute("type", "text/string");
  1123. xml.Attribute("name", "Command Line");
  1124. xml.Element("Value", result->FullCommandLine);
  1125. xml.EndElement(); // NamedMeasurement
  1126. std::map<std::string,std::string>::iterator measureIt;
  1127. for ( measureIt = result->Properties->Measurements.begin();
  1128. measureIt != result->Properties->Measurements.end();
  1129. ++ measureIt )
  1130. {
  1131. xml.StartElement("NamedMeasurement");
  1132. xml.Attribute("type", "text/string");
  1133. xml.Attribute("name", measureIt->first);
  1134. xml.Element("Value", measureIt->second);
  1135. xml.EndElement(); // NamedMeasurement
  1136. }
  1137. xml.StartElement("Measurement");
  1138. xml.StartElement("Value");
  1139. if (result->CompressOutput)
  1140. {
  1141. xml.Attribute("encoding", "base64");
  1142. xml.Attribute("compression", "gzip");
  1143. }
  1144. xml.Content(result->Output);
  1145. xml.EndElement(); // Value
  1146. xml.EndElement(); // Measurement
  1147. xml.EndElement(); // Results
  1148. this->AttachFiles(xml, result);
  1149. this->WriteTestResultFooter(xml, result);
  1150. }
  1151. xml.Element("EndDateTime", this->EndTest);
  1152. xml.Element("EndTestTime", this->EndTestTime);
  1153. xml.Element("ElapsedMinutes",
  1154. static_cast<int>(this->ElapsedTestingTime/6)/10.0);
  1155. xml.EndElement(); // Testing
  1156. this->CTest->EndXML(xml);
  1157. }
  1158. //----------------------------------------------------------------------------
  1159. void cmCTestTestHandler::WriteTestResultHeader(cmXMLWriter& xml,
  1160. cmCTestTestResult* result)
  1161. {
  1162. xml.StartElement("Test");
  1163. if ( result->Status == cmCTestTestHandler::COMPLETED )
  1164. {
  1165. xml.Attribute("Status", "passed");
  1166. }
  1167. else if ( result->Status == cmCTestTestHandler::NOT_RUN )
  1168. {
  1169. xml.Attribute("Status", "notrun");
  1170. }
  1171. else
  1172. {
  1173. xml.Attribute("Status", "failed");
  1174. }
  1175. std::string testPath = result->Path + "/" + result->Name;
  1176. xml.Element("Name", result->Name);
  1177. xml.Element("Path", this->CTest->GetShortPathToFile(result->Path.c_str()));
  1178. xml.Element("FullName", this->CTest->GetShortPathToFile(testPath.c_str()));
  1179. xml.Element("FullCommandLine", result->FullCommandLine);
  1180. }
  1181. //----------------------------------------------------------------------------
  1182. void cmCTestTestHandler::WriteTestResultFooter(cmXMLWriter& xml,
  1183. cmCTestTestResult* result)
  1184. {
  1185. if(!result->Properties->Labels.empty())
  1186. {
  1187. xml.StartElement("Labels");
  1188. std::vector<std::string> const& labels = result->Properties->Labels;
  1189. for(std::vector<std::string>::const_iterator li = labels.begin();
  1190. li != labels.end(); ++li)
  1191. {
  1192. xml.Element("Label", *li);
  1193. }
  1194. xml.EndElement(); // Labels
  1195. }
  1196. xml.EndElement(); // Test
  1197. }
  1198. //----------------------------------------------------------------------
  1199. void cmCTestTestHandler::AttachFiles(cmXMLWriter& xml,
  1200. cmCTestTestResult* result)
  1201. {
  1202. if(result->Status != cmCTestTestHandler::COMPLETED
  1203. && result->Properties->AttachOnFail.size())
  1204. {
  1205. result->Properties->AttachedFiles.insert(
  1206. result->Properties->AttachedFiles.end(),
  1207. result->Properties->AttachOnFail.begin(),
  1208. result->Properties->AttachOnFail.end());
  1209. }
  1210. for(std::vector<std::string>::const_iterator file =
  1211. result->Properties->AttachedFiles.begin();
  1212. file != result->Properties->AttachedFiles.end(); ++file)
  1213. {
  1214. const std::string &base64 = this->CTest->Base64GzipEncodeFile(*file);
  1215. std::string fname = cmSystemTools::GetFilenameName(*file);
  1216. xml.StartElement("NamedMeasurement");
  1217. xml.Attribute("name", "Attached File");
  1218. xml.Attribute("encoding", "base64");
  1219. xml.Attribute("compression", "tar/gzip");
  1220. xml.Attribute("filename", fname);
  1221. xml.Attribute("type", "file");
  1222. xml.Element("Value", base64);
  1223. xml.EndElement(); // NamedMeasurement
  1224. }
  1225. }
  1226. //----------------------------------------------------------------------
  1227. int cmCTestTestHandler::ExecuteCommands(std::vector<std::string>& vec)
  1228. {
  1229. std::vector<std::string>::iterator it;
  1230. for ( it = vec.begin(); it != vec.end(); ++it )
  1231. {
  1232. int retVal = 0;
  1233. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run command: " <<
  1234. *it << std::endl, this->Quiet);
  1235. if ( !cmSystemTools::RunSingleCommand(it->c_str(), 0, 0, &retVal, 0,
  1236. cmSystemTools::OUTPUT_MERGE
  1237. /*this->Verbose*/) || retVal != 0 )
  1238. {
  1239. cmCTestLog(this->CTest, ERROR_MESSAGE, "Problem running command: "
  1240. << *it << std::endl);
  1241. return 0;
  1242. }
  1243. }
  1244. return 1;
  1245. }
  1246. //----------------------------------------------------------------------
  1247. // Find the appropriate executable to run for a test
  1248. std::string cmCTestTestHandler::FindTheExecutable(const char *exe)
  1249. {
  1250. std::string resConfig;
  1251. std::vector<std::string> extraPaths;
  1252. std::vector<std::string> failedPaths;
  1253. if(strcmp(exe, "NOT_AVAILABLE") == 0)
  1254. {
  1255. return exe;
  1256. }
  1257. return cmCTestTestHandler::FindExecutable(this->CTest,
  1258. exe, resConfig,
  1259. extraPaths,
  1260. failedPaths);
  1261. }
  1262. // add additional configurations to the search path
  1263. void cmCTestTestHandler
  1264. ::AddConfigurations(cmCTest *ctest,
  1265. std::vector<std::string> &attempted,
  1266. std::vector<std::string> &attemptedConfigs,
  1267. std::string filepath,
  1268. std::string &filename)
  1269. {
  1270. std::string tempPath;
  1271. if (!filepath.empty() &&
  1272. filepath[filepath.size()-1] != '/')
  1273. {
  1274. filepath += "/";
  1275. }
  1276. tempPath = filepath + filename;
  1277. attempted.push_back(tempPath);
  1278. attemptedConfigs.push_back("");
  1279. if(!ctest->GetConfigType().empty())
  1280. {
  1281. tempPath = filepath;
  1282. tempPath += ctest->GetConfigType();
  1283. tempPath += "/";
  1284. tempPath += filename;
  1285. attempted.push_back(tempPath);
  1286. attemptedConfigs.push_back(ctest->GetConfigType());
  1287. // If the file is an OSX bundle then the configtype
  1288. // will be at the start of the path
  1289. tempPath = ctest->GetConfigType();
  1290. tempPath += "/";
  1291. tempPath += filepath;
  1292. tempPath += filename;
  1293. attempted.push_back(tempPath);
  1294. attemptedConfigs.push_back(ctest->GetConfigType());
  1295. }
  1296. else
  1297. {
  1298. // no config specified - try some options...
  1299. tempPath = filepath;
  1300. tempPath += "Release/";
  1301. tempPath += filename;
  1302. attempted.push_back(tempPath);
  1303. attemptedConfigs.push_back("Release");
  1304. tempPath = filepath;
  1305. tempPath += "Debug/";
  1306. tempPath += filename;
  1307. attempted.push_back(tempPath);
  1308. attemptedConfigs.push_back("Debug");
  1309. tempPath = filepath;
  1310. tempPath += "MinSizeRel/";
  1311. tempPath += filename;
  1312. attempted.push_back(tempPath);
  1313. attemptedConfigs.push_back("MinSizeRel");
  1314. tempPath = filepath;
  1315. tempPath += "RelWithDebInfo/";
  1316. tempPath += filename;
  1317. attempted.push_back(tempPath);
  1318. attemptedConfigs.push_back("RelWithDebInfo");
  1319. tempPath = filepath;
  1320. tempPath += "Deployment/";
  1321. tempPath += filename;
  1322. attempted.push_back(tempPath);
  1323. attemptedConfigs.push_back("Deployment");
  1324. tempPath = filepath;
  1325. tempPath += "Development/";
  1326. tempPath += filename;
  1327. attempted.push_back(tempPath);
  1328. attemptedConfigs.push_back("Deployment");
  1329. }
  1330. }
  1331. //----------------------------------------------------------------------
  1332. // Find the appropriate executable to run for a test
  1333. std::string cmCTestTestHandler
  1334. ::FindExecutable(cmCTest *ctest,
  1335. const char *testCommand,
  1336. std::string &resultingConfig,
  1337. std::vector<std::string> &extraPaths,
  1338. std::vector<std::string> &failed)
  1339. {
  1340. // now run the compiled test if we can find it
  1341. std::vector<std::string> attempted;
  1342. std::vector<std::string> attemptedConfigs;
  1343. std::string tempPath;
  1344. std::string filepath =
  1345. cmSystemTools::GetFilenamePath(testCommand);
  1346. std::string filename =
  1347. cmSystemTools::GetFilenameName(testCommand);
  1348. cmCTestTestHandler::AddConfigurations(ctest, attempted,
  1349. attemptedConfigs,
  1350. filepath,filename);
  1351. // even if a fullpath was specified also try it relative to the current
  1352. // directory
  1353. if (!filepath.empty() && filepath[0] == '/')
  1354. {
  1355. std::string localfilepath = filepath.substr(1,filepath.size()-1);
  1356. cmCTestTestHandler::AddConfigurations(ctest, attempted,
  1357. attemptedConfigs,
  1358. localfilepath,filename);
  1359. }
  1360. // if extraPaths are provided and we were not passed a full path, try them,
  1361. // try any extra paths
  1362. if (filepath.empty())
  1363. {
  1364. for (unsigned int i = 0; i < extraPaths.size(); ++i)
  1365. {
  1366. std::string filepathExtra =
  1367. cmSystemTools::GetFilenamePath(extraPaths[i]);
  1368. std::string filenameExtra =
  1369. cmSystemTools::GetFilenameName(extraPaths[i]);
  1370. cmCTestTestHandler::AddConfigurations(ctest,attempted,
  1371. attemptedConfigs,
  1372. filepathExtra,
  1373. filenameExtra);
  1374. }
  1375. }
  1376. // store the final location in fullPath
  1377. std::string fullPath;
  1378. // now look in the paths we specified above
  1379. for(unsigned int ai=0;
  1380. ai < attempted.size() && fullPath.empty(); ++ai)
  1381. {
  1382. // first check without exe extension
  1383. if(cmSystemTools::FileExists(attempted[ai].c_str())
  1384. && !cmSystemTools::FileIsDirectory(attempted[ai]))
  1385. {
  1386. fullPath = cmSystemTools::CollapseFullPath(attempted[ai]);
  1387. resultingConfig = attemptedConfigs[ai];
  1388. }
  1389. // then try with the exe extension
  1390. else
  1391. {
  1392. failed.push_back(attempted[ai]);
  1393. tempPath = attempted[ai];
  1394. tempPath += cmSystemTools::GetExecutableExtension();
  1395. if(cmSystemTools::FileExists(tempPath.c_str())
  1396. && !cmSystemTools::FileIsDirectory(tempPath))
  1397. {
  1398. fullPath = cmSystemTools::CollapseFullPath(tempPath);
  1399. resultingConfig = attemptedConfigs[ai];
  1400. }
  1401. else
  1402. {
  1403. failed.push_back(tempPath);
  1404. }
  1405. }
  1406. }
  1407. // if everything else failed, check the users path, but only if a full path
  1408. // wasn't specified
  1409. if (fullPath.empty() && filepath.empty())
  1410. {
  1411. std::string path = cmSystemTools::FindProgram(filename.c_str());
  1412. if (path != "")
  1413. {
  1414. resultingConfig = "";
  1415. return path;
  1416. }
  1417. }
  1418. if(fullPath.empty())
  1419. {
  1420. cmCTestLog(ctest, HANDLER_OUTPUT,
  1421. "Could not find executable " << testCommand << "\n"
  1422. << "Looked in the following places:\n");
  1423. for(std::vector<std::string>::iterator i = failed.begin();
  1424. i != failed.end(); ++i)
  1425. {
  1426. cmCTestLog(ctest, HANDLER_OUTPUT, i->c_str() << "\n");
  1427. }
  1428. }
  1429. return fullPath;
  1430. }
  1431. //----------------------------------------------------------------------
  1432. void cmCTestTestHandler::GetListOfTests()
  1433. {
  1434. if ( !this->IncludeLabelRegExp.empty() )
  1435. {
  1436. this->IncludeLabelRegularExpression.
  1437. compile(this->IncludeLabelRegExp.c_str());
  1438. }
  1439. if ( !this->ExcludeLabelRegExp.empty() )
  1440. {
  1441. this->ExcludeLabelRegularExpression.
  1442. compile(this->ExcludeLabelRegExp.c_str());
  1443. }
  1444. if ( !this->IncludeRegExp.empty() )
  1445. {
  1446. this->IncludeTestsRegularExpression.compile(this->IncludeRegExp.c_str());
  1447. }
  1448. if ( !this->ExcludeRegExp.empty() )
  1449. {
  1450. this->ExcludeTestsRegularExpression.compile(this->ExcludeRegExp.c_str());
  1451. }
  1452. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1453. "Constructing a list of tests" << std::endl, this->Quiet);
  1454. cmake cm;
  1455. cm.SetHomeDirectory("");
  1456. cm.SetHomeOutputDirectory("");
  1457. cm.GetCurrentSnapshot().SetDefaultDefinitions();
  1458. cmGlobalGenerator gg(&cm);
  1459. cmsys::auto_ptr<cmMakefile> mf(new cmMakefile(&gg, cm.GetCurrentSnapshot()));
  1460. mf->AddDefinition("CTEST_CONFIGURATION_TYPE",
  1461. this->CTest->GetConfigType().c_str());
  1462. // Add handler for ADD_TEST
  1463. cmCTestAddTestCommand* newCom1 = new cmCTestAddTestCommand;
  1464. newCom1->TestHandler = this;
  1465. cm.GetState()->AddCommand(newCom1);
  1466. // Add handler for SUBDIRS
  1467. cmCTestSubdirCommand* newCom2 =
  1468. new cmCTestSubdirCommand;
  1469. newCom2->TestHandler = this;
  1470. cm.GetState()->AddCommand(newCom2);
  1471. // Add handler for ADD_SUBDIRECTORY
  1472. cmCTestAddSubdirectoryCommand* newCom3 =
  1473. new cmCTestAddSubdirectoryCommand;
  1474. newCom3->TestHandler = this;
  1475. cm.GetState()->AddCommand(newCom3);
  1476. // Add handler for SET_SOURCE_FILES_PROPERTIES
  1477. cmCTestSetTestsPropertiesCommand* newCom4
  1478. = new cmCTestSetTestsPropertiesCommand;
  1479. newCom4->TestHandler = this;
  1480. cm.GetState()->AddCommand(newCom4);
  1481. const char* testFilename;
  1482. if( cmSystemTools::FileExists("CTestTestfile.cmake") )
  1483. {
  1484. // does the CTestTestfile.cmake exist ?
  1485. testFilename = "CTestTestfile.cmake";
  1486. }
  1487. else if( cmSystemTools::FileExists("DartTestfile.txt") )
  1488. {
  1489. // does the DartTestfile.txt exist ?
  1490. testFilename = "DartTestfile.txt";
  1491. }
  1492. else
  1493. {
  1494. return;
  1495. }
  1496. if ( !mf->ReadListFile(testFilename) )
  1497. {
  1498. return;
  1499. }
  1500. if ( cmSystemTools::GetErrorOccuredFlag() )
  1501. {
  1502. return;
  1503. }
  1504. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1505. "Done constructing a list of tests" << std::endl, this->Quiet);
  1506. }
  1507. //----------------------------------------------------------------------
  1508. void cmCTestTestHandler::UseIncludeRegExp()
  1509. {
  1510. this->UseIncludeRegExpFlag = true;
  1511. }
  1512. //----------------------------------------------------------------------
  1513. void cmCTestTestHandler::UseExcludeRegExp()
  1514. {
  1515. this->UseExcludeRegExpFlag = true;
  1516. this->UseExcludeRegExpFirst = this->UseIncludeRegExpFlag ? false : true;
  1517. }
  1518. //----------------------------------------------------------------------
  1519. const char* cmCTestTestHandler::GetTestStatus(int status)
  1520. {
  1521. static const char statuses[][100] = {
  1522. "Not Run",
  1523. "Timeout",
  1524. "SEGFAULT",
  1525. "ILLEGAL",
  1526. "INTERRUPT",
  1527. "NUMERICAL",
  1528. "OTHER_FAULT",
  1529. "Failed",
  1530. "BAD_COMMAND",
  1531. "Completed"
  1532. };
  1533. if ( status < cmCTestTestHandler::NOT_RUN ||
  1534. status > cmCTestTestHandler::COMPLETED )
  1535. {
  1536. return "No Status";
  1537. }
  1538. return statuses[status];
  1539. }
  1540. //----------------------------------------------------------------------
  1541. void cmCTestTestHandler::ExpandTestsToRunInformation(size_t numTests)
  1542. {
  1543. if (this->TestsToRunString.empty())
  1544. {
  1545. return;
  1546. }
  1547. int start;
  1548. int end = -1;
  1549. double stride = -1;
  1550. std::string::size_type pos = 0;
  1551. std::string::size_type pos2;
  1552. // read start
  1553. if(GetNextNumber(this->TestsToRunString, start, pos, pos2))
  1554. {
  1555. // read end
  1556. if(GetNextNumber(this->TestsToRunString, end, pos, pos2))
  1557. {
  1558. // read stride
  1559. if(GetNextRealNumber(this->TestsToRunString, stride, pos, pos2))
  1560. {
  1561. int val =0;
  1562. // now read specific numbers
  1563. while(GetNextNumber(this->TestsToRunString, val, pos, pos2))
  1564. {
  1565. this->TestsToRun.push_back(val);
  1566. }
  1567. this->TestsToRun.push_back(val);
  1568. }
  1569. }
  1570. }
  1571. // if start is not specified then we assume we start at 1
  1572. if(start == -1)
  1573. {
  1574. start = 1;
  1575. }
  1576. // if end isnot specified then we assume we end with the last test
  1577. if(end == -1)
  1578. {
  1579. end = static_cast<int>(numTests);
  1580. }
  1581. // if the stride wasn't specified then it defaults to 1
  1582. if(stride == -1)
  1583. {
  1584. stride = 1;
  1585. }
  1586. // if we have a range then add it
  1587. if(end != -1 && start != -1 && stride > 0)
  1588. {
  1589. int i = 0;
  1590. while (i*stride + start <= end)
  1591. {
  1592. this->TestsToRun.push_back(static_cast<int>(i*stride+start));
  1593. ++i;
  1594. }
  1595. }
  1596. // sort the array
  1597. std::sort(this->TestsToRun.begin(), this->TestsToRun.end(),
  1598. std::less<int>());
  1599. // remove duplicates
  1600. std::vector<int>::iterator new_end =
  1601. std::unique(this->TestsToRun.begin(), this->TestsToRun.end());
  1602. this->TestsToRun.erase(new_end, this->TestsToRun.end());
  1603. }
  1604. void cmCTestTestHandler::ExpandTestsToRunInformationForRerunFailed()
  1605. {
  1606. std::string dirName = this->CTest->GetBinaryDir() + "/Testing/Temporary";
  1607. cmsys::Directory directory;
  1608. if (directory.Load(dirName) == 0)
  1609. {
  1610. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unable to read the contents of "
  1611. << dirName << std::endl);
  1612. return;
  1613. }
  1614. int numFiles = static_cast<int>
  1615. (cmsys::Directory::GetNumberOfFilesInDirectory(dirName));
  1616. std::string pattern = "LastTestsFailed";
  1617. std::string logName = "";
  1618. for (int i = 0; i < numFiles; ++i)
  1619. {
  1620. std::string fileName = directory.GetFile(i);
  1621. // bcc crashes if we attempt a normal substring comparison,
  1622. // hence the following workaround
  1623. std::string fileNameSubstring = fileName.substr(0, pattern.length());
  1624. if (fileNameSubstring.compare(pattern) != 0)
  1625. {
  1626. continue;
  1627. }
  1628. if (logName == "")
  1629. {
  1630. logName = fileName;
  1631. }
  1632. else
  1633. {
  1634. // if multiple matching logs were found we use the most recently
  1635. // modified one.
  1636. int res;
  1637. cmSystemTools::FileTimeCompare(logName, fileName, &res);
  1638. if (res == -1)
  1639. {
  1640. logName = fileName;
  1641. }
  1642. }
  1643. }
  1644. std::string lastTestsFailedLog = this->CTest->GetBinaryDir()
  1645. + "/Testing/Temporary/" + logName;
  1646. if ( !cmSystemTools::FileExists(lastTestsFailedLog.c_str()) )
  1647. {
  1648. if ( !this->CTest->GetShowOnly() && !this->CTest->ShouldPrintLabels() )
  1649. {
  1650. cmCTestLog(this->CTest, ERROR_MESSAGE, lastTestsFailedLog
  1651. << " does not exist!" << std::endl);
  1652. }
  1653. return;
  1654. }
  1655. // parse the list of tests to rerun from LastTestsFailed.log
  1656. cmsys::ifstream ifs(lastTestsFailedLog.c_str());
  1657. if ( ifs )
  1658. {
  1659. std::string line;
  1660. std::string::size_type pos;
  1661. while ( cmSystemTools::GetLineFromStream(ifs, line) )
  1662. {
  1663. pos = line.find(':', 0);
  1664. if (pos == line.npos)
  1665. {
  1666. continue;
  1667. }
  1668. int val = atoi(line.substr(0, pos).c_str());
  1669. this->TestsToRun.push_back(val);
  1670. }
  1671. ifs.close();
  1672. }
  1673. else if ( !this->CTest->GetShowOnly() && !this->CTest->ShouldPrintLabels() )
  1674. {
  1675. cmCTestLog(this->CTest, ERROR_MESSAGE, "Problem reading file: "
  1676. << lastTestsFailedLog <<
  1677. " while generating list of previously failed tests." << std::endl);
  1678. }
  1679. }
  1680. //----------------------------------------------------------------------
  1681. // Just for convenience
  1682. #define SPACE_REGEX "[ \t\r\n]"
  1683. //----------------------------------------------------------------------
  1684. void cmCTestTestHandler::GenerateRegressionImages(
  1685. cmXMLWriter& xml, const std::string& dart)
  1686. {
  1687. cmsys::RegularExpression twoattributes(
  1688. "<DartMeasurement"
  1689. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1690. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1691. SPACE_REGEX "*>([^<]*)</DartMeasurement>");
  1692. cmsys::RegularExpression threeattributes(
  1693. "<DartMeasurement"
  1694. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1695. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1696. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1697. SPACE_REGEX "*>([^<]*)</DartMeasurement>");
  1698. cmsys::RegularExpression fourattributes(
  1699. "<DartMeasurement"
  1700. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1701. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1702. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1703. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1704. SPACE_REGEX "*>([^<]*)</DartMeasurement>");
  1705. cmsys::RegularExpression cdatastart(
  1706. "<DartMeasurement"
  1707. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1708. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1709. SPACE_REGEX "*>"
  1710. SPACE_REGEX "*<!\\[CDATA\\[");
  1711. cmsys::RegularExpression cdataend(
  1712. "]]>"
  1713. SPACE_REGEX "*</DartMeasurement>");
  1714. cmsys::RegularExpression measurementfile(
  1715. "<DartMeasurementFile"
  1716. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1717. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1718. SPACE_REGEX "*>([^<]*)</DartMeasurementFile>");
  1719. bool done = false;
  1720. std::string cxml = dart;
  1721. while ( ! done )
  1722. {
  1723. if ( twoattributes.find(cxml) )
  1724. {
  1725. xml.StartElement("NamedMeasurement");
  1726. xml.Attribute(twoattributes.match(1).c_str(),
  1727. twoattributes.match(2));
  1728. xml.Attribute(twoattributes.match(3).c_str(),
  1729. twoattributes.match(4));
  1730. xml.Element("Value", twoattributes.match(5));
  1731. xml.EndElement();
  1732. cxml.erase(twoattributes.start(),
  1733. twoattributes.end() - twoattributes.start());
  1734. }
  1735. else if ( threeattributes.find(cxml) )
  1736. {
  1737. xml.StartElement("NamedMeasurement");
  1738. xml.Attribute(threeattributes.match(1).c_str(),
  1739. threeattributes.match(2));
  1740. xml.Attribute(threeattributes.match(3).c_str(),
  1741. threeattributes.match(4));
  1742. xml.Attribute(threeattributes.match(5).c_str(),
  1743. threeattributes.match(6));
  1744. xml.Element("Value", twoattributes.match(7));
  1745. xml.EndElement();
  1746. cxml.erase(threeattributes.start(),
  1747. threeattributes.end() - threeattributes.start());
  1748. }
  1749. else if ( fourattributes.find(cxml) )
  1750. {
  1751. xml.StartElement("NamedMeasurement");
  1752. xml.Attribute(fourattributes.match(1).c_str(),
  1753. fourattributes.match(2));
  1754. xml.Attribute(fourattributes.match(3).c_str(),
  1755. fourattributes.match(4));
  1756. xml.Attribute(fourattributes.match(5).c_str(),
  1757. fourattributes.match(6));
  1758. xml.Attribute(fourattributes.match(7).c_str(),
  1759. fourattributes.match(8));
  1760. xml.Element("Value", twoattributes.match(9));
  1761. xml.EndElement();
  1762. cxml.erase(fourattributes.start(),
  1763. fourattributes.end() - fourattributes.start());
  1764. }
  1765. else if ( cdatastart.find(cxml) && cdataend.find(cxml) )
  1766. {
  1767. xml.StartElement("NamedMeasurement");
  1768. xml.Attribute(cdatastart.match(1).c_str(), cdatastart.match(2));
  1769. xml.Attribute(cdatastart.match(3).c_str(), cdatastart.match(4));
  1770. xml.StartElement("Value");
  1771. xml.CData(
  1772. cxml.substr(cdatastart.end(), cdataend.start() - cdatastart.end()));
  1773. xml.EndElement(); // Value
  1774. xml.EndElement(); // NamedMeasurement
  1775. cxml.erase(cdatastart.start(),
  1776. cdataend.end() - cdatastart.start());
  1777. }
  1778. else if ( measurementfile.find(cxml) )
  1779. {
  1780. const std::string& filename =
  1781. cmCTest::CleanString(measurementfile.match(5));
  1782. if ( cmSystemTools::FileExists(filename.c_str()) )
  1783. {
  1784. long len = cmSystemTools::FileLength(filename);
  1785. if ( len == 0 )
  1786. {
  1787. std::string k1 = measurementfile.match(1);
  1788. std::string v1 = measurementfile.match(2);
  1789. std::string k2 = measurementfile.match(3);
  1790. std::string v2 = measurementfile.match(4);
  1791. if ( cmSystemTools::LowerCase(k1) == "type" )
  1792. {
  1793. v1 = "text/string";
  1794. }
  1795. if ( cmSystemTools::LowerCase(k2) == "type" )
  1796. {
  1797. v2 = "text/string";
  1798. }
  1799. xml.StartElement("NamedMeasurement");
  1800. xml.Attribute(k1.c_str(), v1);
  1801. xml.Attribute(k2.c_str(), v2);
  1802. xml.Attribute("encoding", "none");
  1803. xml.Element("Value", "Image " + filename + " is empty");
  1804. xml.EndElement();
  1805. }
  1806. else
  1807. {
  1808. cmsys::ifstream ifs(filename.c_str(), std::ios::in
  1809. #ifdef _WIN32
  1810. | std::ios::binary
  1811. #endif
  1812. );
  1813. unsigned char *file_buffer = new unsigned char [ len + 1 ];
  1814. ifs.read(reinterpret_cast<char*>(file_buffer), len);
  1815. unsigned char *encoded_buffer
  1816. = new unsigned char [ static_cast<int>(
  1817. static_cast<double>(len) * 1.5 + 5.0) ];
  1818. size_t rlen
  1819. = cmsysBase64_Encode(file_buffer, len, encoded_buffer, 1);
  1820. xml.StartElement("NamedMeasurement");
  1821. xml.Attribute(measurementfile.match(1).c_str(),
  1822. measurementfile.match(2));
  1823. xml.Attribute(measurementfile.match(3).c_str(),
  1824. measurementfile.match(4));
  1825. xml.Attribute("encoding", "base64");
  1826. std::stringstream ostr;
  1827. for (size_t cc = 0; cc < rlen; cc ++ )
  1828. {
  1829. ostr << encoded_buffer[cc];
  1830. if ( cc % 60 == 0 && cc )
  1831. {
  1832. ostr << std::endl;
  1833. }
  1834. }
  1835. xml.Element("Value", ostr.str());
  1836. xml.EndElement(); // NamedMeasurement
  1837. delete [] file_buffer;
  1838. delete [] encoded_buffer;
  1839. }
  1840. }
  1841. else
  1842. {
  1843. int idx = 4;
  1844. if ( measurementfile.match(1) == "name" )
  1845. {
  1846. idx = 2;
  1847. }
  1848. xml.StartElement("NamedMeasurement");
  1849. xml.Attribute("name", measurementfile.match(idx));
  1850. xml.Attribute("text", "text/string");
  1851. xml.Element("Value", "File " + filename + " not found");
  1852. xml.EndElement();
  1853. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "File \"" << filename
  1854. << "\" not found." << std::endl, this->Quiet);
  1855. }
  1856. cxml.erase(measurementfile.start(),
  1857. measurementfile.end() - measurementfile.start());
  1858. }
  1859. else
  1860. {
  1861. done = true;
  1862. }
  1863. }
  1864. }
  1865. //----------------------------------------------------------------------
  1866. void cmCTestTestHandler::SetIncludeRegExp(const char *arg)
  1867. {
  1868. this->IncludeRegExp = arg;
  1869. }
  1870. //----------------------------------------------------------------------
  1871. void cmCTestTestHandler::SetExcludeRegExp(const char *arg)
  1872. {
  1873. this->ExcludeRegExp = arg;
  1874. }
  1875. //----------------------------------------------------------------------
  1876. void cmCTestTestHandler::SetTestsToRunInformation(const char* in)
  1877. {
  1878. if ( !in )
  1879. {
  1880. return;
  1881. }
  1882. this->TestsToRunString = in;
  1883. // if the argument is a file, then read it and use the contents as the
  1884. // string
  1885. if(cmSystemTools::FileExists(in))
  1886. {
  1887. cmsys::ifstream fin(in);
  1888. unsigned long filelen = cmSystemTools::FileLength(in);
  1889. char* buff = new char[filelen+1];
  1890. fin.getline(buff, filelen);
  1891. buff[fin.gcount()] = 0;
  1892. this->TestsToRunString = buff;
  1893. delete [] buff;
  1894. }
  1895. }
  1896. //----------------------------------------------------------------------------
  1897. bool cmCTestTestHandler::CleanTestOutput(std::string& output, size_t length)
  1898. {
  1899. if(!length || length >= output.size() ||
  1900. output.find("CTEST_FULL_OUTPUT") != output.npos)
  1901. {
  1902. return true;
  1903. }
  1904. // Truncate at given length but do not break in the middle of a multi-byte
  1905. // UTF-8 encoding.
  1906. char const* const begin = output.c_str();
  1907. char const* const end = begin + output.size();
  1908. char const* const truncate = begin + length;
  1909. char const* current = begin;
  1910. while(current < truncate)
  1911. {
  1912. unsigned int ch;
  1913. if(const char* next = cm_utf8_decode_character(current, end, &ch))
  1914. {
  1915. if(next > truncate)
  1916. {
  1917. break;
  1918. }
  1919. current = next;
  1920. }
  1921. else // Bad byte will be handled by cmXMLWriter.
  1922. {
  1923. ++current;
  1924. }
  1925. }
  1926. output = output.substr(0, current - begin);
  1927. // Append truncation message.
  1928. std::ostringstream msg;
  1929. msg << "...\n"
  1930. "The rest of the test output was removed since it exceeds the threshold "
  1931. "of " << length << " bytes.\n";
  1932. output += msg.str();
  1933. return true;
  1934. }
  1935. //----------------------------------------------------------------------
  1936. bool cmCTestTestHandler::SetTestsProperties(
  1937. const std::vector<std::string>& args)
  1938. {
  1939. std::vector<std::string>::const_iterator it;
  1940. std::vector<std::string> tests;
  1941. bool found = false;
  1942. for ( it = args.begin(); it != args.end(); ++ it )
  1943. {
  1944. if ( *it == "PROPERTIES" )
  1945. {
  1946. found = true;
  1947. break;
  1948. }
  1949. tests.push_back(*it);
  1950. }
  1951. if ( !found )
  1952. {
  1953. return false;
  1954. }
  1955. ++ it; // skip PROPERTIES
  1956. for ( ; it != args.end(); ++ it )
  1957. {
  1958. std::string key = *it;
  1959. ++ it;
  1960. if ( it == args.end() )
  1961. {
  1962. break;
  1963. }
  1964. std::string val = *it;
  1965. std::vector<std::string>::const_iterator tit;
  1966. for ( tit = tests.begin(); tit != tests.end(); ++ tit )
  1967. {
  1968. cmCTestTestHandler::ListOfTests::iterator rtit;
  1969. for ( rtit = this->TestList.begin();
  1970. rtit != this->TestList.end();
  1971. ++ rtit )
  1972. {
  1973. if ( *tit == rtit->Name )
  1974. {
  1975. if ( key == "WILL_FAIL" )
  1976. {
  1977. rtit->WillFail = cmSystemTools::IsOn(val.c_str());
  1978. }
  1979. if ( key == "ATTACHED_FILES" )
  1980. {
  1981. cmSystemTools::ExpandListArgument(val, rtit->AttachedFiles);
  1982. }
  1983. if ( key == "ATTACHED_FILES_ON_FAIL" )
  1984. {
  1985. cmSystemTools::ExpandListArgument(val, rtit->AttachOnFail);
  1986. }
  1987. if ( key == "RESOURCE_LOCK" )
  1988. {
  1989. std::vector<std::string> lval;
  1990. cmSystemTools::ExpandListArgument(val, lval);
  1991. rtit->LockedResources.insert(lval.begin(), lval.end());
  1992. }
  1993. if ( key == "TIMEOUT" )
  1994. {
  1995. rtit->Timeout = atof(val.c_str());
  1996. rtit->ExplicitTimeout = true;
  1997. }
  1998. if ( key == "COST" )
  1999. {
  2000. rtit->Cost = static_cast<float>(atof(val.c_str()));
  2001. }
  2002. if ( key == "REQUIRED_FILES" )
  2003. {
  2004. cmSystemTools::ExpandListArgument(val, rtit->RequiredFiles);
  2005. }
  2006. if ( key == "RUN_SERIAL" )
  2007. {
  2008. rtit->RunSerial = cmSystemTools::IsOn(val.c_str());
  2009. }
  2010. if ( key == "FAIL_REGULAR_EXPRESSION" )
  2011. {
  2012. std::vector<std::string> lval;
  2013. cmSystemTools::ExpandListArgument(val, lval);
  2014. std::vector<std::string>::iterator crit;
  2015. for ( crit = lval.begin(); crit != lval.end(); ++ crit )
  2016. {
  2017. rtit->ErrorRegularExpressions.push_back(
  2018. std::pair<cmsys::RegularExpression, std::string>(
  2019. cmsys::RegularExpression(crit->c_str()),
  2020. std::string(*crit)));
  2021. }
  2022. }
  2023. if ( key == "PROCESSORS" )
  2024. {
  2025. rtit->Processors = atoi(val.c_str());
  2026. if(rtit->Processors < 1)
  2027. {
  2028. rtit->Processors = 1;
  2029. }
  2030. }
  2031. if ( key == "SKIP_RETURN_CODE" )
  2032. {
  2033. rtit->SkipReturnCode = atoi(val.c_str());
  2034. if(rtit->SkipReturnCode < 0 || rtit->SkipReturnCode > 255)
  2035. {
  2036. rtit->SkipReturnCode = -1;
  2037. }
  2038. }
  2039. if ( key == "DEPENDS" )
  2040. {
  2041. cmSystemTools::ExpandListArgument(val, rtit->Depends);
  2042. }
  2043. if ( key == "ENVIRONMENT" )
  2044. {
  2045. cmSystemTools::ExpandListArgument(val, rtit->Environment);
  2046. }
  2047. if ( key == "LABELS" )
  2048. {
  2049. cmSystemTools::ExpandListArgument(val, rtit->Labels);
  2050. }
  2051. if ( key == "MEASUREMENT" )
  2052. {
  2053. size_t pos = val.find_first_of("=");
  2054. if ( pos != val.npos )
  2055. {
  2056. std::string mKey = val.substr(0, pos);
  2057. const char* mVal = val.c_str() + pos + 1;
  2058. rtit->Measurements[mKey] = mVal;
  2059. }
  2060. else
  2061. {
  2062. rtit->Measurements[val] = "1";
  2063. }
  2064. }
  2065. if ( key == "PASS_REGULAR_EXPRESSION" )
  2066. {
  2067. std::vector<std::string> lval;
  2068. cmSystemTools::ExpandListArgument(val, lval);
  2069. std::vector<std::string>::iterator crit;
  2070. for ( crit = lval.begin(); crit != lval.end(); ++ crit )
  2071. {
  2072. rtit->RequiredRegularExpressions.push_back(
  2073. std::pair<cmsys::RegularExpression, std::string>(
  2074. cmsys::RegularExpression(crit->c_str()),
  2075. std::string(*crit)));
  2076. }
  2077. }
  2078. if ( key == "WORKING_DIRECTORY" )
  2079. {
  2080. rtit->Directory = val;
  2081. }
  2082. }
  2083. }
  2084. }
  2085. }
  2086. return true;
  2087. }
  2088. //----------------------------------------------------------------------
  2089. bool cmCTestTestHandler::AddTest(const std::vector<std::string>& args)
  2090. {
  2091. const std::string& testname = args[0];
  2092. cmCTestOptionalLog(this->CTest, DEBUG, "Add test: " << args[0] << std::endl,
  2093. this->Quiet);
  2094. if (this->UseExcludeRegExpFlag &&
  2095. this->UseExcludeRegExpFirst &&
  2096. this->ExcludeTestsRegularExpression.find(testname.c_str()))
  2097. {
  2098. return true;
  2099. }
  2100. if ( this->MemCheck )
  2101. {
  2102. std::vector<std::string>::iterator it;
  2103. bool found = false;
  2104. for ( it = this->CustomTestsIgnore.begin();
  2105. it != this->CustomTestsIgnore.end(); ++ it )
  2106. {
  2107. if ( *it == testname )
  2108. {
  2109. found = true;
  2110. break;
  2111. }
  2112. }
  2113. if ( found )
  2114. {
  2115. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  2116. "Ignore memcheck: " << *it << std::endl, this->Quiet);
  2117. return true;
  2118. }
  2119. }
  2120. else
  2121. {
  2122. std::vector<std::string>::iterator it;
  2123. bool found = false;
  2124. for ( it = this->CustomTestsIgnore.begin();
  2125. it != this->CustomTestsIgnore.end(); ++ it )
  2126. {
  2127. if ( *it == testname )
  2128. {
  2129. found = true;
  2130. break;
  2131. }
  2132. }
  2133. if ( found )
  2134. {
  2135. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Ignore test: "
  2136. << *it << std::endl, this->Quiet);
  2137. return true;
  2138. }
  2139. }
  2140. cmCTestTestProperties test;
  2141. test.Name = testname;
  2142. test.Args = args;
  2143. test.Directory = cmSystemTools::GetCurrentWorkingDirectory();
  2144. cmCTestOptionalLog(this->CTest, DEBUG, "Set test directory: "
  2145. << test.Directory << std::endl, this->Quiet);
  2146. test.IsInBasedOnREOptions = true;
  2147. test.WillFail = false;
  2148. test.RunSerial = false;
  2149. test.Timeout = 0;
  2150. test.ExplicitTimeout = false;
  2151. test.Cost = 0;
  2152. test.Processors = 1;
  2153. test.SkipReturnCode = -1;
  2154. test.PreviousRuns = 0;
  2155. if (this->UseIncludeRegExpFlag &&
  2156. !this->IncludeTestsRegularExpression.find(testname.c_str()))
  2157. {
  2158. test.IsInBasedOnREOptions = false;
  2159. }
  2160. else if (this->UseExcludeRegExpFlag &&
  2161. !this->UseExcludeRegExpFirst &&
  2162. this->ExcludeTestsRegularExpression.find(testname.c_str()))
  2163. {
  2164. test.IsInBasedOnREOptions = false;
  2165. }
  2166. this->TestList.push_back(test);
  2167. return true;
  2168. }