From ee6a8dcb0fd6b3b04cfa8495274c0919b478cf02 Mon Sep 17 00:00:00 2001 From: Scott K Logan Date: Tue, 2 Jun 2020 16:57:45 -0700 Subject: [PATCH] Use absolute path in fake home directory Signed-off-by: Scott K Logan --- .../test/test_logging_interface.cpp | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/rcl_logging_spdlog/test/test_logging_interface.cpp b/rcl_logging_spdlog/test/test_logging_interface.cpp index bf176a4..c4f6816 100644 --- a/rcl_logging_spdlog/test/test_logging_interface.cpp +++ b/rcl_logging_spdlog/test/test_logging_interface.cpp @@ -61,6 +61,29 @@ class RestoreEnvVar const std::string value_; }; +// TODO(cottsay): Remove when ros2/rcpputils#63 is resolved +static fs::path current_path() +{ + char * cwd; +#ifdef _WIN32 +#ifdef UNICODE +#error "rcpputils::fs does not support Unicode paths" +#endif + cwd = _getcwd(NULL, 0); +#else + cwd = getcwd(NULL, 0); +#endif + if (nullptr == cwd) { + std::error_code ec{errno, std::system_category()}; + errno = 0; + throw std::system_error{ec, "cannot get current working directory"}; + } + + std::string ret(cwd); + free(cwd); + return fs::path(ret); +} + TEST_F(LoggingTest, init_invalid) { // Config files are not supported by spdlog @@ -84,7 +107,7 @@ TEST_F(LoggingTest, init_failure) rcutils_reset_error(); // Force failure to create directories - fs::path fake_home("fake_home_dir"); + fs::path fake_home = current_path() / "fake_home_dir"; ASSERT_TRUE(fs::create_directories(fake_home)); ASSERT_EQ(true, rcutils_set_env("HOME", fake_home.string().c_str()));