diff options
author | Abseil Team <absl-team@google.com> | 2022-05-11 05:42:35 (GMT) |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2022-05-11 05:43:09 (GMT) |
commit | 5e6a533680fc8292c31f31664d80c48440d4a526 (patch) | |
tree | 396a2549e90f96913525a01ecaa1fa495c7bb89c | |
parent | 4224c770a3274e46344c596d59ce9f4e0c62801a (diff) | |
download | googletest-5e6a533680fc8292c31f31664d80c48440d4a526.zip googletest-5e6a533680fc8292c31f31664d80c48440d4a526.tar.gz googletest-5e6a533680fc8292c31f31664d80c48440d4a526.tar.bz2 |
Do a consistent way of searching for a temporary directory.
The way temporary directories were discovered from
environment variables in different operating systems
was inconsistent.
On Windows, only the system-specific environment variable
TEMP was considered, but not the bazel-common TEST_TMPDIR.
On Unix, only TEST_TMPDIR was considered, but not
the otherwise typical system specific TMPDIR.
Now, always consider TEST_TMPDIR first, followed by the
system-typical environment variable (TEMP on Windows, TMPDIR on Unix)
before falling back to a default on that particular system.
Also: the value for the temporary directory coming from the
environment variable was only checked for a trailing directory
separator on Windows (and appended if needed), not on the other
platforms.
Make this also consistent accross the systems: Now always apply
the same logic to all of these (to accomodate
tests that just concatenate TempDir() without directory separator).
PiperOrigin-RevId: 447909830
Change-Id: I9ea17acdf5944eb2a965615fd9cf142878c33a58
-rw-r--r-- | googletest/src/gtest.cc | 51 |
1 files changed, 26 insertions, 25 deletions
diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index 73312d4..1acad59 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -44,6 +44,7 @@ #include <chrono> // NOLINT #include <cmath> #include <cstdint> +#include <initializer_list> #include <iomanip> #include <iterator> #include <limits> @@ -6745,36 +6746,36 @@ void InitGoogleTest() { #endif // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) } +#if !defined(GTEST_CUSTOM_TEMPDIR_FUNCTION_) +// Return value of first environment variable that is set and contains +// a non-empty string. If there are none, return the "fallback" string. +// Since we like the temporary directory to have a directory separator suffix, +// add it if not provided in the environment variable value. +static std::string GetTempDirFromEnv( + std::initializer_list<const char*> environment_variables, + const char* fallback, char separator) { + for (const char* variable_name : environment_variables) { + const char* value = internal::posix::GetEnv(variable_name); + if (value != nullptr && value[0] != '\0') { + if (value[strlen(value) - 1] != separator) { + return std::string(value).append(1, separator); + } + return value; + } + } + return fallback; +} +#endif + std::string TempDir() { #if defined(GTEST_CUSTOM_TEMPDIR_FUNCTION_) return GTEST_CUSTOM_TEMPDIR_FUNCTION_(); -#elif GTEST_OS_WINDOWS_MOBILE - return "\\temp\\"; -#elif GTEST_OS_WINDOWS - const char* temp_dir = internal::posix::GetEnv("TEMP"); - if (temp_dir == nullptr || temp_dir[0] == '\0') { - return "\\temp\\"; - } else if (temp_dir[strlen(temp_dir) - 1] == '\\') { - return temp_dir; - } else { - return std::string(temp_dir) + "\\"; - } +#elif GTEST_OS_WINDOWS || GTEST_OS_WINDOWS_MOBILE + return GetTempDirFromEnv({"TEST_TMPDIR", "TEMP"}, "\\temp\\", '\\'); #elif GTEST_OS_LINUX_ANDROID - const char* temp_dir = internal::posix::GetEnv("TEST_TMPDIR"); - if (temp_dir == nullptr || temp_dir[0] == '\0') { - return "/data/local/tmp/"; - } else { - return temp_dir; - } -#elif GTEST_OS_LINUX || GTEST_OS_MAC - const char* temp_dir = internal::posix::GetEnv("TEST_TMPDIR"); - if (temp_dir == nullptr || temp_dir[0] == '\0') { - return "/tmp/"; - } else { - return temp_dir; - } + return GetTempDirFromEnv({"TEST_TMPDIR", "TMPDIR"}, "/data/local/tmp/", '/'); #else - return "/tmp/"; + return GetTempDirFromEnv({"TEST_TMPDIR", "TMPDIR"}, "/tmp/", '/'); #endif } |