cmCTestTestHandler.cxx 53 KB

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