environment variable for location of temporary files (includes patch)

962 views
Skip to first unread message

Brian Silverman

unread,
Jul 23, 2015, 11:32:24 AM7/23/15
to googletes...@googlegroups.com
Hi,

To make sure tests don't interfere with each other, it is nice to be
able to mount /tmp read-only (likely in a separate mount namespace).
However, googletest unconditionally writes files there. Here is a
patch which looks for TEST_TMPDIR in the environment first. I like
this choice of variable name because it's logical and works nicely
with Bazel.

Index: src/gtest-port.cc
===================================================================
--- src/gtest-port.cc (revision 733)
+++ src/gtest-port.cc (working copy)
@@ -953,7 +953,15 @@
// use /sdcard.
char name_template[] = "/sdcard/gtest_captured_stream.XXXXXX";
# else
- char name_template[] = "/tmp/captured_stream.XXXXXX";
+ char name_template[256];
+ char *test_tmpdir = getenv("TEST_TMPDIR");
+ if (test_tmpdir != NULL && test_tmpdir[0] != '\0') {
+ snprintf(name_template, sizeof(name_template),
+ "%s/captured_stream.XXXXXX", test_tmpdir);
+ } else {
+ const char *default_template = "/tmp/captured_stream.XXXXXX";
+ strcpy(name_template, default_template);
+ }
# endif // GTEST_OS_LINUX_ANDROID
const int captured_fd = mkstemp(name_template);
filename_ = name_template;

I have already accepted the individual CLA. Is there anything else I
need to do to help get this committed?

Thanks,
Brian Silverman

Brian Silverman

unread,
Sep 12, 2015, 6:12:07 PM9/12/15
to Google C++ Testing Framework
Hi,

What's the status on this? I can submit a GitHub pull request if that would be easier.

Brian

Billy Donahue

unread,
Sep 12, 2015, 6:16:32 PM9/12/15
to Brian Silverman, Google C++ Testing Framework
I like giving user choice over this. I would like to keep the googletest env vars in the GTEST_ namespace, though. I really want to avoid using vars that another like Bazel uses, because then it's impossible to change one behavior without changing the other.

So GTEST_TMP_DIR maybe?

--

---
You received this message because you are subscribed to the Google Groups "Google C++ Testing Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googletestframe...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brian Silverman

unread,
Sep 15, 2015, 11:17:56 AM9/15/15
to Billy Donahue, Google C++ Testing Framework
Sounds good to me. Revised patch:

commit c100f7501ccc63817b6ba99f3915b54963b49a27
Author: Brian Silverman <bsilve...@gmail.com>
Date: Sat Sep 12 14:31:41 2015 -0400

Add an environment variable to control the location of temporary files

Bazel says in their test environment documentation
(http://bazel.io/docs/test-encyclopedia.html) that tests should not
write to /tmp. However, googletest unconditionally writes files there.
This fixes that by having googletest use the GTEST_TMP_DIR
environment variable instead, which is easy to have Bazel set when
running tests.

diff --git a/googletest/src/gtest-port.cc b/googletest/src/gtest-port.cc
index 3842c41..8f3fc82 100644
--- a/googletest/src/gtest-port.cc
+++ b/googletest/src/gtest-port.cc
@@ -953,7 +953,15 @@ class CapturedStream {
// use /sdcard.
char name_template[] = "/sdcard/gtest_captured_stream.XXXXXX";
# else
- char name_template[] = "/tmp/captured_stream.XXXXXX";
+ char name_template[256];
+ char *test_tmpdir = getenv("GTEST_TMP_DIR");
+ if (test_tmpdir != NULL && test_tmpdir[0] != '\0') {
+ snprintf(name_template, sizeof(name_template),
+ "%s/captured_stream.XXXXXX", test_tmpdir);
+ } else {
+ const char *default_template = "/tmp/captured_stream.XXXXXX";
+ strcpy(name_template, default_template);
+ }
# endif // GTEST_OS_LINUX_ANDROID
const int captured_fd = mkstemp(name_template);
filename_ = name_template;

Brian Silverman

unread,
Oct 2, 2015, 2:44:41 PM10/2/15
to Billy Donahue, Google C++ Testing Framework
Ping?

Billy Donahue

unread,
Dec 13, 2015, 2:17:29 PM12/13/15
to Brian Silverman, Google C++ Testing Framework
Snprintf could fail. Can you catch that?

On Sat, Dec 12, 2015 at 5:12 PM, Brian Silverman <bsilve...@gmail.com> wrote:
Any update on this? I submitted it as #619 too if that's easier to merge.

Brian Silverman

unread,
Jan 7, 2016, 5:00:53 PM1/7/16
to Billy Donahue, Google C++ Testing Framework
Sure. Here's a revised version (which can be applied with `git am`):

From f3bc07c08c50bde3dc9678953f153058a2446849 Mon Sep 17 00:00:00 2001
From: Brian Silverman <bsilve...@gmail.com>
Date: Tue, 5 Jan 2016 16:56:47 -0800
Subject: [PATCH] Add an environment variable to control the location of
 temporary files

Bazel says in their test environment documentation
write to /tmp. However, googletest unconditionally writes files there.
This fixes that by having googletest use the GTEST_TMP_DIR
environment variable instead, which is easy to have Bazel set when
running tests.
---
 googletest/src/gtest-port.cc | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/googletest/src/gtest-port.cc b/googletest/src/gtest-port.cc
index 0162fac..d7d6ea1 100644
--- a/googletest/src/gtest-port.cc
+++ b/googletest/src/gtest-port.cc
@@ -971,7 +971,18 @@ class CapturedStream {
     // use /sdcard.
     char name_template[] = "/sdcard/gtest_captured_stream.XXXXXX";
 #  else
-    char name_template[] = "/tmp/captured_stream.XXXXXX";
+    char name_template[256];
+    char *test_tmpdir = getenv("GTEST_TMP_DIR");
+    if (test_tmpdir != NULL && test_tmpdir[0] != '\0') {
+      const int result = snprintf(name_template, sizeof(name_template),
+                                  "%s/captured_stream.XXXXXX", test_tmpdir);
+      GTEST_CHECK_(result > 0);
+      GTEST_CHECK_(result < sizeof(name_template))
+          << "GTEST_TMP_DIR (" << test_tmpdir << ") is too long";
+    } else {
+      const char *default_template = "/tmp/captured_stream.XXXXXX";
+      strcpy(name_template, default_template);
+    }
 #  endif  // GTEST_OS_LINUX_ANDROID
     const int captured_fd = mkstemp(name_template);
     filename_ = name_template;
-- 
2.1.4

Reply all
Reply to author
Forward
0 new messages