Cleanup test files on Windows after executing test binary (#1910)

* Cleanup test files on windows after executing test script

* Formatting

* Fix strange formatting issue
This commit is contained in:
Wesley Shillingford 2019-04-19 07:28:07 +01:00 committed by GitHub
commit ebcd2dd3c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 73 additions and 13 deletions

View file

@ -1,4 +1,5 @@
#include "gtest/gtest.h"
#include <nano/secure/utility.hpp>
namespace nano
{
void force_nano_test_network ();
@ -8,5 +9,7 @@ GTEST_API_ int main (int argc, char ** argv)
printf ("Running main() from core_test_main.cc\n");
nano::force_nano_test_network ();
testing::InitGoogleTest (&argc, argv);
return RUN_ALL_TESTS ();
auto res = RUN_ALL_TESTS ();
nano::cleanp_test_directories_on_exit ();
return res;
}

View file

@ -5,9 +5,11 @@
#include <boost/property_tree/ptree.hpp>
#include <nano/node/logging.hpp>
boost::shared_ptr<boost::log::sinks::synchronous_sink<boost::log::sinks::text_file_backend>> nano::logging::file_sink;
std::atomic_flag nano::logging::logging_already_added{ ATOMIC_FLAG_INIT };
void nano::logging::init (boost::filesystem::path const & application_path_a)
{
static std::atomic_flag logging_already_added = ATOMIC_FLAG_INIT;
if (!logging_already_added.test_and_set ())
{
boost::log::add_common_attributes ();
@ -16,7 +18,16 @@ void nano::logging::init (boost::filesystem::path const & application_path_a)
boost::log::add_console_log (std::cerr, boost::log::keywords::format = "[%TimeStamp%]: %Message%");
}
auto path = application_path_a / "log";
boost::log::add_file_log (boost::log::keywords::target = path, boost::log::keywords::file_name = path / "log_%Y-%m-%d_%H-%M-%S.%N.log", boost::log::keywords::rotation_size = rotation_size, boost::log::keywords::auto_flush = flush, boost::log::keywords::scan_method = boost::log::sinks::file::scan_method::scan_matching, boost::log::keywords::max_size = max_size, boost::log::keywords::format = "[%TimeStamp%]: %Message%");
file_sink = boost::log::add_file_log (boost::log::keywords::target = path, boost::log::keywords::file_name = path / "log_%Y-%m-%d_%H-%M-%S.%N.log", boost::log::keywords::rotation_size = rotation_size, boost::log::keywords::auto_flush = flush, boost::log::keywords::scan_method = boost::log::sinks::file::scan_method::scan_matching, boost::log::keywords::max_size = max_size, boost::log::keywords::format = "[%TimeStamp%]: %Message%");
}
}
void nano::logging::release_file_sink ()
{
if (logging_already_added.test_and_set ())
{
boost::log::core::get ()->remove_sink (nano::logging::file_sink);
nano::logging::file_sink.reset ();
}
}

View file

@ -3,6 +3,7 @@
#include <boost/filesystem.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <cstdint>
#include <nano/lib/errors.hpp>
#include <nano/lib/jsonconfig.hpp>
@ -64,9 +65,14 @@ public:
uintmax_t rotation_size{ 4 * 1024 * 1024 };
std::chrono::milliseconds min_time_between_log_output{ 5 };
nano::logger_mt logger{ min_time_between_log_output };
static void release_file_sink ();
int json_version () const
{
return 7;
}
private:
static boost::shared_ptr<boost::log::sinks::synchronous_sink<boost::log::sinks::text_file_backend>> file_sink;
static std::atomic_flag logging_already_added;
};
}

View file

@ -77,6 +77,11 @@ nano::system::~system ()
i->stop ();
}
// clang-format off
// Windows cannot remove the log and data files while they are still owned by this process.
// They will be removed later
// clang-format on
#ifndef _WIN32
// 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.
@ -84,6 +89,7 @@ nano::system::~system ()
{
nano::remove_temporary_directories ();
}
#endif
}
std::shared_ptr<nano::wallet> nano::system::wallet (size_t index_a)

View file

@ -1,6 +1,6 @@
#include <QApplication>
#include <gtest/gtest.h>
#include <nano/secure/utility.hpp>
QApplication * test_application = nullptr;
namespace nano
{
@ -13,5 +13,7 @@ int main (int argc, char ** argv)
QApplication application (argc, argv);
test_application = &application;
testing::InitGoogleTest (&argc, argv);
return RUN_ALL_TESTS ();
auto res = RUN_ALL_TESTS ();
nano::cleanp_test_directories_on_exit ();
return res;
}

View file

@ -2,7 +2,7 @@ add_executable (rpc_test
entry.cpp
rpc.cpp)
target_link_libraries(rpc_test gtest gtest_main rpc)
target_link_libraries(rpc_test gtest rpc)
target_compile_definitions(rpc_test
PUBLIC

View file

@ -1,5 +1,5 @@
#include <gtest/gtest.h>
#include <nano/secure/utility.hpp>
namespace nano
{
void force_nano_test_network ();
@ -9,5 +9,7 @@ int main (int argc, char ** argv)
{
nano::force_nano_test_network ();
testing::InitGoogleTest (&argc, argv);
return RUN_ALL_TESTS ();
auto res = RUN_ALL_TESTS ();
nano::cleanp_test_directories_on_exit ();
return res;
}

View file

@ -1,5 +1,6 @@
#include <nano/lib/config.hpp>
#include <nano/lib/interface.h>
#include <nano/node/logging.hpp>
#include <nano/node/working.hpp>
#include <nano/secure/utility.hpp>
@ -88,7 +89,7 @@ boost::filesystem::path nano::unique_path ()
return result;
}
std::vector<boost::filesystem::path> nano::remove_temporary_directories ()
void nano::remove_temporary_directories ()
{
for (auto & path : all_unique_paths)
{
@ -108,5 +109,17 @@ std::vector<boost::filesystem::path> nano::remove_temporary_directories ()
std::cerr << "Could not remove temporary lock file: " << ec.message () << std::endl;
}
}
return all_unique_paths;
}
void nano::cleanp_test_directories_on_exit ()
{
// Makes sure everything is cleaned up
nano::logging::release_file_sink ();
// 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)
{
nano::remove_temporary_directories ();
}
}

View file

@ -27,6 +27,7 @@ bool migrate_working_path (std::string &);
// 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<boost::filesystem::path> remove_temporary_directories ();
// Remove all unique tmp directories created by the process
void remove_temporary_directories ();
void cleanp_test_directories_on_exit ();
}

View file

@ -1,4 +1,5 @@
add_executable (slow_test
entry.cpp
node.cpp)
target_link_libraries (slow_test node secure gtest_main gtest libminiupnpc-static Boost::boost)
target_link_libraries (slow_test node secure gtest libminiupnpc-static Boost::boost)

15
nano/slow_test/entry.cpp Normal file
View file

@ -0,0 +1,15 @@
#include <gtest/gtest.h>
#include <nano/secure/utility.hpp>
namespace nano
{
void force_nano_test_network ();
}
int main (int argc, char ** argv)
{
nano::force_nano_test_network ();
testing::InitGoogleTest (&argc, argv);
auto res = RUN_ALL_TESTS ();
nano::cleanp_test_directories_on_exit ();
return res;
}