cmCTestTestHandler.cxx 41 KB

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