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
}
}
+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 folly
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 folly
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[]) {
testing::InitGoogleTest(&argc, argv);
gflags::ParseCommandLineFlags(&argc, &argv, true);