tests: remove temporary directory on setup and teardown
Change-Id: I012b11a9d6a46769dc5f6e388ab6bd3f2644607e
diff --git a/tests/global-configuration.cpp b/tests/global-configuration.cpp
index dfae750..402dcf8 100644
--- a/tests/global-configuration.cpp
+++ b/tests/global-configuration.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2022, Regents of the University of California,
+ * Copyright (c) 2014-2024, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -23,12 +23,14 @@
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
-#include "core/common.hpp"
-
#include "tests/boost-test.hpp"
-#include <boost/filesystem.hpp>
-#include <fstream>
+#include <ndn-cxx/util/exception.hpp>
+
+#include <boost/filesystem/operations.hpp>
+#include <boost/filesystem/path.hpp>
+
+#include <stdexcept>
#include <stdlib.h>
namespace nfd::tests {
@@ -40,17 +42,16 @@
{
const char* envHome = ::getenv("HOME");
if (envHome)
- m_home = envHome;
+ m_home.assign(envHome);
- auto testHome = boost::filesystem::path(UNIT_TESTS_TMPDIR) / "test-home";
- if (::setenv("HOME", testHome.c_str(), 1) != 0)
- NDN_THROW(std::runtime_error("setenv() failed"));
+ // in case an earlier test run crashed without a chance to run the destructor
+ boost::filesystem::remove_all(TESTDIR);
+ auto testHome = TESTDIR / "test-home";
boost::filesystem::create_directories(testHome);
- std::ofstream clientConf((testHome / ".ndn" / "client.conf").c_str());
- clientConf << "pib=pib-sqlite3" << std::endl
- << "tpm=tpm-file" << std::endl;
+ if (::setenv("HOME", testHome.c_str(), 1) != 0)
+ NDN_THROW_NO_STACK(std::runtime_error("setenv() failed"));
}
~GlobalConfiguration() noexcept
@@ -59,9 +60,13 @@
::unsetenv("HOME");
else
::setenv("HOME", m_home.data(), 1);
+
+ boost::system::error_code ec;
+ boost::filesystem::remove_all(TESTDIR, ec); // ignore error
}
private:
+ static inline const boost::filesystem::path TESTDIR{UNIT_TESTS_TMPDIR};
std::string m_home;
};