Skip to content

Commit 1f5dad1

Browse files
committed
Add 3 more tests to improve coverage a little bit
Signed-off-by: Scott K Logan <logans@cottsay.net>
1 parent 2fcfff9 commit 1f5dad1

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

rcl_logging_spdlog/test/test_logging_interface.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#include <rcpputils/filesystem_helper.hpp>
16+
#include <rcpputils/get_env.hpp>
1517
#include <rcutils/allocator.h>
18+
#include <rcutils/env.h>
1619
#include <rcutils/error_handling.h>
1720
#include <rcutils/logging.h>
1821

@@ -23,6 +26,8 @@
2326
#include "gtest/gtest.h"
2427
#include "rcl_logging_spdlog/logging_interface.h"
2528

29+
namespace fs = rcpputils::fs;
30+
2631
const int logger_levels[] =
2732
{
2833
RCUTILS_LOG_SEVERITY_UNSET,
@@ -33,6 +38,29 @@ const int logger_levels[] =
3338
RCUTILS_LOG_SEVERITY_FATAL,
3439
};
3540

41+
// This is a helper class that resets an environment
42+
// variable when leaving scope
43+
class RestoreEnvVar
44+
{
45+
public:
46+
explicit RestoreEnvVar(const std::string & name)
47+
: name_(name),
48+
value_(rcpputils::get_env_var(name.c_str()))
49+
{
50+
}
51+
52+
~RestoreEnvVar()
53+
{
54+
if (!rcutils_set_env(name_.c_str(), value_.c_str())) {
55+
std::cerr << "Failed to restore value of environment variable: " << name_ << std::endl;
56+
}
57+
}
58+
59+
private:
60+
const std::string name_;
61+
const std::string value_;
62+
};
63+
3664
TEST_F(LoggingTest, init_invalid)
3765
{
3866
// Config files are not supported by spdlog
@@ -44,6 +72,39 @@ TEST_F(LoggingTest, init_invalid)
4472
rcutils_reset_error();
4573
}
4674

75+
TEST_F(LoggingTest, init_failure)
76+
{
77+
RestoreEnvVar home_var("HOME");
78+
RestoreEnvVar userprofile_var("USERPROFILE");
79+
80+
// No home directory to write log to
81+
ASSERT_EQ(true, rcutils_set_env("HOME", nullptr));
82+
ASSERT_EQ(true, rcutils_set_env("USERPROFILE", nullptr));
83+
EXPECT_EQ(2, rcl_logging_external_initialize(nullptr, allocator));
84+
rcutils_reset_error();
85+
86+
// Force failure to create directories
87+
fs::path fake_home("fake_home_dir");
88+
ASSERT_TRUE(fs::create_directories(fake_home));
89+
ASSERT_EQ(true, rcutils_set_env("HOME", fake_home.string().c_str()));
90+
91+
// ...fail to create .ros dir
92+
fs::path ros_dir = fake_home / ".ros";
93+
std::fstream(ros_dir.string(), std::ios_base::out).close();
94+
EXPECT_EQ(2, rcl_logging_external_initialize(nullptr, allocator));
95+
ASSERT_TRUE(fs::remove(ros_dir));
96+
97+
// ...fail to create .ros/log dir
98+
ASSERT_TRUE(fs::create_directories(ros_dir));
99+
fs::path ros_log_dir = ros_dir / "log";
100+
std::fstream(ros_log_dir.string(), std::ios_base::out).close();
101+
EXPECT_EQ(2, rcl_logging_external_initialize(nullptr, allocator));
102+
ASSERT_TRUE(fs::remove(ros_log_dir));
103+
ASSERT_TRUE(fs::remove(ros_dir));
104+
105+
ASSERT_TRUE(fs::remove(fake_home));
106+
}
107+
47108
TEST_F(LoggingTest, full_cycle)
48109
{
49110
ASSERT_EQ(0, rcl_logging_external_initialize(nullptr, allocator));

0 commit comments

Comments
 (0)