gtest_dirs_test.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include <sys/stat.h>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <string>
  5. #include "gtest/gtest.h"
  6. #include "gtest/internal/gtest-port.h"
  7. #if GTEST_HAS_FILE_SYSTEM
  8. namespace {
  9. class SetEnv {
  10. public:
  11. // Sets the environment value with name `name` to `value`, unless `value` is
  12. // nullptr, in which case it unsets it. Restores the original value on
  13. // destruction.
  14. SetEnv(const char* name, const char* value) : name_(name) {
  15. const char* old_value = getenv(name);
  16. if (old_value != nullptr) {
  17. saved_value_ = old_value;
  18. have_saved_value_ = true;
  19. }
  20. if (value == nullptr) {
  21. GTEST_CHECK_POSIX_SUCCESS_(unsetenv(name));
  22. } else {
  23. GTEST_CHECK_POSIX_SUCCESS_(setenv(name, value, 1 /*overwrite*/));
  24. }
  25. }
  26. ~SetEnv() {
  27. if (have_saved_value_) {
  28. GTEST_CHECK_POSIX_SUCCESS_(
  29. setenv(name_.c_str(), saved_value_.c_str(), 1 /*overwrite*/));
  30. } else {
  31. GTEST_CHECK_POSIX_SUCCESS_(unsetenv(name_.c_str()));
  32. }
  33. }
  34. private:
  35. std::string name_;
  36. bool have_saved_value_ = false;
  37. std::string saved_value_;
  38. };
  39. class MakeTempDir {
  40. public:
  41. // Creates a directory with a unique name including `testname`.
  42. // The destructor removes it.
  43. explicit MakeTempDir(const std::string& testname) {
  44. // mkdtemp requires that the last 6 characters of the input pattern
  45. // are Xs, and the string is modified by replacing those characters.
  46. std::string pattern = "/tmp/" + testname + "_XXXXXX";
  47. GTEST_CHECK_(mkdtemp(pattern.data()) != nullptr);
  48. dirname_ = pattern;
  49. }
  50. ~MakeTempDir() { GTEST_CHECK_POSIX_SUCCESS_(rmdir(dirname_.c_str())); }
  51. const char* DirName() const { return dirname_.c_str(); }
  52. private:
  53. std::string dirname_;
  54. };
  55. bool StartsWith(const std::string& str, const std::string& prefix) {
  56. return str.substr(0, prefix.size()) == prefix;
  57. }
  58. TEST(TempDirTest, InEnvironment) {
  59. // Since the test infrastructure might be verifying directory existence or
  60. // even creating subdirectories, we need to be careful that the directories we
  61. // specify are actually valid.
  62. MakeTempDir temp_dir("TempDirTest_InEnvironment");
  63. SetEnv set_env("TEST_TMPDIR", temp_dir.DirName());
  64. EXPECT_TRUE(StartsWith(testing::TempDir(), temp_dir.DirName()));
  65. }
  66. TEST(TempDirTest, NotInEnvironment) {
  67. SetEnv set_env("TEST_TMPDIR", nullptr);
  68. EXPECT_NE(testing::TempDir(), "");
  69. }
  70. TEST(SrcDirTest, InEnvironment) {
  71. // Since the test infrastructure might be verifying directory existence or
  72. // even creating subdirectories, we need to be careful that the directories we
  73. // specify are actually valid.
  74. MakeTempDir temp_dir("SrcDirTest_InEnvironment");
  75. SetEnv set_env("TEST_SRCDIR", temp_dir.DirName());
  76. EXPECT_TRUE(StartsWith(testing::SrcDir(), temp_dir.DirName()));
  77. }
  78. TEST(SrcDirTest, NotInEnvironment) {
  79. SetEnv set_env("TEST_SRCDIR", nullptr);
  80. EXPECT_NE(testing::SrcDir(), "");
  81. }
  82. #endif // GTEST_HAS_FILE_SYSTEM
  83. } // namespace