cmCTestTestHandler.cxx 79 KB

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