cmCTestTestHandler.cxx 70 KB

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