cmAddTestCommand.cxx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 "cmAddTestCommand.h"
  14. // cmExecutableCommand
  15. bool cmAddTestCommand::InitialPass(std::vector<std::string> const& args)
  16. {
  17. // First argument is the name of the test
  18. // Second argument is the name of the executable to run (a target or external
  19. // program)
  20. // Remaining arguments are the arguments to pass to the executable
  21. if(args.size() < 2 )
  22. {
  23. this->SetError("called with incorrect number of arguments");
  24. return false;
  25. }
  26. // store the arguments for the final pass
  27. // also expand any CMake variables
  28. m_Args = args;
  29. return true;
  30. }
  31. // we append to the file in the final pass because Enable Testing command
  32. // creates the file in the final pass.
  33. void cmAddTestCommand::FinalPass()
  34. {
  35. // Create a full path filename for output Testfile
  36. std::string fname;
  37. fname = m_Makefile->GetStartOutputDirectory();
  38. fname += "/";
  39. fname += "DartTestfile.txt";
  40. // If the file doesn't exist, then ENABLE_TESTING hasn't been run
  41. if (cmSystemTools::FileExists(fname.c_str()))
  42. {
  43. // Open the output Testfile
  44. std::ofstream fout(fname.c_str(), std::ios::app);
  45. if (!fout)
  46. {
  47. cmSystemTools::Error("Error Writing ", fname.c_str());
  48. return;
  49. }
  50. std::vector<std::string>::iterator it;
  51. // for each arg in the test
  52. fout << "ADD_TEST(";
  53. it = m_Args.begin();
  54. fout << (*it).c_str();
  55. ++it;
  56. for (; it != m_Args.end(); ++it)
  57. {
  58. // Just double-quote all arguments so they are re-parsed
  59. // correctly by the test system.
  60. fout << " \"";
  61. for(std::string::iterator c = it->begin(); c != it->end(); ++c)
  62. {
  63. // Escape quotes and backslashes within arguments.
  64. if((*c == '"') || (*c == '\\'))
  65. {
  66. fout << '\\';
  67. }
  68. fout << *c;
  69. }
  70. fout << "\"";
  71. }
  72. fout << ")" << std::endl;
  73. fout.close();
  74. }
  75. return;
  76. }