mirror of
https://github.com/google/googletest.git
synced 2024-12-27 18:31:21 +08:00
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
This commit is contained in:
parent
4224c770a3
commit
5e6a533680
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user