cmCTestTestHandler.cxx 80 KB

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