cmCTestTestHandler.cxx 68 KB

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