cmCTestTestHandler.cxx 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmCTestTestHandler.h"
  14. #include "cmCTest.h"
  15. #include "cmake.h"
  16. #include "cmGeneratedFileStream.h"
  17. #include <cmsys/Process.h>
  18. #include <cmsys/RegularExpression.hxx>
  19. #include <cmsys/Base64.h>
  20. #include "cmMakefile.h"
  21. #include "cmGlobalGenerator.h"
  22. #include "cmLocalGenerator.h"
  23. #include "cmCommand.h"
  24. #include "cmSystemTools.h"
  25. #include <stdlib.h>
  26. #include <math.h>
  27. #include <float.h>
  28. #include <memory> // auto_ptr
  29. //----------------------------------------------------------------------
  30. class cmCTestSubdirCommand : public cmCommand
  31. {
  32. public:
  33. /**
  34. * This is a virtual constructor for the command.
  35. */
  36. virtual cmCommand* Clone()
  37. {
  38. cmCTestSubdirCommand* c = new cmCTestSubdirCommand;
  39. c->TestHandler = this->TestHandler;
  40. return c;
  41. }
  42. /**
  43. * This is called when the command is first encountered in
  44. * the CMakeLists.txt file.
  45. */
  46. virtual bool InitialPass(std::vector<std::string> const& args);
  47. /**
  48. * The name of the command as specified in CMakeList.txt.
  49. */
  50. virtual const char* GetName() { return "SUBDIRS";}
  51. // Unused methods
  52. virtual const char* GetTerseDocumentation() { return ""; }
  53. virtual const char* GetFullDocumentation() { return ""; }
  54. cmTypeMacro(cmCTestSubdirCommand, cmCommand);
  55. cmCTestTestHandler* TestHandler;
  56. };
  57. //----------------------------------------------------------------------
  58. bool cmCTestSubdirCommand::InitialPass(std::vector<std::string> const& args)
  59. {
  60. #undef cout
  61. if(args.size() < 1 )
  62. {
  63. this->SetError("called with incorrect number of arguments");
  64. return false;
  65. }
  66. std::vector<std::string>::const_iterator it;
  67. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  68. std::cout << __LINE__ << " Current directory: " << cwd.c_str() << std::endl;
  69. for ( it = args.begin(); it != args.end(); ++ it )
  70. {
  71. cmSystemTools::ChangeDirectory(cwd.c_str());
  72. std::cout << __LINE__ << " Current directory: " << cwd.c_str() << std::endl;
  73. std::string fname = cwd;
  74. fname += "/";
  75. fname += *it;
  76. if ( !cmSystemTools::FileExists(fname.c_str()) )
  77. {
  78. // No subdirectory? So what...
  79. continue;
  80. }
  81. cmSystemTools::ChangeDirectory(fname.c_str());
  82. std::cout << __LINE__ << " Current directory: " << fname.c_str() << std::endl;
  83. const char* testFilename;
  84. if( cmSystemTools::FileExists("CTestTestfile.cmake") )
  85. {
  86. // does the CTestTestfile.cmake exist ?
  87. testFilename = "CTestTestfile.cmake";
  88. }
  89. else if( cmSystemTools::FileExists("DartTestfile.txt") )
  90. {
  91. // does the DartTestfile.txt exist ?
  92. testFilename = "DartTestfile.txt";
  93. }
  94. else
  95. {
  96. // No DartTestfile.txt? Who cares...
  97. cmSystemTools::ChangeDirectory(cwd.c_str());
  98. continue;
  99. }
  100. fname += "/";
  101. fname += testFilename;
  102. bool readit =
  103. this->Makefile->ReadListFile(this->Makefile->GetCurrentListFile(),
  104. fname.c_str());
  105. cmSystemTools::ChangeDirectory(cwd.c_str());
  106. if(!readit)
  107. {
  108. std::string m = "Could not find include file: ";
  109. m += fname;
  110. this->SetError(m.c_str());
  111. return false;
  112. }
  113. }
  114. return true;
  115. }
  116. //----------------------------------------------------------------------
  117. class cmCTestAddTestCommand : public cmCommand
  118. {
  119. public:
  120. /**
  121. * This is a virtual constructor for the command.
  122. */
  123. virtual cmCommand* Clone()
  124. {
  125. cmCTestAddTestCommand* c = new cmCTestAddTestCommand;
  126. c->TestHandler = this->TestHandler;
  127. return c;
  128. }
  129. /**
  130. * This is called when the command is first encountered in
  131. * the CMakeLists.txt file.
  132. */
  133. virtual bool InitialPass(std::vector<std::string> const&);
  134. /**
  135. * The name of the command as specified in CMakeList.txt.
  136. */
  137. virtual const char* GetName() { return "ADD_TEST";}
  138. // Unused methods
  139. virtual const char* GetTerseDocumentation() { return ""; }
  140. virtual const char* GetFullDocumentation() { return ""; }
  141. cmTypeMacro(cmCTestAddTestCommand, cmCommand);
  142. cmCTestTestHandler* TestHandler;
  143. };
  144. //----------------------------------------------------------------------
  145. bool cmCTestAddTestCommand::InitialPass(std::vector<std::string> const& args)
  146. {
  147. if ( args.size() < 2 )
  148. {
  149. this->SetError("called with incorrect number of arguments");
  150. return false;
  151. }
  152. return this->TestHandler->AddTest(args);
  153. }
  154. //----------------------------------------------------------------------
  155. class cmCTestSetTestsPropertiesCommand : public cmCommand
  156. {
  157. public:
  158. /**
  159. * This is a virtual constructor for the command.
  160. */
  161. virtual cmCommand* Clone()
  162. {
  163. cmCTestSetTestsPropertiesCommand* c
  164. = new cmCTestSetTestsPropertiesCommand;
  165. c->TestHandler = this->TestHandler;
  166. return c;
  167. }
  168. /**
  169. * This is called when the command is first encountered in
  170. * the CMakeLists.txt file.
  171. */
  172. virtual bool InitialPass(std::vector<std::string> const&);
  173. /**
  174. * The name of the command as specified in CMakeList.txt.
  175. */
  176. virtual const char* GetName() { return "SET_TESTS_PROPERTIES";}
  177. // Unused methods
  178. virtual const char* GetTerseDocumentation() { return ""; }
  179. virtual const char* GetFullDocumentation() { return ""; }
  180. cmTypeMacro(cmCTestSetTestsPropertiesCommand, cmCommand);
  181. cmCTestTestHandler* TestHandler;
  182. };
  183. //----------------------------------------------------------------------
  184. bool cmCTestSetTestsPropertiesCommand::InitialPass(
  185. std::vector<std::string> const& args)
  186. {
  187. return this->TestHandler->SetTestsProperties(args);
  188. }
  189. //----------------------------------------------------------------------
  190. // Try to find an executable, if found fullPath will be set to the full path
  191. // of where it was found. The directory and filename to search for are passed
  192. // in as well an a subdir (typically used for configuraitons such as
  193. // Release/Debug/etc)
  194. bool TryExecutable(const char *dir, const char *file,
  195. std::string *fullPath, const char *subdir)
  196. {
  197. // try current directory
  198. std::string tryPath;
  199. if (dir && strcmp(dir,""))
  200. {
  201. tryPath = dir;
  202. tryPath += "/";
  203. }
  204. if (subdir && strcmp(subdir,""))
  205. {
  206. tryPath += subdir;
  207. tryPath += "/";
  208. }
  209. tryPath += file;
  210. // find the file without an executable extension
  211. if(cmSystemTools::FileExists(tryPath.c_str()))
  212. {
  213. *fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  214. return true;
  215. }
  216. // if not found try it with the executable extension
  217. tryPath += cmSystemTools::GetExecutableExtension();
  218. if(cmSystemTools::FileExists(tryPath.c_str()))
  219. {
  220. *fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  221. return true;
  222. }
  223. // not found at all, return false
  224. return false;
  225. }
  226. //----------------------------------------------------------------------
  227. // get the next number in a string with numbers separated by ,
  228. // pos is the start of the search and pos2 is the end of the search
  229. // pos becomes pos2 after a call to GetNextNumber.
  230. // -1 is returned at the end of the list.
  231. inline int GetNextNumber(std::string const& in,
  232. int& val,
  233. std::string::size_type& pos,
  234. std::string::size_type& pos2)
  235. {
  236. pos2 = in.find(',', pos);
  237. if(pos2 != in.npos)
  238. {
  239. if(pos2-pos == 0)
  240. {
  241. val = -1;
  242. }
  243. else
  244. {
  245. val = atoi(in.substr(pos, pos2-pos).c_str());
  246. }
  247. pos = pos2+1;
  248. return 1;
  249. }
  250. else
  251. {
  252. if(in.size()-pos == 0)
  253. {
  254. val = -1;
  255. }
  256. else
  257. {
  258. val = atoi(in.substr(pos, in.size()-pos).c_str());
  259. }
  260. return 0;
  261. }
  262. }
  263. //----------------------------------------------------------------------
  264. // get the next number in a string with numbers separated by ,
  265. // pos is the start of the search and pos2 is the end of the search
  266. // pos becomes pos2 after a call to GetNextNumber.
  267. // -1 is returned at the end of the list.
  268. inline int GetNextRealNumber(std::string const& in,
  269. double& val,
  270. std::string::size_type& pos,
  271. std::string::size_type& pos2)
  272. {
  273. pos2 = in.find(',', pos);
  274. if(pos2 != in.npos)
  275. {
  276. if(pos2-pos == 0)
  277. {
  278. val = -1;
  279. }
  280. else
  281. {
  282. val = atof(in.substr(pos, pos2-pos).c_str());
  283. }
  284. pos = pos2+1;
  285. return 1;
  286. }
  287. else
  288. {
  289. if(in.size()-pos == 0)
  290. {
  291. val = -1;
  292. }
  293. else
  294. {
  295. val = atof(in.substr(pos, in.size()-pos).c_str());
  296. }
  297. return 0;
  298. }
  299. }
  300. //----------------------------------------------------------------------
  301. cmCTestTestHandler::cmCTestTestHandler()
  302. {
  303. this->UseUnion = false;
  304. this->UseIncludeRegExpFlag = false;
  305. this->UseExcludeRegExpFlag = false;
  306. this->UseExcludeRegExpFirst = false;
  307. this->CustomMaximumPassedTestOutputSize = 1 * 1024;
  308. this->CustomMaximumFailedTestOutputSize = 300 * 1024;
  309. this->MemCheck = false;
  310. this->LogFile = 0;
  311. this->DartStuff.compile("(<DartMeasurement.*/DartMeasurement[a-zA-Z]*>)");
  312. }
  313. //----------------------------------------------------------------------
  314. void cmCTestTestHandler::Initialize()
  315. {
  316. this->Superclass::Initialize();
  317. this->ElapsedTestingTime = -1;
  318. this->TestResults.clear();
  319. this->CustomTestsIgnore.clear();
  320. this->StartTest = "";
  321. this->EndTest = "";
  322. this->CustomPreTest.clear();
  323. this->CustomPostTest.clear();
  324. this->CustomMaximumPassedTestOutputSize = 1 * 1024;
  325. this->CustomMaximumFailedTestOutputSize = 300 * 1024;
  326. this->TestsToRun.clear();
  327. this->UseIncludeRegExpFlag = false;
  328. this->UseExcludeRegExpFlag = false;
  329. this->UseExcludeRegExpFirst = false;
  330. this->IncludeRegExp = "";
  331. this->ExcludeRegExp = "";
  332. TestsToRunString = "";
  333. this->UseUnion = false;
  334. this->TestList.clear();
  335. }
  336. //----------------------------------------------------------------------
  337. void cmCTestTestHandler::PopulateCustomVectors(cmMakefile *mf)
  338. {
  339. this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_PRE_TEST",
  340. this->CustomPreTest);
  341. this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_POST_TEST",
  342. this->CustomPostTest);
  343. this->CTest->PopulateCustomVector(mf,
  344. "CTEST_CUSTOM_TESTS_IGNORE",
  345. this->CustomTestsIgnore);
  346. this->CTest->PopulateCustomInteger(mf,
  347. "CTEST_CUSTOM_MAXIMUM_PASSED_TEST_OUTPUT_SIZE",
  348. this->CustomMaximumPassedTestOutputSize);
  349. this->CTest->PopulateCustomInteger(mf,
  350. "CTEST_CUSTOM_MAXIMUM_FAILED_TEST_OUTPUT_SIZE",
  351. this->CustomMaximumFailedTestOutputSize);
  352. }
  353. //----------------------------------------------------------------------
  354. int cmCTestTestHandler::PreProcessHandler()
  355. {
  356. if ( !this->ExecuteCommands(this->CustomPreTest) )
  357. {
  358. cmCTestLog(this->CTest, ERROR_MESSAGE,
  359. "Problem executing pre-test command(s)." << std::endl);
  360. return 0;
  361. }
  362. return 1;
  363. }
  364. //----------------------------------------------------------------------
  365. int cmCTestTestHandler::PostProcessHandler()
  366. {
  367. if ( !this->ExecuteCommands(this->CustomPostTest) )
  368. {
  369. cmCTestLog(this->CTest, ERROR_MESSAGE,
  370. "Problem executing post-test command(s)." << std::endl);
  371. return 0;
  372. }
  373. return 1;
  374. }
  375. //----------------------------------------------------------------------
  376. //clearly it would be nice if this were broken up into a few smaller
  377. //functions and commented...
  378. int cmCTestTestHandler::ProcessHandler()
  379. {
  380. // Update internal data structure from generic one
  381. this->SetTestsToRunInformation(this->GetOption("TestsToRunInformation"));
  382. this->SetUseUnion(cmSystemTools::IsOn(this->GetOption("UseUnion")));
  383. const char* val;
  384. val = this->GetOption("IncludeRegularExpression");
  385. if ( val )
  386. {
  387. this->UseIncludeRegExp();
  388. this->SetIncludeRegExp(val);
  389. }
  390. val = this->GetOption("ExcludeRegularExpression");
  391. if ( val )
  392. {
  393. this->UseExcludeRegExp();
  394. this->SetExcludeRegExp(val);
  395. }
  396. this->TestResults.clear();
  397. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  398. (this->MemCheck ? "Memory check" : "Test")
  399. << " project" << std::endl);
  400. if ( ! this->PreProcessHandler() )
  401. {
  402. return -1;
  403. }
  404. cmGeneratedFileStream mLogFile;
  405. this->StartLogFile((this->MemCheck ? "DynamicAnalysis" : "Test"), mLogFile);
  406. this->LogFile = &mLogFile;
  407. std::vector<cmStdString> passed;
  408. std::vector<cmStdString> failed;
  409. int total;
  410. this->ProcessDirectory(passed, failed);
  411. total = int(passed.size()) + int(failed.size());
  412. if (total == 0)
  413. {
  414. if ( !this->CTest->GetShowOnly() )
  415. {
  416. cmCTestLog(this->CTest, ERROR_MESSAGE, "No tests were found!!!"
  417. << std::endl);
  418. }
  419. }
  420. else
  421. {
  422. if (this->HandlerVerbose && passed.size() &&
  423. (this->UseIncludeRegExpFlag || this->UseExcludeRegExpFlag))
  424. {
  425. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl
  426. << "The following tests passed:" << std::endl);
  427. for(std::vector<cmStdString>::iterator j = passed.begin();
  428. j != passed.end(); ++j)
  429. {
  430. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "\t" << *j
  431. << std::endl);
  432. }
  433. }
  434. float percent = float(passed.size()) * 100.0f / total;
  435. if ( failed.size() > 0 && percent > 99)
  436. {
  437. percent = 99;
  438. }
  439. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl
  440. << static_cast<int>(percent + .5) << "% tests passed, "
  441. << failed.size() << " tests failed out of " << total << std::endl);
  442. //fprintf(stderr,"\n%.0f%% tests passed, %i tests failed out of %i\n",
  443. // percent, int(failed.size()), total);
  444. if (failed.size())
  445. {
  446. cmGeneratedFileStream ofs;
  447. cmCTestLog(this->CTest, ERROR_MESSAGE, std::endl
  448. << "The following tests FAILED:" << std::endl);
  449. this->StartLogFile("TestsFailed", ofs);
  450. std::vector<cmCTestTestHandler::cmCTestTestResult>::iterator ftit;
  451. for(ftit = this->TestResults.begin();
  452. ftit != this->TestResults.end(); ++ftit)
  453. {
  454. if ( ftit->Status != cmCTestTestHandler::COMPLETED )
  455. {
  456. ofs << ftit->TestCount << ":" << ftit->Name << std::endl;
  457. cmCTestLog(this->CTest, HANDLER_OUTPUT, "\t" << std::setw(3)
  458. << ftit->TestCount << " - " << ftit->Name.c_str() << " ("
  459. << this->GetTestStatus(ftit->Status) << ")" << std::endl);
  460. }
  461. }
  462. }
  463. }
  464. if ( this->CTest->GetProduceXML() )
  465. {
  466. cmGeneratedFileStream xmlfile;
  467. if( !this->StartResultingXML(
  468. (this->MemCheck ? "DynamicAnalysis" : "Test"), xmlfile) )
  469. {
  470. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot create "
  471. << (this->MemCheck ? "memory check" : "testing")
  472. << " XML file" << std::endl);
  473. this->LogFile = 0;
  474. return 1;
  475. }
  476. this->GenerateDartOutput(xmlfile);
  477. }
  478. if ( ! this->PostProcessHandler() )
  479. {
  480. this->LogFile = 0;
  481. return -1;
  482. }
  483. if ( !failed.empty() )
  484. {
  485. this->LogFile = 0;
  486. return -1;
  487. }
  488. this->LogFile = 0;
  489. return 0;
  490. }
  491. //----------------------------------------------------------------------
  492. void cmCTestTestHandler::ProcessDirectory(std::vector<cmStdString> &passed,
  493. std::vector<cmStdString> &failed)
  494. {
  495. std::string current_dir = cmSystemTools::GetCurrentWorkingDirectory();
  496. this->TestList.clear();
  497. this->GetListOfTests();
  498. cmCTestTestHandler::ListOfTests::size_type tmsize = this->TestList.size();
  499. this->StartTest = this->CTest->CurrentTime();
  500. double elapsed_time_start = cmSystemTools::GetTime();
  501. *this->LogFile << "Start testing: " << this->StartTest << std::endl
  502. << "----------------------------------------------------------"
  503. << std::endl;
  504. // how many tests are in based on RegExp?
  505. int inREcnt = 0;
  506. cmCTestTestHandler::ListOfTests::iterator it;
  507. for ( it = this->TestList.begin(); it != this->TestList.end(); it ++ )
  508. {
  509. if (it->IsInBasedOnREOptions)
  510. {
  511. inREcnt ++;
  512. }
  513. }
  514. // expand the test list based on the union flag
  515. if (this->UseUnion)
  516. {
  517. this->ExpandTestsToRunInformation((int)tmsize);
  518. }
  519. else
  520. {
  521. this->ExpandTestsToRunInformation(inREcnt);
  522. }
  523. int cnt = 0;
  524. inREcnt = 0;
  525. std::string last_directory = "";
  526. for ( it = this->TestList.begin(); it != this->TestList.end(); it ++ )
  527. {
  528. cnt ++;
  529. if (it->IsInBasedOnREOptions)
  530. {
  531. inREcnt++;
  532. }
  533. const std::string& testname = it->Name;
  534. std::vector<std::string>& args = it->Args;
  535. cmCTestTestResult cres;
  536. cres.ExecutionTime = 0;
  537. cres.ReturnValue = -1;
  538. cres.Status = cmCTestTestHandler::NOT_RUN;
  539. cres.TestCount = cnt;
  540. if (!(last_directory == it->Directory))
  541. {
  542. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  543. "Changing directory into " << it->Directory.c_str() << "\n");
  544. *this->LogFile << "Changing directory into: " << it->Directory.c_str()
  545. << std::endl;
  546. last_directory = it->Directory;
  547. cmSystemTools::ChangeDirectory(it->Directory.c_str());
  548. }
  549. cres.Name = testname;
  550. cres.Path = it->Directory.c_str();
  551. if (this->UseUnion)
  552. {
  553. // if it is not in the list and not in the regexp then skip
  554. if ((this->TestsToRun.size() &&
  555. std::find(this->TestsToRun.begin(), this->TestsToRun.end(), cnt)
  556. == this->TestsToRun.end()) && !it->IsInBasedOnREOptions)
  557. {
  558. continue;
  559. }
  560. }
  561. else
  562. {
  563. // is this test in the list of tests to run? If not then skip it
  564. if ((this->TestsToRun.size() &&
  565. std::find(this->TestsToRun.begin(),
  566. this->TestsToRun.end(), inREcnt)
  567. == this->TestsToRun.end()) || !it->IsInBasedOnREOptions)
  568. {
  569. continue;
  570. }
  571. }
  572. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(3) << cnt << "/");
  573. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(3) << tmsize << " ");
  574. if ( this->MemCheck )
  575. {
  576. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Memory Check");
  577. }
  578. else
  579. {
  580. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Testing");
  581. }
  582. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  583. std::string outname = testname;
  584. outname.resize(30, ' ');
  585. *this->LogFile << cnt << "/" << tmsize << " Testing: " << testname
  586. << std::endl;
  587. if ( this->CTest->GetShowOnly() )
  588. {
  589. cmCTestLog(this->CTest, HANDLER_OUTPUT, outname.c_str() << std::endl);
  590. }
  591. else
  592. {
  593. cmCTestLog(this->CTest, HANDLER_OUTPUT, outname.c_str());
  594. }
  595. cmCTestLog(this->CTest, DEBUG, "Testing " << args[0].c_str() << " ... ");
  596. // find the test executable
  597. std::string actualCommand = this->FindTheExecutable(args[1].c_str());
  598. std::string testCommand
  599. = cmSystemTools::ConvertToOutputPath(actualCommand.c_str());
  600. // continue if we did not find the executable
  601. if (testCommand == "")
  602. {
  603. *this->LogFile << "Unable to find executable: " << args[1].c_str()
  604. << std::endl;
  605. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unable to find executable: "
  606. << args[1].c_str() << std::endl);
  607. if ( !this->CTest->GetShowOnly() )
  608. {
  609. cres.FullCommandLine = actualCommand;
  610. this->TestResults.push_back( cres );
  611. failed.push_back(testname);
  612. continue;
  613. }
  614. }
  615. // add the arguments
  616. std::vector<std::string>::const_iterator j = args.begin();
  617. ++j;
  618. ++j;
  619. std::vector<const char*> arguments;
  620. this->GenerateTestCommand(arguments);
  621. arguments.push_back(actualCommand.c_str());
  622. for(;j != args.end(); ++j)
  623. {
  624. testCommand += " ";
  625. testCommand += cmSystemTools::EscapeSpaces(j->c_str());
  626. arguments.push_back(j->c_str());
  627. }
  628. arguments.push_back(0);
  629. /**
  630. * Run an executable command and put the stdout in output.
  631. */
  632. std::string output;
  633. int retVal = 0;
  634. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl
  635. << (this->MemCheck?"MemCheck":"Test") << " command: " << testCommand
  636. << std::endl);
  637. *this->LogFile << cnt << "/" << tmsize
  638. << " Test: " << testname.c_str() << std::endl;
  639. *this->LogFile << "Command: ";
  640. std::vector<cmStdString>::size_type ll;
  641. for ( ll = 0; ll < arguments.size()-1; ll ++ )
  642. {
  643. *this->LogFile << "\"" << arguments[ll] << "\" ";
  644. }
  645. *this->LogFile
  646. << std::endl
  647. << "Directory: " << it->Directory << std::endl
  648. << "\"" << testname.c_str() << "\" start time: "
  649. << this->CTest->CurrentTime() << std::endl
  650. << "Output:" << std::endl
  651. << "----------------------------------------------------------"
  652. << std::endl;
  653. int res = 0;
  654. double clock_start, clock_finish;
  655. clock_start = cmSystemTools::GetTime();
  656. if ( !this->CTest->GetShowOnly() )
  657. {
  658. res = this->CTest->RunTest(arguments, &output, &retVal, this->LogFile);
  659. }
  660. clock_finish = cmSystemTools::GetTime();
  661. if ( this->LogFile )
  662. {
  663. double ttime = clock_finish - clock_start;
  664. int hours = static_cast<int>(ttime / (60 * 60));
  665. int minutes = static_cast<int>(ttime / 60) % 60;
  666. int seconds = static_cast<int>(ttime) % 60;
  667. char buffer[100];
  668. sprintf(buffer, "%02d:%02d:%02d", hours, minutes, seconds);
  669. *this->LogFile
  670. << "----------------------------------------------------------"
  671. << std::endl
  672. << "\"" << testname.c_str() << "\" end time: "
  673. << this->CTest->CurrentTime() << std::endl
  674. << "\"" << testname.c_str() << "\" time elapsed: "
  675. << buffer << std::endl
  676. << "----------------------------------------------------------"
  677. << std::endl << std::endl;
  678. }
  679. cres.ExecutionTime = (double)(clock_finish - clock_start);
  680. cres.FullCommandLine = testCommand;
  681. if ( !this->CTest->GetShowOnly() )
  682. {
  683. bool testFailed = false;
  684. std::vector<cmsys::RegularExpression>::iterator passIt;
  685. bool forceFail = false;
  686. if ( it->RequiredRegularExpressions.size() > 0 )
  687. {
  688. bool found = false;
  689. for ( passIt = it->RequiredRegularExpressions.begin();
  690. passIt != it->RequiredRegularExpressions.end();
  691. ++ passIt )
  692. {
  693. if ( passIt->find(output.c_str()) )
  694. {
  695. found = true;
  696. }
  697. }
  698. if ( !found )
  699. {
  700. forceFail = true;
  701. }
  702. }
  703. if ( it->ErrorRegularExpressions.size() > 0 )
  704. {
  705. for ( passIt = it->ErrorRegularExpressions.begin();
  706. passIt != it->ErrorRegularExpressions.end();
  707. ++ passIt )
  708. {
  709. if ( passIt->find(output.c_str()) )
  710. {
  711. forceFail = true;
  712. }
  713. }
  714. }
  715. if (res == cmsysProcess_State_Exited &&
  716. (retVal == 0 || it->RequiredRegularExpressions.size()) &&
  717. !forceFail)
  718. {
  719. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Passed");
  720. if ( it->WillFail )
  721. {
  722. cmCTestLog(this->CTest, HANDLER_OUTPUT, " - But it should fail!");
  723. cres.Status = cmCTestTestHandler::FAILED;
  724. testFailed = true;
  725. }
  726. else
  727. {
  728. cres.Status = cmCTestTestHandler::COMPLETED;
  729. }
  730. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl);
  731. }
  732. else
  733. {
  734. testFailed = true;
  735. cres.Status = cmCTestTestHandler::FAILED;
  736. if ( res == cmsysProcess_State_Expired )
  737. {
  738. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Timeout" << std::endl);
  739. cres.Status = cmCTestTestHandler::TIMEOUT;
  740. }
  741. else if ( res == cmsysProcess_State_Exception )
  742. {
  743. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Exception: ");
  744. switch ( retVal )
  745. {
  746. case cmsysProcess_Exception_Fault:
  747. cmCTestLog(this->CTest, HANDLER_OUTPUT, "SegFault");
  748. cres.Status = cmCTestTestHandler::SEGFAULT;
  749. break;
  750. case cmsysProcess_Exception_Illegal:
  751. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Illegal");
  752. cres.Status = cmCTestTestHandler::ILLEGAL;
  753. break;
  754. case cmsysProcess_Exception_Interrupt:
  755. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Interrupt");
  756. cres.Status = cmCTestTestHandler::INTERRUPT;
  757. break;
  758. case cmsysProcess_Exception_Numerical:
  759. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Numerical");
  760. cres.Status = cmCTestTestHandler::NUMERICAL;
  761. break;
  762. default:
  763. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Other");
  764. cres.Status = cmCTestTestHandler::OTHER_FAULT;
  765. }
  766. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl);
  767. }
  768. else if ( res == cmsysProcess_State_Error )
  769. {
  770. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Bad command " << res
  771. << std::endl);
  772. cres.Status = cmCTestTestHandler::BAD_COMMAND;
  773. }
  774. else
  775. {
  776. // Force fail will also be here?
  777. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***Failed");
  778. if ( it->WillFail )
  779. {
  780. cres.Status = cmCTestTestHandler::COMPLETED;
  781. cmCTestLog(this->CTest, HANDLER_OUTPUT, " - supposed to fail");
  782. testFailed = false;
  783. }
  784. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl);
  785. }
  786. }
  787. if ( testFailed )
  788. {
  789. failed.push_back(testname);
  790. }
  791. else
  792. {
  793. passed.push_back(testname);
  794. }
  795. if (!output.empty() && output.find("<DartMeasurement") != output.npos)
  796. {
  797. if (this->DartStuff.find(output.c_str()))
  798. {
  799. std::string dartString = this->DartStuff.match(1);
  800. cmSystemTools::ReplaceString(output, dartString.c_str(),"");
  801. cres.RegressionImages
  802. = this->GenerateRegressionImages(dartString);
  803. }
  804. }
  805. }
  806. if ( cres.Status == cmCTestTestHandler::COMPLETED )
  807. {
  808. this->CleanTestOutput(output, static_cast<size_t>(
  809. this->CustomMaximumPassedTestOutputSize));
  810. }
  811. else
  812. {
  813. this->CleanTestOutput(output, static_cast<size_t>(
  814. this->CustomMaximumFailedTestOutputSize));
  815. }
  816. cres.Output = output;
  817. cres.ReturnValue = retVal;
  818. cres.CompletionStatus = "Completed";
  819. this->TestResults.push_back( cres );
  820. }
  821. this->EndTest = this->CTest->CurrentTime();
  822. this->ElapsedTestingTime = cmSystemTools::GetTime() - elapsed_time_start;
  823. if ( this->LogFile )
  824. {
  825. *this->LogFile << "End testing: " << this->EndTest << std::endl;
  826. }
  827. cmSystemTools::ChangeDirectory(current_dir.c_str());
  828. }
  829. //----------------------------------------------------------------------
  830. void cmCTestTestHandler::GenerateTestCommand(std::vector<const char*>&)
  831. {
  832. }
  833. //----------------------------------------------------------------------
  834. void cmCTestTestHandler::GenerateDartOutput(std::ostream& os)
  835. {
  836. if ( !this->CTest->GetProduceXML() )
  837. {
  838. return;
  839. }
  840. this->CTest->StartXML(os);
  841. os << "<Testing>\n"
  842. << "\t<StartDateTime>" << this->StartTest << "</StartDateTime>\n"
  843. << "\t<TestList>\n";
  844. cmCTestTestHandler::TestResultsVector::size_type cc;
  845. for ( cc = 0; cc < this->TestResults.size(); cc ++ )
  846. {
  847. cmCTestTestResult *result = &this->TestResults[cc];
  848. std::string testPath = result->Path + "/" + result->Name;
  849. os << "\t\t<Test>" << cmCTest::MakeXMLSafe(
  850. this->CTest->GetShortPathToFile(testPath.c_str()))
  851. << "</Test>" << std::endl;
  852. }
  853. os << "\t</TestList>\n";
  854. for ( cc = 0; cc < this->TestResults.size(); cc ++ )
  855. {
  856. cmCTestTestResult *result = &this->TestResults[cc];
  857. os << "\t<Test Status=\"";
  858. if ( result->Status == cmCTestTestHandler::COMPLETED )
  859. {
  860. os << "passed";
  861. }
  862. else if ( result->Status == cmCTestTestHandler::NOT_RUN )
  863. {
  864. os << "notrun";
  865. }
  866. else
  867. {
  868. os << "failed";
  869. }
  870. std::string testPath = result->Path + "/" + result->Name;
  871. os << "\">\n"
  872. << "\t\t<Name>" << cmCTest::MakeXMLSafe(result->Name) << "</Name>\n"
  873. << "\t\t<Path>" << cmCTest::MakeXMLSafe(
  874. this->CTest->GetShortPathToFile(result->Path.c_str())) << "</Path>\n"
  875. << "\t\t<FullName>" << cmCTest::MakeXMLSafe(
  876. this->CTest->GetShortPathToFile(testPath.c_str())) << "</FullName>\n"
  877. << "\t\t<FullCommandLine>"
  878. << cmCTest::MakeXMLSafe(result->FullCommandLine)
  879. << "</FullCommandLine>\n"
  880. << "\t\t<Results>" << std::endl;
  881. if ( result->Status != cmCTestTestHandler::NOT_RUN )
  882. {
  883. if ( result->Status != cmCTestTestHandler::COMPLETED ||
  884. result->ReturnValue )
  885. {
  886. os << "\t\t\t<NamedMeasurement type=\"text/string\" "
  887. "name=\"Exit Code\"><Value>"
  888. << this->GetTestStatus(result->Status) << "</Value>"
  889. "</NamedMeasurement>\n"
  890. << "\t\t\t<NamedMeasurement type=\"text/string\" "
  891. "name=\"Exit Value\"><Value>"
  892. << result->ReturnValue << "</Value></NamedMeasurement>"
  893. << std::endl;
  894. }
  895. os << result->RegressionImages;
  896. os << "\t\t\t<NamedMeasurement type=\"numeric/double\" "
  897. << "name=\"Execution Time\"><Value>"
  898. << result->ExecutionTime << "</Value></NamedMeasurement>\n";
  899. os
  900. << "\t\t\t<NamedMeasurement type=\"text/string\" "
  901. << "name=\"Completion Status\"><Value>"
  902. << result->CompletionStatus << "</Value></NamedMeasurement>\n";
  903. }
  904. os
  905. << "\t\t\t<Measurement>\n"
  906. << "\t\t\t\t<Value>";
  907. os << cmCTest::MakeXMLSafe(result->Output);
  908. os
  909. << "</Value>\n"
  910. << "\t\t\t</Measurement>\n"
  911. << "\t\t</Results>\n"
  912. << "\t</Test>" << std::endl;
  913. }
  914. os << "\t<EndDateTime>" << this->EndTest << "</EndDateTime>\n"
  915. << "<ElapsedMinutes>"
  916. << static_cast<int>(this->ElapsedTestingTime/6)/10.0
  917. << "</ElapsedMinutes>"
  918. << "</Testing>" << std::endl;
  919. this->CTest->EndXML(os);
  920. }
  921. //----------------------------------------------------------------------
  922. int cmCTestTestHandler::ExecuteCommands(std::vector<cmStdString>& vec)
  923. {
  924. std::vector<cmStdString>::iterator it;
  925. for ( it = vec.begin(); it != vec.end(); ++it )
  926. {
  927. int retVal = 0;
  928. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run command: " << *it
  929. << std::endl);
  930. if ( !cmSystemTools::RunSingleCommand(it->c_str(), 0, &retVal, 0, true
  931. /*this->Verbose*/) || retVal != 0 )
  932. {
  933. cmCTestLog(this->CTest, ERROR_MESSAGE, "Problem running command: "
  934. << *it << std::endl);
  935. return 0;
  936. }
  937. }
  938. return 1;
  939. }
  940. //----------------------------------------------------------------------
  941. // Find the appropriate executable to run for a test
  942. std::string cmCTestTestHandler::FindTheExecutable(const char *exe)
  943. {
  944. std::string fullPath = "";
  945. std::string dir;
  946. std::string file;
  947. cmSystemTools::SplitProgramPath(exe, dir, file);
  948. // first try to find the executable given a config type subdir if there is
  949. // one
  950. if(this->CTest->GetConfigType() != "" &&
  951. ::TryExecutable(dir.c_str(), file.c_str(), &fullPath,
  952. this->CTest->GetConfigType().c_str()))
  953. {
  954. return fullPath;
  955. }
  956. // next try the current directory as the subdir
  957. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"."))
  958. {
  959. return fullPath;
  960. }
  961. // try without the config subdir
  962. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,""))
  963. {
  964. return fullPath;
  965. }
  966. if ( this->CTest->GetConfigType() == "" )
  967. {
  968. // No config type, so try to guess it
  969. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Deployment"))
  970. {
  971. return fullPath;
  972. }
  973. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Development"))
  974. {
  975. return fullPath;
  976. }
  977. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Release"))
  978. {
  979. return fullPath;
  980. }
  981. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Debug"))
  982. {
  983. return fullPath;
  984. }
  985. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"MinSizeRel"))
  986. {
  987. return fullPath;
  988. }
  989. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"RelWithDebInfo"))
  990. {
  991. return fullPath;
  992. }
  993. }
  994. // if everything else failed, check the users path, but only if a full path
  995. // wasn;t specified
  996. if (dir.size() == 0)
  997. {
  998. std::string path = cmSystemTools::FindProgram(file.c_str());
  999. if (path != "")
  1000. {
  1001. return path;
  1002. }
  1003. }
  1004. if ( this->CTest->GetConfigType() != "" )
  1005. {
  1006. dir += "/";
  1007. dir += this->CTest->GetConfigType();
  1008. dir += "/";
  1009. dir += file;
  1010. cmSystemTools::Error("config type specified on the command line, but "
  1011. "test executable not found.",
  1012. dir.c_str());
  1013. return "";
  1014. }
  1015. return fullPath;
  1016. }
  1017. //----------------------------------------------------------------------
  1018. void cmCTestTestHandler::GetListOfTests()
  1019. {
  1020. if ( !this->IncludeRegExp.empty() )
  1021. {
  1022. this->IncludeTestsRegularExpression.compile(this->IncludeRegExp.c_str());
  1023. }
  1024. if ( !this->ExcludeRegExp.empty() )
  1025. {
  1026. this->ExcludeTestsRegularExpression.compile(this->ExcludeRegExp.c_str());
  1027. }
  1028. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1029. "Constructing a list of tests" << std::endl);
  1030. cmake cm;
  1031. cmGlobalGenerator gg;
  1032. gg.SetCMakeInstance(&cm);
  1033. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  1034. lg->SetGlobalGenerator(&gg);
  1035. cmMakefile *mf = lg->GetMakefile();
  1036. mf->AddDefinition("CTEST_CONFIGURATION_TYPE",
  1037. this->CTest->GetConfigType().c_str());
  1038. // Add handler for ADD_TEST
  1039. cmCTestAddTestCommand* newCom1 = new cmCTestAddTestCommand;
  1040. newCom1->TestHandler = this;
  1041. cm.AddCommand(newCom1);
  1042. // Add handler for SUBDIR
  1043. cmCTestSubdirCommand* newCom2 = new cmCTestSubdirCommand;
  1044. newCom2->TestHandler = this;
  1045. cm.AddCommand(newCom2);
  1046. // Add handler for SET_SOURCE_FILES_PROPERTIES
  1047. cmCTestSetTestsPropertiesCommand* newCom3
  1048. = new cmCTestSetTestsPropertiesCommand;
  1049. newCom3->TestHandler = this;
  1050. cm.AddCommand(newCom3);
  1051. const char* testFilename;
  1052. if( cmSystemTools::FileExists("CTestTestfile.cmake") )
  1053. {
  1054. // does the CTestTestfile.cmake exist ?
  1055. testFilename = "CTestTestfile.cmake";
  1056. }
  1057. else if( cmSystemTools::FileExists("DartTestfile.txt") )
  1058. {
  1059. // does the DartTestfile.txt exist ?
  1060. testFilename = "DartTestfile.txt";
  1061. }
  1062. else
  1063. {
  1064. return;
  1065. }
  1066. if ( !mf->ReadListFile(0, testFilename) )
  1067. {
  1068. return;
  1069. }
  1070. if ( cmSystemTools::GetErrorOccuredFlag() )
  1071. {
  1072. return;
  1073. }
  1074. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1075. "Done constructing a list of tests" << std::endl);
  1076. }
  1077. //----------------------------------------------------------------------
  1078. void cmCTestTestHandler::UseIncludeRegExp()
  1079. {
  1080. this->UseIncludeRegExpFlag = true;
  1081. }
  1082. //----------------------------------------------------------------------
  1083. void cmCTestTestHandler::UseExcludeRegExp()
  1084. {
  1085. this->UseExcludeRegExpFlag = true;
  1086. this->UseExcludeRegExpFirst = this->UseIncludeRegExpFlag ? false : true;
  1087. }
  1088. //----------------------------------------------------------------------
  1089. const char* cmCTestTestHandler::GetTestStatus(int status)
  1090. {
  1091. static const char statuses[][100] = {
  1092. "Not Run",
  1093. "Timeout",
  1094. "SEGFAULT",
  1095. "ILLEGAL",
  1096. "INTERRUPT",
  1097. "NUMERICAL",
  1098. "OTHER_FAULT",
  1099. "Failed",
  1100. "BAD_COMMAND",
  1101. "Completed"
  1102. };
  1103. if ( status < cmCTestTestHandler::NOT_RUN ||
  1104. status > cmCTestTestHandler::COMPLETED )
  1105. {
  1106. return "No Status";
  1107. }
  1108. return statuses[status];
  1109. }
  1110. //----------------------------------------------------------------------
  1111. void cmCTestTestHandler::ExpandTestsToRunInformation(int numTests)
  1112. {
  1113. if (this->TestsToRunString.empty())
  1114. {
  1115. return;
  1116. }
  1117. int start;
  1118. int end = -1;
  1119. double stride = -1;
  1120. std::string::size_type pos = 0;
  1121. std::string::size_type pos2;
  1122. // read start
  1123. if(GetNextNumber(this->TestsToRunString, start, pos, pos2))
  1124. {
  1125. // read end
  1126. if(GetNextNumber(this->TestsToRunString, end, pos, pos2))
  1127. {
  1128. // read stride
  1129. if(GetNextRealNumber(this->TestsToRunString, stride, pos, pos2))
  1130. {
  1131. int val =0;
  1132. // now read specific numbers
  1133. while(GetNextNumber(this->TestsToRunString, val, pos, pos2))
  1134. {
  1135. this->TestsToRun.push_back(val);
  1136. }
  1137. this->TestsToRun.push_back(val);
  1138. }
  1139. }
  1140. }
  1141. // if start is not specified then we assume we start at 1
  1142. if(start == -1)
  1143. {
  1144. start = 1;
  1145. }
  1146. // if end isnot specified then we assume we end with the last test
  1147. if(end == -1)
  1148. {
  1149. end = numTests;
  1150. }
  1151. // if the stride wasn't specified then it defaults to 1
  1152. if(stride == -1)
  1153. {
  1154. stride = 1;
  1155. }
  1156. // if we have a range then add it
  1157. if(end != -1 && start != -1 && stride > 0)
  1158. {
  1159. int i = 0;
  1160. while (i*stride + start <= end)
  1161. {
  1162. this->TestsToRun.push_back(static_cast<int>(i*stride+start));
  1163. ++i;
  1164. }
  1165. }
  1166. // sort the array
  1167. std::sort(this->TestsToRun.begin(), this->TestsToRun.end(),
  1168. std::less<int>());
  1169. // remove duplicates
  1170. std::vector<int>::iterator new_end =
  1171. std::unique(this->TestsToRun.begin(), this->TestsToRun.end());
  1172. this->TestsToRun.erase(new_end, this->TestsToRun.end());
  1173. }
  1174. //----------------------------------------------------------------------
  1175. // Just for convenience
  1176. #define SPACE_REGEX "[ \t\r\n]"
  1177. //----------------------------------------------------------------------
  1178. std::string cmCTestTestHandler::GenerateRegressionImages(
  1179. const std::string& xml)
  1180. {
  1181. cmsys::RegularExpression twoattributes(
  1182. "<DartMeasurement"
  1183. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1184. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1185. SPACE_REGEX "*>([^<]*)</DartMeasurement>");
  1186. cmsys::RegularExpression threeattributes(
  1187. "<DartMeasurement"
  1188. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1189. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1190. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1191. SPACE_REGEX "*>([^<]*)</DartMeasurement>");
  1192. cmsys::RegularExpression fourattributes(
  1193. "<DartMeasurement"
  1194. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1195. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1196. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1197. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1198. SPACE_REGEX "*>([^<]*)</DartMeasurement>");
  1199. cmsys::RegularExpression measurementfile(
  1200. "<DartMeasurementFile"
  1201. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1202. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1203. SPACE_REGEX "*>([^<]*)</DartMeasurementFile>");
  1204. cmOStringStream ostr;
  1205. bool done = false;
  1206. std::string cxml = xml;
  1207. while ( ! done )
  1208. {
  1209. if ( twoattributes.find(cxml) )
  1210. {
  1211. ostr
  1212. << "\t\t\t<NamedMeasurement"
  1213. << " " << twoattributes.match(1) << "=\""
  1214. << twoattributes.match(2) << "\""
  1215. << " " << twoattributes.match(3) << "=\""
  1216. << twoattributes.match(4) << "\""
  1217. << "><Value>" << twoattributes.match(5)
  1218. << "</Value></NamedMeasurement>"
  1219. << std::endl;
  1220. cxml.erase(twoattributes.start(),
  1221. twoattributes.end() - twoattributes.start());
  1222. }
  1223. else if ( threeattributes.find(cxml) )
  1224. {
  1225. ostr
  1226. << "\t\t\t<NamedMeasurement"
  1227. << " " << threeattributes.match(1) << "=\""
  1228. << threeattributes.match(2) << "\""
  1229. << " " << threeattributes.match(3) << "=\""
  1230. << threeattributes.match(4) << "\""
  1231. << " " << threeattributes.match(5) << "=\""
  1232. << threeattributes.match(6) << "\""
  1233. << "><Value>" << threeattributes.match(7)
  1234. << "</Value></NamedMeasurement>"
  1235. << std::endl;
  1236. cxml.erase(threeattributes.start(),
  1237. threeattributes.end() - threeattributes.start());
  1238. }
  1239. else if ( fourattributes.find(cxml) )
  1240. {
  1241. ostr
  1242. << "\t\t\t<NamedMeasurement"
  1243. << " " << fourattributes.match(1) << "=\""
  1244. << fourattributes.match(2) << "\""
  1245. << " " << fourattributes.match(3) << "=\""
  1246. << fourattributes.match(4) << "\""
  1247. << " " << fourattributes.match(5) << "=\""
  1248. << fourattributes.match(6) << "\""
  1249. << " " << fourattributes.match(7) << "=\""
  1250. << fourattributes.match(8) << "\""
  1251. << "><Value>" << fourattributes.match(9)
  1252. << "</Value></NamedMeasurement>"
  1253. << std::endl;
  1254. cxml.erase(fourattributes.start(),
  1255. fourattributes.end() - fourattributes.start());
  1256. }
  1257. else if ( measurementfile.find(cxml) )
  1258. {
  1259. const std::string& filename =
  1260. cmCTest::CleanString(measurementfile.match(5));
  1261. if ( cmSystemTools::FileExists(filename.c_str()) )
  1262. {
  1263. long len = cmSystemTools::FileLength(filename.c_str());
  1264. if ( len == 0 )
  1265. {
  1266. std::string k1 = measurementfile.match(1);
  1267. std::string v1 = measurementfile.match(2);
  1268. std::string k2 = measurementfile.match(3);
  1269. std::string v2 = measurementfile.match(4);
  1270. if ( cmSystemTools::LowerCase(k1) == "type" )
  1271. {
  1272. v1 = "text/string";
  1273. }
  1274. if ( cmSystemTools::LowerCase(k2) == "type" )
  1275. {
  1276. v2 = "text/string";
  1277. }
  1278. ostr
  1279. << "\t\t\t<NamedMeasurement"
  1280. << " " << k1 << "=\"" << v1 << "\""
  1281. << " " << k2 << "=\"" << v2 << "\""
  1282. << " encoding=\"none\""
  1283. << "><Value>Image " << filename.c_str()
  1284. << " is empty</Value></NamedMeasurement>";
  1285. }
  1286. else
  1287. {
  1288. std::ifstream ifs(filename.c_str(), std::ios::in
  1289. #ifdef _WIN32
  1290. | std::ios::binary
  1291. #endif
  1292. );
  1293. unsigned char *file_buffer = new unsigned char [ len + 1 ];
  1294. ifs.read(reinterpret_cast<char*>(file_buffer), len);
  1295. unsigned char *encoded_buffer
  1296. = new unsigned char [ static_cast<int>(len * 1.5 + 5) ];
  1297. unsigned long rlen
  1298. = cmsysBase64_Encode(file_buffer, len, encoded_buffer, 1);
  1299. unsigned long cc;
  1300. ostr
  1301. << "\t\t\t<NamedMeasurement"
  1302. << " " << measurementfile.match(1) << "=\""
  1303. << measurementfile.match(2) << "\""
  1304. << " " << measurementfile.match(3) << "=\""
  1305. << measurementfile.match(4) << "\""
  1306. << " encoding=\"base64\""
  1307. << ">" << std::endl << "\t\t\t\t<Value>";
  1308. for ( cc = 0; cc < rlen; cc ++ )
  1309. {
  1310. ostr << encoded_buffer[cc];
  1311. if ( cc % 60 == 0 && cc )
  1312. {
  1313. ostr << std::endl;
  1314. }
  1315. }
  1316. ostr
  1317. << "</Value>" << std::endl << "\t\t\t</NamedMeasurement>"
  1318. << std::endl;
  1319. delete [] file_buffer;
  1320. delete [] encoded_buffer;
  1321. }
  1322. }
  1323. else
  1324. {
  1325. int idx = 4;
  1326. if ( measurementfile.match(1) == "name" )
  1327. {
  1328. idx = 2;
  1329. }
  1330. ostr
  1331. << "\t\t\t<NamedMeasurement"
  1332. << " name=\"" << measurementfile.match(idx) << "\""
  1333. << " text=\"text/string\""
  1334. << "><Value>File " << filename.c_str()
  1335. << " not found</Value></NamedMeasurement>"
  1336. << std::endl;
  1337. cmCTestLog(this->CTest, HANDLER_OUTPUT, "File \"" << filename.c_str()
  1338. << "\" not found." << std::endl);
  1339. }
  1340. cxml.erase(measurementfile.start(),
  1341. measurementfile.end() - measurementfile.start());
  1342. }
  1343. else
  1344. {
  1345. done = true;
  1346. }
  1347. }
  1348. return ostr.str();
  1349. }
  1350. //----------------------------------------------------------------------
  1351. void cmCTestTestHandler::SetIncludeRegExp(const char *arg)
  1352. {
  1353. this->IncludeRegExp = arg;
  1354. }
  1355. //----------------------------------------------------------------------
  1356. void cmCTestTestHandler::SetExcludeRegExp(const char *arg)
  1357. {
  1358. this->ExcludeRegExp = arg;
  1359. }
  1360. //----------------------------------------------------------------------
  1361. void cmCTestTestHandler::SetTestsToRunInformation(const char* in)
  1362. {
  1363. if ( !in )
  1364. {
  1365. return;
  1366. }
  1367. this->TestsToRunString = in;
  1368. // if the argument is a file, then read it and use the contents as the
  1369. // string
  1370. if(cmSystemTools::FileExists(in))
  1371. {
  1372. std::ifstream fin(in);
  1373. unsigned long filelen = cmSystemTools::FileLength(in);
  1374. char* buff = new char[filelen+1];
  1375. fin.getline(buff, filelen);
  1376. buff[fin.gcount()] = 0;
  1377. this->TestsToRunString = buff;
  1378. }
  1379. }
  1380. //----------------------------------------------------------------------
  1381. bool cmCTestTestHandler::CleanTestOutput(std::string& output,
  1382. size_t remove_threshold)
  1383. {
  1384. if ( remove_threshold == 0 )
  1385. {
  1386. return true;
  1387. }
  1388. if ( output.find("CTEST_FULL_OUTPUT") != output.npos )
  1389. {
  1390. return true;
  1391. }
  1392. cmOStringStream ostr;
  1393. std::string::size_type cc;
  1394. std::string::size_type skipsize = 0;
  1395. int inTag = 0;
  1396. int skipped = 0;
  1397. for ( cc = 0; cc < output.size(); cc ++ )
  1398. {
  1399. int ch = output[cc];
  1400. if ( ch < 0 || ch > 255 )
  1401. {
  1402. break;
  1403. }
  1404. if ( ch == '<' )
  1405. {
  1406. inTag = 1;
  1407. }
  1408. if ( !inTag )
  1409. {
  1410. int notskip = 0;
  1411. // Skip
  1412. if ( skipsize < remove_threshold )
  1413. {
  1414. ostr << static_cast<char>(ch);
  1415. notskip = 1;
  1416. }
  1417. skipsize ++;
  1418. if ( notskip && skipsize >= remove_threshold )
  1419. {
  1420. skipped = 1;
  1421. }
  1422. }
  1423. else
  1424. {
  1425. ostr << static_cast<char>(ch);
  1426. }
  1427. if ( ch == '>' )
  1428. {
  1429. inTag = 0;
  1430. }
  1431. }
  1432. if ( skipped )
  1433. {
  1434. ostr << "..." << std::endl << "The rest of the test output was removed "
  1435. "since it exceeds the threshold of "
  1436. << remove_threshold << " characters." << std::endl;
  1437. }
  1438. output = ostr.str();
  1439. return true;
  1440. }
  1441. //----------------------------------------------------------------------
  1442. bool cmCTestTestHandler::SetTestsProperties(
  1443. const std::vector<std::string>& args)
  1444. {
  1445. std::vector<std::string>::const_iterator it;
  1446. std::vector<cmStdString> tests;
  1447. bool found = false;
  1448. for ( it = args.begin(); it != args.end(); ++ it )
  1449. {
  1450. if ( *it == "PROPERTIES" )
  1451. {
  1452. found = true;
  1453. break;
  1454. }
  1455. tests.push_back(*it);
  1456. }
  1457. if ( !found )
  1458. {
  1459. return false;
  1460. }
  1461. ++ it; // skip PROPERTIES
  1462. for ( ; it != args.end(); ++ it )
  1463. {
  1464. std::string key = *it;
  1465. ++ it;
  1466. if ( it == args.end() )
  1467. {
  1468. break;
  1469. }
  1470. std::string val = *it;
  1471. std::vector<cmStdString>::const_iterator tit;
  1472. for ( tit = tests.begin(); tit != tests.end(); ++ tit )
  1473. {
  1474. cmCTestTestHandler::ListOfTests::iterator rtit;
  1475. for ( rtit = this->TestList.begin();
  1476. rtit != this->TestList.end();
  1477. ++ rtit )
  1478. {
  1479. if ( *tit == rtit->Name )
  1480. {
  1481. if ( key == "WILL_FAIL" )
  1482. {
  1483. rtit->WillFail = cmSystemTools::IsOn(val.c_str());
  1484. }
  1485. if ( key == "FAIL_REGULAR_EXPRESSION" )
  1486. {
  1487. std::vector<std::string> lval;
  1488. cmSystemTools::ExpandListArgument(val.c_str(), lval);
  1489. std::vector<std::string>::iterator crit;
  1490. for ( crit = lval.begin(); crit != lval.end(); ++ crit )
  1491. {
  1492. rtit->ErrorRegularExpressions.push_back(
  1493. cmsys::RegularExpression(crit->c_str()));
  1494. }
  1495. }
  1496. if ( key == "PASS_REGULAR_EXPRESSION" )
  1497. {
  1498. std::vector<std::string> lval;
  1499. cmSystemTools::ExpandListArgument(val.c_str(), lval);
  1500. std::vector<std::string>::iterator crit;
  1501. for ( crit = lval.begin(); crit != lval.end(); ++ crit )
  1502. {
  1503. rtit->RequiredRegularExpressions.push_back(
  1504. cmsys::RegularExpression(crit->c_str()));
  1505. }
  1506. }
  1507. }
  1508. }
  1509. }
  1510. }
  1511. return true;
  1512. }
  1513. //----------------------------------------------------------------------
  1514. bool cmCTestTestHandler::AddTest(const std::vector<std::string>& args)
  1515. {
  1516. const std::string& testname = args[0];
  1517. cmCTestLog(this->CTest, ERROR_MESSAGE, "Add test: " << args[0] << std::endl);
  1518. if (this->UseExcludeRegExpFlag &&
  1519. this->UseExcludeRegExpFirst &&
  1520. this->ExcludeTestsRegularExpression.find(testname.c_str()))
  1521. {
  1522. return true;
  1523. }
  1524. if ( this->MemCheck )
  1525. {
  1526. std::vector<cmStdString>::iterator it;
  1527. bool found = false;
  1528. for ( it = this->CustomTestsIgnore.begin();
  1529. it != this->CustomTestsIgnore.end(); ++ it )
  1530. {
  1531. if ( *it == testname )
  1532. {
  1533. found = true;
  1534. break;
  1535. }
  1536. }
  1537. if ( found )
  1538. {
  1539. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Ignore memcheck: "
  1540. << *it << std::endl);
  1541. return true;
  1542. }
  1543. }
  1544. else
  1545. {
  1546. std::vector<cmStdString>::iterator it;
  1547. bool found = false;
  1548. for ( it = this->CustomTestsIgnore.begin();
  1549. it != this->CustomTestsIgnore.end(); ++ it )
  1550. {
  1551. if ( *it == testname )
  1552. {
  1553. found = true;
  1554. break;
  1555. }
  1556. }
  1557. if ( found )
  1558. {
  1559. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Ignore test: "
  1560. << *it << std::endl);
  1561. return true;
  1562. }
  1563. }
  1564. cmCTestTestProperties test;
  1565. test.Name = testname;
  1566. test.Args = args;
  1567. test.Directory = cmSystemTools::GetCurrentWorkingDirectory();
  1568. cmCTestLog(this->CTest, ERROR_MESSAGE, "Set test directory: " << test.Directory << std::endl);
  1569. test.IsInBasedOnREOptions = true;
  1570. test.WillFail = false;
  1571. if (this->UseIncludeRegExpFlag &&
  1572. !this->IncludeTestsRegularExpression.find(testname.c_str()))
  1573. {
  1574. test.IsInBasedOnREOptions = false;
  1575. }
  1576. else if (this->UseExcludeRegExpFlag &&
  1577. !this->UseExcludeRegExpFirst &&
  1578. this->ExcludeTestsRegularExpression.find(testname.c_str()))
  1579. {
  1580. test.IsInBasedOnREOptions = false;
  1581. }
  1582. this->TestList.push_back(test);
  1583. return true;
  1584. }