cmCTestTestHandler.cxx 67 KB

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