cmCTestTestHandler.cxx 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350
  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[0];
  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. std::map<std::string, double> labelTimes;
  587. std::map<std::string, int> labelCounts;
  588. std::set<std::string> labels;
  589. // initialize maps
  590. std::string::size_type maxlen = 0;
  591. for(; it != this->TestList.end(); ++it)
  592. {
  593. cmCTestTestProperties& p = *it;
  594. if(!p.Labels.empty())
  595. {
  596. for(std::vector<std::string>::iterator l = p.Labels.begin();
  597. l != p.Labels.end(); ++l)
  598. {
  599. if((*l).size() > maxlen)
  600. {
  601. maxlen = (*l).size();
  602. }
  603. labels.insert(*l);
  604. labelTimes[*l] = 0;
  605. labelCounts[*l] = 0;
  606. }
  607. }
  608. }
  609. cmCTestTestHandler::TestResultsVector::iterator ri =
  610. this->TestResults.begin();
  611. // fill maps
  612. for(; ri != this->TestResults.end(); ++ri)
  613. {
  614. cmCTestTestResult &result = *ri;
  615. cmCTestTestProperties& p = *result.Properties;
  616. if(!p.Labels.empty())
  617. {
  618. for(std::vector<std::string>::iterator l = p.Labels.begin();
  619. l != p.Labels.end(); ++l)
  620. {
  621. labelTimes[*l] += result.ExecutionTime;
  622. ++labelCounts[*l];
  623. }
  624. }
  625. }
  626. // now print times
  627. if(!labels.empty())
  628. {
  629. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "\nLabel Time Summary:",
  630. this->Quiet);
  631. }
  632. for(std::set<std::string>::const_iterator i = labels.begin();
  633. i != labels.end(); ++i)
  634. {
  635. std::string label = *i;
  636. label.resize(maxlen +3, ' ');
  637. char buf[1024];
  638. sprintf(buf, "%6.2f sec", labelTimes[*i]);
  639. std::ostringstream labelCountStr;
  640. labelCountStr << "(" << labelCounts[*i] << " test";
  641. if (labelCounts[*i] > 1)
  642. {
  643. labelCountStr << "s";
  644. }
  645. labelCountStr << ")";
  646. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "\n"
  647. << label << " = " << buf << " " << labelCountStr.str(),
  648. this->Quiet );
  649. if ( this->LogFile )
  650. {
  651. *this->LogFile << "\n" << *i << " = "
  652. << buf << "\n";
  653. }
  654. }
  655. if(!labels.empty())
  656. {
  657. if(this->LogFile)
  658. {
  659. *this->LogFile << "\n";
  660. }
  661. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "\n", this->Quiet);
  662. }
  663. }
  664. //----------------------------------------------------------------------
  665. void cmCTestTestHandler::CheckLabelFilterInclude(cmCTestTestProperties& it)
  666. {
  667. // if not using Labels to filter then return
  668. if (!this->UseIncludeLabelRegExpFlag )
  669. {
  670. return;
  671. }
  672. // if there are no labels and we are filtering by labels
  673. // then exclude the test as it does not have the label
  674. if(it.Labels.empty())
  675. {
  676. it.IsInBasedOnREOptions = false;
  677. return;
  678. }
  679. // check to see if the label regular expression matches
  680. bool found = false; // assume it does not match
  681. // loop over all labels and look for match
  682. for(std::vector<std::string>::iterator l = it.Labels.begin();
  683. l != it.Labels.end(); ++l)
  684. {
  685. if(this->IncludeLabelRegularExpression.find(*l))
  686. {
  687. found = true;
  688. }
  689. }
  690. // if no match was found, exclude the test
  691. if(!found)
  692. {
  693. it.IsInBasedOnREOptions = false;
  694. }
  695. }
  696. //----------------------------------------------------------------------
  697. void cmCTestTestHandler::CheckLabelFilterExclude(cmCTestTestProperties& it)
  698. {
  699. // if not using Labels to filter then return
  700. if (!this->UseExcludeLabelRegExpFlag )
  701. {
  702. return;
  703. }
  704. // if there are no labels and we are excluding by labels
  705. // then do nothing as a no label can not be a match
  706. if(it.Labels.empty())
  707. {
  708. return;
  709. }
  710. // check to see if the label regular expression matches
  711. bool found = false; // assume it does not match
  712. // loop over all labels and look for match
  713. for(std::vector<std::string>::iterator l = it.Labels.begin();
  714. l != it.Labels.end(); ++l)
  715. {
  716. if(this->ExcludeLabelRegularExpression.find(*l))
  717. {
  718. found = true;
  719. }
  720. }
  721. // if match was found, exclude the test
  722. if(found)
  723. {
  724. it.IsInBasedOnREOptions = false;
  725. }
  726. }
  727. //----------------------------------------------------------------------
  728. void cmCTestTestHandler::CheckLabelFilter(cmCTestTestProperties& it)
  729. {
  730. this->CheckLabelFilterInclude(it);
  731. this->CheckLabelFilterExclude(it);
  732. }
  733. //----------------------------------------------------------------------
  734. void cmCTestTestHandler::ComputeTestList()
  735. {
  736. this->TestList.clear(); // clear list of test
  737. this->GetListOfTests();
  738. if (this->RerunFailed)
  739. {
  740. this->ComputeTestListForRerunFailed();
  741. return;
  742. }
  743. cmCTestTestHandler::ListOfTests::size_type tmsize = this->TestList.size();
  744. // how many tests are in based on RegExp?
  745. int inREcnt = 0;
  746. cmCTestTestHandler::ListOfTests::iterator it;
  747. for ( it = this->TestList.begin(); it != this->TestList.end(); it ++ )
  748. {
  749. this->CheckLabelFilter(*it);
  750. if (it->IsInBasedOnREOptions)
  751. {
  752. inREcnt ++;
  753. }
  754. }
  755. // expand the test list based on the union flag
  756. if (this->UseUnion)
  757. {
  758. this->ExpandTestsToRunInformation((int)tmsize);
  759. }
  760. else
  761. {
  762. this->ExpandTestsToRunInformation(inREcnt);
  763. }
  764. // Now create a final list of tests to run
  765. int cnt = 0;
  766. inREcnt = 0;
  767. std::string last_directory = "";
  768. ListOfTests finalList;
  769. for ( it = this->TestList.begin(); it != this->TestList.end(); it ++ )
  770. {
  771. cnt ++;
  772. if (it->IsInBasedOnREOptions)
  773. {
  774. inREcnt++;
  775. }
  776. if (this->UseUnion)
  777. {
  778. // if it is not in the list and not in the regexp then skip
  779. if ((!this->TestsToRun.empty() &&
  780. std::find(this->TestsToRun.begin(), this->TestsToRun.end(), cnt)
  781. == this->TestsToRun.end()) && !it->IsInBasedOnREOptions)
  782. {
  783. continue;
  784. }
  785. }
  786. else
  787. {
  788. // is this test in the list of tests to run? If not then skip it
  789. if ((!this->TestsToRun.empty() &&
  790. std::find(this->TestsToRun.begin(),
  791. this->TestsToRun.end(), inREcnt)
  792. == this->TestsToRun.end()) || !it->IsInBasedOnREOptions)
  793. {
  794. continue;
  795. }
  796. }
  797. it->Index = cnt; // save the index into the test list for this test
  798. finalList.push_back(*it);
  799. }
  800. // Save the total number of tests before exclusions
  801. this->TotalNumberOfTests = this->TestList.size();
  802. // Set the TestList to the final list of all test
  803. this->TestList = finalList;
  804. this->UpdateMaxTestNameWidth();
  805. }
  806. void cmCTestTestHandler::ComputeTestListForRerunFailed()
  807. {
  808. this->ExpandTestsToRunInformationForRerunFailed();
  809. cmCTestTestHandler::ListOfTests::iterator it;
  810. ListOfTests finalList;
  811. int cnt = 0;
  812. for ( it = this->TestList.begin(); it != this->TestList.end(); it ++ )
  813. {
  814. cnt ++;
  815. // if this test is not in our list of tests to run, then skip it.
  816. if ((!this->TestsToRun.empty() &&
  817. std::find(this->TestsToRun.begin(), this->TestsToRun.end(), cnt)
  818. == this->TestsToRun.end()))
  819. {
  820. continue;
  821. }
  822. it->Index = cnt;
  823. finalList.push_back(*it);
  824. }
  825. // Save the total number of tests before exclusions
  826. this->TotalNumberOfTests = this->TestList.size();
  827. // Set the TestList to the list of failed tests to rerun
  828. this->TestList = finalList;
  829. this->UpdateMaxTestNameWidth();
  830. }
  831. void cmCTestTestHandler::UpdateMaxTestNameWidth()
  832. {
  833. std::string::size_type max = this->CTest->GetMaxTestNameWidth();
  834. for ( cmCTestTestHandler::ListOfTests::iterator it = this->TestList.begin();
  835. it != this->TestList.end(); it ++ )
  836. {
  837. cmCTestTestProperties& p = *it;
  838. if(max < p.Name.size())
  839. {
  840. max = p.Name.size();
  841. }
  842. }
  843. if(static_cast<std::string::size_type>(this->CTest->GetMaxTestNameWidth())
  844. != max)
  845. {
  846. this->CTest->SetMaxTestNameWidth(static_cast<int>(max));
  847. }
  848. }
  849. bool cmCTestTestHandler::GetValue(const char* tag,
  850. int& value,
  851. std::istream& fin)
  852. {
  853. std::string line;
  854. bool ret = true;
  855. cmSystemTools::GetLineFromStream(fin, line);
  856. if(line == tag)
  857. {
  858. fin >> value;
  859. ret = cmSystemTools::GetLineFromStream(fin, line); // read blank line
  860. }
  861. else
  862. {
  863. cmCTestLog(this->CTest, ERROR_MESSAGE,
  864. "parse error: missing tag: "
  865. << tag << " found [" << line << "]" << std::endl);
  866. ret = false;
  867. }
  868. return ret;
  869. }
  870. bool cmCTestTestHandler::GetValue(const char* tag,
  871. double& value,
  872. std::istream& fin)
  873. {
  874. std::string line;
  875. cmSystemTools::GetLineFromStream(fin, line);
  876. bool ret = true;
  877. if(line == tag)
  878. {
  879. fin >> value;
  880. ret = cmSystemTools::GetLineFromStream(fin, line); // read blank line
  881. }
  882. else
  883. {
  884. cmCTestLog(this->CTest, ERROR_MESSAGE,
  885. "parse error: missing tag: "
  886. << tag << " found [" << line << "]" << std::endl);
  887. ret = false;
  888. }
  889. return ret;
  890. }
  891. bool cmCTestTestHandler::GetValue(const char* tag,
  892. bool& value,
  893. std::istream& fin)
  894. {
  895. std::string line;
  896. cmSystemTools::GetLineFromStream(fin, line);
  897. bool ret = true;
  898. if(line == tag)
  899. {
  900. #ifdef __HAIKU__
  901. int tmp = 0;
  902. fin >> tmp;
  903. value = false;
  904. if(tmp)
  905. {
  906. value = true;
  907. }
  908. #else
  909. fin >> value;
  910. #endif
  911. ret = cmSystemTools::GetLineFromStream(fin, line); // read blank line
  912. }
  913. else
  914. {
  915. cmCTestLog(this->CTest, ERROR_MESSAGE,
  916. "parse error: missing tag: "
  917. << tag << " found [" << line << "]" << std::endl);
  918. ret = false;
  919. }
  920. return ret;
  921. }
  922. bool cmCTestTestHandler::GetValue(const char* tag,
  923. size_t& value,
  924. std::istream& fin)
  925. {
  926. std::string line;
  927. cmSystemTools::GetLineFromStream(fin, line);
  928. bool ret = true;
  929. if(line == tag)
  930. {
  931. fin >> value;
  932. ret = cmSystemTools::GetLineFromStream(fin, line); // read blank line
  933. }
  934. else
  935. {
  936. cmCTestLog(this->CTest, ERROR_MESSAGE,
  937. "parse error: missing tag: "
  938. << tag << " found [" << line << "]" << std::endl);
  939. ret = false;
  940. }
  941. return ret;
  942. }
  943. bool cmCTestTestHandler::GetValue(const char* tag,
  944. std::string& value,
  945. std::istream& fin)
  946. {
  947. std::string line;
  948. cmSystemTools::GetLineFromStream(fin, line);
  949. bool ret = true;
  950. if(line == tag)
  951. {
  952. ret = cmSystemTools::GetLineFromStream(fin, value);
  953. }
  954. else
  955. {
  956. cmCTestLog(this->CTest, ERROR_MESSAGE,
  957. "parse error: missing tag: "
  958. << tag << " found [" << line << "]" << std::endl);
  959. ret = false;
  960. }
  961. return ret;
  962. }
  963. //---------------------------------------------------------------------
  964. void cmCTestTestHandler::ProcessDirectory(std::vector<std::string> &passed,
  965. std::vector<std::string> &failed)
  966. {
  967. this->ComputeTestList();
  968. this->StartTest = this->CTest->CurrentTime();
  969. this->StartTestTime = static_cast<unsigned int>(cmSystemTools::GetTime());
  970. double elapsed_time_start = cmSystemTools::GetTime();
  971. cmCTestMultiProcessHandler* parallel = this->CTest->GetBatchJobs() ?
  972. new cmCTestBatchTestHandler : new cmCTestMultiProcessHandler;
  973. parallel->SetCTest(this->CTest);
  974. parallel->SetParallelLevel(this->CTest->GetParallelLevel());
  975. parallel->SetTestHandler(this);
  976. parallel->SetQuiet(this->Quiet);
  977. if(this->TestLoad > 0)
  978. {
  979. parallel->SetTestLoad(this->TestLoad);
  980. }
  981. else
  982. {
  983. parallel->SetTestLoad(this->CTest->GetTestLoad());
  984. }
  985. *this->LogFile << "Start testing: "
  986. << this->CTest->CurrentTime() << std::endl
  987. << "----------------------------------------------------------"
  988. << std::endl;
  989. cmCTestMultiProcessHandler::TestMap tests;
  990. cmCTestMultiProcessHandler::PropertiesMap properties;
  991. bool randomSchedule = this->CTest->GetScheduleType() == "Random";
  992. if(randomSchedule)
  993. {
  994. srand((unsigned)time(0));
  995. }
  996. for (ListOfTests::iterator it = this->TestList.begin();
  997. it != this->TestList.end(); ++it)
  998. {
  999. cmCTestTestProperties& p = *it;
  1000. cmCTestMultiProcessHandler::TestSet depends;
  1001. if(randomSchedule)
  1002. {
  1003. p.Cost = static_cast<float>(rand());
  1004. }
  1005. if(p.Timeout == 0 && this->CTest->GetGlobalTimeout() != 0)
  1006. {
  1007. p.Timeout = this->CTest->GetGlobalTimeout();
  1008. }
  1009. if(!p.Depends.empty())
  1010. {
  1011. for(std::vector<std::string>::iterator i = p.Depends.begin();
  1012. i != p.Depends.end(); ++i)
  1013. {
  1014. for(ListOfTests::iterator it2 = this->TestList.begin();
  1015. it2 != this->TestList.end(); ++it2)
  1016. {
  1017. if(it2->Name == *i)
  1018. {
  1019. depends.insert(it2->Index);
  1020. break; // break out of test loop as name can only match 1
  1021. }
  1022. }
  1023. }
  1024. }
  1025. tests[it->Index] = depends;
  1026. properties[it->Index] = &*it;
  1027. }
  1028. parallel->SetTests(tests, properties);
  1029. parallel->SetPassFailVectors(&passed, &failed);
  1030. this->TestResults.clear();
  1031. parallel->SetTestResults(&this->TestResults);
  1032. if(this->CTest->ShouldPrintLabels())
  1033. {
  1034. parallel->PrintLabels();
  1035. }
  1036. else if(this->CTest->GetShowOnly())
  1037. {
  1038. parallel->PrintTestList();
  1039. }
  1040. else
  1041. {
  1042. parallel->RunTests();
  1043. }
  1044. delete parallel;
  1045. this->EndTest = this->CTest->CurrentTime();
  1046. this->EndTestTime = static_cast<unsigned int>(cmSystemTools::GetTime());
  1047. this->ElapsedTestingTime = cmSystemTools::GetTime() - elapsed_time_start;
  1048. *this->LogFile << "End testing: "
  1049. << this->CTest->CurrentTime() << std::endl;
  1050. }
  1051. //----------------------------------------------------------------------
  1052. void cmCTestTestHandler::GenerateTestCommand(std::vector<std::string>&, int)
  1053. {
  1054. }
  1055. //----------------------------------------------------------------------
  1056. void cmCTestTestHandler::GenerateDartOutput(cmXMLWriter& xml)
  1057. {
  1058. if ( !this->CTest->GetProduceXML() )
  1059. {
  1060. return;
  1061. }
  1062. this->CTest->StartXML(xml, this->AppendXML);
  1063. xml.StartElement("Testing");
  1064. xml.Element("StartDateTime", this->StartTest);
  1065. xml.Element("StartTestTime", this->StartTestTime);
  1066. xml.StartElement("TestList");
  1067. cmCTestTestHandler::TestResultsVector::size_type cc;
  1068. for ( cc = 0; cc < this->TestResults.size(); cc ++ )
  1069. {
  1070. cmCTestTestResult *result = &this->TestResults[cc];
  1071. std::string testPath = result->Path + "/" + result->Name;
  1072. xml.Element("Test", this->CTest->GetShortPathToFile(testPath.c_str()));
  1073. }
  1074. xml.EndElement(); // TestList
  1075. for ( cc = 0; cc < this->TestResults.size(); cc ++ )
  1076. {
  1077. cmCTestTestResult *result = &this->TestResults[cc];
  1078. this->WriteTestResultHeader(xml, result);
  1079. xml.StartElement("Results");
  1080. if ( result->Status != cmCTestTestHandler::NOT_RUN )
  1081. {
  1082. if ( result->Status != cmCTestTestHandler::COMPLETED ||
  1083. result->ReturnValue )
  1084. {
  1085. xml.StartElement("NamedMeasurement");
  1086. xml.Attribute("type", "text/string");
  1087. xml.Attribute("name", "Exit Code");
  1088. xml.Element("Value", this->GetTestStatus(result->Status));
  1089. xml.EndElement(); // NamedMeasurement
  1090. xml.StartElement("NamedMeasurement");
  1091. xml.Attribute("type", "text/string");
  1092. xml.Attribute("name", "Exit Value");
  1093. xml.Element("Value", result->ReturnValue);
  1094. xml.EndElement(); // NamedMeasurement
  1095. }
  1096. this->GenerateRegressionImages(xml, result->DartString);
  1097. xml.StartElement("NamedMeasurement");
  1098. xml.Attribute("type", "numeric/double");
  1099. xml.Attribute("name", "Execution Time");
  1100. xml.Element("Value", result->ExecutionTime);
  1101. xml.EndElement(); // NamedMeasurement
  1102. if(!result->Reason.empty())
  1103. {
  1104. const char* reasonType = "Pass Reason";
  1105. if(result->Status != cmCTestTestHandler::COMPLETED &&
  1106. result->Status != cmCTestTestHandler::NOT_RUN)
  1107. {
  1108. reasonType = "Fail Reason";
  1109. }
  1110. xml.StartElement("NamedMeasurement");
  1111. xml.Attribute("type", "text/string");
  1112. xml.Attribute("name", reasonType);
  1113. xml.Element("Value", result->Reason);
  1114. xml.EndElement(); // NamedMeasurement
  1115. }
  1116. xml.StartElement("NamedMeasurement");
  1117. xml.Attribute("type", "text/string");
  1118. xml.Attribute("name", "Completion Status");
  1119. xml.Element("Value", result->CompletionStatus);
  1120. xml.EndElement(); // NamedMeasurement
  1121. }
  1122. xml.StartElement("NamedMeasurement");
  1123. xml.Attribute("type", "text/string");
  1124. xml.Attribute("name", "Command Line");
  1125. xml.Element("Value", result->FullCommandLine);
  1126. xml.EndElement(); // NamedMeasurement
  1127. std::map<std::string,std::string>::iterator measureIt;
  1128. for ( measureIt = result->Properties->Measurements.begin();
  1129. measureIt != result->Properties->Measurements.end();
  1130. ++ measureIt )
  1131. {
  1132. xml.StartElement("NamedMeasurement");
  1133. xml.Attribute("type", "text/string");
  1134. xml.Attribute("name", measureIt->first);
  1135. xml.Element("Value", measureIt->second);
  1136. xml.EndElement(); // NamedMeasurement
  1137. }
  1138. xml.StartElement("Measurement");
  1139. xml.StartElement("Value");
  1140. if (result->CompressOutput)
  1141. {
  1142. xml.Attribute("encoding", "base64");
  1143. xml.Attribute("compression", "gzip");
  1144. }
  1145. xml.Content(result->Output);
  1146. xml.EndElement(); // Value
  1147. xml.EndElement(); // Measurement
  1148. xml.EndElement(); // Results
  1149. this->AttachFiles(xml, result);
  1150. this->WriteTestResultFooter(xml, result);
  1151. }
  1152. xml.Element("EndDateTime", this->EndTest);
  1153. xml.Element("EndTestTime", this->EndTestTime);
  1154. xml.Element("ElapsedMinutes",
  1155. static_cast<int>(this->ElapsedTestingTime/6)/10.0);
  1156. xml.EndElement(); // Testing
  1157. this->CTest->EndXML(xml);
  1158. }
  1159. //----------------------------------------------------------------------------
  1160. void cmCTestTestHandler::WriteTestResultHeader(cmXMLWriter& xml,
  1161. cmCTestTestResult* result)
  1162. {
  1163. xml.StartElement("Test");
  1164. if ( result->Status == cmCTestTestHandler::COMPLETED )
  1165. {
  1166. xml.Attribute("Status", "passed");
  1167. }
  1168. else if ( result->Status == cmCTestTestHandler::NOT_RUN )
  1169. {
  1170. xml.Attribute("Status", "notrun");
  1171. }
  1172. else
  1173. {
  1174. xml.Attribute("Status", "failed");
  1175. }
  1176. std::string testPath = result->Path + "/" + result->Name;
  1177. xml.Element("Name", result->Name);
  1178. xml.Element("Path", this->CTest->GetShortPathToFile(result->Path.c_str()));
  1179. xml.Element("FullName", this->CTest->GetShortPathToFile(testPath.c_str()));
  1180. xml.Element("FullCommandLine", result->FullCommandLine);
  1181. }
  1182. //----------------------------------------------------------------------------
  1183. void cmCTestTestHandler::WriteTestResultFooter(cmXMLWriter& xml,
  1184. cmCTestTestResult* result)
  1185. {
  1186. if(!result->Properties->Labels.empty())
  1187. {
  1188. xml.StartElement("Labels");
  1189. std::vector<std::string> const& labels = result->Properties->Labels;
  1190. for(std::vector<std::string>::const_iterator li = labels.begin();
  1191. li != labels.end(); ++li)
  1192. {
  1193. xml.Element("Label", *li);
  1194. }
  1195. xml.EndElement(); // Labels
  1196. }
  1197. xml.EndElement(); // Test
  1198. }
  1199. //----------------------------------------------------------------------
  1200. void cmCTestTestHandler::AttachFiles(cmXMLWriter& xml,
  1201. cmCTestTestResult* result)
  1202. {
  1203. if(result->Status != cmCTestTestHandler::COMPLETED
  1204. && result->Properties->AttachOnFail.size())
  1205. {
  1206. result->Properties->AttachedFiles.insert(
  1207. result->Properties->AttachedFiles.end(),
  1208. result->Properties->AttachOnFail.begin(),
  1209. result->Properties->AttachOnFail.end());
  1210. }
  1211. for(std::vector<std::string>::const_iterator file =
  1212. result->Properties->AttachedFiles.begin();
  1213. file != result->Properties->AttachedFiles.end(); ++file)
  1214. {
  1215. const std::string &base64 = this->CTest->Base64GzipEncodeFile(*file);
  1216. std::string fname = cmSystemTools::GetFilenameName(*file);
  1217. xml.StartElement("NamedMeasurement");
  1218. xml.Attribute("name", "Attached File");
  1219. xml.Attribute("encoding", "base64");
  1220. xml.Attribute("compression", "tar/gzip");
  1221. xml.Attribute("filename", fname);
  1222. xml.Attribute("type", "file");
  1223. xml.Element("Value", base64);
  1224. xml.EndElement(); // NamedMeasurement
  1225. }
  1226. }
  1227. //----------------------------------------------------------------------
  1228. int cmCTestTestHandler::ExecuteCommands(std::vector<std::string>& vec)
  1229. {
  1230. std::vector<std::string>::iterator it;
  1231. for ( it = vec.begin(); it != vec.end(); ++it )
  1232. {
  1233. int retVal = 0;
  1234. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run command: " <<
  1235. *it << std::endl, this->Quiet);
  1236. if ( !cmSystemTools::RunSingleCommand(it->c_str(), 0, 0, &retVal, 0,
  1237. cmSystemTools::OUTPUT_MERGE
  1238. /*this->Verbose*/) || retVal != 0 )
  1239. {
  1240. cmCTestLog(this->CTest, ERROR_MESSAGE, "Problem running command: "
  1241. << *it << std::endl);
  1242. return 0;
  1243. }
  1244. }
  1245. return 1;
  1246. }
  1247. //----------------------------------------------------------------------
  1248. // Find the appropriate executable to run for a test
  1249. std::string cmCTestTestHandler::FindTheExecutable(const char *exe)
  1250. {
  1251. std::string resConfig;
  1252. std::vector<std::string> extraPaths;
  1253. std::vector<std::string> failedPaths;
  1254. if(strcmp(exe, "NOT_AVAILABLE") == 0)
  1255. {
  1256. return exe;
  1257. }
  1258. return cmCTestTestHandler::FindExecutable(this->CTest,
  1259. exe, resConfig,
  1260. extraPaths,
  1261. failedPaths);
  1262. }
  1263. // add additional configurations to the search path
  1264. void cmCTestTestHandler
  1265. ::AddConfigurations(cmCTest *ctest,
  1266. std::vector<std::string> &attempted,
  1267. std::vector<std::string> &attemptedConfigs,
  1268. std::string filepath,
  1269. std::string &filename)
  1270. {
  1271. std::string tempPath;
  1272. if (!filepath.empty() &&
  1273. filepath[filepath.size()-1] != '/')
  1274. {
  1275. filepath += "/";
  1276. }
  1277. tempPath = filepath + filename;
  1278. attempted.push_back(tempPath);
  1279. attemptedConfigs.push_back("");
  1280. if(!ctest->GetConfigType().empty())
  1281. {
  1282. tempPath = filepath;
  1283. tempPath += ctest->GetConfigType();
  1284. tempPath += "/";
  1285. tempPath += filename;
  1286. attempted.push_back(tempPath);
  1287. attemptedConfigs.push_back(ctest->GetConfigType());
  1288. // If the file is an OSX bundle then the configtype
  1289. // will be at the start of the path
  1290. tempPath = ctest->GetConfigType();
  1291. tempPath += "/";
  1292. tempPath += filepath;
  1293. tempPath += filename;
  1294. attempted.push_back(tempPath);
  1295. attemptedConfigs.push_back(ctest->GetConfigType());
  1296. }
  1297. else
  1298. {
  1299. // no config specified - try some options...
  1300. tempPath = filepath;
  1301. tempPath += "Release/";
  1302. tempPath += filename;
  1303. attempted.push_back(tempPath);
  1304. attemptedConfigs.push_back("Release");
  1305. tempPath = filepath;
  1306. tempPath += "Debug/";
  1307. tempPath += filename;
  1308. attempted.push_back(tempPath);
  1309. attemptedConfigs.push_back("Debug");
  1310. tempPath = filepath;
  1311. tempPath += "MinSizeRel/";
  1312. tempPath += filename;
  1313. attempted.push_back(tempPath);
  1314. attemptedConfigs.push_back("MinSizeRel");
  1315. tempPath = filepath;
  1316. tempPath += "RelWithDebInfo/";
  1317. tempPath += filename;
  1318. attempted.push_back(tempPath);
  1319. attemptedConfigs.push_back("RelWithDebInfo");
  1320. tempPath = filepath;
  1321. tempPath += "Deployment/";
  1322. tempPath += filename;
  1323. attempted.push_back(tempPath);
  1324. attemptedConfigs.push_back("Deployment");
  1325. tempPath = filepath;
  1326. tempPath += "Development/";
  1327. tempPath += filename;
  1328. attempted.push_back(tempPath);
  1329. attemptedConfigs.push_back("Deployment");
  1330. }
  1331. }
  1332. //----------------------------------------------------------------------
  1333. // Find the appropriate executable to run for a test
  1334. std::string cmCTestTestHandler
  1335. ::FindExecutable(cmCTest *ctest,
  1336. const char *testCommand,
  1337. std::string &resultingConfig,
  1338. std::vector<std::string> &extraPaths,
  1339. std::vector<std::string> &failed)
  1340. {
  1341. // now run the compiled test if we can find it
  1342. std::vector<std::string> attempted;
  1343. std::vector<std::string> attemptedConfigs;
  1344. std::string tempPath;
  1345. std::string filepath =
  1346. cmSystemTools::GetFilenamePath(testCommand);
  1347. std::string filename =
  1348. cmSystemTools::GetFilenameName(testCommand);
  1349. cmCTestTestHandler::AddConfigurations(ctest, attempted,
  1350. attemptedConfigs,
  1351. filepath,filename);
  1352. // even if a fullpath was specified also try it relative to the current
  1353. // directory
  1354. if (!filepath.empty() && filepath[0] == '/')
  1355. {
  1356. std::string localfilepath = filepath.substr(1,filepath.size()-1);
  1357. cmCTestTestHandler::AddConfigurations(ctest, attempted,
  1358. attemptedConfigs,
  1359. localfilepath,filename);
  1360. }
  1361. // if extraPaths are provided and we were not passed a full path, try them,
  1362. // try any extra paths
  1363. if (filepath.empty())
  1364. {
  1365. for (unsigned int i = 0; i < extraPaths.size(); ++i)
  1366. {
  1367. std::string filepathExtra =
  1368. cmSystemTools::GetFilenamePath(extraPaths[i]);
  1369. std::string filenameExtra =
  1370. cmSystemTools::GetFilenameName(extraPaths[i]);
  1371. cmCTestTestHandler::AddConfigurations(ctest,attempted,
  1372. attemptedConfigs,
  1373. filepathExtra,
  1374. filenameExtra);
  1375. }
  1376. }
  1377. // store the final location in fullPath
  1378. std::string fullPath;
  1379. // now look in the paths we specified above
  1380. for(unsigned int ai=0;
  1381. ai < attempted.size() && fullPath.empty(); ++ai)
  1382. {
  1383. // first check without exe extension
  1384. if(cmSystemTools::FileExists(attempted[ai].c_str())
  1385. && !cmSystemTools::FileIsDirectory(attempted[ai]))
  1386. {
  1387. fullPath = cmSystemTools::CollapseFullPath(attempted[ai]);
  1388. resultingConfig = attemptedConfigs[ai];
  1389. }
  1390. // then try with the exe extension
  1391. else
  1392. {
  1393. failed.push_back(attempted[ai]);
  1394. tempPath = attempted[ai];
  1395. tempPath += cmSystemTools::GetExecutableExtension();
  1396. if(cmSystemTools::FileExists(tempPath.c_str())
  1397. && !cmSystemTools::FileIsDirectory(tempPath))
  1398. {
  1399. fullPath = cmSystemTools::CollapseFullPath(tempPath);
  1400. resultingConfig = attemptedConfigs[ai];
  1401. }
  1402. else
  1403. {
  1404. failed.push_back(tempPath);
  1405. }
  1406. }
  1407. }
  1408. // if everything else failed, check the users path, but only if a full path
  1409. // wasn't specified
  1410. if (fullPath.empty() && filepath.empty())
  1411. {
  1412. std::string path = cmSystemTools::FindProgram(filename.c_str());
  1413. if (path != "")
  1414. {
  1415. resultingConfig = "";
  1416. return path;
  1417. }
  1418. }
  1419. if(fullPath.empty())
  1420. {
  1421. cmCTestLog(ctest, HANDLER_OUTPUT,
  1422. "Could not find executable " << testCommand << "\n"
  1423. << "Looked in the following places:\n");
  1424. for(std::vector<std::string>::iterator i = failed.begin();
  1425. i != failed.end(); ++i)
  1426. {
  1427. cmCTestLog(ctest, HANDLER_OUTPUT, i->c_str() << "\n");
  1428. }
  1429. }
  1430. return fullPath;
  1431. }
  1432. //----------------------------------------------------------------------
  1433. void cmCTestTestHandler::GetListOfTests()
  1434. {
  1435. if ( !this->IncludeLabelRegExp.empty() )
  1436. {
  1437. this->IncludeLabelRegularExpression.
  1438. compile(this->IncludeLabelRegExp.c_str());
  1439. }
  1440. if ( !this->ExcludeLabelRegExp.empty() )
  1441. {
  1442. this->ExcludeLabelRegularExpression.
  1443. compile(this->ExcludeLabelRegExp.c_str());
  1444. }
  1445. if ( !this->IncludeRegExp.empty() )
  1446. {
  1447. this->IncludeTestsRegularExpression.compile(this->IncludeRegExp.c_str());
  1448. }
  1449. if ( !this->ExcludeRegExp.empty() )
  1450. {
  1451. this->ExcludeTestsRegularExpression.compile(this->ExcludeRegExp.c_str());
  1452. }
  1453. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1454. "Constructing a list of tests" << std::endl, this->Quiet);
  1455. cmake cm;
  1456. cm.SetHomeDirectory("");
  1457. cm.SetHomeOutputDirectory("");
  1458. cmGlobalGenerator gg(&cm);
  1459. cmsys::auto_ptr<cmMakefile> mf(new cmMakefile(&gg, cm.GetCurrentSnapshot()));
  1460. cmsys::auto_ptr<cmLocalGenerator> lg(
  1461. gg.CreateLocalGenerator(mf.get()));
  1462. mf->AddDefinition("CTEST_CONFIGURATION_TYPE",
  1463. this->CTest->GetConfigType().c_str());
  1464. // Add handler for ADD_TEST
  1465. cmCTestAddTestCommand* newCom1 = new cmCTestAddTestCommand;
  1466. newCom1->TestHandler = this;
  1467. cm.GetState()->AddCommand(newCom1);
  1468. // Add handler for SUBDIRS
  1469. cmCTestSubdirCommand* newCom2 =
  1470. new cmCTestSubdirCommand;
  1471. newCom2->TestHandler = this;
  1472. cm.GetState()->AddCommand(newCom2);
  1473. // Add handler for ADD_SUBDIRECTORY
  1474. cmCTestAddSubdirectoryCommand* newCom3 =
  1475. new cmCTestAddSubdirectoryCommand;
  1476. newCom3->TestHandler = this;
  1477. cm.GetState()->AddCommand(newCom3);
  1478. // Add handler for SET_SOURCE_FILES_PROPERTIES
  1479. cmCTestSetTestsPropertiesCommand* newCom4
  1480. = new cmCTestSetTestsPropertiesCommand;
  1481. newCom4->TestHandler = this;
  1482. cm.GetState()->AddCommand(newCom4);
  1483. const char* testFilename;
  1484. if( cmSystemTools::FileExists("CTestTestfile.cmake") )
  1485. {
  1486. // does the CTestTestfile.cmake exist ?
  1487. testFilename = "CTestTestfile.cmake";
  1488. }
  1489. else if( cmSystemTools::FileExists("DartTestfile.txt") )
  1490. {
  1491. // does the DartTestfile.txt exist ?
  1492. testFilename = "DartTestfile.txt";
  1493. }
  1494. else
  1495. {
  1496. return;
  1497. }
  1498. if ( !mf->ReadListFile(testFilename) )
  1499. {
  1500. return;
  1501. }
  1502. if ( cmSystemTools::GetErrorOccuredFlag() )
  1503. {
  1504. return;
  1505. }
  1506. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1507. "Done constructing a list of tests" << std::endl, this->Quiet);
  1508. }
  1509. //----------------------------------------------------------------------
  1510. void cmCTestTestHandler::UseIncludeRegExp()
  1511. {
  1512. this->UseIncludeRegExpFlag = true;
  1513. }
  1514. //----------------------------------------------------------------------
  1515. void cmCTestTestHandler::UseExcludeRegExp()
  1516. {
  1517. this->UseExcludeRegExpFlag = true;
  1518. this->UseExcludeRegExpFirst = this->UseIncludeRegExpFlag ? false : true;
  1519. }
  1520. //----------------------------------------------------------------------
  1521. const char* cmCTestTestHandler::GetTestStatus(int status)
  1522. {
  1523. static const char statuses[][100] = {
  1524. "Not Run",
  1525. "Timeout",
  1526. "SEGFAULT",
  1527. "ILLEGAL",
  1528. "INTERRUPT",
  1529. "NUMERICAL",
  1530. "OTHER_FAULT",
  1531. "Failed",
  1532. "BAD_COMMAND",
  1533. "Completed"
  1534. };
  1535. if ( status < cmCTestTestHandler::NOT_RUN ||
  1536. status > cmCTestTestHandler::COMPLETED )
  1537. {
  1538. return "No Status";
  1539. }
  1540. return statuses[status];
  1541. }
  1542. //----------------------------------------------------------------------
  1543. void cmCTestTestHandler::ExpandTestsToRunInformation(size_t numTests)
  1544. {
  1545. if (this->TestsToRunString.empty())
  1546. {
  1547. return;
  1548. }
  1549. int start;
  1550. int end = -1;
  1551. double stride = -1;
  1552. std::string::size_type pos = 0;
  1553. std::string::size_type pos2;
  1554. // read start
  1555. if(GetNextNumber(this->TestsToRunString, start, pos, pos2))
  1556. {
  1557. // read end
  1558. if(GetNextNumber(this->TestsToRunString, end, pos, pos2))
  1559. {
  1560. // read stride
  1561. if(GetNextRealNumber(this->TestsToRunString, stride, pos, pos2))
  1562. {
  1563. int val =0;
  1564. // now read specific numbers
  1565. while(GetNextNumber(this->TestsToRunString, val, pos, pos2))
  1566. {
  1567. this->TestsToRun.push_back(val);
  1568. }
  1569. this->TestsToRun.push_back(val);
  1570. }
  1571. }
  1572. }
  1573. // if start is not specified then we assume we start at 1
  1574. if(start == -1)
  1575. {
  1576. start = 1;
  1577. }
  1578. // if end isnot specified then we assume we end with the last test
  1579. if(end == -1)
  1580. {
  1581. end = static_cast<int>(numTests);
  1582. }
  1583. // if the stride wasn't specified then it defaults to 1
  1584. if(stride == -1)
  1585. {
  1586. stride = 1;
  1587. }
  1588. // if we have a range then add it
  1589. if(end != -1 && start != -1 && stride > 0)
  1590. {
  1591. int i = 0;
  1592. while (i*stride + start <= end)
  1593. {
  1594. this->TestsToRun.push_back(static_cast<int>(i*stride+start));
  1595. ++i;
  1596. }
  1597. }
  1598. // sort the array
  1599. std::sort(this->TestsToRun.begin(), this->TestsToRun.end(),
  1600. std::less<int>());
  1601. // remove duplicates
  1602. std::vector<int>::iterator new_end =
  1603. std::unique(this->TestsToRun.begin(), this->TestsToRun.end());
  1604. this->TestsToRun.erase(new_end, this->TestsToRun.end());
  1605. }
  1606. void cmCTestTestHandler::ExpandTestsToRunInformationForRerunFailed()
  1607. {
  1608. std::string dirName = this->CTest->GetBinaryDir() + "/Testing/Temporary";
  1609. cmsys::Directory directory;
  1610. if (directory.Load(dirName) == 0)
  1611. {
  1612. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unable to read the contents of "
  1613. << dirName << std::endl);
  1614. return;
  1615. }
  1616. int numFiles = static_cast<int>
  1617. (cmsys::Directory::GetNumberOfFilesInDirectory(dirName));
  1618. std::string pattern = "LastTestsFailed";
  1619. std::string logName = "";
  1620. for (int i = 0; i < numFiles; ++i)
  1621. {
  1622. std::string fileName = directory.GetFile(i);
  1623. // bcc crashes if we attempt a normal substring comparison,
  1624. // hence the following workaround
  1625. std::string fileNameSubstring = fileName.substr(0, pattern.length());
  1626. if (fileNameSubstring.compare(pattern) != 0)
  1627. {
  1628. continue;
  1629. }
  1630. if (logName == "")
  1631. {
  1632. logName = fileName;
  1633. }
  1634. else
  1635. {
  1636. // if multiple matching logs were found we use the most recently
  1637. // modified one.
  1638. int res;
  1639. cmSystemTools::FileTimeCompare(logName, fileName, &res);
  1640. if (res == -1)
  1641. {
  1642. logName = fileName;
  1643. }
  1644. }
  1645. }
  1646. std::string lastTestsFailedLog = this->CTest->GetBinaryDir()
  1647. + "/Testing/Temporary/" + logName;
  1648. if ( !cmSystemTools::FileExists(lastTestsFailedLog.c_str()) )
  1649. {
  1650. if ( !this->CTest->GetShowOnly() && !this->CTest->ShouldPrintLabels() )
  1651. {
  1652. cmCTestLog(this->CTest, ERROR_MESSAGE, lastTestsFailedLog
  1653. << " does not exist!" << std::endl);
  1654. }
  1655. return;
  1656. }
  1657. // parse the list of tests to rerun from LastTestsFailed.log
  1658. cmsys::ifstream ifs(lastTestsFailedLog.c_str());
  1659. if ( ifs )
  1660. {
  1661. std::string line;
  1662. std::string::size_type pos;
  1663. while ( cmSystemTools::GetLineFromStream(ifs, line) )
  1664. {
  1665. pos = line.find(':', 0);
  1666. if (pos == line.npos)
  1667. {
  1668. continue;
  1669. }
  1670. int val = atoi(line.substr(0, pos).c_str());
  1671. this->TestsToRun.push_back(val);
  1672. }
  1673. ifs.close();
  1674. }
  1675. else if ( !this->CTest->GetShowOnly() && !this->CTest->ShouldPrintLabels() )
  1676. {
  1677. cmCTestLog(this->CTest, ERROR_MESSAGE, "Problem reading file: "
  1678. << lastTestsFailedLog <<
  1679. " while generating list of previously failed tests." << std::endl);
  1680. }
  1681. }
  1682. //----------------------------------------------------------------------
  1683. // Just for convenience
  1684. #define SPACE_REGEX "[ \t\r\n]"
  1685. //----------------------------------------------------------------------
  1686. void cmCTestTestHandler::GenerateRegressionImages(
  1687. cmXMLWriter& xml, const std::string& dart)
  1688. {
  1689. cmsys::RegularExpression twoattributes(
  1690. "<DartMeasurement"
  1691. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1692. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1693. SPACE_REGEX "*>([^<]*)</DartMeasurement>");
  1694. cmsys::RegularExpression threeattributes(
  1695. "<DartMeasurement"
  1696. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1697. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1698. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1699. SPACE_REGEX "*>([^<]*)</DartMeasurement>");
  1700. cmsys::RegularExpression fourattributes(
  1701. "<DartMeasurement"
  1702. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1703. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1704. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1705. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1706. SPACE_REGEX "*>([^<]*)</DartMeasurement>");
  1707. cmsys::RegularExpression cdatastart(
  1708. "<DartMeasurement"
  1709. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1710. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1711. SPACE_REGEX "*>"
  1712. SPACE_REGEX "*<!\\[CDATA\\[");
  1713. cmsys::RegularExpression cdataend(
  1714. "]]>"
  1715. SPACE_REGEX "*</DartMeasurement>");
  1716. cmsys::RegularExpression measurementfile(
  1717. "<DartMeasurementFile"
  1718. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1719. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1720. SPACE_REGEX "*>([^<]*)</DartMeasurementFile>");
  1721. bool done = false;
  1722. std::string cxml = dart;
  1723. while ( ! done )
  1724. {
  1725. if ( twoattributes.find(cxml) )
  1726. {
  1727. xml.StartElement("NamedMeasurement");
  1728. xml.Attribute(twoattributes.match(1).c_str(),
  1729. twoattributes.match(2));
  1730. xml.Attribute(twoattributes.match(3).c_str(),
  1731. twoattributes.match(4));
  1732. xml.Element("Value", twoattributes.match(5));
  1733. xml.EndElement();
  1734. cxml.erase(twoattributes.start(),
  1735. twoattributes.end() - twoattributes.start());
  1736. }
  1737. else if ( threeattributes.find(cxml) )
  1738. {
  1739. xml.StartElement("NamedMeasurement");
  1740. xml.Attribute(threeattributes.match(1).c_str(),
  1741. threeattributes.match(2));
  1742. xml.Attribute(threeattributes.match(3).c_str(),
  1743. threeattributes.match(4));
  1744. xml.Attribute(threeattributes.match(5).c_str(),
  1745. threeattributes.match(6));
  1746. xml.Element("Value", twoattributes.match(7));
  1747. xml.EndElement();
  1748. cxml.erase(threeattributes.start(),
  1749. threeattributes.end() - threeattributes.start());
  1750. }
  1751. else if ( fourattributes.find(cxml) )
  1752. {
  1753. xml.StartElement("NamedMeasurement");
  1754. xml.Attribute(fourattributes.match(1).c_str(),
  1755. fourattributes.match(2));
  1756. xml.Attribute(fourattributes.match(3).c_str(),
  1757. fourattributes.match(4));
  1758. xml.Attribute(fourattributes.match(5).c_str(),
  1759. fourattributes.match(6));
  1760. xml.Attribute(fourattributes.match(7).c_str(),
  1761. fourattributes.match(8));
  1762. xml.Element("Value", twoattributes.match(9));
  1763. xml.EndElement();
  1764. cxml.erase(fourattributes.start(),
  1765. fourattributes.end() - fourattributes.start());
  1766. }
  1767. else if ( cdatastart.find(cxml) && cdataend.find(cxml) )
  1768. {
  1769. xml.StartElement("NamedMeasurement");
  1770. xml.Attribute(cdatastart.match(1).c_str(), cdatastart.match(2));
  1771. xml.Attribute(cdatastart.match(3).c_str(), cdatastart.match(4));
  1772. xml.StartElement("Value");
  1773. xml.CData(
  1774. cxml.substr(cdatastart.end(), cdataend.start() - cdatastart.end()));
  1775. xml.EndElement(); // Value
  1776. xml.EndElement(); // NamedMeasurement
  1777. cxml.erase(cdatastart.start(),
  1778. cdataend.end() - cdatastart.start());
  1779. }
  1780. else if ( measurementfile.find(cxml) )
  1781. {
  1782. const std::string& filename =
  1783. cmCTest::CleanString(measurementfile.match(5));
  1784. if ( cmSystemTools::FileExists(filename.c_str()) )
  1785. {
  1786. long len = cmSystemTools::FileLength(filename);
  1787. if ( len == 0 )
  1788. {
  1789. std::string k1 = measurementfile.match(1);
  1790. std::string v1 = measurementfile.match(2);
  1791. std::string k2 = measurementfile.match(3);
  1792. std::string v2 = measurementfile.match(4);
  1793. if ( cmSystemTools::LowerCase(k1) == "type" )
  1794. {
  1795. v1 = "text/string";
  1796. }
  1797. if ( cmSystemTools::LowerCase(k2) == "type" )
  1798. {
  1799. v2 = "text/string";
  1800. }
  1801. xml.StartElement("NamedMeasurement");
  1802. xml.Attribute(k1.c_str(), v1);
  1803. xml.Attribute(k2.c_str(), v2);
  1804. xml.Attribute("encoding", "none");
  1805. xml.Element("Value", "Image " + filename + " is empty");
  1806. xml.EndElement();
  1807. }
  1808. else
  1809. {
  1810. cmsys::ifstream ifs(filename.c_str(), std::ios::in
  1811. #ifdef _WIN32
  1812. | std::ios::binary
  1813. #endif
  1814. );
  1815. unsigned char *file_buffer = new unsigned char [ len + 1 ];
  1816. ifs.read(reinterpret_cast<char*>(file_buffer), len);
  1817. unsigned char *encoded_buffer
  1818. = new unsigned char [ static_cast<int>(
  1819. static_cast<double>(len) * 1.5 + 5.0) ];
  1820. size_t rlen
  1821. = cmsysBase64_Encode(file_buffer, len, encoded_buffer, 1);
  1822. xml.StartElement("NamedMeasurement");
  1823. xml.Attribute(measurementfile.match(1).c_str(),
  1824. measurementfile.match(2));
  1825. xml.Attribute(measurementfile.match(3).c_str(),
  1826. measurementfile.match(4));
  1827. xml.Attribute("encoding", "base64");
  1828. std::stringstream ostr;
  1829. for (size_t cc = 0; cc < rlen; cc ++ )
  1830. {
  1831. ostr << encoded_buffer[cc];
  1832. if ( cc % 60 == 0 && cc )
  1833. {
  1834. ostr << std::endl;
  1835. }
  1836. }
  1837. xml.Element("Value", ostr.str());
  1838. xml.EndElement(); // NamedMeasurement
  1839. delete [] file_buffer;
  1840. delete [] encoded_buffer;
  1841. }
  1842. }
  1843. else
  1844. {
  1845. int idx = 4;
  1846. if ( measurementfile.match(1) == "name" )
  1847. {
  1848. idx = 2;
  1849. }
  1850. xml.StartElement("NamedMeasurement");
  1851. xml.Attribute("name", measurementfile.match(idx));
  1852. xml.Attribute("text", "text/string");
  1853. xml.Element("Value", "File " + filename + " not found");
  1854. xml.EndElement();
  1855. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "File \"" << filename
  1856. << "\" not found." << std::endl, this->Quiet);
  1857. }
  1858. cxml.erase(measurementfile.start(),
  1859. measurementfile.end() - measurementfile.start());
  1860. }
  1861. else
  1862. {
  1863. done = true;
  1864. }
  1865. }
  1866. }
  1867. //----------------------------------------------------------------------
  1868. void cmCTestTestHandler::SetIncludeRegExp(const char *arg)
  1869. {
  1870. this->IncludeRegExp = arg;
  1871. }
  1872. //----------------------------------------------------------------------
  1873. void cmCTestTestHandler::SetExcludeRegExp(const char *arg)
  1874. {
  1875. this->ExcludeRegExp = arg;
  1876. }
  1877. //----------------------------------------------------------------------
  1878. void cmCTestTestHandler::SetTestsToRunInformation(const char* in)
  1879. {
  1880. if ( !in )
  1881. {
  1882. return;
  1883. }
  1884. this->TestsToRunString = in;
  1885. // if the argument is a file, then read it and use the contents as the
  1886. // string
  1887. if(cmSystemTools::FileExists(in))
  1888. {
  1889. cmsys::ifstream fin(in);
  1890. unsigned long filelen = cmSystemTools::FileLength(in);
  1891. char* buff = new char[filelen+1];
  1892. fin.getline(buff, filelen);
  1893. buff[fin.gcount()] = 0;
  1894. this->TestsToRunString = buff;
  1895. delete [] buff;
  1896. }
  1897. }
  1898. //----------------------------------------------------------------------------
  1899. bool cmCTestTestHandler::CleanTestOutput(std::string& output, size_t length)
  1900. {
  1901. if(!length || length >= output.size() ||
  1902. output.find("CTEST_FULL_OUTPUT") != output.npos)
  1903. {
  1904. return true;
  1905. }
  1906. // Truncate at given length but do not break in the middle of a multi-byte
  1907. // UTF-8 encoding.
  1908. char const* const begin = output.c_str();
  1909. char const* const end = begin + output.size();
  1910. char const* const truncate = begin + length;
  1911. char const* current = begin;
  1912. while(current < truncate)
  1913. {
  1914. unsigned int ch;
  1915. if(const char* next = cm_utf8_decode_character(current, end, &ch))
  1916. {
  1917. if(next > truncate)
  1918. {
  1919. break;
  1920. }
  1921. current = next;
  1922. }
  1923. else // Bad byte will be handled by cmXMLSafe.
  1924. {
  1925. ++current;
  1926. }
  1927. }
  1928. output = output.substr(0, current - begin);
  1929. // Append truncation message.
  1930. std::ostringstream msg;
  1931. msg << "...\n"
  1932. "The rest of the test output was removed since it exceeds the threshold "
  1933. "of " << length << " bytes.\n";
  1934. output += msg.str();
  1935. return true;
  1936. }
  1937. //----------------------------------------------------------------------
  1938. bool cmCTestTestHandler::SetTestsProperties(
  1939. const std::vector<std::string>& args)
  1940. {
  1941. std::vector<std::string>::const_iterator it;
  1942. std::vector<std::string> tests;
  1943. bool found = false;
  1944. for ( it = args.begin(); it != args.end(); ++ it )
  1945. {
  1946. if ( *it == "PROPERTIES" )
  1947. {
  1948. found = true;
  1949. break;
  1950. }
  1951. tests.push_back(*it);
  1952. }
  1953. if ( !found )
  1954. {
  1955. return false;
  1956. }
  1957. ++ it; // skip PROPERTIES
  1958. for ( ; it != args.end(); ++ it )
  1959. {
  1960. std::string key = *it;
  1961. ++ it;
  1962. if ( it == args.end() )
  1963. {
  1964. break;
  1965. }
  1966. std::string val = *it;
  1967. std::vector<std::string>::const_iterator tit;
  1968. for ( tit = tests.begin(); tit != tests.end(); ++ tit )
  1969. {
  1970. cmCTestTestHandler::ListOfTests::iterator rtit;
  1971. for ( rtit = this->TestList.begin();
  1972. rtit != this->TestList.end();
  1973. ++ rtit )
  1974. {
  1975. if ( *tit == rtit->Name )
  1976. {
  1977. if ( key == "WILL_FAIL" )
  1978. {
  1979. rtit->WillFail = cmSystemTools::IsOn(val.c_str());
  1980. }
  1981. if ( key == "ATTACHED_FILES" )
  1982. {
  1983. cmSystemTools::ExpandListArgument(val, rtit->AttachedFiles);
  1984. }
  1985. if ( key == "ATTACHED_FILES_ON_FAIL" )
  1986. {
  1987. cmSystemTools::ExpandListArgument(val, rtit->AttachOnFail);
  1988. }
  1989. if ( key == "RESOURCE_LOCK" )
  1990. {
  1991. std::vector<std::string> lval;
  1992. cmSystemTools::ExpandListArgument(val, lval);
  1993. rtit->LockedResources.insert(lval.begin(), lval.end());
  1994. }
  1995. if ( key == "TIMEOUT" )
  1996. {
  1997. rtit->Timeout = atof(val.c_str());
  1998. rtit->ExplicitTimeout = true;
  1999. }
  2000. if ( key == "COST" )
  2001. {
  2002. rtit->Cost = static_cast<float>(atof(val.c_str()));
  2003. }
  2004. if ( key == "REQUIRED_FILES" )
  2005. {
  2006. cmSystemTools::ExpandListArgument(val, rtit->RequiredFiles);
  2007. }
  2008. if ( key == "RUN_SERIAL" )
  2009. {
  2010. rtit->RunSerial = cmSystemTools::IsOn(val.c_str());
  2011. }
  2012. if ( key == "FAIL_REGULAR_EXPRESSION" )
  2013. {
  2014. std::vector<std::string> lval;
  2015. cmSystemTools::ExpandListArgument(val, lval);
  2016. std::vector<std::string>::iterator crit;
  2017. for ( crit = lval.begin(); crit != lval.end(); ++ crit )
  2018. {
  2019. rtit->ErrorRegularExpressions.push_back(
  2020. std::pair<cmsys::RegularExpression, std::string>(
  2021. cmsys::RegularExpression(crit->c_str()),
  2022. std::string(*crit)));
  2023. }
  2024. }
  2025. if ( key == "PROCESSORS" )
  2026. {
  2027. rtit->Processors = atoi(val.c_str());
  2028. if(rtit->Processors < 1)
  2029. {
  2030. rtit->Processors = 1;
  2031. }
  2032. }
  2033. if ( key == "SKIP_RETURN_CODE" )
  2034. {
  2035. rtit->SkipReturnCode = atoi(val.c_str());
  2036. if(rtit->SkipReturnCode < 0 || rtit->SkipReturnCode > 255)
  2037. {
  2038. rtit->SkipReturnCode = -1;
  2039. }
  2040. }
  2041. if ( key == "DEPENDS" )
  2042. {
  2043. cmSystemTools::ExpandListArgument(val, rtit->Depends);
  2044. }
  2045. if ( key == "ENVIRONMENT" )
  2046. {
  2047. cmSystemTools::ExpandListArgument(val, rtit->Environment);
  2048. }
  2049. if ( key == "LABELS" )
  2050. {
  2051. cmSystemTools::ExpandListArgument(val, rtit->Labels);
  2052. }
  2053. if ( key == "MEASUREMENT" )
  2054. {
  2055. size_t pos = val.find_first_of("=");
  2056. if ( pos != val.npos )
  2057. {
  2058. std::string mKey = val.substr(0, pos);
  2059. const char* mVal = val.c_str() + pos + 1;
  2060. rtit->Measurements[mKey] = mVal;
  2061. }
  2062. else
  2063. {
  2064. rtit->Measurements[val] = "1";
  2065. }
  2066. }
  2067. if ( key == "PASS_REGULAR_EXPRESSION" )
  2068. {
  2069. std::vector<std::string> lval;
  2070. cmSystemTools::ExpandListArgument(val, lval);
  2071. std::vector<std::string>::iterator crit;
  2072. for ( crit = lval.begin(); crit != lval.end(); ++ crit )
  2073. {
  2074. rtit->RequiredRegularExpressions.push_back(
  2075. std::pair<cmsys::RegularExpression, std::string>(
  2076. cmsys::RegularExpression(crit->c_str()),
  2077. std::string(*crit)));
  2078. }
  2079. }
  2080. if ( key == "WORKING_DIRECTORY" )
  2081. {
  2082. rtit->Directory = val;
  2083. }
  2084. }
  2085. }
  2086. }
  2087. }
  2088. return true;
  2089. }
  2090. //----------------------------------------------------------------------
  2091. bool cmCTestTestHandler::AddTest(const std::vector<std::string>& args)
  2092. {
  2093. const std::string& testname = args[0];
  2094. cmCTestOptionalLog(this->CTest, DEBUG, "Add test: " << args[0] << std::endl,
  2095. this->Quiet);
  2096. if (this->UseExcludeRegExpFlag &&
  2097. this->UseExcludeRegExpFirst &&
  2098. this->ExcludeTestsRegularExpression.find(testname.c_str()))
  2099. {
  2100. return true;
  2101. }
  2102. if ( this->MemCheck )
  2103. {
  2104. std::vector<std::string>::iterator it;
  2105. bool found = false;
  2106. for ( it = this->CustomTestsIgnore.begin();
  2107. it != this->CustomTestsIgnore.end(); ++ it )
  2108. {
  2109. if ( *it == testname )
  2110. {
  2111. found = true;
  2112. break;
  2113. }
  2114. }
  2115. if ( found )
  2116. {
  2117. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  2118. "Ignore memcheck: " << *it << std::endl, this->Quiet);
  2119. return true;
  2120. }
  2121. }
  2122. else
  2123. {
  2124. std::vector<std::string>::iterator it;
  2125. bool found = false;
  2126. for ( it = this->CustomTestsIgnore.begin();
  2127. it != this->CustomTestsIgnore.end(); ++ it )
  2128. {
  2129. if ( *it == testname )
  2130. {
  2131. found = true;
  2132. break;
  2133. }
  2134. }
  2135. if ( found )
  2136. {
  2137. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Ignore test: "
  2138. << *it << std::endl, this->Quiet);
  2139. return true;
  2140. }
  2141. }
  2142. cmCTestTestProperties test;
  2143. test.Name = testname;
  2144. test.Args = args;
  2145. test.Directory = cmSystemTools::GetCurrentWorkingDirectory();
  2146. cmCTestOptionalLog(this->CTest, DEBUG, "Set test directory: "
  2147. << test.Directory << std::endl, this->Quiet);
  2148. test.IsInBasedOnREOptions = true;
  2149. test.WillFail = false;
  2150. test.RunSerial = false;
  2151. test.Timeout = 0;
  2152. test.ExplicitTimeout = false;
  2153. test.Cost = 0;
  2154. test.Processors = 1;
  2155. test.SkipReturnCode = -1;
  2156. test.PreviousRuns = 0;
  2157. if (this->UseIncludeRegExpFlag &&
  2158. !this->IncludeTestsRegularExpression.find(testname.c_str()))
  2159. {
  2160. test.IsInBasedOnREOptions = false;
  2161. }
  2162. else if (this->UseExcludeRegExpFlag &&
  2163. !this->UseExcludeRegExpFirst &&
  2164. this->ExcludeTestsRegularExpression.find(testname.c_str()))
  2165. {
  2166. test.IsInBasedOnREOptions = false;
  2167. }
  2168. this->TestList.push_back(test);
  2169. return true;
  2170. }