cmCTestTestHandler.cxx 79 KB

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