Skip to content

Commit

Permalink
Add 3 more tests to improve coverage a little bit
Browse files Browse the repository at this point in the history
Signed-off-by: Scott K Logan <logans@cottsay.net>
  • Loading branch information
cottsay committed Jun 2, 2020
1 parent 2fcfff9 commit 1f5dad1
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions rcl_logging_spdlog/test/test_logging_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <rcpputils/filesystem_helper.hpp>
#include <rcpputils/get_env.hpp>
#include <rcutils/allocator.h>
#include <rcutils/env.h>
#include <rcutils/error_handling.h>
#include <rcutils/logging.h>

Expand All @@ -23,6 +26,8 @@
#include "gtest/gtest.h"
#include "rcl_logging_spdlog/logging_interface.h"

namespace fs = rcpputils::fs;

const int logger_levels[] =
{
RCUTILS_LOG_SEVERITY_UNSET,
Expand All @@ -33,6 +38,29 @@ const int logger_levels[] =
RCUTILS_LOG_SEVERITY_FATAL,
};

// This is a helper class that resets an environment
// variable when leaving scope
class RestoreEnvVar
{
public:
explicit RestoreEnvVar(const std::string & name)
: name_(name),
value_(rcpputils::get_env_var(name.c_str()))
{
}

~RestoreEnvVar()
{
if (!rcutils_set_env(name_.c_str(), value_.c_str())) {
std::cerr << "Failed to restore value of environment variable: " << name_ << std::endl;
}
}

private:
const std::string name_;
const std::string value_;
};

TEST_F(LoggingTest, init_invalid)
{
// Config files are not supported by spdlog
Expand All @@ -44,6 +72,39 @@ TEST_F(LoggingTest, init_invalid)
rcutils_reset_error();
}

TEST_F(LoggingTest, init_failure)
{
RestoreEnvVar home_var("HOME");
RestoreEnvVar userprofile_var("USERPROFILE");

// No home directory to write log to
ASSERT_EQ(true, rcutils_set_env("HOME", nullptr));
ASSERT_EQ(true, rcutils_set_env("USERPROFILE", nullptr));
EXPECT_EQ(2, rcl_logging_external_initialize(nullptr, allocator));
rcutils_reset_error();

// Force failure to create directories
fs::path fake_home("fake_home_dir");
ASSERT_TRUE(fs::create_directories(fake_home));
ASSERT_EQ(true, rcutils_set_env("HOME", fake_home.string().c_str()));

// ...fail to create .ros dir
fs::path ros_dir = fake_home / ".ros";
std::fstream(ros_dir.string(), std::ios_base::out).close();
EXPECT_EQ(2, rcl_logging_external_initialize(nullptr, allocator));
ASSERT_TRUE(fs::remove(ros_dir));

// ...fail to create .ros/log dir
ASSERT_TRUE(fs::create_directories(ros_dir));
fs::path ros_log_dir = ros_dir / "log";
std::fstream(ros_log_dir.string(), std::ios_base::out).close();
EXPECT_EQ(2, rcl_logging_external_initialize(nullptr, allocator));
ASSERT_TRUE(fs::remove(ros_log_dir));
ASSERT_TRUE(fs::remove(ros_dir));

ASSERT_TRUE(fs::remove(fake_home));
}

TEST_F(LoggingTest, full_cycle)
{
ASSERT_EQ(0, rcl_logging_external_initialize(nullptr, allocator));
Expand Down

0 comments on commit 1f5dad1

Please sign in to comment.