cmCTestTestHandler.cxx 81 KB

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