#pragma once #include #include #include namespace xrpl { /** * Write a key file dir and remove when done. */ class KeyFileGuard { private: using path = boost::filesystem::path; path subDir_; beast::unit_test::Suite& test_; void rmDir(path const& toRm) { boost::system::error_code ec; if (is_directory(toRm, ec)) remove_all(toRm, ec); else test_.log << "Expected " << toRm.string() << " to be an existing directory." << std::endl; } public: KeyFileGuard(beast::unit_test::Suite& test, std::string const& subDir) : subDir_(subDir), test_(test) { using namespace boost::filesystem; if (!exists(subDir_)) create_directory(subDir_); else // Cannot run the test. Someone created a file or directory // where we want to put our directory throw std::runtime_error("Cannot create directory: " + subDir_.string()); } ~KeyFileGuard() { rmDir(subDir_); } }; } // namespace xrpl