diff options
author | Abseil Team <absl-team@google.com> | 2022-11-11 20:58:35 (GMT) |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2022-11-11 20:59:10 (GMT) |
commit | 912db742531bf82efb01194bc08140416e3b3467 (patch) | |
tree | 0438b1eebf894f688f3d577f4a6169382a4b6548 /googletest/test | |
parent | 44c03643cfbc649488a0f437cd18e05f11960d19 (diff) | |
download | googletest-912db742531bf82efb01194bc08140416e3b3467.zip googletest-912db742531bf82efb01194bc08140416e3b3467.tar.gz googletest-912db742531bf82efb01194bc08140416e3b3467.tar.bz2 |
Defined a testing::SrcDir() function that returns the name of a directory
where ancillary data files can be found.
PiperOrigin-RevId: 487896836
Change-Id: Ie6b1ba734e900fa33872b63090879ee6efe33411
Diffstat (limited to 'googletest/test')
-rw-r--r-- | googletest/test/gtest_dirs_test.cc | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/googletest/test/gtest_dirs_test.cc b/googletest/test/gtest_dirs_test.cc new file mode 100644 index 0000000..c0da9ac --- /dev/null +++ b/googletest/test/gtest_dirs_test.cc @@ -0,0 +1,97 @@ +#include <sys/stat.h> + +#include <cstdlib> +#include <cstring> +#include <string> + +#include "gtest/gtest.h" +#include "gtest/internal/gtest-port.h" + +namespace { + +class SetEnv { + public: + // Sets the environment value with name `name` to `value`, unless `value` is + // nullptr, in which case it unsets it. Restores the original value on + // destruction. + SetEnv(const char* name, const char* value) : name_(name) { + const char* old_value = getenv(name); + if (old_value != nullptr) { + saved_value_ = old_value; + have_saved_value_ = true; + } + if (value == nullptr) { + GTEST_CHECK_POSIX_SUCCESS_(unsetenv(name)); + } else { + GTEST_CHECK_POSIX_SUCCESS_(setenv(name, value, 1 /*overwrite*/)); + } + } + + ~SetEnv() { + if (have_saved_value_) { + GTEST_CHECK_POSIX_SUCCESS_( + setenv(name_.c_str(), saved_value_.c_str(), 1 /*overwrite*/)); + } else { + GTEST_CHECK_POSIX_SUCCESS_(unsetenv(name_.c_str())); + } + } + + private: + std::string name_; + bool have_saved_value_ = false; + std::string saved_value_; +}; + +class MakeTempDir { + public: + // Creates a directory with a unique name including `testname`. + // The destructor removes it. + explicit MakeTempDir(const std::string& testname) { + // mkdtemp requires that the last 6 characters of the input pattern + // are Xs, and the string is modified by replacing those characters. + std::string pattern = "/tmp/" + testname + "_XXXXXX"; + GTEST_CHECK_(mkdtemp(pattern.data()) != nullptr); + dirname_ = pattern; + } + + ~MakeTempDir() { GTEST_CHECK_POSIX_SUCCESS_(rmdir(dirname_.c_str())); } + + const char* DirName() const { return dirname_.c_str(); } + + private: + std::string dirname_; +}; + +bool StartsWith(const std::string& str, const std::string& prefix) { + return str.substr(0, prefix.size()) == prefix; +} + +TEST(TempDirTest, InEnvironment) { + // Since the test infrastructure might be verifying directory existence or + // even creating subdirectories, we need to be careful that the directories we + // specify are actually valid. + MakeTempDir temp_dir("TempDirTest_InEnvironment"); + SetEnv set_env("TEST_TMPDIR", temp_dir.DirName()); + EXPECT_TRUE(StartsWith(testing::TempDir(), temp_dir.DirName())); +} + +TEST(TempDirTest, NotInEnvironment) { + SetEnv set_env("TEST_TMPDIR", nullptr); + EXPECT_NE(testing::TempDir(), ""); +} + +TEST(SrcDirTest, InEnvironment) { + // Since the test infrastructure might be verifying directory existence or + // even creating subdirectories, we need to be careful that the directories we + // specify are actually valid. + MakeTempDir temp_dir("SrcDirTest_InEnvironment"); + SetEnv set_env("TEST_SRCDIR", temp_dir.DirName()); + EXPECT_TRUE(StartsWith(testing::SrcDir(), temp_dir.DirName())); +} + +TEST(SrcDirTest, NotInEnvironment) { + SetEnv set_env("TEST_SRCDIR", nullptr); + EXPECT_NE(testing::SrcDir(), ""); +} + +} // namespace |