forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add and use drake::temp_directory function instead of using /tmp
- Loading branch information
Jamie Snape
committed
Mar 29, 2018
1 parent
54dde2c
commit 75e6bd8
Showing
17 changed files
with
126 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include "drake/common/temp_directory.h" | ||
|
||
#include <cstdlib> | ||
|
||
#include <spruce.hh> | ||
|
||
#include "drake/common/drake_throw.h" | ||
|
||
namespace drake { | ||
|
||
std::string temp_directory() { | ||
// TODO(jamiesnape): Use mkdtemp instead of simply returning /tmp for | ||
// applications that do not require a hardcoded /tmp. | ||
const char* path_str = nullptr; | ||
(path_str = std::getenv("TEST_TMPDIR")) || (path_str = "/tmp"); | ||
|
||
// Spruce normalizes the path and strips any trailing /. | ||
spruce::path path(path_str); | ||
DRAKE_THROW_UNLESS(path.isDir()); | ||
|
||
return path.getStr(); | ||
} | ||
|
||
} // namespace drake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
namespace drake { | ||
|
||
/// Returns a directory location suitable for temporary files. | ||
/// @return The value of the environment variable TEST_TMPDIR if defined or | ||
/// otherwise /tmp. Any trailing / will be stripped from the output. | ||
/// @throws std::runtime_error If the path referred to by TEST_TMPDIR or /tmp | ||
/// does not exist or is not a directory. | ||
std::string temp_directory(); | ||
|
||
} // namespace drake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include "drake/common/temp_directory.h" | ||
|
||
#include <cstdlib> | ||
#include <string> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
namespace drake { | ||
namespace { | ||
|
||
GTEST_TEST(TempDirectoryTest, TestTmpdirSet) { | ||
const char* test_tmpdir = std::getenv("TEST_TMPDIR"); | ||
ASSERT_STRNE(nullptr, test_tmpdir); | ||
|
||
const std::string temp_directory_with_test_tmpdir_set = temp_directory(); | ||
EXPECT_NE('/', temp_directory_with_test_tmpdir_set.back()); | ||
EXPECT_EQ(std::string(test_tmpdir), temp_directory_with_test_tmpdir_set); | ||
} | ||
|
||
GTEST_TEST(TempDirectoryTest, TestTmpdirUnset) { | ||
const char* test_tmpdir = std::getenv("TEST_TMPDIR"); | ||
ASSERT_STRNE(nullptr, test_tmpdir); | ||
|
||
const int unset_result = ::unsetenv("TEST_TMPDIR"); | ||
ASSERT_EQ(0, unset_result); | ||
|
||
const std::string temp_directory_with_test_tmpdir_unset = temp_directory(); | ||
EXPECT_EQ("/tmp", temp_directory_with_test_tmpdir_unset); | ||
|
||
const int setenv_result = ::setenv("TEST_TMPDIR", test_tmpdir, 1); | ||
ASSERT_EQ(0, setenv_result); | ||
} | ||
|
||
} // namespace | ||
} // namespace drake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters