Skip to content

Commit

Permalink
tests: remove temporary directory on setup and teardown
Browse files Browse the repository at this point in the history
Change-Id: I012b11a9d6a46769dc5f6e388ab6bd3f2644607e
  • Loading branch information
Pesa committed Feb 22, 2024
1 parent caa60cc commit 152874a
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 28 deletions.
4 changes: 3 additions & 1 deletion daemon/face/unix-stream-channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
#include "unix-stream-transport.hpp"
#include "common/global.hpp"

#include <boost/filesystem.hpp>
#include <boost/filesystem/exception.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>

namespace nfd::face {

Expand Down
2 changes: 1 addition & 1 deletion daemon/face/unix-stream-factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

#include "unix-stream-factory.hpp"

#include <boost/filesystem.hpp>
#include <boost/filesystem/operations.hpp>

namespace nfd::face {

Expand Down
5 changes: 3 additions & 2 deletions daemon/mgmt/command-authenticator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@
#include "command-authenticator.hpp"
#include "common/logger.hpp"

#include <ndn-cxx/tag.hpp>
#include <ndn-cxx/security/certificate-fetcher-offline.hpp>
#include <ndn-cxx/security/certificate-request.hpp>
#include <ndn-cxx/security/validation-policy.hpp>
#include <ndn-cxx/security/validation-policy-accept-all.hpp>
#include <ndn-cxx/security/validation-policy-command-interest.hpp>
#include <ndn-cxx/tag.hpp>
#include <ndn-cxx/util/io.hpp>

#include <boost/filesystem.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>

namespace security = ndn::security;

Expand Down
11 changes: 3 additions & 8 deletions tests/daemon/face/unix-stream-channel.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,12 @@ class UnixStreamChannelFixture : public ChannelFixture<UnixStreamChannel, unix_s
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>
Expand Down Expand Up @@ -86,8 +81,8 @@ class UnixStreamChannelFixture : public ChannelFixture<UnixStreamChannel, unix_s
}

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)
Expand Down
2 changes: 1 addition & 1 deletion tests/daemon/face/unix-stream-transport-fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
29 changes: 17 additions & 12 deletions tests/global-configuration.cpp
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 {
Expand All @@ -40,17 +42,16 @@ class GlobalConfiguration
{
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
Expand All @@ -59,9 +60,13 @@ class GlobalConfiguration
::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;
};

Expand Down
4 changes: 2 additions & 2 deletions tests/key-chain-fixture.cpp
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -27,7 +27,7 @@

#include <ndn-cxx/util/io.hpp>

#include <boost/filesystem.hpp>
#include <boost/filesystem/operations.hpp>

namespace nfd::tests {

Expand Down
2 changes: 1 addition & 1 deletion tests/wscript
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ top = '..'
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(
Expand Down

0 comments on commit 152874a

Please sign in to comment.