Skip to content

Commit

Permalink
Remove temporary directory platform-specific logic from test fixture (r…
Browse files Browse the repository at this point in the history
…os2#660)

* Remove temporary directory platform-specific logic from test fixture

Signed-off-by: Emerson Knapp <eknapp@amazon.com>
  • Loading branch information
emersonknapp authored Feb 18, 2021
1 parent ffb73b1 commit e4ce24c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,7 @@

#include <string>

#ifdef _WIN32
# include <direct.h>
# include <Windows.h>
#else
# include <unistd.h>
# include <sys/types.h>
# include <dirent.h>
#endif
#include "rcpputils/filesystem_helper.hpp"

using namespace ::testing; // NOLINT

Expand All @@ -38,64 +31,12 @@ class TemporaryDirectoryFixture : public Test
public:
TemporaryDirectoryFixture()
{
char template_char[] = "tmp_test_dir.XXXXXX";
#ifdef _WIN32
char temp_path[255];
GetTempPathA(255, temp_path);
_mktemp_s(template_char, strnlen(template_char, 20) + 1);
temporary_dir_path_ = std::string(temp_path) + std::string(template_char);
_mkdir(temporary_dir_path_.c_str());
#else
char * dir_name = mkdtemp(template_char);
temporary_dir_path_ = dir_name;
#endif
temporary_dir_path_ = rcpputils::fs::create_temp_directory("tmp_test_dir_").string();
}

~TemporaryDirectoryFixture() override
{
remove_directory_recursively(temporary_dir_path_);
}

void remove_directory_recursively(const std::string & directory_path)
{
#ifdef _WIN32
// We need a string of type PCZZTSTR, which is a double null terminated char ptr
size_t length = strlen(directory_path.c_str());
TCHAR * temp_dir = new TCHAR[length + 2];
memcpy(temp_dir, temporary_dir_path_.c_str(), length);
temp_dir[length] = 0;
temp_dir[length + 1] = 0; // double null terminated

SHFILEOPSTRUCT file_options;
file_options.hwnd = nullptr;
file_options.wFunc = FO_DELETE; // delete (recursively)
file_options.pFrom = temp_dir;
file_options.pTo = nullptr;
file_options.fFlags = FOF_NOCONFIRMATION | FOF_SILENT; // do not prompt user
file_options.fAnyOperationsAborted = FALSE;
file_options.lpszProgressTitle = nullptr;
file_options.hNameMappings = nullptr;

SHFileOperation(&file_options);
delete[] temp_dir;
#else
DIR * dir = opendir(directory_path.c_str());
if (!dir) {
return;
}
struct dirent * directory_entry;
while ((directory_entry = readdir(dir)) != nullptr) {
// Make sure to not call ".." or "." entries in directory (might delete everything)
if (strcmp(directory_entry->d_name, ".") != 0 && strcmp(directory_entry->d_name, "..") != 0) {
if (directory_entry->d_type == DT_DIR) {
remove_directory_recursively(directory_path + "/" + directory_entry->d_name);
}
remove((directory_path + "/" + directory_entry->d_name).c_str());
}
}
closedir(dir);
remove(temporary_dir_path_.c_str());
#endif
rcpputils::fs::remove_all(rcpputils::fs::path(temporary_dir_path_));
}

std::string temporary_dir_path_;
Expand Down
6 changes: 2 additions & 4 deletions rosbag2_tests/test/rosbag2_tests/record_fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ class RecordFixture : public TemporaryDirectoryFixture
// Clean up potentially leftover bag files.
// There may be leftovers if the system reallocates a temp directory
// used by a previous test execution and the test did not have a clean exit.
if (root_bag_path_.exists()) {
remove_directory_recursively(root_bag_path_.string());
}
rcpputils::fs::remove_all(root_bag_path_);
}

static void SetUpTestCase()
Expand All @@ -64,7 +62,7 @@ class RecordFixture : public TemporaryDirectoryFixture

void TearDown() override
{
remove_directory_recursively(root_bag_path_.string());
rcpputils::fs::remove_all(root_bag_path_);
}

static void TearDownTestCase()
Expand Down

0 comments on commit e4ce24c

Please sign in to comment.