cmCTestTestHandler.cxx 71 KB

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