gtest_dirs_test.cc 2.8 KB

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