cmCTestTestHandler.cxx 81 KB

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