cmCTestTestHandler.cxx 64 KB

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