test_workdir.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. /* Having this as a comment lets gtest_add_tests() recognize the tests we fake
  5. here without requiring googletest
  6. TEST_F( WorkDirWithSpaces, test1 )
  7. {
  8. }
  9. TEST_F( WorkDirWithSpaces, test2 )
  10. {
  11. }
  12. */
  13. int main(int argc, char** argv)
  14. {
  15. // Fake gtest behavior so we don't actually require gtest.
  16. // Both listing tests and test execution try to read a file from
  17. // the current directory. If we are not handling spaces in the
  18. // working directory correctly, the files we expect won't exist.
  19. if (argc > 2 && std::string(argv[1]) == "--gtest_list_tests" &&
  20. std::string(argv[2]).find("--gtest_output=json:") != std::string::npos) {
  21. std::ifstream inTestListFile("test_list_output.txt");
  22. if (!inTestListFile) {
  23. std::cout << "ERROR: Failed to open test_list_output.txt" << std::endl;
  24. return 1;
  25. }
  26. std::cout << inTestListFile.rdbuf();
  27. std::ifstream inJsonFile("test_list_output.json");
  28. if (!inJsonFile) {
  29. std::cout << "ERROR: Failed to open test_list_output.json" << std::endl;
  30. return 1;
  31. }
  32. std::string output_param(argv[2]);
  33. std::string::size_type split = output_param.find(":");
  34. std::string filepath = output_param.substr(split + 1);
  35. // The full file path is passed
  36. std::ofstream ostrm(filepath.c_str(), std::ios::binary);
  37. if (!ostrm) {
  38. std::cerr << "Failed to create file: " << filepath.c_str() << std::endl;
  39. return 1;
  40. }
  41. ostrm << inJsonFile.rdbuf();
  42. return 0;
  43. }
  44. std::ifstream inFile("test_output.txt");
  45. if (!inFile) {
  46. std::cout << "ERROR: Failed to open test_output.txt" << std::endl;
  47. return 1;
  48. }
  49. std::cout << "Test output read from file to follow:" << std::endl;
  50. std::cout << inFile.rdbuf();
  51. return 0;
  52. }