Commit 87a4b661 authored by Alexey Spiridonov's avatar Alexey Spiridonov Committed by Noam Lerner

Add ChangeToTempDir to TestUtil

Summary: It's convenient to be able to write tempfiles in the current directory. This provides a one-liner way of doing that in tests.

Test Plan: unit test

Reviewed By: yfeldblum@fb.com

Subscribers: simpkins, folly-diffs@, yfeldblum

FB internal diff: D1931918

Signature: t1:1931918:1426913634:f988fb3c5061f5909e309dcaf42742d9b2ed18c6
parent 278d263c
...@@ -107,5 +107,15 @@ TemporaryDirectory::~TemporaryDirectory() { ...@@ -107,5 +107,15 @@ TemporaryDirectory::~TemporaryDirectory() {
} }
} }
ChangeToTempDir::ChangeToTempDir() : initialPath_(fs::current_path()) {
std::string p = dir_.path().native();
::chdir(p.c_str());
}
ChangeToTempDir::~ChangeToTempDir() {
std::string p = initialPath_.native();
::chdir(p.c_str());
}
} // namespace test } // namespace test
} // namespace folly } // namespace folly
...@@ -88,6 +88,22 @@ class TemporaryDirectory { ...@@ -88,6 +88,22 @@ class TemporaryDirectory {
fs::path path_; fs::path path_;
}; };
/**
* Changes into a temporary directory, and deletes it with all its contents
* upon destruction, also changing back to the original working directory.
*/
class ChangeToTempDir {
public:
ChangeToTempDir();
~ChangeToTempDir();
const fs::path& path() const { return dir_.path(); }
private:
fs::path initialPath_;
TemporaryDirectory dir_;
};
} // namespace test } // namespace test
} // namespace folly } // namespace folly
......
...@@ -100,6 +100,15 @@ TEST(TemporaryDirectory, DeleteOnDestruction) { ...@@ -100,6 +100,15 @@ TEST(TemporaryDirectory, DeleteOnDestruction) {
testTemporaryDirectory(TemporaryDirectory::Scope::DELETE_ON_DESTRUCTION); testTemporaryDirectory(TemporaryDirectory::Scope::DELETE_ON_DESTRUCTION);
} }
TEST(ChangeToTempDir, ChangeDir) {
auto pwd1 = fs::current_path();
{
ChangeToTempDir d;
EXPECT_NE(pwd1, fs::current_path());
}
EXPECT_EQ(pwd1, fs::current_path());
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
testing::InitGoogleTest(&argc, argv); testing::InitGoogleTest(&argc, argv);
gflags::ParseCommandLineFlags(&argc, &argv, true); gflags::ParseCommandLineFlags(&argc, &argv, true);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment