cmCTestTestHandler.cxx 79 KB

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