cmCTestTestHandler.cxx 79 KB

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