diff --git a/ecal/core/src/config/ecal_path_processing.cpp b/ecal/core/src/config/ecal_path_processing.cpp index 250a45bc0e..4389254646 100644 --- a/ecal/core/src/config/ecal_path_processing.cpp +++ b/ecal/core/src/config/ecal_path_processing.cpp @@ -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 log_paths = { - getEnvVar(ECAL_LOG_VAR), + eCALLogEnvPath(), eCALDataEnvPath(), eCAL::GetConfiguration().logging.provider.file_config.path, eCALLocalUserDir(), diff --git a/ecal/tests/cpp/config_test/src/path_processing_test.cpp b/ecal/tests/cpp/config_test/src/path_processing_test.cpp index 68e8a06243..26b0c0d549 100644 --- a/ecal/tests/cpp/config_test/src/path_processing_test.cpp +++ b/ecal/tests/cpp/config_test/src/path_processing_test.cpp @@ -22,6 +22,7 @@ #include "ecal_path_processing.h" #include "ecal_utils/filesystem.h" +#include "util/getenvvar.h" #include #include @@ -29,59 +30,38 @@ #include -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(), ""); } \ No newline at end of file