cmCTestTestHandler.cxx 79 KB

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