cmCTestTestHandler.cxx 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198
  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 <algorithm>
  5. #include <cmsys/Base64.h>
  6. #include <cmsys/Directory.hxx>
  7. #include <cmsys/FStream.hxx>
  8. #include <cmsys/RegularExpression.hxx>
  9. #include <functional>
  10. #include <iomanip>
  11. #include <iterator>
  12. #include <set>
  13. #include <sstream>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <time.h>
  18. #include "cmCTest.h"
  19. #include "cmCTestBatchTestHandler.h"
  20. #include "cmCTestMultiProcessHandler.h"
  21. #include "cmCommand.h"
  22. #include "cmGeneratedFileStream.h"
  23. #include "cmGlobalGenerator.h"
  24. #include "cmMakefile.h"
  25. #include "cmState.h"
  26. #include "cmStateSnapshot.h"
  27. #include "cmSystemTools.h"
  28. #include "cmWorkingDirectory.h"
  29. #include "cmXMLWriter.h"
  30. #include "cm_auto_ptr.hxx"
  31. #include "cm_utf8.h"
  32. #include "cmake.h"
  33. class cmExecutionStatus;
  34. class cmCTestSubdirCommand : public cmCommand
  35. {
  36. public:
  37. /**
  38. * This is a virtual constructor for the command.
  39. */
  40. cmCommand* Clone() CM_OVERRIDE
  41. {
  42. cmCTestSubdirCommand* c = new cmCTestSubdirCommand;
  43. c->TestHandler = this->TestHandler;
  44. return c;
  45. }
  46. /**
  47. * This is called when the command is first encountered in
  48. * the CMakeLists.txt file.
  49. */
  50. bool InitialPass(std::vector<std::string> const& args,
  51. cmExecutionStatus& /*unused*/) CM_OVERRIDE;
  52. /**
  53. * The name of the command as specified in CMakeList.txt.
  54. */
  55. std::string GetName() const CM_OVERRIDE { return "subdirs"; }
  56. cmCTestTestHandler* TestHandler;
  57. };
  58. bool cmCTestSubdirCommand::InitialPass(std::vector<std::string> const& args,
  59. cmExecutionStatus& /*unused*/)
  60. {
  61. if (args.empty()) {
  62. this->SetError("called with incorrect number of arguments");
  63. return false;
  64. }
  65. std::vector<std::string>::const_iterator it;
  66. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  67. for (it = args.begin(); it != args.end(); ++it) {
  68. std::string fname;
  69. if (cmSystemTools::FileIsFullPath(it->c_str())) {
  70. fname = *it;
  71. } else {
  72. fname = cwd;
  73. fname += "/";
  74. fname += *it;
  75. }
  76. if (!cmSystemTools::FileIsDirectory(fname)) {
  77. // No subdirectory? So what...
  78. continue;
  79. }
  80. bool readit = false;
  81. {
  82. cmWorkingDirectory workdir(fname);
  83. const char* testFilename;
  84. if (cmSystemTools::FileExists("CTestTestfile.cmake")) {
  85. // does the CTestTestfile.cmake exist ?
  86. testFilename = "CTestTestfile.cmake";
  87. } else if (cmSystemTools::FileExists("DartTestfile.txt")) {
  88. // does the DartTestfile.txt exist ?
  89. testFilename = "DartTestfile.txt";
  90. } else {
  91. // No CTestTestfile? Who cares...
  92. continue;
  93. }
  94. fname += "/";
  95. fname += testFilename;
  96. readit = this->Makefile->ReadDependentFile(fname.c_str());
  97. }
  98. if (!readit) {
  99. std::string m = "Could not find include file: ";
  100. m += fname;
  101. this->SetError(m);
  102. return false;
  103. }
  104. }
  105. return true;
  106. }
  107. class cmCTestAddSubdirectoryCommand : public cmCommand
  108. {
  109. public:
  110. /**
  111. * This is a virtual constructor for the command.
  112. */
  113. cmCommand* Clone() CM_OVERRIDE
  114. {
  115. cmCTestAddSubdirectoryCommand* c = new cmCTestAddSubdirectoryCommand;
  116. c->TestHandler = this->TestHandler;
  117. return c;
  118. }
  119. /**
  120. * This is called when the command is first encountered in
  121. * the CMakeLists.txt file.
  122. */
  123. bool InitialPass(std::vector<std::string> const& args,
  124. cmExecutionStatus& /*unused*/) CM_OVERRIDE;
  125. /**
  126. * The name of the command as specified in CMakeList.txt.
  127. */
  128. std::string GetName() const CM_OVERRIDE { return "add_subdirectory"; }
  129. cmCTestTestHandler* TestHandler;
  130. };
  131. bool cmCTestAddSubdirectoryCommand::InitialPass(
  132. std::vector<std::string> const& args, cmExecutionStatus& /*unused*/)
  133. {
  134. if (args.empty()) {
  135. this->SetError("called with incorrect number of arguments");
  136. return false;
  137. }
  138. std::string fname = cmSystemTools::GetCurrentWorkingDirectory();
  139. fname += "/";
  140. fname += args[0];
  141. if (!cmSystemTools::FileExists(fname.c_str())) {
  142. // No subdirectory? So what...
  143. return true;
  144. }
  145. bool readit = false;
  146. {
  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. return true;
  157. }
  158. fname += "/";
  159. fname += testFilename;
  160. readit = this->Makefile->ReadDependentFile(fname.c_str());
  161. }
  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 ListOfTests::const_iterator TestIterator;
  712. typedef std::multimap<std::string, TestIterator> FixtureDependencies;
  713. typedef FixtureDependencies::const_iterator FixtureDepsIterator;
  714. FixtureDependencies fixtureSetups;
  715. FixtureDependencies fixtureDeps;
  716. for (ListOfTests::const_iterator it = this->TestList.begin();
  717. it != this->TestList.end(); ++it) {
  718. const cmCTestTestProperties& p = *it;
  719. const std::set<std::string>& setups = p.FixturesSetup;
  720. for (std::set<std::string>::const_iterator depsIt = setups.begin();
  721. depsIt != setups.end(); ++depsIt) {
  722. fixtureSetups.insert(std::make_pair(*depsIt, it));
  723. fixtureDeps.insert(std::make_pair(*depsIt, it));
  724. }
  725. const std::set<std::string>& cleanups = p.FixturesCleanup;
  726. for (std::set<std::string>::const_iterator depsIt = cleanups.begin();
  727. depsIt != cleanups.end(); ++depsIt) {
  728. fixtureDeps.insert(std::make_pair(*depsIt, it));
  729. }
  730. }
  731. // Prepare fast lookup of tests already included in our list of tests
  732. std::set<std::string> addedTests;
  733. for (ListOfTests::const_iterator it = tests.begin(); it != tests.end();
  734. ++it) {
  735. const cmCTestTestProperties& p = *it;
  736. addedTests.insert(p.Name);
  737. }
  738. // These are lookups of fixture name to a list of indices into the final
  739. // tests array for tests which require that fixture and tests which are
  740. // setups for that fixture. They are needed at the end to populate
  741. // dependencies of the cleanup tests in our final list of tests.
  742. std::map<std::string, std::vector<size_t> > fixtureRequirements;
  743. std::map<std::string, std::vector<size_t> > setupFixturesAdded;
  744. // Use integer index for iteration because we append to
  745. // the tests vector as we go
  746. size_t fixtureTestsAdded = 0;
  747. std::set<std::string> addedFixtures;
  748. for (size_t i = 0; i < tests.size(); ++i) {
  749. // There are two things to do for each test:
  750. // 1. For every fixture required by this test, record that fixture as
  751. // being required and create dependencies on that fixture's setup
  752. // tests.
  753. // 2. Record all setup tests in the final test list so we can later make
  754. // cleanup tests in the test list depend on their associated setup
  755. // tests to enforce correct ordering.
  756. // 1. Handle fixture requirements
  757. //
  758. // Must copy the set of fixtures required because we may invalidate
  759. // the tests array by appending to it
  760. std::set<std::string> fixtures = tests[i].FixturesRequired;
  761. for (std::set<std::string>::const_iterator fixturesIt = fixtures.begin();
  762. fixturesIt != fixtures.end(); ++fixturesIt) {
  763. const std::string& requiredFixtureName = *fixturesIt;
  764. if (requiredFixtureName.empty()) {
  765. continue;
  766. }
  767. fixtureRequirements[requiredFixtureName].push_back(i);
  768. // Add dependencies to this test for all of the setup tests
  769. // associated with the required fixture. If any of those setup
  770. // tests fail, this test should not run. We make the fixture's
  771. // cleanup tests depend on this test case later.
  772. std::pair<FixtureDepsIterator, FixtureDepsIterator> setupRange =
  773. fixtureSetups.equal_range(requiredFixtureName);
  774. for (FixtureDepsIterator sIt = setupRange.first;
  775. sIt != setupRange.second; ++sIt) {
  776. const std::string& setupTestName = sIt->second->Name;
  777. tests[i].RequireSuccessDepends.insert(setupTestName);
  778. if (std::find(tests[i].Depends.begin(), tests[i].Depends.end(),
  779. setupTestName) == tests[i].Depends.end()) {
  780. tests[i].Depends.push_back(setupTestName);
  781. }
  782. }
  783. // Append any fixture setup/cleanup tests to our test list if they
  784. // are not already in it (they could have been in the original
  785. // set of tests passed to us at the outset or have already been
  786. // added from a previously checked test). A fixture isn't required
  787. // to have setup/cleanup tests.
  788. if (!addedFixtures.insert(requiredFixtureName).second) {
  789. // Already added this fixture
  790. continue;
  791. }
  792. std::pair<FixtureDepsIterator, FixtureDepsIterator> fixtureRange =
  793. fixtureDeps.equal_range(requiredFixtureName);
  794. for (FixtureDepsIterator it = fixtureRange.first;
  795. it != fixtureRange.second; ++it) {
  796. ListOfTests::const_iterator lotIt = it->second;
  797. const cmCTestTestProperties& p = *lotIt;
  798. if (!addedTests.insert(p.Name).second) {
  799. // Already have p in our test list
  800. continue;
  801. }
  802. // This is a test not yet in our list, so add it and
  803. // update its index to reflect where it was in the original
  804. // full list of all tests (needed to track individual tests
  805. // across ctest runs for re-run failed, etc.)
  806. tests.push_back(p);
  807. tests.back().Index =
  808. 1 + static_cast<int>(std::distance(this->TestList.begin(), lotIt));
  809. ++fixtureTestsAdded;
  810. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Added test "
  811. << p.Name << " required by fixture "
  812. << requiredFixtureName << std::endl,
  813. this->Quiet);
  814. }
  815. }
  816. // 2. Record all setup fixtures included in the final list of tests
  817. for (std::set<std::string>::const_iterator fixturesIt =
  818. tests[i].FixturesSetup.begin();
  819. fixturesIt != tests[i].FixturesSetup.end(); ++fixturesIt) {
  820. const std::string& setupFixtureName = *fixturesIt;
  821. if (setupFixtureName.empty()) {
  822. continue;
  823. }
  824. setupFixturesAdded[setupFixtureName].push_back(i);
  825. }
  826. }
  827. // Now that we have the final list of tests, we can update all cleanup
  828. // tests to depend on those tests which require that fixture and on any
  829. // setup tests for that fixture. The latter is required to handle the
  830. // pathological case where setup and cleanup tests are in the test set
  831. // but no other test has that fixture as a requirement.
  832. for (ListOfTests::iterator tIt = tests.begin(); tIt != tests.end(); ++tIt) {
  833. cmCTestTestProperties& p = *tIt;
  834. const std::set<std::string>& cleanups = p.FixturesCleanup;
  835. for (std::set<std::string>::const_iterator fIt = cleanups.begin();
  836. fIt != cleanups.end(); ++fIt) {
  837. const std::string& fixture = *fIt;
  838. // This cleanup test could be part of the original test list that was
  839. // passed in. It is then possible that no other test requires the
  840. // fIt fixture, so we have to check for this.
  841. std::map<std::string, std::vector<size_t> >::const_iterator cIt =
  842. fixtureRequirements.find(fixture);
  843. if (cIt != fixtureRequirements.end()) {
  844. const std::vector<size_t>& indices = cIt->second;
  845. for (std::vector<size_t>::const_iterator indexIt = indices.begin();
  846. indexIt != indices.end(); ++indexIt) {
  847. const std::string& reqTestName = tests[*indexIt].Name;
  848. if (std::find(p.Depends.begin(), p.Depends.end(), reqTestName) ==
  849. p.Depends.end()) {
  850. p.Depends.push_back(reqTestName);
  851. }
  852. }
  853. }
  854. // Ensure fixture cleanup tests always run after their setup tests, even
  855. // if no other test cases require the fixture
  856. cIt = setupFixturesAdded.find(fixture);
  857. if (cIt != setupFixturesAdded.end()) {
  858. const std::vector<size_t>& indices = cIt->second;
  859. for (std::vector<size_t>::const_iterator indexIt = indices.begin();
  860. indexIt != indices.end(); ++indexIt) {
  861. const std::string& setupTestName = tests[*indexIt].Name;
  862. if (std::find(p.Depends.begin(), p.Depends.end(), setupTestName) ==
  863. p.Depends.end()) {
  864. p.Depends.push_back(setupTestName);
  865. }
  866. }
  867. }
  868. }
  869. }
  870. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Added "
  871. << fixtureTestsAdded
  872. << " tests to meet fixture requirements" << std::endl,
  873. this->Quiet);
  874. }
  875. void cmCTestTestHandler::UpdateMaxTestNameWidth()
  876. {
  877. std::string::size_type max = this->CTest->GetMaxTestNameWidth();
  878. for (cmCTestTestHandler::ListOfTests::iterator it = this->TestList.begin();
  879. it != this->TestList.end(); it++) {
  880. cmCTestTestProperties& p = *it;
  881. if (max < p.Name.size()) {
  882. max = p.Name.size();
  883. }
  884. }
  885. if (static_cast<std::string::size_type>(
  886. this->CTest->GetMaxTestNameWidth()) != max) {
  887. this->CTest->SetMaxTestNameWidth(static_cast<int>(max));
  888. }
  889. }
  890. bool cmCTestTestHandler::GetValue(const char* tag, int& value,
  891. std::istream& fin)
  892. {
  893. std::string line;
  894. bool ret = true;
  895. cmSystemTools::GetLineFromStream(fin, line);
  896. if (line == tag) {
  897. fin >> value;
  898. ret = cmSystemTools::GetLineFromStream(fin, line); // read blank line
  899. } else {
  900. cmCTestLog(this->CTest, ERROR_MESSAGE, "parse error: missing tag: "
  901. << tag << " found [" << line << "]" << std::endl);
  902. ret = false;
  903. }
  904. return ret;
  905. }
  906. bool cmCTestTestHandler::GetValue(const char* tag, double& value,
  907. std::istream& fin)
  908. {
  909. std::string line;
  910. cmSystemTools::GetLineFromStream(fin, line);
  911. bool ret = true;
  912. if (line == tag) {
  913. fin >> value;
  914. ret = cmSystemTools::GetLineFromStream(fin, line); // read blank line
  915. } else {
  916. cmCTestLog(this->CTest, ERROR_MESSAGE, "parse error: missing tag: "
  917. << tag << " found [" << line << "]" << std::endl);
  918. ret = false;
  919. }
  920. return ret;
  921. }
  922. bool cmCTestTestHandler::GetValue(const char* tag, bool& value,
  923. std::istream& fin)
  924. {
  925. std::string line;
  926. cmSystemTools::GetLineFromStream(fin, line);
  927. bool ret = true;
  928. if (line == tag) {
  929. #ifdef __HAIKU__
  930. int tmp = 0;
  931. fin >> tmp;
  932. value = false;
  933. if (tmp) {
  934. value = true;
  935. }
  936. #else
  937. fin >> value;
  938. #endif
  939. ret = cmSystemTools::GetLineFromStream(fin, line); // read blank line
  940. } else {
  941. cmCTestLog(this->CTest, ERROR_MESSAGE, "parse error: missing tag: "
  942. << tag << " found [" << line << "]" << std::endl);
  943. ret = false;
  944. }
  945. return ret;
  946. }
  947. bool cmCTestTestHandler::GetValue(const char* tag, size_t& value,
  948. std::istream& fin)
  949. {
  950. std::string line;
  951. cmSystemTools::GetLineFromStream(fin, line);
  952. bool ret = true;
  953. if (line == tag) {
  954. fin >> value;
  955. ret = cmSystemTools::GetLineFromStream(fin, line); // read blank line
  956. } else {
  957. cmCTestLog(this->CTest, ERROR_MESSAGE, "parse error: missing tag: "
  958. << tag << " found [" << line << "]" << std::endl);
  959. ret = false;
  960. }
  961. return ret;
  962. }
  963. bool cmCTestTestHandler::GetValue(const char* tag, std::string& value,
  964. std::istream& fin)
  965. {
  966. std::string line;
  967. cmSystemTools::GetLineFromStream(fin, line);
  968. bool ret = true;
  969. if (line == tag) {
  970. ret = cmSystemTools::GetLineFromStream(fin, value);
  971. } else {
  972. cmCTestLog(this->CTest, ERROR_MESSAGE, "parse error: missing tag: "
  973. << tag << " found [" << line << "]" << std::endl);
  974. ret = false;
  975. }
  976. return ret;
  977. }
  978. void cmCTestTestHandler::ProcessDirectory(std::vector<std::string>& passed,
  979. std::vector<std::string>& failed)
  980. {
  981. this->ComputeTestList();
  982. this->StartTest = this->CTest->CurrentTime();
  983. this->StartTestTime = static_cast<unsigned int>(cmSystemTools::GetTime());
  984. double elapsed_time_start = cmSystemTools::GetTime();
  985. cmCTestMultiProcessHandler* parallel = this->CTest->GetBatchJobs()
  986. ? new cmCTestBatchTestHandler
  987. : new cmCTestMultiProcessHandler;
  988. parallel->SetCTest(this->CTest);
  989. parallel->SetParallelLevel(this->CTest->GetParallelLevel());
  990. parallel->SetTestHandler(this);
  991. parallel->SetQuiet(this->Quiet);
  992. if (this->TestLoad > 0) {
  993. parallel->SetTestLoad(this->TestLoad);
  994. } else {
  995. parallel->SetTestLoad(this->CTest->GetTestLoad());
  996. }
  997. *this->LogFile
  998. << "Start testing: " << this->CTest->CurrentTime() << std::endl
  999. << "----------------------------------------------------------"
  1000. << std::endl;
  1001. cmCTestMultiProcessHandler::TestMap tests;
  1002. cmCTestMultiProcessHandler::PropertiesMap properties;
  1003. bool randomSchedule = this->CTest->GetScheduleType() == "Random";
  1004. if (randomSchedule) {
  1005. srand((unsigned)time(CM_NULLPTR));
  1006. }
  1007. for (ListOfTests::iterator it = this->TestList.begin();
  1008. it != this->TestList.end(); ++it) {
  1009. cmCTestTestProperties& p = *it;
  1010. cmCTestMultiProcessHandler::TestSet depends;
  1011. if (randomSchedule) {
  1012. p.Cost = static_cast<float>(rand());
  1013. }
  1014. if (p.Timeout == 0 && this->CTest->GetGlobalTimeout() != 0) {
  1015. p.Timeout = this->CTest->GetGlobalTimeout();
  1016. }
  1017. if (!p.Depends.empty()) {
  1018. for (std::vector<std::string>::iterator i = p.Depends.begin();
  1019. i != p.Depends.end(); ++i) {
  1020. for (ListOfTests::iterator it2 = this->TestList.begin();
  1021. it2 != this->TestList.end(); ++it2) {
  1022. if (it2->Name == *i) {
  1023. depends.insert(it2->Index);
  1024. break; // break out of test loop as name can only match 1
  1025. }
  1026. }
  1027. }
  1028. }
  1029. tests[it->Index] = depends;
  1030. properties[it->Index] = &*it;
  1031. }
  1032. parallel->SetTests(tests, properties);
  1033. parallel->SetPassFailVectors(&passed, &failed);
  1034. this->TestResults.clear();
  1035. parallel->SetTestResults(&this->TestResults);
  1036. if (this->CTest->ShouldPrintLabels()) {
  1037. parallel->PrintLabels();
  1038. } else if (this->CTest->GetShowOnly()) {
  1039. parallel->PrintTestList();
  1040. } else {
  1041. parallel->RunTests();
  1042. }
  1043. delete parallel;
  1044. this->EndTest = this->CTest->CurrentTime();
  1045. this->EndTestTime = static_cast<unsigned int>(cmSystemTools::GetTime());
  1046. this->ElapsedTestingTime = cmSystemTools::GetTime() - elapsed_time_start;
  1047. *this->LogFile << "End testing: " << this->CTest->CurrentTime() << std::endl;
  1048. }
  1049. void cmCTestTestHandler::GenerateTestCommand(
  1050. std::vector<std::string>& /*unused*/, int /*unused*/)
  1051. {
  1052. }
  1053. void cmCTestTestHandler::GenerateDartOutput(cmXMLWriter& xml)
  1054. {
  1055. if (!this->CTest->GetProduceXML()) {
  1056. return;
  1057. }
  1058. this->CTest->StartXML(xml, this->AppendXML);
  1059. xml.StartElement("Testing");
  1060. xml.Element("StartDateTime", this->StartTest);
  1061. xml.Element("StartTestTime", this->StartTestTime);
  1062. xml.StartElement("TestList");
  1063. cmCTestTestHandler::TestResultsVector::size_type cc;
  1064. for (cc = 0; cc < this->TestResults.size(); cc++) {
  1065. cmCTestTestResult* result = &this->TestResults[cc];
  1066. std::string testPath = result->Path + "/" + result->Name;
  1067. xml.Element("Test", this->CTest->GetShortPathToFile(testPath.c_str()));
  1068. }
  1069. xml.EndElement(); // TestList
  1070. for (cc = 0; cc < this->TestResults.size(); cc++) {
  1071. cmCTestTestResult* result = &this->TestResults[cc];
  1072. this->WriteTestResultHeader(xml, result);
  1073. xml.StartElement("Results");
  1074. if (result->Status != cmCTestTestHandler::NOT_RUN) {
  1075. if (result->Status != cmCTestTestHandler::COMPLETED ||
  1076. result->ReturnValue) {
  1077. xml.StartElement("NamedMeasurement");
  1078. xml.Attribute("type", "text/string");
  1079. xml.Attribute("name", "Exit Code");
  1080. xml.Element("Value", this->GetTestStatus(result->Status));
  1081. xml.EndElement(); // NamedMeasurement
  1082. xml.StartElement("NamedMeasurement");
  1083. xml.Attribute("type", "text/string");
  1084. xml.Attribute("name", "Exit Value");
  1085. xml.Element("Value", result->ReturnValue);
  1086. xml.EndElement(); // NamedMeasurement
  1087. }
  1088. this->GenerateRegressionImages(xml, result->DartString);
  1089. xml.StartElement("NamedMeasurement");
  1090. xml.Attribute("type", "numeric/double");
  1091. xml.Attribute("name", "Execution Time");
  1092. xml.Element("Value", result->ExecutionTime);
  1093. xml.EndElement(); // NamedMeasurement
  1094. if (!result->Reason.empty()) {
  1095. const char* reasonType = "Pass Reason";
  1096. if (result->Status != cmCTestTestHandler::COMPLETED) {
  1097. reasonType = "Fail Reason";
  1098. }
  1099. xml.StartElement("NamedMeasurement");
  1100. xml.Attribute("type", "text/string");
  1101. xml.Attribute("name", reasonType);
  1102. xml.Element("Value", result->Reason);
  1103. xml.EndElement(); // NamedMeasurement
  1104. }
  1105. }
  1106. xml.StartElement("NamedMeasurement");
  1107. xml.Attribute("type", "text/string");
  1108. xml.Attribute("name", "Completion Status");
  1109. xml.Element("Value", result->CompletionStatus);
  1110. xml.EndElement(); // NamedMeasurement
  1111. xml.StartElement("NamedMeasurement");
  1112. xml.Attribute("type", "text/string");
  1113. xml.Attribute("name", "Command Line");
  1114. xml.Element("Value", result->FullCommandLine);
  1115. xml.EndElement(); // NamedMeasurement
  1116. std::map<std::string, std::string>::iterator measureIt;
  1117. for (measureIt = result->Properties->Measurements.begin();
  1118. measureIt != result->Properties->Measurements.end(); ++measureIt) {
  1119. xml.StartElement("NamedMeasurement");
  1120. xml.Attribute("type", "text/string");
  1121. xml.Attribute("name", measureIt->first);
  1122. xml.Element("Value", measureIt->second);
  1123. xml.EndElement(); // NamedMeasurement
  1124. }
  1125. xml.StartElement("Measurement");
  1126. xml.StartElement("Value");
  1127. if (result->CompressOutput) {
  1128. xml.Attribute("encoding", "base64");
  1129. xml.Attribute("compression", "gzip");
  1130. }
  1131. xml.Content(result->Output);
  1132. xml.EndElement(); // Value
  1133. xml.EndElement(); // Measurement
  1134. xml.EndElement(); // Results
  1135. this->AttachFiles(xml, result);
  1136. this->WriteTestResultFooter(xml, result);
  1137. }
  1138. xml.Element("EndDateTime", this->EndTest);
  1139. xml.Element("EndTestTime", this->EndTestTime);
  1140. xml.Element("ElapsedMinutes",
  1141. static_cast<int>(this->ElapsedTestingTime / 6) / 10.0);
  1142. xml.EndElement(); // Testing
  1143. this->CTest->EndXML(xml);
  1144. }
  1145. void cmCTestTestHandler::WriteTestResultHeader(cmXMLWriter& xml,
  1146. cmCTestTestResult* result)
  1147. {
  1148. xml.StartElement("Test");
  1149. if (result->Status == cmCTestTestHandler::COMPLETED) {
  1150. xml.Attribute("Status", "passed");
  1151. } else if (result->Status == cmCTestTestHandler::NOT_RUN) {
  1152. xml.Attribute("Status", "notrun");
  1153. } else {
  1154. xml.Attribute("Status", "failed");
  1155. }
  1156. std::string testPath = result->Path + "/" + result->Name;
  1157. xml.Element("Name", result->Name);
  1158. xml.Element("Path", this->CTest->GetShortPathToFile(result->Path.c_str()));
  1159. xml.Element("FullName", this->CTest->GetShortPathToFile(testPath.c_str()));
  1160. xml.Element("FullCommandLine", result->FullCommandLine);
  1161. }
  1162. void cmCTestTestHandler::WriteTestResultFooter(cmXMLWriter& xml,
  1163. cmCTestTestResult* result)
  1164. {
  1165. if (!result->Properties->Labels.empty()) {
  1166. xml.StartElement("Labels");
  1167. std::vector<std::string> const& labels = result->Properties->Labels;
  1168. for (std::vector<std::string>::const_iterator li = labels.begin();
  1169. li != labels.end(); ++li) {
  1170. xml.Element("Label", *li);
  1171. }
  1172. xml.EndElement(); // Labels
  1173. }
  1174. xml.EndElement(); // Test
  1175. }
  1176. void cmCTestTestHandler::AttachFiles(cmXMLWriter& xml,
  1177. cmCTestTestResult* result)
  1178. {
  1179. if (result->Status != cmCTestTestHandler::COMPLETED &&
  1180. !result->Properties->AttachOnFail.empty()) {
  1181. result->Properties->AttachedFiles.insert(
  1182. result->Properties->AttachedFiles.end(),
  1183. result->Properties->AttachOnFail.begin(),
  1184. result->Properties->AttachOnFail.end());
  1185. }
  1186. for (std::vector<std::string>::const_iterator file =
  1187. result->Properties->AttachedFiles.begin();
  1188. file != result->Properties->AttachedFiles.end(); ++file) {
  1189. const std::string& base64 = this->CTest->Base64GzipEncodeFile(*file);
  1190. std::string fname = cmSystemTools::GetFilenameName(*file);
  1191. xml.StartElement("NamedMeasurement");
  1192. xml.Attribute("name", "Attached File");
  1193. xml.Attribute("encoding", "base64");
  1194. xml.Attribute("compression", "tar/gzip");
  1195. xml.Attribute("filename", fname);
  1196. xml.Attribute("type", "file");
  1197. xml.Element("Value", base64);
  1198. xml.EndElement(); // NamedMeasurement
  1199. }
  1200. }
  1201. int cmCTestTestHandler::ExecuteCommands(std::vector<std::string>& vec)
  1202. {
  1203. std::vector<std::string>::iterator it;
  1204. for (it = vec.begin(); it != vec.end(); ++it) {
  1205. int retVal = 0;
  1206. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1207. "Run command: " << *it << std::endl, this->Quiet);
  1208. if (!cmSystemTools::RunSingleCommand(it->c_str(), CM_NULLPTR, CM_NULLPTR,
  1209. &retVal, CM_NULLPTR,
  1210. cmSystemTools::OUTPUT_MERGE
  1211. /*this->Verbose*/) ||
  1212. retVal != 0) {
  1213. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1214. "Problem running command: " << *it << std::endl);
  1215. return 0;
  1216. }
  1217. }
  1218. return 1;
  1219. }
  1220. // Find the appropriate executable to run for a test
  1221. std::string cmCTestTestHandler::FindTheExecutable(const char* exe)
  1222. {
  1223. std::string resConfig;
  1224. std::vector<std::string> extraPaths;
  1225. std::vector<std::string> failedPaths;
  1226. if (strcmp(exe, "NOT_AVAILABLE") == 0) {
  1227. return exe;
  1228. }
  1229. return cmCTestTestHandler::FindExecutable(this->CTest, exe, resConfig,
  1230. extraPaths, failedPaths);
  1231. }
  1232. // add additional configurations to the search path
  1233. void cmCTestTestHandler::AddConfigurations(
  1234. cmCTest* ctest, std::vector<std::string>& attempted,
  1235. std::vector<std::string>& attemptedConfigs, std::string filepath,
  1236. std::string& filename)
  1237. {
  1238. std::string tempPath;
  1239. if (!filepath.empty() && filepath[filepath.size() - 1] != '/') {
  1240. filepath += "/";
  1241. }
  1242. tempPath = filepath + filename;
  1243. attempted.push_back(tempPath);
  1244. attemptedConfigs.push_back("");
  1245. if (!ctest->GetConfigType().empty()) {
  1246. tempPath = filepath;
  1247. tempPath += ctest->GetConfigType();
  1248. tempPath += "/";
  1249. tempPath += filename;
  1250. attempted.push_back(tempPath);
  1251. attemptedConfigs.push_back(ctest->GetConfigType());
  1252. // If the file is an OSX bundle then the configtype
  1253. // will be at the start of the path
  1254. tempPath = ctest->GetConfigType();
  1255. tempPath += "/";
  1256. tempPath += filepath;
  1257. tempPath += filename;
  1258. attempted.push_back(tempPath);
  1259. attemptedConfigs.push_back(ctest->GetConfigType());
  1260. } else {
  1261. // no config specified - try some options...
  1262. tempPath = filepath;
  1263. tempPath += "Release/";
  1264. tempPath += filename;
  1265. attempted.push_back(tempPath);
  1266. attemptedConfigs.push_back("Release");
  1267. tempPath = filepath;
  1268. tempPath += "Debug/";
  1269. tempPath += filename;
  1270. attempted.push_back(tempPath);
  1271. attemptedConfigs.push_back("Debug");
  1272. tempPath = filepath;
  1273. tempPath += "MinSizeRel/";
  1274. tempPath += filename;
  1275. attempted.push_back(tempPath);
  1276. attemptedConfigs.push_back("MinSizeRel");
  1277. tempPath = filepath;
  1278. tempPath += "RelWithDebInfo/";
  1279. tempPath += filename;
  1280. attempted.push_back(tempPath);
  1281. attemptedConfigs.push_back("RelWithDebInfo");
  1282. tempPath = filepath;
  1283. tempPath += "Deployment/";
  1284. tempPath += filename;
  1285. attempted.push_back(tempPath);
  1286. attemptedConfigs.push_back("Deployment");
  1287. tempPath = filepath;
  1288. tempPath += "Development/";
  1289. tempPath += filename;
  1290. attempted.push_back(tempPath);
  1291. attemptedConfigs.push_back("Deployment");
  1292. }
  1293. }
  1294. // Find the appropriate executable to run for a test
  1295. std::string cmCTestTestHandler::FindExecutable(
  1296. cmCTest* ctest, const char* testCommand, std::string& resultingConfig,
  1297. std::vector<std::string>& extraPaths, std::vector<std::string>& failed)
  1298. {
  1299. // now run the compiled test if we can find it
  1300. std::vector<std::string> attempted;
  1301. std::vector<std::string> attemptedConfigs;
  1302. std::string tempPath;
  1303. std::string filepath = cmSystemTools::GetFilenamePath(testCommand);
  1304. std::string filename = cmSystemTools::GetFilenameName(testCommand);
  1305. cmCTestTestHandler::AddConfigurations(ctest, attempted, attemptedConfigs,
  1306. filepath, filename);
  1307. // even if a fullpath was specified also try it relative to the current
  1308. // directory
  1309. if (!filepath.empty() && filepath[0] == '/') {
  1310. std::string localfilepath = filepath.substr(1, filepath.size() - 1);
  1311. cmCTestTestHandler::AddConfigurations(ctest, attempted, attemptedConfigs,
  1312. localfilepath, filename);
  1313. }
  1314. // if extraPaths are provided and we were not passed a full path, try them,
  1315. // try any extra paths
  1316. if (filepath.empty()) {
  1317. for (unsigned int i = 0; i < extraPaths.size(); ++i) {
  1318. std::string filepathExtra =
  1319. cmSystemTools::GetFilenamePath(extraPaths[i]);
  1320. std::string filenameExtra =
  1321. cmSystemTools::GetFilenameName(extraPaths[i]);
  1322. cmCTestTestHandler::AddConfigurations(ctest, attempted, attemptedConfigs,
  1323. filepathExtra, filenameExtra);
  1324. }
  1325. }
  1326. // store the final location in fullPath
  1327. std::string fullPath;
  1328. // now look in the paths we specified above
  1329. for (unsigned int ai = 0; ai < attempted.size() && fullPath.empty(); ++ai) {
  1330. // first check without exe extension
  1331. if (cmSystemTools::FileExists(attempted[ai].c_str()) &&
  1332. !cmSystemTools::FileIsDirectory(attempted[ai])) {
  1333. fullPath = cmSystemTools::CollapseFullPath(attempted[ai]);
  1334. resultingConfig = attemptedConfigs[ai];
  1335. }
  1336. // then try with the exe extension
  1337. else {
  1338. failed.push_back(attempted[ai]);
  1339. tempPath = attempted[ai];
  1340. tempPath += cmSystemTools::GetExecutableExtension();
  1341. if (cmSystemTools::FileExists(tempPath.c_str()) &&
  1342. !cmSystemTools::FileIsDirectory(tempPath)) {
  1343. fullPath = cmSystemTools::CollapseFullPath(tempPath);
  1344. resultingConfig = attemptedConfigs[ai];
  1345. } else {
  1346. failed.push_back(tempPath);
  1347. }
  1348. }
  1349. }
  1350. // if everything else failed, check the users path, but only if a full path
  1351. // wasn't specified
  1352. if (fullPath.empty() && filepath.empty()) {
  1353. std::string path = cmSystemTools::FindProgram(filename.c_str());
  1354. if (path != "") {
  1355. resultingConfig = "";
  1356. return path;
  1357. }
  1358. }
  1359. if (fullPath.empty()) {
  1360. cmCTestLog(ctest, HANDLER_OUTPUT, "Could not find executable "
  1361. << testCommand << "\n"
  1362. << "Looked in the following places:\n");
  1363. for (std::vector<std::string>::iterator i = failed.begin();
  1364. i != failed.end(); ++i) {
  1365. cmCTestLog(ctest, HANDLER_OUTPUT, *i << "\n");
  1366. }
  1367. }
  1368. return fullPath;
  1369. }
  1370. void cmCTestTestHandler::GetListOfTests()
  1371. {
  1372. if (!this->IncludeLabelRegExp.empty()) {
  1373. this->IncludeLabelRegularExpression.compile(
  1374. this->IncludeLabelRegExp.c_str());
  1375. }
  1376. if (!this->ExcludeLabelRegExp.empty()) {
  1377. this->ExcludeLabelRegularExpression.compile(
  1378. this->ExcludeLabelRegExp.c_str());
  1379. }
  1380. if (!this->IncludeRegExp.empty()) {
  1381. this->IncludeTestsRegularExpression.compile(this->IncludeRegExp.c_str());
  1382. }
  1383. if (!this->ExcludeRegExp.empty()) {
  1384. this->ExcludeTestsRegularExpression.compile(this->ExcludeRegExp.c_str());
  1385. }
  1386. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1387. "Constructing a list of tests" << std::endl, this->Quiet);
  1388. cmake cm;
  1389. cm.SetHomeDirectory("");
  1390. cm.SetHomeOutputDirectory("");
  1391. cm.GetCurrentSnapshot().SetDefaultDefinitions();
  1392. cmGlobalGenerator gg(&cm);
  1393. CM_AUTO_PTR<cmMakefile> mf(new cmMakefile(&gg, cm.GetCurrentSnapshot()));
  1394. mf->AddDefinition("CTEST_CONFIGURATION_TYPE",
  1395. this->CTest->GetConfigType().c_str());
  1396. // Add handler for ADD_TEST
  1397. cmCTestAddTestCommand* newCom1 = new cmCTestAddTestCommand;
  1398. newCom1->TestHandler = this;
  1399. cm.GetState()->AddCommand(newCom1);
  1400. // Add handler for SUBDIRS
  1401. cmCTestSubdirCommand* newCom2 = new cmCTestSubdirCommand;
  1402. newCom2->TestHandler = this;
  1403. cm.GetState()->AddCommand(newCom2);
  1404. // Add handler for ADD_SUBDIRECTORY
  1405. cmCTestAddSubdirectoryCommand* newCom3 = new cmCTestAddSubdirectoryCommand;
  1406. newCom3->TestHandler = this;
  1407. cm.GetState()->AddCommand(newCom3);
  1408. // Add handler for SET_SOURCE_FILES_PROPERTIES
  1409. cmCTestSetTestsPropertiesCommand* newCom4 =
  1410. new cmCTestSetTestsPropertiesCommand;
  1411. newCom4->TestHandler = this;
  1412. cm.GetState()->AddCommand(newCom4);
  1413. const char* testFilename;
  1414. if (cmSystemTools::FileExists("CTestTestfile.cmake")) {
  1415. // does the CTestTestfile.cmake exist ?
  1416. testFilename = "CTestTestfile.cmake";
  1417. } else if (cmSystemTools::FileExists("DartTestfile.txt")) {
  1418. // does the DartTestfile.txt exist ?
  1419. testFilename = "DartTestfile.txt";
  1420. } else {
  1421. return;
  1422. }
  1423. if (!mf->ReadListFile(testFilename)) {
  1424. return;
  1425. }
  1426. if (cmSystemTools::GetErrorOccuredFlag()) {
  1427. return;
  1428. }
  1429. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1430. "Done constructing a list of tests" << std::endl,
  1431. this->Quiet);
  1432. }
  1433. void cmCTestTestHandler::UseIncludeRegExp()
  1434. {
  1435. this->UseIncludeRegExpFlag = true;
  1436. }
  1437. void cmCTestTestHandler::UseExcludeRegExp()
  1438. {
  1439. this->UseExcludeRegExpFlag = true;
  1440. this->UseExcludeRegExpFirst = !this->UseIncludeRegExpFlag;
  1441. }
  1442. const char* cmCTestTestHandler::GetTestStatus(int status)
  1443. {
  1444. static const char statuses[][100] = {
  1445. "Not Run", "Timeout", "SEGFAULT", "ILLEGAL", "INTERRUPT",
  1446. "NUMERICAL", "OTHER_FAULT", "Failed", "BAD_COMMAND", "Completed"
  1447. };
  1448. if (status < cmCTestTestHandler::NOT_RUN ||
  1449. status > cmCTestTestHandler::COMPLETED) {
  1450. return "No Status";
  1451. }
  1452. return statuses[status];
  1453. }
  1454. void cmCTestTestHandler::ExpandTestsToRunInformation(size_t numTests)
  1455. {
  1456. if (this->TestsToRunString.empty()) {
  1457. return;
  1458. }
  1459. int start;
  1460. int end = -1;
  1461. double stride = -1;
  1462. std::string::size_type pos = 0;
  1463. std::string::size_type pos2;
  1464. // read start
  1465. if (GetNextNumber(this->TestsToRunString, start, pos, pos2)) {
  1466. // read end
  1467. if (GetNextNumber(this->TestsToRunString, end, pos, pos2)) {
  1468. // read stride
  1469. if (GetNextRealNumber(this->TestsToRunString, stride, pos, pos2)) {
  1470. int val = 0;
  1471. // now read specific numbers
  1472. while (GetNextNumber(this->TestsToRunString, val, pos, pos2)) {
  1473. this->TestsToRun.push_back(val);
  1474. }
  1475. this->TestsToRun.push_back(val);
  1476. }
  1477. }
  1478. }
  1479. // if start is not specified then we assume we start at 1
  1480. if (start == -1) {
  1481. start = 1;
  1482. }
  1483. // if end isnot specified then we assume we end with the last test
  1484. if (end == -1) {
  1485. end = static_cast<int>(numTests);
  1486. }
  1487. // if the stride wasn't specified then it defaults to 1
  1488. if (stride == -1) {
  1489. stride = 1;
  1490. }
  1491. // if we have a range then add it
  1492. if (end != -1 && start != -1 && stride > 0) {
  1493. int i = 0;
  1494. while (i * stride + start <= end) {
  1495. this->TestsToRun.push_back(static_cast<int>(i * stride + start));
  1496. ++i;
  1497. }
  1498. }
  1499. // sort the array
  1500. std::sort(this->TestsToRun.begin(), this->TestsToRun.end(),
  1501. std::less<int>());
  1502. // remove duplicates
  1503. std::vector<int>::iterator new_end =
  1504. std::unique(this->TestsToRun.begin(), this->TestsToRun.end());
  1505. this->TestsToRun.erase(new_end, this->TestsToRun.end());
  1506. }
  1507. void cmCTestTestHandler::ExpandTestsToRunInformationForRerunFailed()
  1508. {
  1509. std::string dirName = this->CTest->GetBinaryDir() + "/Testing/Temporary";
  1510. cmsys::Directory directory;
  1511. if (directory.Load(dirName) == 0) {
  1512. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unable to read the contents of "
  1513. << dirName << std::endl);
  1514. return;
  1515. }
  1516. int numFiles =
  1517. static_cast<int>(cmsys::Directory::GetNumberOfFilesInDirectory(dirName));
  1518. std::string pattern = "LastTestsFailed";
  1519. std::string logName;
  1520. for (int i = 0; i < numFiles; ++i) {
  1521. std::string fileName = directory.GetFile(i);
  1522. // bcc crashes if we attempt a normal substring comparison,
  1523. // hence the following workaround
  1524. std::string fileNameSubstring = fileName.substr(0, pattern.length());
  1525. if (fileNameSubstring.compare(pattern) != 0) {
  1526. continue;
  1527. }
  1528. if (logName == "") {
  1529. logName = fileName;
  1530. } else {
  1531. // if multiple matching logs were found we use the most recently
  1532. // modified one.
  1533. int res;
  1534. cmSystemTools::FileTimeCompare(logName, fileName, &res);
  1535. if (res == -1) {
  1536. logName = fileName;
  1537. }
  1538. }
  1539. }
  1540. std::string lastTestsFailedLog =
  1541. this->CTest->GetBinaryDir() + "/Testing/Temporary/" + logName;
  1542. if (!cmSystemTools::FileExists(lastTestsFailedLog.c_str())) {
  1543. if (!this->CTest->GetShowOnly() && !this->CTest->ShouldPrintLabels()) {
  1544. cmCTestLog(this->CTest, ERROR_MESSAGE, lastTestsFailedLog
  1545. << " does not exist!" << std::endl);
  1546. }
  1547. return;
  1548. }
  1549. // parse the list of tests to rerun from LastTestsFailed.log
  1550. cmsys::ifstream ifs(lastTestsFailedLog.c_str());
  1551. if (ifs) {
  1552. std::string line;
  1553. std::string::size_type pos;
  1554. while (cmSystemTools::GetLineFromStream(ifs, line)) {
  1555. pos = line.find(':', 0);
  1556. if (pos == line.npos) {
  1557. continue;
  1558. }
  1559. int val = atoi(line.substr(0, pos).c_str());
  1560. this->TestsToRun.push_back(val);
  1561. }
  1562. ifs.close();
  1563. } else if (!this->CTest->GetShowOnly() &&
  1564. !this->CTest->ShouldPrintLabels()) {
  1565. cmCTestLog(this->CTest, ERROR_MESSAGE, "Problem reading file: "
  1566. << lastTestsFailedLog
  1567. << " while generating list of previously failed tests."
  1568. << std::endl);
  1569. }
  1570. }
  1571. // Just for convenience
  1572. #define SPACE_REGEX "[ \t\r\n]"
  1573. void cmCTestTestHandler::GenerateRegressionImages(cmXMLWriter& xml,
  1574. const std::string& dart)
  1575. {
  1576. cmsys::RegularExpression twoattributes(
  1577. "<DartMeasurement" SPACE_REGEX
  1578. "*(name|type|encoding|compression)=\"([^\"]*)\"" SPACE_REGEX
  1579. "*(name|type|encoding|compression)=\"([^\"]*)\"" SPACE_REGEX
  1580. "*>([^<]*)</DartMeasurement>");
  1581. cmsys::RegularExpression threeattributes(
  1582. "<DartMeasurement" SPACE_REGEX
  1583. "*(name|type|encoding|compression)=\"([^\"]*)\"" SPACE_REGEX
  1584. "*(name|type|encoding|compression)=\"([^\"]*)\"" SPACE_REGEX
  1585. "*(name|type|encoding|compression)=\"([^\"]*)\"" SPACE_REGEX
  1586. "*>([^<]*)</DartMeasurement>");
  1587. cmsys::RegularExpression fourattributes(
  1588. "<DartMeasurement" SPACE_REGEX
  1589. "*(name|type|encoding|compression)=\"([^\"]*)\"" SPACE_REGEX
  1590. "*(name|type|encoding|compression)=\"([^\"]*)\"" SPACE_REGEX
  1591. "*(name|type|encoding|compression)=\"([^\"]*)\"" SPACE_REGEX
  1592. "*(name|type|encoding|compression)=\"([^\"]*)\"" SPACE_REGEX
  1593. "*>([^<]*)</DartMeasurement>");
  1594. cmsys::RegularExpression cdatastart(
  1595. "<DartMeasurement" SPACE_REGEX
  1596. "*(name|type|encoding|compression)=\"([^\"]*)\"" SPACE_REGEX
  1597. "*(name|type|encoding|compression)=\"([^\"]*)\"" SPACE_REGEX
  1598. "*>" SPACE_REGEX "*<!\\[CDATA\\[");
  1599. cmsys::RegularExpression cdataend("]]>" SPACE_REGEX "*</DartMeasurement>");
  1600. cmsys::RegularExpression measurementfile(
  1601. "<DartMeasurementFile" SPACE_REGEX
  1602. "*(name|type|encoding|compression)=\"([^\"]*)\"" SPACE_REGEX
  1603. "*(name|type|encoding|compression)=\"([^\"]*)\"" SPACE_REGEX
  1604. "*>([^<]*)</DartMeasurementFile>");
  1605. bool done = false;
  1606. std::string cxml = dart;
  1607. while (!done) {
  1608. if (twoattributes.find(cxml)) {
  1609. xml.StartElement("NamedMeasurement");
  1610. xml.Attribute(twoattributes.match(1).c_str(), twoattributes.match(2));
  1611. xml.Attribute(twoattributes.match(3).c_str(), twoattributes.match(4));
  1612. xml.Element("Value", twoattributes.match(5));
  1613. xml.EndElement();
  1614. cxml.erase(twoattributes.start(),
  1615. twoattributes.end() - twoattributes.start());
  1616. } else if (threeattributes.find(cxml)) {
  1617. xml.StartElement("NamedMeasurement");
  1618. xml.Attribute(threeattributes.match(1).c_str(),
  1619. threeattributes.match(2));
  1620. xml.Attribute(threeattributes.match(3).c_str(),
  1621. threeattributes.match(4));
  1622. xml.Attribute(threeattributes.match(5).c_str(),
  1623. threeattributes.match(6));
  1624. xml.Element("Value", twoattributes.match(7));
  1625. xml.EndElement();
  1626. cxml.erase(threeattributes.start(),
  1627. threeattributes.end() - threeattributes.start());
  1628. } else if (fourattributes.find(cxml)) {
  1629. xml.StartElement("NamedMeasurement");
  1630. xml.Attribute(fourattributes.match(1).c_str(), fourattributes.match(2));
  1631. xml.Attribute(fourattributes.match(3).c_str(), fourattributes.match(4));
  1632. xml.Attribute(fourattributes.match(5).c_str(), fourattributes.match(6));
  1633. xml.Attribute(fourattributes.match(7).c_str(), fourattributes.match(8));
  1634. xml.Element("Value", twoattributes.match(9));
  1635. xml.EndElement();
  1636. cxml.erase(fourattributes.start(),
  1637. fourattributes.end() - fourattributes.start());
  1638. } else if (cdatastart.find(cxml) && cdataend.find(cxml)) {
  1639. xml.StartElement("NamedMeasurement");
  1640. xml.Attribute(cdatastart.match(1).c_str(), cdatastart.match(2));
  1641. xml.Attribute(cdatastart.match(3).c_str(), cdatastart.match(4));
  1642. xml.StartElement("Value");
  1643. xml.CData(
  1644. cxml.substr(cdatastart.end(), cdataend.start() - cdatastart.end()));
  1645. xml.EndElement(); // Value
  1646. xml.EndElement(); // NamedMeasurement
  1647. cxml.erase(cdatastart.start(), cdataend.end() - cdatastart.start());
  1648. } else if (measurementfile.find(cxml)) {
  1649. const std::string& filename =
  1650. cmCTest::CleanString(measurementfile.match(5));
  1651. if (cmSystemTools::FileExists(filename.c_str())) {
  1652. long len = cmSystemTools::FileLength(filename);
  1653. if (len == 0) {
  1654. std::string k1 = measurementfile.match(1);
  1655. std::string v1 = measurementfile.match(2);
  1656. std::string k2 = measurementfile.match(3);
  1657. std::string v2 = measurementfile.match(4);
  1658. if (cmSystemTools::LowerCase(k1) == "type") {
  1659. v1 = "text/string";
  1660. }
  1661. if (cmSystemTools::LowerCase(k2) == "type") {
  1662. v2 = "text/string";
  1663. }
  1664. xml.StartElement("NamedMeasurement");
  1665. xml.Attribute(k1.c_str(), v1);
  1666. xml.Attribute(k2.c_str(), v2);
  1667. xml.Attribute("encoding", "none");
  1668. xml.Element("Value", "Image " + filename + " is empty");
  1669. xml.EndElement();
  1670. } else {
  1671. cmsys::ifstream ifs(filename.c_str(), std::ios::in
  1672. #ifdef _WIN32
  1673. | std::ios::binary
  1674. #endif
  1675. );
  1676. unsigned char* file_buffer = new unsigned char[len + 1];
  1677. ifs.read(reinterpret_cast<char*>(file_buffer), len);
  1678. unsigned char* encoded_buffer = new unsigned char[static_cast<int>(
  1679. static_cast<double>(len) * 1.5 + 5.0)];
  1680. size_t rlen =
  1681. cmsysBase64_Encode(file_buffer, len, encoded_buffer, 1);
  1682. xml.StartElement("NamedMeasurement");
  1683. xml.Attribute(measurementfile.match(1).c_str(),
  1684. measurementfile.match(2));
  1685. xml.Attribute(measurementfile.match(3).c_str(),
  1686. measurementfile.match(4));
  1687. xml.Attribute("encoding", "base64");
  1688. std::ostringstream ostr;
  1689. for (size_t cc = 0; cc < rlen; cc++) {
  1690. ostr << encoded_buffer[cc];
  1691. if (cc % 60 == 0 && cc) {
  1692. ostr << std::endl;
  1693. }
  1694. }
  1695. xml.Element("Value", ostr.str());
  1696. xml.EndElement(); // NamedMeasurement
  1697. delete[] file_buffer;
  1698. delete[] encoded_buffer;
  1699. }
  1700. } else {
  1701. int idx = 4;
  1702. if (measurementfile.match(1) == "name") {
  1703. idx = 2;
  1704. }
  1705. xml.StartElement("NamedMeasurement");
  1706. xml.Attribute("name", measurementfile.match(idx));
  1707. xml.Attribute("text", "text/string");
  1708. xml.Element("Value", "File " + filename + " not found");
  1709. xml.EndElement();
  1710. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "File \""
  1711. << filename << "\" not found." << std::endl,
  1712. this->Quiet);
  1713. }
  1714. cxml.erase(measurementfile.start(),
  1715. measurementfile.end() - measurementfile.start());
  1716. } else {
  1717. done = true;
  1718. }
  1719. }
  1720. }
  1721. void cmCTestTestHandler::SetIncludeRegExp(const char* arg)
  1722. {
  1723. this->IncludeRegExp = arg;
  1724. }
  1725. void cmCTestTestHandler::SetExcludeRegExp(const char* arg)
  1726. {
  1727. this->ExcludeRegExp = arg;
  1728. }
  1729. void cmCTestTestHandler::SetTestsToRunInformation(const char* in)
  1730. {
  1731. if (!in) {
  1732. return;
  1733. }
  1734. this->TestsToRunString = in;
  1735. // if the argument is a file, then read it and use the contents as the
  1736. // string
  1737. if (cmSystemTools::FileExists(in)) {
  1738. cmsys::ifstream fin(in);
  1739. unsigned long filelen = cmSystemTools::FileLength(in);
  1740. char* buff = new char[filelen + 1];
  1741. fin.getline(buff, filelen);
  1742. buff[fin.gcount()] = 0;
  1743. this->TestsToRunString = buff;
  1744. delete[] buff;
  1745. }
  1746. }
  1747. bool cmCTestTestHandler::CleanTestOutput(std::string& output, size_t length)
  1748. {
  1749. if (!length || length >= output.size() ||
  1750. output.find("CTEST_FULL_OUTPUT") != output.npos) {
  1751. return true;
  1752. }
  1753. // Truncate at given length but do not break in the middle of a multi-byte
  1754. // UTF-8 encoding.
  1755. char const* const begin = output.c_str();
  1756. char const* const end = begin + output.size();
  1757. char const* const truncate = begin + length;
  1758. char const* current = begin;
  1759. while (current < truncate) {
  1760. unsigned int ch;
  1761. if (const char* next = cm_utf8_decode_character(current, end, &ch)) {
  1762. if (next > truncate) {
  1763. break;
  1764. }
  1765. current = next;
  1766. } else // Bad byte will be handled by cmXMLWriter.
  1767. {
  1768. ++current;
  1769. }
  1770. }
  1771. output = output.substr(0, current - begin);
  1772. // Append truncation message.
  1773. std::ostringstream msg;
  1774. msg << "...\n"
  1775. "The rest of the test output was removed since it exceeds the "
  1776. "threshold "
  1777. "of "
  1778. << length << " bytes.\n";
  1779. output += msg.str();
  1780. return true;
  1781. }
  1782. bool cmCTestTestHandler::SetTestsProperties(
  1783. const std::vector<std::string>& args)
  1784. {
  1785. std::vector<std::string>::const_iterator it;
  1786. std::vector<std::string> tests;
  1787. bool found = false;
  1788. for (it = args.begin(); it != args.end(); ++it) {
  1789. if (*it == "PROPERTIES") {
  1790. found = true;
  1791. break;
  1792. }
  1793. tests.push_back(*it);
  1794. }
  1795. if (!found) {
  1796. return false;
  1797. }
  1798. ++it; // skip PROPERTIES
  1799. for (; it != args.end(); ++it) {
  1800. std::string key = *it;
  1801. ++it;
  1802. if (it == args.end()) {
  1803. break;
  1804. }
  1805. std::string val = *it;
  1806. std::vector<std::string>::const_iterator tit;
  1807. for (tit = tests.begin(); tit != tests.end(); ++tit) {
  1808. cmCTestTestHandler::ListOfTests::iterator rtit;
  1809. for (rtit = this->TestList.begin(); rtit != this->TestList.end();
  1810. ++rtit) {
  1811. if (*tit == rtit->Name) {
  1812. if (key == "WILL_FAIL") {
  1813. rtit->WillFail = cmSystemTools::IsOn(val.c_str());
  1814. }
  1815. if (key == "ATTACHED_FILES") {
  1816. cmSystemTools::ExpandListArgument(val, rtit->AttachedFiles);
  1817. }
  1818. if (key == "ATTACHED_FILES_ON_FAIL") {
  1819. cmSystemTools::ExpandListArgument(val, rtit->AttachOnFail);
  1820. }
  1821. if (key == "RESOURCE_LOCK") {
  1822. std::vector<std::string> lval;
  1823. cmSystemTools::ExpandListArgument(val, lval);
  1824. rtit->LockedResources.insert(lval.begin(), lval.end());
  1825. }
  1826. if (key == "FIXTURES_SETUP") {
  1827. std::vector<std::string> lval;
  1828. cmSystemTools::ExpandListArgument(val, lval);
  1829. rtit->FixturesSetup.insert(lval.begin(), lval.end());
  1830. }
  1831. if (key == "FIXTURES_CLEANUP") {
  1832. std::vector<std::string> lval;
  1833. cmSystemTools::ExpandListArgument(val, lval);
  1834. rtit->FixturesCleanup.insert(lval.begin(), lval.end());
  1835. }
  1836. if (key == "FIXTURES_REQUIRED") {
  1837. std::vector<std::string> lval;
  1838. cmSystemTools::ExpandListArgument(val, lval);
  1839. rtit->FixturesRequired.insert(lval.begin(), lval.end());
  1840. }
  1841. if (key == "TIMEOUT") {
  1842. rtit->Timeout = atof(val.c_str());
  1843. rtit->ExplicitTimeout = true;
  1844. }
  1845. if (key == "COST") {
  1846. rtit->Cost = static_cast<float>(atof(val.c_str()));
  1847. }
  1848. if (key == "REQUIRED_FILES") {
  1849. cmSystemTools::ExpandListArgument(val, rtit->RequiredFiles);
  1850. }
  1851. if (key == "RUN_SERIAL") {
  1852. rtit->RunSerial = cmSystemTools::IsOn(val.c_str());
  1853. }
  1854. if (key == "FAIL_REGULAR_EXPRESSION") {
  1855. std::vector<std::string> lval;
  1856. cmSystemTools::ExpandListArgument(val, lval);
  1857. std::vector<std::string>::iterator crit;
  1858. for (crit = lval.begin(); crit != lval.end(); ++crit) {
  1859. rtit->ErrorRegularExpressions.push_back(
  1860. std::pair<cmsys::RegularExpression, std::string>(
  1861. cmsys::RegularExpression(crit->c_str()),
  1862. std::string(*crit)));
  1863. }
  1864. }
  1865. if (key == "PROCESSORS") {
  1866. rtit->Processors = atoi(val.c_str());
  1867. if (rtit->Processors < 1) {
  1868. rtit->Processors = 1;
  1869. }
  1870. }
  1871. if (key == "SKIP_RETURN_CODE") {
  1872. rtit->SkipReturnCode = atoi(val.c_str());
  1873. if (rtit->SkipReturnCode < 0 || rtit->SkipReturnCode > 255) {
  1874. rtit->SkipReturnCode = -1;
  1875. }
  1876. }
  1877. if (key == "DEPENDS") {
  1878. cmSystemTools::ExpandListArgument(val, rtit->Depends);
  1879. }
  1880. if (key == "ENVIRONMENT") {
  1881. cmSystemTools::ExpandListArgument(val, rtit->Environment);
  1882. }
  1883. if (key == "LABELS") {
  1884. cmSystemTools::ExpandListArgument(val, rtit->Labels);
  1885. }
  1886. if (key == "MEASUREMENT") {
  1887. size_t pos = val.find_first_of('=');
  1888. if (pos != val.npos) {
  1889. std::string mKey = val.substr(0, pos);
  1890. const char* mVal = val.c_str() + pos + 1;
  1891. rtit->Measurements[mKey] = mVal;
  1892. } else {
  1893. rtit->Measurements[val] = "1";
  1894. }
  1895. }
  1896. if (key == "PASS_REGULAR_EXPRESSION") {
  1897. std::vector<std::string> lval;
  1898. cmSystemTools::ExpandListArgument(val, lval);
  1899. std::vector<std::string>::iterator crit;
  1900. for (crit = lval.begin(); crit != lval.end(); ++crit) {
  1901. rtit->RequiredRegularExpressions.push_back(
  1902. std::pair<cmsys::RegularExpression, std::string>(
  1903. cmsys::RegularExpression(crit->c_str()),
  1904. std::string(*crit)));
  1905. }
  1906. }
  1907. if (key == "WORKING_DIRECTORY") {
  1908. rtit->Directory = val;
  1909. }
  1910. if (key == "TIMEOUT_AFTER_MATCH") {
  1911. std::vector<std::string> propArgs;
  1912. cmSystemTools::ExpandListArgument(val, propArgs);
  1913. if (propArgs.size() != 2) {
  1914. cmCTestLog(this->CTest, WARNING,
  1915. "TIMEOUT_AFTER_MATCH expects two arguments, found "
  1916. << propArgs.size() << std::endl);
  1917. } else {
  1918. rtit->AlternateTimeout = atof(propArgs[0].c_str());
  1919. std::vector<std::string> lval;
  1920. cmSystemTools::ExpandListArgument(propArgs[1], lval);
  1921. std::vector<std::string>::iterator crit;
  1922. for (crit = lval.begin(); crit != lval.end(); ++crit) {
  1923. rtit->TimeoutRegularExpressions.push_back(
  1924. std::pair<cmsys::RegularExpression, std::string>(
  1925. cmsys::RegularExpression(crit->c_str()),
  1926. std::string(*crit)));
  1927. }
  1928. }
  1929. }
  1930. }
  1931. }
  1932. }
  1933. }
  1934. return true;
  1935. }
  1936. bool cmCTestTestHandler::AddTest(const std::vector<std::string>& args)
  1937. {
  1938. const std::string& testname = args[0];
  1939. cmCTestOptionalLog(this->CTest, DEBUG, "Add test: " << args[0] << std::endl,
  1940. this->Quiet);
  1941. if (this->UseExcludeRegExpFlag && this->UseExcludeRegExpFirst &&
  1942. this->ExcludeTestsRegularExpression.find(testname.c_str())) {
  1943. return true;
  1944. }
  1945. if (this->MemCheck) {
  1946. std::vector<std::string>::iterator it;
  1947. bool found = false;
  1948. for (it = this->CustomTestsIgnore.begin();
  1949. it != this->CustomTestsIgnore.end(); ++it) {
  1950. if (*it == testname) {
  1951. found = true;
  1952. break;
  1953. }
  1954. }
  1955. if (found) {
  1956. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1957. "Ignore memcheck: " << *it << std::endl, this->Quiet);
  1958. return true;
  1959. }
  1960. } else {
  1961. std::vector<std::string>::iterator it;
  1962. bool found = false;
  1963. for (it = this->CustomTestsIgnore.begin();
  1964. it != this->CustomTestsIgnore.end(); ++it) {
  1965. if (*it == testname) {
  1966. found = true;
  1967. break;
  1968. }
  1969. }
  1970. if (found) {
  1971. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1972. "Ignore test: " << *it << std::endl, this->Quiet);
  1973. return true;
  1974. }
  1975. }
  1976. cmCTestTestProperties test;
  1977. test.Name = testname;
  1978. test.Args = args;
  1979. test.Directory = cmSystemTools::GetCurrentWorkingDirectory();
  1980. cmCTestOptionalLog(this->CTest, DEBUG,
  1981. "Set test directory: " << test.Directory << std::endl,
  1982. this->Quiet);
  1983. test.IsInBasedOnREOptions = true;
  1984. test.WillFail = false;
  1985. test.RunSerial = false;
  1986. test.Timeout = 0;
  1987. test.ExplicitTimeout = false;
  1988. test.Cost = 0;
  1989. test.Processors = 1;
  1990. test.SkipReturnCode = -1;
  1991. test.PreviousRuns = 0;
  1992. if (this->UseIncludeRegExpFlag &&
  1993. !this->IncludeTestsRegularExpression.find(testname.c_str())) {
  1994. test.IsInBasedOnREOptions = false;
  1995. } else if (this->UseExcludeRegExpFlag && !this->UseExcludeRegExpFirst &&
  1996. this->ExcludeTestsRegularExpression.find(testname.c_str())) {
  1997. test.IsInBasedOnREOptions = false;
  1998. }
  1999. this->TestList.push_back(test);
  2000. return true;
  2001. }