tests: remove temporary directory on setup and teardown
Change-Id: I012b11a9d6a46769dc5f6e388ab6bd3f2644607e
diff --git a/tests/daemon/face/unix-stream-channel.t.cpp b/tests/daemon/face/unix-stream-channel.t.cpp
index a767d23..605597d 100644
--- a/tests/daemon/face/unix-stream-channel.t.cpp
+++ b/tests/daemon/face/unix-stream-channel.t.cpp
@@ -42,17 +42,12 @@
UnixStreamChannelFixture()
{
listenerEp = unix_stream::Endpoint(socketPath.string());
-
- // in case an earlier test run crashed without a chance to run the destructor
- boost::system::error_code ec;
- fs::remove_all(testDir, ec);
}
~UnixStreamChannelFixture() override
{
- // cleanup
boost::system::error_code ec;
- fs::remove_all(testDir, ec);
+ fs::remove_all(testDir, ec); // ignore error
}
shared_ptr<UnixStreamChannel>
@@ -86,8 +81,8 @@
}
protected:
- fs::path testDir = fs::path(UNIT_TESTS_TMPDIR) / "unix-stream-channel";
- fs::path socketPath = testDir / "test" / "foo.sock";
+ static inline const fs::path testDir = fs::path(UNIT_TESTS_TMPDIR) / "unix-stream-channel";
+ static inline const fs::path socketPath = testDir / "test" / "foo.sock";
};
BOOST_AUTO_TEST_SUITE(Face)
diff --git a/tests/daemon/face/unix-stream-transport-fixture.hpp b/tests/daemon/face/unix-stream-transport-fixture.hpp
index 253e05c..8aca3da 100644
--- a/tests/daemon/face/unix-stream-transport-fixture.hpp
+++ b/tests/daemon/face/unix-stream-transport-fixture.hpp
@@ -34,7 +34,7 @@
#include "tests/daemon/face/dummy-link-service.hpp"
#include "tests/daemon/face/transport-test-common.hpp"
-#include <boost/filesystem.hpp>
+#include <boost/filesystem/operations.hpp>
namespace nfd::tests {
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;
};
diff --git a/tests/key-chain-fixture.cpp b/tests/key-chain-fixture.cpp
index 21a6b9f..57bb057 100644
--- a/tests/key-chain-fixture.cpp
+++ b/tests/key-chain-fixture.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,
@@ -27,7 +27,7 @@
#include <ndn-cxx/util/io.hpp>
-#include <boost/filesystem.hpp>
+#include <boost/filesystem/operations.hpp>
namespace nfd::tests {
diff --git a/tests/wscript b/tests/wscript
index 956fd0d..4092db1 100644
--- a/tests/wscript
+++ b/tests/wscript
@@ -28,7 +28,7 @@
def build(bld):
# Unit tests
if bld.env.WITH_TESTS:
- tmpdir = 'UNIT_TESTS_TMPDIR="%s"' % bld.bldnode.make_node('tmp-files')
+ tmpdir = 'UNIT_TESTS_TMPDIR="%s"' % bld.bldnode.make_node('tests-tmp')
# common test objects
bld.objects(