cmCTestTestHandler.cxx 80 KB

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