summaryrefslogtreecommitdiffstats
path: root/googletest/src
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2022-11-11 20:58:35 (GMT)
committerCopybara-Service <copybara-worker@google.com>2022-11-11 20:59:10 (GMT)
commit912db742531bf82efb01194bc08140416e3b3467 (patch)
tree0438b1eebf894f688f3d577f4a6169382a4b6548 /googletest/src
parent44c03643cfbc649488a0f437cd18e05f11960d19 (diff)
downloadgoogletest-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/src')
-rw-r--r--googletest/src/gtest.cc43
1 files changed, 34 insertions, 9 deletions
diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc
index 2276d07..7669d22 100644
--- a/googletest/src/gtest.cc
+++ b/googletest/src/gtest.cc
@@ -6754,12 +6754,13 @@ 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(
+#if !defined(GTEST_CUSTOM_TEMPDIR_FUNCTION_) || \
+ !defined(GTEST_CUSTOM_SRCDIR_FUNCTION_)
+// Returns the value of the first environment variable that is set and contains
+// a non-empty string. If there are none, returns the "fallback" string. Adds
+// the director-separator character as a suffix if not provided in the
+// environment variable value.
+static std::string GetDirFromEnv(
std::initializer_list<const char*> environment_variables,
const char* fallback, char separator) {
for (const char* variable_name : environment_variables) {
@@ -6779,11 +6780,35 @@ std::string TempDir() {
#if defined(GTEST_CUSTOM_TEMPDIR_FUNCTION_)
return GTEST_CUSTOM_TEMPDIR_FUNCTION_();
#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
- return GetTempDirFromEnv({"TEST_TMPDIR", "TMPDIR"}, "/data/local/tmp/", '/');
+ return GetDirFromEnv({"TEST_TMPDIR", "TMPDIR"}, "/data/local/tmp/", '/');
#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
}