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
This commit is contained in:
		
				
					committed by
					
						
						Copybara-Service
					
				
			
			
				
	
			
			
			
						parent
						
							44c03643cf
						
					
				
				
					commit
					912db74253
				
			@@ -2201,10 +2201,17 @@ constexpr bool StaticAssertTypeEq() noexcept {
 | 
				
			|||||||
#define TEST_F(test_fixture, test_name) GTEST_TEST_F(test_fixture, test_name)
 | 
					#define TEST_F(test_fixture, test_name) GTEST_TEST_F(test_fixture, test_name)
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Returns a path to temporary directory.
 | 
					// Returns a path to a temporary directory, which should be writable. It is
 | 
				
			||||||
// Tries to determine an appropriate directory for the platform.
 | 
					// implementation-dependent whether or not the path is terminated by the
 | 
				
			||||||
 | 
					// directory-separator character.
 | 
				
			||||||
GTEST_API_ std::string TempDir();
 | 
					GTEST_API_ std::string TempDir();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Returns a path to a directory that contains ancillary data files that might
 | 
				
			||||||
 | 
					// be used by tests. It is implementation dependent whether or not the path is
 | 
				
			||||||
 | 
					// terminated by the directory-separator character. The directory and the files
 | 
				
			||||||
 | 
					// in it should be considered read-only.
 | 
				
			||||||
 | 
					GTEST_API_ std::string SrcDir();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef _MSC_VER
 | 
					#ifdef _MSC_VER
 | 
				
			||||||
#pragma warning(pop)
 | 
					#pragma warning(pop)
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6754,12 +6754,13 @@ void InitGoogleTest() {
 | 
				
			|||||||
#endif  // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
 | 
					#endif  // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if !defined(GTEST_CUSTOM_TEMPDIR_FUNCTION_)
 | 
					#if !defined(GTEST_CUSTOM_TEMPDIR_FUNCTION_) || \
 | 
				
			||||||
// Return value of first environment variable that is set and contains
 | 
					    !defined(GTEST_CUSTOM_SRCDIR_FUNCTION_)
 | 
				
			||||||
// a non-empty string. If there are none, return the "fallback" string.
 | 
					// Returns the value of the first environment variable that is set and contains
 | 
				
			||||||
// Since we like the temporary directory to have a directory separator suffix,
 | 
					// a non-empty string. If there are none, returns the "fallback" string. Adds
 | 
				
			||||||
// add it if not provided in the environment variable value.
 | 
					// the director-separator character as a suffix if not provided in the
 | 
				
			||||||
static std::string GetTempDirFromEnv(
 | 
					// environment variable value.
 | 
				
			||||||
 | 
					static std::string GetDirFromEnv(
 | 
				
			||||||
    std::initializer_list<const char*> environment_variables,
 | 
					    std::initializer_list<const char*> environment_variables,
 | 
				
			||||||
    const char* fallback, char separator) {
 | 
					    const char* fallback, char separator) {
 | 
				
			||||||
  for (const char* variable_name : environment_variables) {
 | 
					  for (const char* variable_name : environment_variables) {
 | 
				
			||||||
@@ -6779,11 +6780,35 @@ std::string TempDir() {
 | 
				
			|||||||
#if defined(GTEST_CUSTOM_TEMPDIR_FUNCTION_)
 | 
					#if defined(GTEST_CUSTOM_TEMPDIR_FUNCTION_)
 | 
				
			||||||
  return GTEST_CUSTOM_TEMPDIR_FUNCTION_();
 | 
					  return GTEST_CUSTOM_TEMPDIR_FUNCTION_();
 | 
				
			||||||
#elif GTEST_OS_WINDOWS || GTEST_OS_WINDOWS_MOBILE
 | 
					#elif GTEST_OS_WINDOWS || GTEST_OS_WINDOWS_MOBILE
 | 
				
			||||||
  return GetTempDirFromEnv({"TEST_TMPDIR", "TEMP"}, "\\temp\\", '\\');
 | 
					  return GetDirFromEnv({"TEST_TMPDIR", "TEMP"}, "\\temp\\", '\\');
 | 
				
			||||||
#elif GTEST_OS_LINUX_ANDROID
 | 
					#elif GTEST_OS_LINUX_ANDROID
 | 
				
			||||||
  return GetTempDirFromEnv({"TEST_TMPDIR", "TMPDIR"}, "/data/local/tmp/", '/');
 | 
					  return GetDirFromEnv({"TEST_TMPDIR", "TMPDIR"}, "/data/local/tmp/", '/');
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
  return GetTempDirFromEnv({"TEST_TMPDIR", "TMPDIR"}, "/tmp/", '/');
 | 
					  return GetDirFromEnv({"TEST_TMPDIR", "TMPDIR"}, "/tmp/", '/');
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if !defined(GTEST_CUSTOM_SRCDIR_FUNCTION_)
 | 
				
			||||||
 | 
					// Returns the directory path (including terminating separator) of the current
 | 
				
			||||||
 | 
					// executable as derived from argv[0].
 | 
				
			||||||
 | 
					static std::string GetCurrentExecutableDirectory() {
 | 
				
			||||||
 | 
					  internal::FilePath argv_0(internal::GetArgvs()[0]);
 | 
				
			||||||
 | 
					  return argv_0.RemoveFileName().string();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					std::string SrcDir() {
 | 
				
			||||||
 | 
					#if defined(GTEST_CUSTOM_SRCDIR_FUNCTION_)
 | 
				
			||||||
 | 
					  return GTEST_CUSTOM_SRCDIR_FUNCTION_();
 | 
				
			||||||
 | 
					#elif GTEST_OS_WINDOWS || GTEST_OS_WINDOWS_MOBILE
 | 
				
			||||||
 | 
					  return GetDirFromEnv({"TEST_SRCDIR"}, GetCurrentExecutableDirectory().c_str(),
 | 
				
			||||||
 | 
					                       '\\');
 | 
				
			||||||
 | 
					#elif GTEST_OS_LINUX_ANDROID
 | 
				
			||||||
 | 
					  return GetDirFromEnv({"TEST_SRCDIR"}, GetCurrentExecutableDirectory().c_str(),
 | 
				
			||||||
 | 
					                       '/');
 | 
				
			||||||
 | 
					#else
 | 
				
			||||||
 | 
					  return GetDirFromEnv({"TEST_SRCDIR"}, GetCurrentExecutableDirectory().c_str(),
 | 
				
			||||||
 | 
					                       '/');
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										97
									
								
								googletest/test/gtest_dirs_test.cc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										97
									
								
								googletest/test/gtest_dirs_test.cc
									
									
									
									
									
										Normal file
									
								
							@@ -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
 | 
				
			||||||
		Reference in New Issue
	
	Block a user