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