cmCTestTestHandler.cxx 80 KB

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