cmCTestTestHandler.cxx 80 KB

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