cmCTestTestHandler.cxx 79 KB

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