cmCTestTestHandler.cxx 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  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 <stdlib.h>
  25. #include <math.h>
  26. #include <float.h>
  27. #include <memory> // auto_ptr
  28. //----------------------------------------------------------------------
  29. class cmCTestSubdirCommand : public cmCommand
  30. {
  31. public:
  32. /**
  33. * This is a virtual constructor for the command.
  34. */
  35. virtual cmCommand* Clone()
  36. {
  37. cmCTestSubdirCommand* c = new cmCTestSubdirCommand;
  38. c->m_TestHandler = m_TestHandler;
  39. return c;
  40. }
  41. /**
  42. * This is called when the command is first encountered in
  43. * the CMakeLists.txt file.
  44. */
  45. virtual bool InitialPass(std::vector<std::string> const& args);
  46. /**
  47. * The name of the command as specified in CMakeList.txt.
  48. */
  49. virtual const char* GetName() { return "SUBDIRS";}
  50. // Unused methods
  51. virtual const char* GetTerseDocumentation() { return ""; }
  52. virtual const char* GetFullDocumentation() { return ""; }
  53. cmTypeMacro(cmCTestSubdirCommand, cmCommand);
  54. cmCTestTestHandler* m_TestHandler;
  55. };
  56. //----------------------------------------------------------------------
  57. bool cmCTestSubdirCommand::InitialPass(std::vector<std::string> const& args)
  58. {
  59. if(args.size() < 1 )
  60. {
  61. this->SetError("called with incorrect number of arguments");
  62. return false;
  63. }
  64. std::vector<std::string>::const_iterator it;
  65. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  66. for ( it = args.begin(); it != args.end(); ++ it )
  67. {
  68. std::string fname = cwd;
  69. fname += "/";
  70. fname += *it;
  71. if ( !cmSystemTools::FileExists(fname.c_str()) )
  72. {
  73. std::string m = "Could not find directory: ";
  74. m += fname;
  75. this->SetError(m.c_str());
  76. return false;
  77. }
  78. cmSystemTools::ChangeDirectory(fname.c_str());
  79. const char* testFilename = 0;
  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. cmSystemTools::ChangeDirectory(cwd.c_str());
  93. return false;
  94. }
  95. fname += "/";
  96. fname += testFilename;
  97. bool readit = m_Makefile->ReadListFile( m_Makefile->GetCurrentListFile(), fname.c_str());
  98. cmSystemTools::ChangeDirectory(cwd.c_str());
  99. if(!readit)
  100. {
  101. std::string m = "Could not find include file: ";
  102. m += fname;
  103. this->SetError(m.c_str());
  104. return false;
  105. }
  106. }
  107. return true;
  108. }
  109. //----------------------------------------------------------------------
  110. class cmCTestAddTestCommand : public cmCommand
  111. {
  112. public:
  113. /**
  114. * This is a virtual constructor for the command.
  115. */
  116. virtual cmCommand* Clone()
  117. {
  118. cmCTestAddTestCommand* c = new cmCTestAddTestCommand;
  119. c->m_TestHandler = m_TestHandler;
  120. return c;
  121. }
  122. /**
  123. * This is called when the command is first encountered in
  124. * the CMakeLists.txt file.
  125. */
  126. virtual bool InitialPass(std::vector<std::string> const&);
  127. /**
  128. * The name of the command as specified in CMakeList.txt.
  129. */
  130. virtual const char* GetName() { return "ADD_TEST";}
  131. // Unused methods
  132. virtual const char* GetTerseDocumentation() { return ""; }
  133. virtual const char* GetFullDocumentation() { return ""; }
  134. cmTypeMacro(cmCTestAddTestCommand, cmCommand);
  135. cmCTestTestHandler* m_TestHandler;
  136. };
  137. //----------------------------------------------------------------------
  138. bool cmCTestAddTestCommand::InitialPass(std::vector<std::string> const& args)
  139. {
  140. if ( args.size() < 2 )
  141. {
  142. this->SetError("called with incorrect number of arguments");
  143. return false;
  144. }
  145. return m_TestHandler->AddTest(args);
  146. }
  147. //----------------------------------------------------------------------
  148. bool TryExecutable(const char *dir, const char *file,
  149. std::string *fullPath, const char *subdir)
  150. {
  151. // try current directory
  152. std::string tryPath;
  153. if (dir && strcmp(dir,""))
  154. {
  155. tryPath = dir;
  156. tryPath += "/";
  157. }
  158. if (subdir && strcmp(subdir,""))
  159. {
  160. tryPath += subdir;
  161. tryPath += "/";
  162. }
  163. tryPath += file;
  164. if(cmSystemTools::FileExists(tryPath.c_str()))
  165. {
  166. *fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  167. return true;
  168. }
  169. tryPath += cmSystemTools::GetExecutableExtension();
  170. if(cmSystemTools::FileExists(tryPath.c_str()))
  171. {
  172. *fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  173. return true;
  174. }
  175. return false;
  176. }
  177. //----------------------------------------------------------------------
  178. // get the next number in a string with numbers separated by ,
  179. // pos is the start of the search and pos2 is the end of the search
  180. // pos becomes pos2 after a call to GetNextNumber.
  181. // -1 is returned at the end of the list.
  182. inline int GetNextNumber(std::string const& in,
  183. int& val,
  184. std::string::size_type& pos,
  185. std::string::size_type& pos2)
  186. {
  187. pos2 = in.find(',', pos);
  188. if(pos2 != in.npos)
  189. {
  190. if(pos2-pos == 0)
  191. {
  192. val = -1;
  193. }
  194. else
  195. {
  196. val = atoi(in.substr(pos, pos2-pos).c_str());
  197. }
  198. pos = pos2+1;
  199. return 1;
  200. }
  201. else
  202. {
  203. if(in.size()-pos == 0)
  204. {
  205. val = -1;
  206. }
  207. else
  208. {
  209. val = atoi(in.substr(pos, in.size()-pos).c_str());
  210. }
  211. return 0;
  212. }
  213. }
  214. //----------------------------------------------------------------------
  215. // get the next number in a string with numbers separated by ,
  216. // pos is the start of the search and pos2 is the end of the search
  217. // pos becomes pos2 after a call to GetNextNumber.
  218. // -1 is returned at the end of the list.
  219. inline int GetNextRealNumber(std::string const& in,
  220. double& val,
  221. std::string::size_type& pos,
  222. std::string::size_type& pos2)
  223. {
  224. pos2 = in.find(',', pos);
  225. if(pos2 != in.npos)
  226. {
  227. if(pos2-pos == 0)
  228. {
  229. val = -1;
  230. }
  231. else
  232. {
  233. val = atof(in.substr(pos, pos2-pos).c_str());
  234. }
  235. pos = pos2+1;
  236. return 1;
  237. }
  238. else
  239. {
  240. if(in.size()-pos == 0)
  241. {
  242. val = -1;
  243. }
  244. else
  245. {
  246. val = atof(in.substr(pos, in.size()-pos).c_str());
  247. }
  248. return 0;
  249. }
  250. }
  251. //----------------------------------------------------------------------
  252. cmCTestTestHandler::cmCTestTestHandler()
  253. {
  254. m_UseUnion = false;
  255. m_UseIncludeRegExp = false;
  256. m_UseExcludeRegExp = false;
  257. m_UseExcludeRegExpFirst = false;
  258. m_CustomMaximumPassedTestOutputSize = 1 * 1024;
  259. m_CustomMaximumFailedTestOutputSize = 300 * 1024;
  260. m_MemCheck = false;
  261. }
  262. //----------------------------------------------------------------------
  263. void cmCTestTestHandler::Initialize()
  264. {
  265. m_ElapsedTestingTime = -1;
  266. m_TestResults.clear();
  267. m_CustomTestsIgnore.clear();
  268. m_StartTest = "";
  269. m_EndTest = "";
  270. m_CustomPreTest.clear();
  271. m_CustomPostTest.clear();
  272. m_CustomMaximumPassedTestOutputSize = 1 * 1024;
  273. m_CustomMaximumFailedTestOutputSize = 300 * 1024;
  274. m_TestsToRun.clear();
  275. m_UseIncludeRegExp = false;
  276. m_UseExcludeRegExp = false;
  277. m_UseExcludeRegExpFirst = false;
  278. m_IncludeRegExp = "";
  279. m_ExcludeRegExp = "";
  280. TestsToRunString = "";
  281. m_UseUnion = false;
  282. m_TestList.clear();
  283. }
  284. //----------------------------------------------------------------------
  285. void cmCTestTestHandler::PopulateCustomVectors(cmMakefile *mf)
  286. {
  287. cmCTest::PopulateCustomVector(mf, "CTEST_CUSTOM_PRE_TEST",
  288. m_CustomPreTest);
  289. cmCTest::PopulateCustomVector(mf, "CTEST_CUSTOM_POST_TEST",
  290. m_CustomPostTest);
  291. cmCTest::PopulateCustomVector(mf,
  292. "CTEST_CUSTOM_TESTS_IGNORE",
  293. m_CustomTestsIgnore);
  294. cmCTest::PopulateCustomInteger(mf,
  295. "CTEST_CUSTOM_MAXIMUM_PASSED_TEST_OUTPUT_SIZE",
  296. m_CustomMaximumPassedTestOutputSize);
  297. cmCTest::PopulateCustomInteger(mf,
  298. "CTEST_CUSTOM_MAXIMUM_FAILED_TEST_OUTPUT_SIZE",
  299. m_CustomMaximumFailedTestOutputSize);
  300. }
  301. //----------------------------------------------------------------------
  302. int cmCTestTestHandler::PreProcessHandler()
  303. {
  304. if ( !this->ExecuteCommands(m_CustomPreTest) )
  305. {
  306. cmCTestLog(m_CTest, ERROR_MESSAGE, "Problem executing pre-test command(s)." << std::endl);
  307. return 0;
  308. }
  309. return 1;
  310. }
  311. //----------------------------------------------------------------------
  312. int cmCTestTestHandler::PostProcessHandler()
  313. {
  314. if ( !this->ExecuteCommands(m_CustomPostTest) )
  315. {
  316. cmCTestLog(m_CTest, ERROR_MESSAGE, "Problem executing post-test command(s)." << std::endl);
  317. return 0;
  318. }
  319. return 1;
  320. }
  321. //----------------------------------------------------------------------
  322. //clearly it would be nice if this were broken up into a few smaller
  323. //functions and commented...
  324. int cmCTestTestHandler::ProcessHandler()
  325. {
  326. // Update internal data structure from generic one
  327. this->SetTestsToRunInformation(this->GetOption("TestsToRunInformation"));
  328. this->SetUseUnion(cmSystemTools::IsOn(this->GetOption("UseUnion")));
  329. const char* val;
  330. val = this->GetOption("IncludeRegularExpression");
  331. if ( val )
  332. {
  333. this->UseIncludeRegExp();
  334. this->SetIncludeRegExp(val);
  335. }
  336. val = this->GetOption("ExcludeRegularExpression");
  337. if ( val )
  338. {
  339. this->UseExcludeRegExp();
  340. this->SetExcludeRegExp(val);
  341. }
  342. m_TestResults.clear();
  343. cmCTestLog(m_CTest, HANDLER_OUTPUT, (m_MemCheck ? "Memory check" : "Test") << " project" << std::endl);
  344. if ( ! this->PreProcessHandler() )
  345. {
  346. return -1;
  347. }
  348. std::vector<cmStdString> passed;
  349. std::vector<cmStdString> failed;
  350. int total;
  351. this->ProcessDirectory(passed, failed);
  352. total = int(passed.size()) + int(failed.size());
  353. if (total == 0)
  354. {
  355. if ( !m_CTest->GetShowOnly() )
  356. {
  357. cmCTestLog(m_CTest, ERROR_MESSAGE, "No tests were found!!!" << std::endl);
  358. }
  359. }
  360. else
  361. {
  362. if (m_HandlerVerbose && passed.size() &&
  363. (m_UseIncludeRegExp || m_UseExcludeRegExp))
  364. {
  365. cmCTestLog(m_CTest, HANDLER_VERBOSE_OUTPUT, std::endl << "The following tests passed:" << std::endl);
  366. for(std::vector<cmStdString>::iterator j = passed.begin();
  367. j != passed.end(); ++j)
  368. {
  369. cmCTestLog(m_CTest, HANDLER_VERBOSE_OUTPUT, "\t" << *j << std::endl);
  370. }
  371. }
  372. float percent = float(passed.size()) * 100.0f / total;
  373. if ( failed.size() > 0 && percent > 99)
  374. {
  375. percent = 99;
  376. }
  377. cmCTestLog(m_CTest, HANDLER_OUTPUT, std::endl << std::setprecision(0) << percent << "% tests passed, "
  378. << failed.size() << " tests failed out of " << total << std::endl);
  379. //fprintf(stderr,"\n%.0f%% tests passed, %i tests failed out of %i\n",
  380. // percent, int(failed.size()), total);
  381. if (failed.size())
  382. {
  383. cmGeneratedFileStream ofs;
  384. cmCTestLog(m_CTest, ERROR_MESSAGE, std::endl << "The following tests FAILED:" << std::endl);
  385. m_CTest->OpenOutputFile("Temporary", "LastTestsFailed.log", ofs);
  386. std::vector<cmCTestTestHandler::cmCTestTestResult>::iterator ftit;
  387. for(ftit = m_TestResults.begin();
  388. ftit != m_TestResults.end(); ++ftit)
  389. {
  390. if ( ftit->m_Status != cmCTestTestHandler::COMPLETED )
  391. {
  392. ofs << ftit->m_TestCount << ":" << ftit->m_Name << std::endl;
  393. cmCTestLog(m_CTest, HANDLER_OUTPUT, "\t" << std::setw(3) << ftit->m_TestCount << " - " << ftit->m_Name.c_str() << " (" << this->GetTestStatus(ftit->m_Status) << ")" << std::endl);
  394. //fprintf(stderr, "\t%3d - %s (%s)\n", ftit->m_TestCount, ftit->m_Name.c_str(),
  395. // this->GetTestStatus(ftit->m_Status));
  396. }
  397. }
  398. }
  399. }
  400. if ( m_CTest->GetProduceXML() )
  401. {
  402. cmGeneratedFileStream xmlfile;
  403. if( !m_CTest->OpenOutputFile(m_CTest->GetCurrentTag(),
  404. (m_MemCheck ? "DynamicAnalysis.xml" : "Test.xml"), xmlfile, true) )
  405. {
  406. cmCTestLog(m_CTest, ERROR_MESSAGE, "Cannot create " << (m_MemCheck ? "memory check" : "testing")
  407. << " XML file" << std::endl);
  408. return 1;
  409. }
  410. this->GenerateDartOutput(xmlfile);
  411. }
  412. if ( ! this->PostProcessHandler() )
  413. {
  414. return -1;
  415. }
  416. if ( !failed.empty() )
  417. {
  418. return -1;
  419. }
  420. return 0;
  421. }
  422. //----------------------------------------------------------------------
  423. void cmCTestTestHandler::ProcessDirectory(std::vector<cmStdString> &passed,
  424. std::vector<cmStdString> &failed)
  425. {
  426. std::string current_dir = cmSystemTools::GetCurrentWorkingDirectory();
  427. cmsys::RegularExpression dartStuff("(<DartMeasurement.*/DartMeasurement[a-zA-Z]*>)");
  428. m_TestList.clear();
  429. this->GetListOfTests();
  430. tm_ListOfTests::size_type tmsize = m_TestList.size();
  431. cmGeneratedFileStream ofs;
  432. cmGeneratedFileStream *olog = 0;
  433. if ( !m_CTest->GetShowOnly() && tmsize > 0 &&
  434. m_CTest->OpenOutputFile("Temporary",
  435. (m_MemCheck?"LastMemCheck.log":"LastTest.log"), ofs) )
  436. {
  437. olog = &ofs;
  438. }
  439. m_StartTest = m_CTest->CurrentTime();
  440. double elapsed_time_start = cmSystemTools::GetTime();
  441. if ( olog )
  442. {
  443. *olog << "Start testing: " << m_StartTest << std::endl
  444. << "----------------------------------------------------------"
  445. << std::endl;
  446. }
  447. // how many tests are in based on RegExp?
  448. int inREcnt = 0;
  449. tm_ListOfTests::iterator it;
  450. for ( it = m_TestList.begin(); it != m_TestList.end(); it ++ )
  451. {
  452. if (it->m_IsInBasedOnREOptions)
  453. {
  454. inREcnt ++;
  455. }
  456. }
  457. // expand the test list based on the union flag
  458. if (m_UseUnion)
  459. {
  460. this->ExpandTestsToRunInformation((int)tmsize);
  461. }
  462. else
  463. {
  464. this->ExpandTestsToRunInformation(inREcnt);
  465. }
  466. int cnt = 0;
  467. inREcnt = 0;
  468. std::string last_directory = "";
  469. for ( it = m_TestList.begin(); it != m_TestList.end(); it ++ )
  470. {
  471. cnt ++;
  472. if (it->m_IsInBasedOnREOptions)
  473. {
  474. inREcnt++;
  475. }
  476. const std::string& testname = it->m_Name;
  477. std::vector<std::string>& args = it->m_Args;
  478. cmCTestTestResult cres;
  479. cres.m_Status = cmCTestTestHandler::NOT_RUN;
  480. cres.m_TestCount = cnt;
  481. if (!(last_directory == it->m_Directory))
  482. {
  483. cmCTestLog(m_CTest, HANDLER_VERBOSE_OUTPUT, "Changing directory into "
  484. << it->m_Directory.c_str() << "\n");
  485. last_directory = it->m_Directory;
  486. cmSystemTools::ChangeDirectory(it->m_Directory.c_str());
  487. }
  488. cres.m_Name = testname;
  489. cres.m_Path = it->m_Directory.c_str();
  490. if (m_UseUnion)
  491. {
  492. // if it is not in the list and not in the regexp then skip
  493. if ((m_TestsToRun.size() &&
  494. std::find(m_TestsToRun.begin(), m_TestsToRun.end(), cnt)
  495. == m_TestsToRun.end()) && !it->m_IsInBasedOnREOptions)
  496. {
  497. continue;
  498. }
  499. }
  500. else
  501. {
  502. // is this test in the list of tests to run? If not then skip it
  503. if ((m_TestsToRun.size() &&
  504. std::find(m_TestsToRun.begin(), m_TestsToRun.end(), inREcnt)
  505. == m_TestsToRun.end()) || !it->m_IsInBasedOnREOptions)
  506. {
  507. continue;
  508. }
  509. }
  510. cmCTestLog(m_CTest, HANDLER_OUTPUT, std::setw(3) << cnt << "/");
  511. cmCTestLog(m_CTest, HANDLER_OUTPUT, std::setw(3) << tmsize << " Testing ");
  512. std::string outname = testname;
  513. outname.resize(30, ' ');
  514. if ( m_CTest->GetShowOnly() )
  515. {
  516. cmCTestLog(m_CTest, HANDLER_OUTPUT, outname.c_str() << std::endl);
  517. }
  518. else
  519. {
  520. cmCTestLog(m_CTest, HANDLER_OUTPUT, outname.c_str());
  521. }
  522. cmCTestLog(m_CTest, DEBUG, "Testing " << args[0].c_str() << " ... ");
  523. // find the test executable
  524. std::string actualCommand = this->FindTheExecutable(args[1].c_str());
  525. std::string testCommand = cmSystemTools::ConvertToOutputPath(actualCommand.c_str());
  526. // continue if we did not find the executable
  527. if (testCommand == "")
  528. {
  529. cmCTestLog(m_CTest, ERROR_MESSAGE, "Unable to find executable: "
  530. << args[1].c_str() << std::endl);
  531. if ( !m_CTest->GetShowOnly() )
  532. {
  533. cres.m_FullCommandLine = actualCommand;
  534. m_TestResults.push_back( cres );
  535. failed.push_back(testname);
  536. continue;
  537. }
  538. }
  539. // add the arguments
  540. std::vector<std::string>::const_iterator j = args.begin();
  541. ++j;
  542. ++j;
  543. std::vector<const char*> arguments;
  544. this->GenerateTestCommand(arguments);
  545. arguments.push_back(actualCommand.c_str());
  546. for(;j != args.end(); ++j)
  547. {
  548. testCommand += " ";
  549. testCommand += cmSystemTools::EscapeSpaces(j->c_str());
  550. arguments.push_back(j->c_str());
  551. }
  552. arguments.push_back(0);
  553. /**
  554. * Run an executable command and put the stdout in output.
  555. */
  556. std::string output;
  557. int retVal = 0;
  558. cmCTestLog(m_CTest, HANDLER_VERBOSE_OUTPUT, std::endl << (m_MemCheck?"MemCheck":"Test") << " command: " << testCommand << std::endl);
  559. if ( olog )
  560. {
  561. *olog << cnt << "/" << tmsize
  562. << " Test: " << testname.c_str() << std::endl;
  563. *olog << "Command: ";
  564. std::vector<cmStdString>::size_type ll;
  565. for ( ll = 0; ll < arguments.size()-1; ll ++ )
  566. {
  567. *olog << "\"" << arguments[ll] << "\" ";
  568. }
  569. *olog
  570. << std::endl
  571. << "Directory: " << it->m_Directory << std::endl
  572. << "\"" << testname.c_str() << "\" start time: "
  573. << m_CTest->CurrentTime() << std::endl
  574. << "Output:" << std::endl
  575. << "----------------------------------------------------------"
  576. << std::endl;
  577. }
  578. int res = 0;
  579. double clock_start, clock_finish;
  580. clock_start = cmSystemTools::GetTime();
  581. if ( !m_CTest->GetShowOnly() )
  582. {
  583. res = m_CTest->RunTest(arguments, &output, &retVal, olog);
  584. }
  585. clock_finish = cmSystemTools::GetTime();
  586. if ( olog )
  587. {
  588. double ttime = clock_finish - clock_start;
  589. int hours = static_cast<int>(ttime / (60 * 60));
  590. int minutes = static_cast<int>(ttime / 60) % 60;
  591. int seconds = static_cast<int>(ttime) % 60;
  592. char buffer[100];
  593. sprintf(buffer, "%02d:%02d:%02d", hours, minutes, seconds);
  594. *olog
  595. << "----------------------------------------------------------"
  596. << std::endl
  597. << "\"" << testname.c_str() << "\" end time: "
  598. << m_CTest->CurrentTime() << std::endl
  599. << "\"" << testname.c_str() << "\" time elapsed: "
  600. << buffer << std::endl
  601. << "----------------------------------------------------------"
  602. << std::endl << std::endl;
  603. }
  604. cres.m_ExecutionTime = (double)(clock_finish - clock_start);
  605. cres.m_FullCommandLine = testCommand;
  606. if ( !m_CTest->GetShowOnly() )
  607. {
  608. if (res == cmsysProcess_State_Exited && retVal == 0)
  609. {
  610. cmCTestLog(m_CTest, HANDLER_OUTPUT, " Passed" << std::endl);
  611. passed.push_back(testname);
  612. cres.m_Status = cmCTestTestHandler::COMPLETED;
  613. }
  614. else
  615. {
  616. cres.m_Status = cmCTestTestHandler::FAILED;
  617. if ( res == cmsysProcess_State_Expired )
  618. {
  619. cmCTestLog(m_CTest, HANDLER_OUTPUT, "***Timeout" << std::endl);
  620. cres.m_Status = cmCTestTestHandler::TIMEOUT;
  621. }
  622. else if ( res == cmsysProcess_State_Exception )
  623. {
  624. cmCTestLog(m_CTest, HANDLER_OUTPUT, "***Exception: ");
  625. switch ( retVal )
  626. {
  627. case cmsysProcess_Exception_Fault:
  628. cmCTestLog(m_CTest, HANDLER_OUTPUT, "SegFault");
  629. cres.m_Status = cmCTestTestHandler::SEGFAULT;
  630. break;
  631. case cmsysProcess_Exception_Illegal:
  632. cmCTestLog(m_CTest, HANDLER_OUTPUT, "Illegal");
  633. cres.m_Status = cmCTestTestHandler::ILLEGAL;
  634. break;
  635. case cmsysProcess_Exception_Interrupt:
  636. cmCTestLog(m_CTest, HANDLER_OUTPUT, "Interrupt");
  637. cres.m_Status = cmCTestTestHandler::INTERRUPT;
  638. break;
  639. case cmsysProcess_Exception_Numerical:
  640. cmCTestLog(m_CTest, HANDLER_OUTPUT, "Numerical");
  641. cres.m_Status = cmCTestTestHandler::NUMERICAL;
  642. break;
  643. default:
  644. cmCTestLog(m_CTest, HANDLER_OUTPUT, "Other");
  645. cres.m_Status = cmCTestTestHandler::OTHER_FAULT;
  646. }
  647. cmCTestLog(m_CTest, HANDLER_OUTPUT, std::endl);
  648. }
  649. else if ( res == cmsysProcess_State_Error )
  650. {
  651. cmCTestLog(m_CTest, HANDLER_OUTPUT, "***Bad command " << res << std::endl);
  652. cres.m_Status = cmCTestTestHandler::BAD_COMMAND;
  653. }
  654. else
  655. {
  656. cmCTestLog(m_CTest, HANDLER_OUTPUT, "***Failed" << std::endl);
  657. }
  658. failed.push_back(testname);
  659. }
  660. if (output != "")
  661. {
  662. if (dartStuff.find(output.c_str()))
  663. {
  664. std::string dartString = dartStuff.match(1);
  665. cmSystemTools::ReplaceString(output, dartString.c_str(),"");
  666. cres.m_RegressionImages = this->GenerateRegressionImages(dartString);
  667. }
  668. }
  669. }
  670. if ( cres.m_Status == cmCTestTestHandler::COMPLETED )
  671. {
  672. this->CleanTestOutput(output, static_cast<size_t>(m_CustomMaximumPassedTestOutputSize));
  673. }
  674. else
  675. {
  676. this->CleanTestOutput(output, static_cast<size_t>(m_CustomMaximumFailedTestOutputSize));
  677. }
  678. cres.m_Output = output;
  679. cres.m_ReturnValue = retVal;
  680. cres.m_CompletionStatus = "Completed";
  681. m_TestResults.push_back( cres );
  682. }
  683. m_EndTest = m_CTest->CurrentTime();
  684. m_ElapsedTestingTime = cmSystemTools::GetTime() - elapsed_time_start;
  685. if ( olog )
  686. {
  687. *olog << "End testing: " << m_EndTest << std::endl;
  688. }
  689. cmSystemTools::ChangeDirectory(current_dir.c_str());
  690. }
  691. //----------------------------------------------------------------------
  692. void cmCTestTestHandler::GenerateTestCommand(std::vector<const char*>&)
  693. {
  694. }
  695. //----------------------------------------------------------------------
  696. void cmCTestTestHandler::GenerateDartOutput(std::ostream& os)
  697. {
  698. if ( !m_CTest->GetProduceXML() )
  699. {
  700. return;
  701. }
  702. m_CTest->StartXML(os);
  703. os << "<Testing>\n"
  704. << "\t<StartDateTime>" << m_StartTest << "</StartDateTime>\n"
  705. << "\t<TestList>\n";
  706. tm_TestResultsVector::size_type cc;
  707. for ( cc = 0; cc < m_TestResults.size(); cc ++ )
  708. {
  709. cmCTestTestResult *result = &m_TestResults[cc];
  710. std::string testPath = result->m_Path + "/" + result->m_Name;
  711. os << "\t\t<Test>" << cmCTest::MakeXMLSafe(
  712. m_CTest->GetShortPathToFile(testPath.c_str()))
  713. << "</Test>" << std::endl;
  714. }
  715. os << "\t</TestList>\n";
  716. for ( cc = 0; cc < m_TestResults.size(); cc ++ )
  717. {
  718. cmCTestTestResult *result = &m_TestResults[cc];
  719. os << "\t<Test Status=\"";
  720. if ( result->m_Status == cmCTestTestHandler::COMPLETED )
  721. {
  722. os << "passed";
  723. }
  724. else if ( result->m_Status == cmCTestTestHandler::NOT_RUN )
  725. {
  726. os << "notrun";
  727. }
  728. else
  729. {
  730. os << "failed";
  731. }
  732. std::string testPath = result->m_Path + "/" + result->m_Name;
  733. os << "\">\n"
  734. << "\t\t<Name>" << cmCTest::MakeXMLSafe(result->m_Name) << "</Name>\n"
  735. << "\t\t<Path>" << cmCTest::MakeXMLSafe(
  736. m_CTest->GetShortPathToFile(result->m_Path.c_str())) << "</Path>\n"
  737. << "\t\t<FullName>" << cmCTest::MakeXMLSafe(
  738. m_CTest->GetShortPathToFile(testPath.c_str())) << "</FullName>\n"
  739. << "\t\t<FullCommandLine>"
  740. << cmCTest::MakeXMLSafe(result->m_FullCommandLine)
  741. << "</FullCommandLine>\n"
  742. << "\t\t<Results>" << std::endl;
  743. if ( result->m_Status != cmCTestTestHandler::NOT_RUN )
  744. {
  745. if ( result->m_Status != cmCTestTestHandler::COMPLETED || result->m_ReturnValue )
  746. {
  747. os << "\t\t\t<NamedMeasurement type=\"text/string\" name=\"Exit Code\"><Value>"
  748. << this->GetTestStatus(result->m_Status) << "</Value></NamedMeasurement>\n"
  749. << "\t\t\t<NamedMeasurement type=\"text/string\" name=\"Exit Value\"><Value>"
  750. << result->m_ReturnValue << "</Value></NamedMeasurement>" << std::endl;
  751. }
  752. os << result->m_RegressionImages;
  753. os << "\t\t\t<NamedMeasurement type=\"numeric/double\" "
  754. << "name=\"Execution Time\"><Value>"
  755. << result->m_ExecutionTime << "</Value></NamedMeasurement>\n";
  756. os
  757. << "\t\t\t<NamedMeasurement type=\"text/string\" "
  758. << "name=\"Completion Status\"><Value>"
  759. << result->m_CompletionStatus << "</Value></NamedMeasurement>\n";
  760. }
  761. os
  762. << "\t\t\t<Measurement>\n"
  763. << "\t\t\t\t<Value>";
  764. os << cmCTest::MakeXMLSafe(result->m_Output);
  765. os
  766. << "</Value>\n"
  767. << "\t\t\t</Measurement>\n"
  768. << "\t\t</Results>\n"
  769. << "\t</Test>" << std::endl;
  770. }
  771. os << "\t<EndDateTime>" << m_EndTest << "</EndDateTime>\n"
  772. << "<ElapsedMinutes>"
  773. << static_cast<int>(m_ElapsedTestingTime/6)/10.0
  774. << "</ElapsedMinutes>"
  775. << "</Testing>" << std::endl;
  776. m_CTest->EndXML(os);
  777. }
  778. //----------------------------------------------------------------------
  779. int cmCTestTestHandler::ExecuteCommands(std::vector<cmStdString>& vec)
  780. {
  781. std::vector<cmStdString>::iterator it;
  782. for ( it = vec.begin(); it != vec.end(); ++it )
  783. {
  784. int retVal = 0;
  785. cmCTestLog(m_CTest, HANDLER_VERBOSE_OUTPUT, "Run command: " << *it << std::endl);
  786. if ( !cmSystemTools::RunSingleCommand(it->c_str(), 0, &retVal, 0, true /*m_Verbose*/) ||
  787. retVal != 0 )
  788. {
  789. cmCTestLog(m_CTest, ERROR_MESSAGE, "Problem running command: " << *it << std::endl);
  790. return 0;
  791. }
  792. }
  793. return 1;
  794. }
  795. //----------------------------------------------------------------------
  796. std::string cmCTestTestHandler::FindTheExecutable(const char *exe)
  797. {
  798. std::string fullPath = "";
  799. std::string dir;
  800. std::string file;
  801. cmSystemTools::SplitProgramPath(exe, dir, file);
  802. if(m_CTest->GetConfigType() != "" &&
  803. ::TryExecutable(dir.c_str(), file.c_str(), &fullPath,
  804. m_CTest->GetConfigType().c_str()))
  805. {
  806. return fullPath;
  807. }
  808. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"."))
  809. {
  810. return fullPath;
  811. }
  812. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,""))
  813. {
  814. return fullPath;
  815. }
  816. if ( m_CTest->GetConfigType() == "" )
  817. {
  818. // No config type, so try to guess it
  819. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Release"))
  820. {
  821. return fullPath;
  822. }
  823. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"Debug"))
  824. {
  825. return fullPath;
  826. }
  827. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"MinSizeRel"))
  828. {
  829. return fullPath;
  830. }
  831. if (::TryExecutable(dir.c_str(),file.c_str(),&fullPath,"RelWithDebInfo"))
  832. {
  833. return fullPath;
  834. }
  835. }
  836. // if everything else failed, check the users path
  837. if (dir != "")
  838. {
  839. std::string path = cmSystemTools::FindProgram(file.c_str());
  840. if (path != "")
  841. {
  842. return path;
  843. }
  844. }
  845. if ( m_CTest->GetConfigType() != "" )
  846. {
  847. dir += "/";
  848. dir += m_CTest->GetConfigType();
  849. dir += "/";
  850. dir += file;
  851. cmSystemTools::Error("config type specified on the command line, but test executable not found.",
  852. dir.c_str());
  853. return "";
  854. }
  855. return fullPath;
  856. }
  857. //----------------------------------------------------------------------
  858. void cmCTestTestHandler::GetListOfTests()
  859. {
  860. if ( !m_IncludeRegExp.empty() )
  861. {
  862. m_IncludeTestsRegularExpression.compile(m_IncludeRegExp.c_str());
  863. }
  864. if ( !m_ExcludeRegExp.empty() )
  865. {
  866. m_ExcludeTestsRegularExpression.compile(m_ExcludeRegExp.c_str());
  867. }
  868. cmCTestLog(m_CTest, HANDLER_VERBOSE_OUTPUT, "Constructing a list of tests" << std::endl);
  869. cmake cm;
  870. cmGlobalGenerator gg;
  871. gg.SetCMakeInstance(&cm);
  872. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  873. lg->SetGlobalGenerator(&gg);
  874. cmMakefile *mf = lg->GetMakefile();
  875. // Add handler for ADD_TEST
  876. cmCTestAddTestCommand* newCom1 = new cmCTestAddTestCommand;
  877. newCom1->m_TestHandler = this;
  878. cm.AddCommand(newCom1);
  879. // Add handler for SUBDIR
  880. cmCTestSubdirCommand* newCom2 = new cmCTestSubdirCommand;
  881. newCom2->m_TestHandler = this;
  882. cm.AddCommand(newCom2);
  883. const char* testFilename = 0;
  884. if( cmSystemTools::FileExists("CTestTestfile.cmake") )
  885. {
  886. // does the CTestTestfile.cmake exist ?
  887. testFilename = "CTestTestfile.cmake";
  888. }
  889. else if( cmSystemTools::FileExists("DartTestfile.txt") )
  890. {
  891. // does the DartTestfile.txt exist ?
  892. testFilename = "DartTestfile.txt";
  893. }
  894. else
  895. {
  896. return;
  897. }
  898. if ( !mf->ReadListFile(0, testFilename) )
  899. {
  900. return;
  901. }
  902. if ( cmSystemTools::GetErrorOccuredFlag() )
  903. {
  904. return;
  905. }
  906. cmCTestLog(m_CTest, HANDLER_VERBOSE_OUTPUT, "Done constructing a list of tests" << std::endl);
  907. }
  908. //----------------------------------------------------------------------
  909. void cmCTestTestHandler::UseIncludeRegExp()
  910. {
  911. this->m_UseIncludeRegExp = true;
  912. }
  913. //----------------------------------------------------------------------
  914. void cmCTestTestHandler::UseExcludeRegExp()
  915. {
  916. this->m_UseExcludeRegExp = true;
  917. this->m_UseExcludeRegExpFirst = this->m_UseIncludeRegExp ? false : true;
  918. }
  919. //----------------------------------------------------------------------
  920. const char* cmCTestTestHandler::GetTestStatus(int status)
  921. {
  922. static const char statuses[][100] = {
  923. "Not Run",
  924. "Timeout",
  925. "SEGFAULT",
  926. "ILLEGAL",
  927. "INTERRUPT",
  928. "NUMERICAL",
  929. "OTHER_FAULT",
  930. "Failed",
  931. "BAD_COMMAND",
  932. "Completed"
  933. };
  934. if ( status < cmCTestTestHandler::NOT_RUN ||
  935. status > cmCTestTestHandler::COMPLETED )
  936. {
  937. return "No Status";
  938. }
  939. return statuses[status];
  940. }
  941. //----------------------------------------------------------------------
  942. void cmCTestTestHandler::ExpandTestsToRunInformation(int numTests)
  943. {
  944. if (this->TestsToRunString.empty())
  945. {
  946. return;
  947. }
  948. int start;
  949. int end = -1;
  950. double stride = -1;
  951. std::string::size_type pos = 0;
  952. std::string::size_type pos2;
  953. // read start
  954. if(GetNextNumber(this->TestsToRunString, start, pos, pos2))
  955. {
  956. // read end
  957. if(GetNextNumber(this->TestsToRunString, end, pos, pos2))
  958. {
  959. // read stride
  960. if(GetNextRealNumber(this->TestsToRunString, stride, pos, pos2))
  961. {
  962. int val =0;
  963. // now read specific numbers
  964. while(GetNextNumber(this->TestsToRunString, val, pos, pos2))
  965. {
  966. m_TestsToRun.push_back(val);
  967. }
  968. m_TestsToRun.push_back(val);
  969. }
  970. }
  971. }
  972. // if start is not specified then we assume we start at 1
  973. if(start == -1)
  974. {
  975. start = 1;
  976. }
  977. // if end isnot specified then we assume we end with the last test
  978. if(end == -1)
  979. {
  980. end = numTests;
  981. }
  982. // if the stride wasn't specified then it defaults to 1
  983. if(stride == -1)
  984. {
  985. stride = 1;
  986. }
  987. // if we have a range then add it
  988. if(end != -1 && start != -1 && stride > 0)
  989. {
  990. int i = 0;
  991. while (i*stride + start <= end)
  992. {
  993. m_TestsToRun.push_back(static_cast<int>(i*stride+start));
  994. ++i;
  995. }
  996. }
  997. // sort the array
  998. std::sort(m_TestsToRun.begin(), m_TestsToRun.end(), std::less<int>());
  999. // remove duplicates
  1000. std::vector<int>::iterator new_end =
  1001. std::unique(m_TestsToRun.begin(), m_TestsToRun.end());
  1002. m_TestsToRun.erase(new_end, m_TestsToRun.end());
  1003. }
  1004. //----------------------------------------------------------------------
  1005. // Just for convenience
  1006. #define SPACE_REGEX "[ \t\r\n]"
  1007. //----------------------------------------------------------------------
  1008. std::string cmCTestTestHandler::GenerateRegressionImages(
  1009. const std::string& xml)
  1010. {
  1011. cmsys::RegularExpression twoattributes(
  1012. "<DartMeasurement"
  1013. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1014. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1015. SPACE_REGEX "*>([^<]*)</DartMeasurement>");
  1016. cmsys::RegularExpression threeattributes(
  1017. "<DartMeasurement"
  1018. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1019. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1020. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1021. SPACE_REGEX "*>([^<]*)</DartMeasurement>");
  1022. cmsys::RegularExpression fourattributes(
  1023. "<DartMeasurement"
  1024. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1025. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1026. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1027. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1028. SPACE_REGEX "*>([^<]*)</DartMeasurement>");
  1029. cmsys::RegularExpression measurementfile(
  1030. "<DartMeasurementFile"
  1031. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1032. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1033. SPACE_REGEX "*>([^<]*)</DartMeasurementFile>");
  1034. cmOStringStream ostr;
  1035. bool done = false;
  1036. std::string cxml = xml;
  1037. while ( ! done )
  1038. {
  1039. if ( twoattributes.find(cxml) )
  1040. {
  1041. ostr
  1042. << "\t\t\t<NamedMeasurement"
  1043. << " " << twoattributes.match(1) << "=\"" << twoattributes.match(2) << "\""
  1044. << " " << twoattributes.match(3) << "=\"" << twoattributes.match(4) << "\""
  1045. << "><Value>" << twoattributes.match(5)
  1046. << "</Value></NamedMeasurement>"
  1047. << std::endl;
  1048. cxml.erase(twoattributes.start(), twoattributes.end() - twoattributes.start());
  1049. }
  1050. else if ( threeattributes.find(cxml) )
  1051. {
  1052. ostr
  1053. << "\t\t\t<NamedMeasurement"
  1054. << " " << threeattributes.match(1) << "=\"" << threeattributes.match(2) << "\""
  1055. << " " << threeattributes.match(3) << "=\"" << threeattributes.match(4) << "\""
  1056. << " " << threeattributes.match(5) << "=\"" << threeattributes.match(6) << "\""
  1057. << "><Value>" << threeattributes.match(7)
  1058. << "</Value></NamedMeasurement>"
  1059. << std::endl;
  1060. cxml.erase(threeattributes.start(), threeattributes.end() - threeattributes.start());
  1061. }
  1062. else if ( fourattributes.find(cxml) )
  1063. {
  1064. ostr
  1065. << "\t\t\t<NamedMeasurement"
  1066. << " " << fourattributes.match(1) << "=\"" << fourattributes.match(2) << "\""
  1067. << " " << fourattributes.match(3) << "=\"" << fourattributes.match(4) << "\""
  1068. << " " << fourattributes.match(5) << "=\"" << fourattributes.match(6) << "\""
  1069. << " " << fourattributes.match(7) << "=\"" << fourattributes.match(8) << "\""
  1070. << "><Value>" << fourattributes.match(9)
  1071. << "</Value></NamedMeasurement>"
  1072. << std::endl;
  1073. cxml.erase(fourattributes.start(), fourattributes.end() - fourattributes.start());
  1074. }
  1075. else if ( measurementfile.find(cxml) )
  1076. {
  1077. const std::string& filename =
  1078. cmCTest::CleanString(measurementfile.match(5));
  1079. if ( cmSystemTools::FileExists(filename.c_str()) )
  1080. {
  1081. long len = cmSystemTools::FileLength(filename.c_str());
  1082. if ( len == 0 )
  1083. {
  1084. std::string k1 = measurementfile.match(1);
  1085. std::string v1 = measurementfile.match(2);
  1086. std::string k2 = measurementfile.match(3);
  1087. std::string v2 = measurementfile.match(4);
  1088. if ( cmSystemTools::LowerCase(k1) == "type" )
  1089. {
  1090. v1 = "text/string";
  1091. }
  1092. if ( cmSystemTools::LowerCase(k2) == "type" )
  1093. {
  1094. v2 = "text/string";
  1095. }
  1096. ostr
  1097. << "\t\t\t<NamedMeasurement"
  1098. << " " << k1 << "=\"" << v1 << "\""
  1099. << " " << k2 << "=\"" << v2 << "\""
  1100. << " encoding=\"none\""
  1101. << "><Value>Image " << filename.c_str()
  1102. << " is empty</Value></NamedMeasurement>";
  1103. }
  1104. else
  1105. {
  1106. std::ifstream ifs(filename.c_str(), std::ios::in
  1107. #ifdef _WIN32
  1108. | std::ios::binary
  1109. #endif
  1110. );
  1111. unsigned char *file_buffer = new unsigned char [ len + 1 ];
  1112. ifs.read(reinterpret_cast<char*>(file_buffer), len);
  1113. unsigned char *encoded_buffer = new unsigned char [ static_cast<int>(len * 1.5 + 5) ];
  1114. unsigned long rlen = cmsysBase64_Encode(file_buffer, len, encoded_buffer, 1);
  1115. unsigned long cc;
  1116. ostr
  1117. << "\t\t\t<NamedMeasurement"
  1118. << " " << measurementfile.match(1) << "=\"" << measurementfile.match(2) << "\""
  1119. << " " << measurementfile.match(3) << "=\"" << measurementfile.match(4) << "\""
  1120. << " encoding=\"base64\""
  1121. << ">" << std::endl << "\t\t\t\t<Value>";
  1122. for ( cc = 0; cc < rlen; cc ++ )
  1123. {
  1124. ostr << encoded_buffer[cc];
  1125. if ( cc % 60 == 0 && cc )
  1126. {
  1127. ostr << std::endl;
  1128. }
  1129. }
  1130. ostr
  1131. << "</Value>" << std::endl << "\t\t\t</NamedMeasurement>"
  1132. << std::endl;
  1133. delete [] file_buffer;
  1134. delete [] encoded_buffer;
  1135. }
  1136. }
  1137. else
  1138. {
  1139. int idx = 4;
  1140. if ( measurementfile.match(1) == "name" )
  1141. {
  1142. idx = 2;
  1143. }
  1144. ostr
  1145. << "\t\t\t<NamedMeasurement"
  1146. << " name=\"" << measurementfile.match(idx) << "\""
  1147. << " text=\"text/string\""
  1148. << "><Value>File " << filename.c_str() << " not found</Value></NamedMeasurement>"
  1149. << std::endl;
  1150. cmCTestLog(m_CTest, HANDLER_OUTPUT, "File \"" << filename.c_str() << "\" not found." << std::endl);
  1151. }
  1152. cxml.erase(measurementfile.start(), measurementfile.end() - measurementfile.start());
  1153. }
  1154. else
  1155. {
  1156. done = true;
  1157. }
  1158. }
  1159. return ostr.str();
  1160. }
  1161. //----------------------------------------------------------------------
  1162. void cmCTestTestHandler::SetIncludeRegExp(const char *arg)
  1163. {
  1164. m_IncludeRegExp = arg;
  1165. }
  1166. //----------------------------------------------------------------------
  1167. void cmCTestTestHandler::SetExcludeRegExp(const char *arg)
  1168. {
  1169. m_ExcludeRegExp = arg;
  1170. }
  1171. //----------------------------------------------------------------------
  1172. void cmCTestTestHandler::SetTestsToRunInformation(const char* in)
  1173. {
  1174. if ( !in )
  1175. {
  1176. return;
  1177. }
  1178. this->TestsToRunString = in;
  1179. // if the argument is a file, then read it and use the contents as the string
  1180. if(cmSystemTools::FileExists(in))
  1181. {
  1182. std::ifstream fin(in);
  1183. unsigned long filelen = cmSystemTools::FileLength(in);
  1184. char* buff = new char[filelen+1];
  1185. fin.getline(buff, filelen);
  1186. buff[fin.gcount()] = 0;
  1187. this->TestsToRunString = buff;
  1188. }
  1189. }
  1190. //----------------------------------------------------------------------
  1191. bool cmCTestTestHandler::CleanTestOutput(std::string& output, size_t remove_threshold)
  1192. {
  1193. if ( remove_threshold == 0 )
  1194. {
  1195. return true;
  1196. }
  1197. if ( output.find("CTEST_FULL_OUTPUT") != output.npos )
  1198. {
  1199. return true;
  1200. }
  1201. cmOStringStream ostr;
  1202. std::string::size_type cc;
  1203. std::string::size_type skipsize = 0;
  1204. int inTag = 0;
  1205. int skipped = 0;
  1206. for ( cc = 0; cc < output.size(); cc ++ )
  1207. {
  1208. int ch = output[cc];
  1209. if ( ch < 0 || ch > 255 )
  1210. {
  1211. break;
  1212. }
  1213. if ( ch == '<' )
  1214. {
  1215. inTag = 1;
  1216. }
  1217. if ( !inTag )
  1218. {
  1219. int notskip = 0;
  1220. // Skip
  1221. if ( skipsize < remove_threshold )
  1222. {
  1223. ostr << static_cast<char>(ch);
  1224. notskip = 1;
  1225. }
  1226. skipsize ++;
  1227. if ( notskip && skipsize >= remove_threshold )
  1228. {
  1229. skipped = 1;
  1230. }
  1231. }
  1232. else
  1233. {
  1234. ostr << static_cast<char>(ch);
  1235. }
  1236. if ( ch == '>' )
  1237. {
  1238. inTag = 0;
  1239. }
  1240. }
  1241. if ( skipped )
  1242. {
  1243. ostr << "..." << std::endl << "The rest of the test output was removed since it exceeds the threshold of "
  1244. << remove_threshold << " characters." << std::endl;
  1245. }
  1246. output = ostr.str();
  1247. return true;
  1248. }
  1249. //----------------------------------------------------------------------
  1250. bool cmCTestTestHandler::AddTest(const std::vector<std::string>& args)
  1251. {
  1252. const std::string& testname = args[0];
  1253. if (this->m_UseExcludeRegExp &&
  1254. this->m_UseExcludeRegExpFirst &&
  1255. m_ExcludeTestsRegularExpression.find(testname.c_str()))
  1256. {
  1257. return true;
  1258. }
  1259. if ( m_MemCheck )
  1260. {
  1261. std::vector<cmStdString>::iterator it;
  1262. bool found = false;
  1263. for ( it = m_CustomTestsIgnore.begin();
  1264. it != m_CustomTestsIgnore.end(); ++ it )
  1265. {
  1266. if ( *it == testname )
  1267. {
  1268. found = true;
  1269. break;
  1270. }
  1271. }
  1272. if ( found )
  1273. {
  1274. cmCTestLog(m_CTest, HANDLER_VERBOSE_OUTPUT, "Ignore memcheck: " << *it << std::endl);
  1275. return true;
  1276. }
  1277. }
  1278. else
  1279. {
  1280. std::vector<cmStdString>::iterator it;
  1281. bool found = false;
  1282. for ( it = m_CustomTestsIgnore.begin();
  1283. it != m_CustomTestsIgnore.end(); ++ it )
  1284. {
  1285. if ( *it == testname )
  1286. {
  1287. found = true;
  1288. break;
  1289. }
  1290. }
  1291. if ( found )
  1292. {
  1293. cmCTestLog(m_CTest, HANDLER_VERBOSE_OUTPUT, "Ignore test: " << *it << std::endl);
  1294. return true;
  1295. }
  1296. }
  1297. cmCTestTestProperties test;
  1298. test.m_Name = testname;
  1299. test.m_Args = args;
  1300. test.m_Directory = cmSystemTools::GetCurrentWorkingDirectory();
  1301. test.m_IsInBasedOnREOptions = true;
  1302. if (this->m_UseIncludeRegExp && !m_IncludeTestsRegularExpression.find(testname.c_str()))
  1303. {
  1304. test.m_IsInBasedOnREOptions = false;
  1305. }
  1306. else if (this->m_UseExcludeRegExp &&
  1307. !this->m_UseExcludeRegExpFirst &&
  1308. m_ExcludeTestsRegularExpression.find(testname.c_str()))
  1309. {
  1310. test.m_IsInBasedOnREOptions = false;
  1311. }
  1312. m_TestList.push_back(test);
  1313. return true;
  1314. }