cmCTestTestHandler.cxx 73 KB

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