diff --git a/rai/node/testing.cpp b/rai/node/testing.cpp index 93f43ede..ca298684 100644 --- a/rai/node/testing.cpp +++ b/rai/node/testing.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -50,6 +51,14 @@ rai::system::~system () { i->stop (); } + + // Clean up tmp directories created by the tests. Since it's sometimes useful to + // see log files after test failures, an environment variable is supported to + // retain the files. + if (std::getenv ("TEST_KEEP_TMPDIRS") == nullptr) + { + rai::remove_temporary_directories (); + } } std::shared_ptr rai::system::wallet (size_t index_a) diff --git a/rai/node/utility.cpp b/rai/node/utility.cpp index 7c38c90c..f35d7f14 100644 --- a/rai/node/utility.cpp +++ b/rai/node/utility.cpp @@ -6,6 +6,8 @@ #include +static std::vector all_unique_paths; + boost::filesystem::path rai::working_path () { auto result (rai::app_path ()); @@ -27,9 +29,33 @@ boost::filesystem::path rai::working_path () boost::filesystem::path rai::unique_path () { auto result (working_path () / boost::filesystem::unique_path ()); + all_unique_paths.push_back (result); return result; } +std::vector rai::remove_temporary_directories () +{ + for (auto & path : all_unique_paths) + { + boost::system::error_code ec; + boost::filesystem::remove_all (path, ec); + if (ec) + { + std::cerr << "Could not remove temporary directory: " << ec.message () << std::endl; + } + + // lmdb creates a -lock suffixed file for its MDB_NOSUBDIR databases + auto lockfile = path; + lockfile += "-lock"; + boost::filesystem::remove (lockfile, ec); + if (ec) + { + std::cerr << "Could not remove temporary lock file: " << ec.message () << std::endl; + } + } + return all_unique_paths; +} + rai::mdb_env::mdb_env (bool & error_a, boost::filesystem::path const & path_a, int max_dbs) { boost::system::error_code error; diff --git a/rai/node/utility.hpp b/rai/node/utility.hpp index d5fb7c87..a17aa632 100644 --- a/rai/node/utility.hpp +++ b/rai/node/utility.hpp @@ -24,8 +24,11 @@ using bufferstream = boost::iostreams::stream_buffer>>; // OS-specific way of finding a path to a home directory. boost::filesystem::path working_path (); -// Get a unique path within the home directory, used for testing +// Get a unique path within the home directory, used for testing. +// Any directories created at this location will be removed when a test finishes. boost::filesystem::path unique_path (); +// Remove all unique tmp directories created by the process. The list of unique paths are returned. +std::vector remove_temporary_directories (); // C++ stream are absolutely horrible so I need this helper function to do the most basic operation of creating a file if it doesn't exist or truncating it. void open_or_create (std::fstream &, std::string const &); // Reads a json object from the stream and if was changed, write the object back to the stream