cmCTestTestHandler.cxx 37 KB

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