cmCTestTestHandler.cxx 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  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;
  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 << static_cast<int>(percent + .5) << "% 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. mf->AddDefinition("CTEST_CONFIGURATION_TYPE", m_CTest->GetConfigType().c_str());
  876. // Add handler for ADD_TEST
  877. cmCTestAddTestCommand* newCom1 = new cmCTestAddTestCommand;
  878. newCom1->m_TestHandler = this;
  879. cm.AddCommand(newCom1);
  880. // Add handler for SUBDIR
  881. cmCTestSubdirCommand* newCom2 = new cmCTestSubdirCommand;
  882. newCom2->m_TestHandler = this;
  883. cm.AddCommand(newCom2);
  884. const char* testFilename;
  885. if( cmSystemTools::FileExists("CTestTestfile.cmake") )
  886. {
  887. // does the CTestTestfile.cmake exist ?
  888. testFilename = "CTestTestfile.cmake";
  889. }
  890. else if( cmSystemTools::FileExists("DartTestfile.txt") )
  891. {
  892. // does the DartTestfile.txt exist ?
  893. testFilename = "DartTestfile.txt";
  894. }
  895. else
  896. {
  897. return;
  898. }
  899. if ( !mf->ReadListFile(0, testFilename) )
  900. {
  901. return;
  902. }
  903. if ( cmSystemTools::GetErrorOccuredFlag() )
  904. {
  905. return;
  906. }
  907. cmCTestLog(m_CTest, HANDLER_VERBOSE_OUTPUT, "Done constructing a list of tests" << std::endl);
  908. }
  909. //----------------------------------------------------------------------
  910. void cmCTestTestHandler::UseIncludeRegExp()
  911. {
  912. this->m_UseIncludeRegExp = true;
  913. }
  914. //----------------------------------------------------------------------
  915. void cmCTestTestHandler::UseExcludeRegExp()
  916. {
  917. this->m_UseExcludeRegExp = true;
  918. this->m_UseExcludeRegExpFirst = this->m_UseIncludeRegExp ? false : true;
  919. }
  920. //----------------------------------------------------------------------
  921. const char* cmCTestTestHandler::GetTestStatus(int status)
  922. {
  923. static const char statuses[][100] = {
  924. "Not Run",
  925. "Timeout",
  926. "SEGFAULT",
  927. "ILLEGAL",
  928. "INTERRUPT",
  929. "NUMERICAL",
  930. "OTHER_FAULT",
  931. "Failed",
  932. "BAD_COMMAND",
  933. "Completed"
  934. };
  935. if ( status < cmCTestTestHandler::NOT_RUN ||
  936. status > cmCTestTestHandler::COMPLETED )
  937. {
  938. return "No Status";
  939. }
  940. return statuses[status];
  941. }
  942. //----------------------------------------------------------------------
  943. void cmCTestTestHandler::ExpandTestsToRunInformation(int numTests)
  944. {
  945. if (this->TestsToRunString.empty())
  946. {
  947. return;
  948. }
  949. int start;
  950. int end = -1;
  951. double stride = -1;
  952. std::string::size_type pos = 0;
  953. std::string::size_type pos2;
  954. // read start
  955. if(GetNextNumber(this->TestsToRunString, start, pos, pos2))
  956. {
  957. // read end
  958. if(GetNextNumber(this->TestsToRunString, end, pos, pos2))
  959. {
  960. // read stride
  961. if(GetNextRealNumber(this->TestsToRunString, stride, pos, pos2))
  962. {
  963. int val =0;
  964. // now read specific numbers
  965. while(GetNextNumber(this->TestsToRunString, val, pos, pos2))
  966. {
  967. m_TestsToRun.push_back(val);
  968. }
  969. m_TestsToRun.push_back(val);
  970. }
  971. }
  972. }
  973. // if start is not specified then we assume we start at 1
  974. if(start == -1)
  975. {
  976. start = 1;
  977. }
  978. // if end isnot specified then we assume we end with the last test
  979. if(end == -1)
  980. {
  981. end = numTests;
  982. }
  983. // if the stride wasn't specified then it defaults to 1
  984. if(stride == -1)
  985. {
  986. stride = 1;
  987. }
  988. // if we have a range then add it
  989. if(end != -1 && start != -1 && stride > 0)
  990. {
  991. int i = 0;
  992. while (i*stride + start <= end)
  993. {
  994. m_TestsToRun.push_back(static_cast<int>(i*stride+start));
  995. ++i;
  996. }
  997. }
  998. // sort the array
  999. std::sort(m_TestsToRun.begin(), m_TestsToRun.end(), std::less<int>());
  1000. // remove duplicates
  1001. std::vector<int>::iterator new_end =
  1002. std::unique(m_TestsToRun.begin(), m_TestsToRun.end());
  1003. m_TestsToRun.erase(new_end, m_TestsToRun.end());
  1004. }
  1005. //----------------------------------------------------------------------
  1006. // Just for convenience
  1007. #define SPACE_REGEX "[ \t\r\n]"
  1008. //----------------------------------------------------------------------
  1009. std::string cmCTestTestHandler::GenerateRegressionImages(
  1010. const std::string& xml)
  1011. {
  1012. cmsys::RegularExpression twoattributes(
  1013. "<DartMeasurement"
  1014. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1015. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1016. SPACE_REGEX "*>([^<]*)</DartMeasurement>");
  1017. cmsys::RegularExpression threeattributes(
  1018. "<DartMeasurement"
  1019. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1020. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1021. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1022. SPACE_REGEX "*>([^<]*)</DartMeasurement>");
  1023. cmsys::RegularExpression fourattributes(
  1024. "<DartMeasurement"
  1025. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1026. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1027. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1028. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1029. SPACE_REGEX "*>([^<]*)</DartMeasurement>");
  1030. cmsys::RegularExpression measurementfile(
  1031. "<DartMeasurementFile"
  1032. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1033. SPACE_REGEX "*(name|type|encoding|compression)=\"([^\"]*)\""
  1034. SPACE_REGEX "*>([^<]*)</DartMeasurementFile>");
  1035. cmOStringStream ostr;
  1036. bool done = false;
  1037. std::string cxml = xml;
  1038. while ( ! done )
  1039. {
  1040. if ( twoattributes.find(cxml) )
  1041. {
  1042. ostr
  1043. << "\t\t\t<NamedMeasurement"
  1044. << " " << twoattributes.match(1) << "=\"" << twoattributes.match(2) << "\""
  1045. << " " << twoattributes.match(3) << "=\"" << twoattributes.match(4) << "\""
  1046. << "><Value>" << twoattributes.match(5)
  1047. << "</Value></NamedMeasurement>"
  1048. << std::endl;
  1049. cxml.erase(twoattributes.start(), twoattributes.end() - twoattributes.start());
  1050. }
  1051. else if ( threeattributes.find(cxml) )
  1052. {
  1053. ostr
  1054. << "\t\t\t<NamedMeasurement"
  1055. << " " << threeattributes.match(1) << "=\"" << threeattributes.match(2) << "\""
  1056. << " " << threeattributes.match(3) << "=\"" << threeattributes.match(4) << "\""
  1057. << " " << threeattributes.match(5) << "=\"" << threeattributes.match(6) << "\""
  1058. << "><Value>" << threeattributes.match(7)
  1059. << "</Value></NamedMeasurement>"
  1060. << std::endl;
  1061. cxml.erase(threeattributes.start(), threeattributes.end() - threeattributes.start());
  1062. }
  1063. else if ( fourattributes.find(cxml) )
  1064. {
  1065. ostr
  1066. << "\t\t\t<NamedMeasurement"
  1067. << " " << fourattributes.match(1) << "=\"" << fourattributes.match(2) << "\""
  1068. << " " << fourattributes.match(3) << "=\"" << fourattributes.match(4) << "\""
  1069. << " " << fourattributes.match(5) << "=\"" << fourattributes.match(6) << "\""
  1070. << " " << fourattributes.match(7) << "=\"" << fourattributes.match(8) << "\""
  1071. << "><Value>" << fourattributes.match(9)
  1072. << "</Value></NamedMeasurement>"
  1073. << std::endl;
  1074. cxml.erase(fourattributes.start(), fourattributes.end() - fourattributes.start());
  1075. }
  1076. else if ( measurementfile.find(cxml) )
  1077. {
  1078. const std::string& filename =
  1079. cmCTest::CleanString(measurementfile.match(5));
  1080. if ( cmSystemTools::FileExists(filename.c_str()) )
  1081. {
  1082. long len = cmSystemTools::FileLength(filename.c_str());
  1083. if ( len == 0 )
  1084. {
  1085. std::string k1 = measurementfile.match(1);
  1086. std::string v1 = measurementfile.match(2);
  1087. std::string k2 = measurementfile.match(3);
  1088. std::string v2 = measurementfile.match(4);
  1089. if ( cmSystemTools::LowerCase(k1) == "type" )
  1090. {
  1091. v1 = "text/string";
  1092. }
  1093. if ( cmSystemTools::LowerCase(k2) == "type" )
  1094. {
  1095. v2 = "text/string";
  1096. }
  1097. ostr
  1098. << "\t\t\t<NamedMeasurement"
  1099. << " " << k1 << "=\"" << v1 << "\""
  1100. << " " << k2 << "=\"" << v2 << "\""
  1101. << " encoding=\"none\""
  1102. << "><Value>Image " << filename.c_str()
  1103. << " is empty</Value></NamedMeasurement>";
  1104. }
  1105. else
  1106. {
  1107. std::ifstream ifs(filename.c_str(), std::ios::in
  1108. #ifdef _WIN32
  1109. | std::ios::binary
  1110. #endif
  1111. );
  1112. unsigned char *file_buffer = new unsigned char [ len + 1 ];
  1113. ifs.read(reinterpret_cast<char*>(file_buffer), len);
  1114. unsigned char *encoded_buffer = new unsigned char [ static_cast<int>(len * 1.5 + 5) ];
  1115. unsigned long rlen = cmsysBase64_Encode(file_buffer, len, encoded_buffer, 1);
  1116. unsigned long cc;
  1117. ostr
  1118. << "\t\t\t<NamedMeasurement"
  1119. << " " << measurementfile.match(1) << "=\"" << measurementfile.match(2) << "\""
  1120. << " " << measurementfile.match(3) << "=\"" << measurementfile.match(4) << "\""
  1121. << " encoding=\"base64\""
  1122. << ">" << std::endl << "\t\t\t\t<Value>";
  1123. for ( cc = 0; cc < rlen; cc ++ )
  1124. {
  1125. ostr << encoded_buffer[cc];
  1126. if ( cc % 60 == 0 && cc )
  1127. {
  1128. ostr << std::endl;
  1129. }
  1130. }
  1131. ostr
  1132. << "</Value>" << std::endl << "\t\t\t</NamedMeasurement>"
  1133. << std::endl;
  1134. delete [] file_buffer;
  1135. delete [] encoded_buffer;
  1136. }
  1137. }
  1138. else
  1139. {
  1140. int idx = 4;
  1141. if ( measurementfile.match(1) == "name" )
  1142. {
  1143. idx = 2;
  1144. }
  1145. ostr
  1146. << "\t\t\t<NamedMeasurement"
  1147. << " name=\"" << measurementfile.match(idx) << "\""
  1148. << " text=\"text/string\""
  1149. << "><Value>File " << filename.c_str() << " not found</Value></NamedMeasurement>"
  1150. << std::endl;
  1151. cmCTestLog(m_CTest, HANDLER_OUTPUT, "File \"" << filename.c_str() << "\" not found." << std::endl);
  1152. }
  1153. cxml.erase(measurementfile.start(), measurementfile.end() - measurementfile.start());
  1154. }
  1155. else
  1156. {
  1157. done = true;
  1158. }
  1159. }
  1160. return ostr.str();
  1161. }
  1162. //----------------------------------------------------------------------
  1163. void cmCTestTestHandler::SetIncludeRegExp(const char *arg)
  1164. {
  1165. m_IncludeRegExp = arg;
  1166. }
  1167. //----------------------------------------------------------------------
  1168. void cmCTestTestHandler::SetExcludeRegExp(const char *arg)
  1169. {
  1170. m_ExcludeRegExp = arg;
  1171. }
  1172. //----------------------------------------------------------------------
  1173. void cmCTestTestHandler::SetTestsToRunInformation(const char* in)
  1174. {
  1175. if ( !in )
  1176. {
  1177. return;
  1178. }
  1179. this->TestsToRunString = in;
  1180. // if the argument is a file, then read it and use the contents as the string
  1181. if(cmSystemTools::FileExists(in))
  1182. {
  1183. std::ifstream fin(in);
  1184. unsigned long filelen = cmSystemTools::FileLength(in);
  1185. char* buff = new char[filelen+1];
  1186. fin.getline(buff, filelen);
  1187. buff[fin.gcount()] = 0;
  1188. this->TestsToRunString = buff;
  1189. }
  1190. }
  1191. //----------------------------------------------------------------------
  1192. bool cmCTestTestHandler::CleanTestOutput(std::string& output, size_t remove_threshold)
  1193. {
  1194. if ( remove_threshold == 0 )
  1195. {
  1196. return true;
  1197. }
  1198. if ( output.find("CTEST_FULL_OUTPUT") != output.npos )
  1199. {
  1200. return true;
  1201. }
  1202. cmOStringStream ostr;
  1203. std::string::size_type cc;
  1204. std::string::size_type skipsize = 0;
  1205. int inTag = 0;
  1206. int skipped = 0;
  1207. for ( cc = 0; cc < output.size(); cc ++ )
  1208. {
  1209. int ch = output[cc];
  1210. if ( ch < 0 || ch > 255 )
  1211. {
  1212. break;
  1213. }
  1214. if ( ch == '<' )
  1215. {
  1216. inTag = 1;
  1217. }
  1218. if ( !inTag )
  1219. {
  1220. int notskip = 0;
  1221. // Skip
  1222. if ( skipsize < remove_threshold )
  1223. {
  1224. ostr << static_cast<char>(ch);
  1225. notskip = 1;
  1226. }
  1227. skipsize ++;
  1228. if ( notskip && skipsize >= remove_threshold )
  1229. {
  1230. skipped = 1;
  1231. }
  1232. }
  1233. else
  1234. {
  1235. ostr << static_cast<char>(ch);
  1236. }
  1237. if ( ch == '>' )
  1238. {
  1239. inTag = 0;
  1240. }
  1241. }
  1242. if ( skipped )
  1243. {
  1244. ostr << "..." << std::endl << "The rest of the test output was removed since it exceeds the threshold of "
  1245. << remove_threshold << " characters." << std::endl;
  1246. }
  1247. output = ostr.str();
  1248. return true;
  1249. }
  1250. //----------------------------------------------------------------------
  1251. bool cmCTestTestHandler::AddTest(const std::vector<std::string>& args)
  1252. {
  1253. const std::string& testname = args[0];
  1254. if (this->m_UseExcludeRegExp &&
  1255. this->m_UseExcludeRegExpFirst &&
  1256. m_ExcludeTestsRegularExpression.find(testname.c_str()))
  1257. {
  1258. return true;
  1259. }
  1260. if ( m_MemCheck )
  1261. {
  1262. std::vector<cmStdString>::iterator it;
  1263. bool found = false;
  1264. for ( it = m_CustomTestsIgnore.begin();
  1265. it != m_CustomTestsIgnore.end(); ++ it )
  1266. {
  1267. if ( *it == testname )
  1268. {
  1269. found = true;
  1270. break;
  1271. }
  1272. }
  1273. if ( found )
  1274. {
  1275. cmCTestLog(m_CTest, HANDLER_VERBOSE_OUTPUT, "Ignore memcheck: " << *it << std::endl);
  1276. return true;
  1277. }
  1278. }
  1279. else
  1280. {
  1281. std::vector<cmStdString>::iterator it;
  1282. bool found = false;
  1283. for ( it = m_CustomTestsIgnore.begin();
  1284. it != m_CustomTestsIgnore.end(); ++ it )
  1285. {
  1286. if ( *it == testname )
  1287. {
  1288. found = true;
  1289. break;
  1290. }
  1291. }
  1292. if ( found )
  1293. {
  1294. cmCTestLog(m_CTest, HANDLER_VERBOSE_OUTPUT, "Ignore test: " << *it << std::endl);
  1295. return true;
  1296. }
  1297. }
  1298. cmCTestTestProperties test;
  1299. test.m_Name = testname;
  1300. test.m_Args = args;
  1301. test.m_Directory = cmSystemTools::GetCurrentWorkingDirectory();
  1302. test.m_IsInBasedOnREOptions = true;
  1303. if (this->m_UseIncludeRegExp && !m_IncludeTestsRegularExpression.find(testname.c_str()))
  1304. {
  1305. test.m_IsInBasedOnREOptions = false;
  1306. }
  1307. else if (this->m_UseExcludeRegExp &&
  1308. !this->m_UseExcludeRegExpFirst &&
  1309. m_ExcludeTestsRegularExpression.find(testname.c_str()))
  1310. {
  1311. test.m_IsInBasedOnREOptions = false;
  1312. }
  1313. m_TestList.push_back(test);
  1314. return true;
  1315. }