cmCTestTestHandler.cxx 42 KB

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