cmCTestTestHandler.cxx 80 KB

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