cmCTestTestHandler.cxx 68 KB

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