Skip to content

Commit

Permalink
Prepare for mocking.
Browse files Browse the repository at this point in the history
  • Loading branch information
Peguen committed Jan 7, 2025
1 parent af70650 commit 67d86cd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 50 deletions.
7 changes: 6 additions & 1 deletion ecal/core/src/config/ecal_path_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,15 @@ namespace eCAL
return getEnvVar(ECAL_DATA_VAR);
}

std::string eCALLogEnvPath()
{
return getEnvVar(ECAL_LOG_VAR);
}

std::string eCALLogDir()
{
const std::vector<std::string> log_paths = {
getEnvVar(ECAL_LOG_VAR),
eCALLogEnvPath(),
eCALDataEnvPath(),
eCAL::GetConfiguration().logging.provider.file_config.path,
eCALLocalUserDir(),
Expand Down
78 changes: 29 additions & 49 deletions ecal/tests/cpp/config_test/src/path_processing_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,66 +22,46 @@

#include "ecal_path_processing.h"
#include "ecal_utils/filesystem.h"
#include "util/getenvvar.h"

#include <ecal/ecal_os.h>
#include <ecal_def.h>
#include <ecal/ecal_util.h>

#include <cstdlib>

class ScopedEnvVar {
public:
ScopedEnvVar(const std::string& key, const std::string& value) : key_(key) {
#ifdef ECAL_OS_WINDOWS
_putenv_s(key.c_str(), value.c_str());
#elif defined(ECAL_OS_LINUX)
setenv(key.c_str(), value.c_str(), 1);
#endif
}

~ScopedEnvVar() {
#ifdef ECAL_OS_WINDOWS
_putenv_s(key_.c_str(), "");
#elif defined(ECAL_OS_LINUX)
unsetenv(key_.c_str());
#endif
}
class MockEnvVar
{
public:
MOCK_METHOD(std::string, eCALDataEnvPath, (), ());
};

private:
std::string key_;
class MockFileSystem
{
public:
MOCK_METHOD(bool, dirExists, (const std::string& path), (const));
};

TEST(core_cpp_path_processing /*unused*/, ecal_data_log_env_vars /*unused*/)
{
const std::string env_ecal_conf_value = "/pathtoconf";
const std::string env_ecal_log_value = "/pathtolog";

EXPECT_EQ(env_ecal_conf_value, env_ecal_conf_value);

// Needs rework - mocking
// {
// // All paths for data, config and log are the same when ECAL_DATA is set
// ScopedEnvVar env_var(ECAL_DATA_VAR, env_ecal_conf_value);
// EXPECT_EQ(eCAL::Config::eCALDataEnvPath(), env_ecal_conf_value);
// }

// {
// ScopedEnvVar env_var(ECAL_LOG_VAR, env_ecal_log_value);
// EXPECT_TRUE(EcalUtils::Filesystem::MkDir(env_ecal_log_value));
// EXPECT_EQ(eCAL::Config::eCALLogDir(), env_ecal_log_value);

// EcalUtils::Filesystem::DeleteDir(env_ecal_log_value);

// // at least a temporary folder would need to be created, so it should not be empty
// const std::string tmp_log_dir = eCAL::Config::eCALLogDir();
// EXPECT_NE(tmp_log_dir, env_ecal_log_value);

// // delete tmp folder again
// EcalUtils::Filesystem::DeleteDir(tmp_log_dir);
// }

// EXPECT_EQ(eCAL::Config::eCALDataEnvPath(), "");

const std::string env_ecal_conf_value = "/path/to/conf";
const std::string env_ecal_log_value = "/path/to/log";

{ // Check for config path
MockEnvVar mock_env_var;
EXPECT_CALL(mock_env_var, eCALDataEnvPath()).WillOnce(::testing::Return(env_ecal_conf_value));

auto data_env_dir = eCAL::Config::eCALDataEnvPath();
EXPECT_EQ(data_env_dir, env_ecal_conf_value);
// EXPECT_EQ(eCAL::Config::eCALDataEnvPath(), env_ecal_conf_value);

// EXPECT_CALL(mock_env_var, getEnvVar(::testing::_, "")).WillOnce(::testing::Return(env_ecal_log_value));
// auto log_dir = eCAL::Config::eCALLogDir();
// // NE because log dir is not created
// EXPECT_NE(log_dir, env_ecal_log_value);
// // not empty, because it should return a temp dir
// EXPECT_NE(log_dir, "");
}

// // at least a temporary folder would need to be created, so it should not be empty
// EXPECT_NE(eCAL::Config::eCALLogDir(), "");
}

0 comments on commit 67d86cd

Please sign in to comment.