Portable setenv
This commit is contained in:
parent
873c51962f
commit
cb96a5fc0a
@ -49,6 +49,7 @@ set(SOURCES
|
||||
system/diskspace.cpp
|
||||
system/filetime_nonwindows.cpp
|
||||
system/filetime_windows.cpp
|
||||
system/env.cpp
|
||||
value_type/ValueType.cpp
|
||||
)
|
||||
|
||||
|
48
src/cpp-utils/system/env.cpp
Normal file
48
src/cpp-utils/system/env.cpp
Normal file
@ -0,0 +1,48 @@
|
||||
#include "env.h"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
#if !defined(_MSC_VER)
|
||||
|
||||
#include <cstdlib>
|
||||
namespace cpputils {
|
||||
|
||||
void setenv(const char* key, const char* value) {
|
||||
int retval = ::setenv(key, value, 1);
|
||||
if (0 != retval) {
|
||||
throw std::runtime_error("Error setting environment variable. Errno: " + std::to_string(errno));
|
||||
}
|
||||
}
|
||||
|
||||
void unsetenv(const char* key) {
|
||||
int retval = ::unsetenv(key);
|
||||
if (0 != retval) {
|
||||
throw std::runtime_error("Error unsetting environment variable. Errno: " + std::to_string(errno));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <Windows.h>
|
||||
#include <sstream>
|
||||
namespace cpputils {
|
||||
|
||||
void setenv(const char* key, const char* value) {
|
||||
std::ostringstream command;
|
||||
command << key << "=" << value;
|
||||
int retval = _putenv(command.str().c_str());
|
||||
if (0 != retval) {
|
||||
throw std::runtime_error("Error setting environment variable. Errno: " + std::to_string(errno));
|
||||
}
|
||||
}
|
||||
|
||||
void unsetenv(const char* key) {
|
||||
setenv(key, "");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
10
src/cpp-utils/system/env.h
Normal file
10
src/cpp-utils/system/env.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#ifndef MESSMER_CPPUTILS_SYSTEM_ENV_H
|
||||
#define MESSMER_CPPUTILS_SYSTEM_ENV_H
|
||||
|
||||
namespace cpputils {
|
||||
void setenv(const char* key, const char* value);
|
||||
void unsetenv(const char* key);
|
||||
}
|
||||
|
||||
#endif
|
@ -51,6 +51,7 @@ set(SOURCES
|
||||
system/FiletimeTest.cpp
|
||||
system/MemoryTest.cpp
|
||||
system/HomedirTest.cpp
|
||||
system/EnvTest.cpp
|
||||
value_type/ValueTypeTest.cpp
|
||||
)
|
||||
|
||||
|
21
test/cpp-utils/system/EnvTest.cpp
Normal file
21
test/cpp-utils/system/EnvTest.cpp
Normal file
@ -0,0 +1,21 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <cpp-utils/system/env.h>
|
||||
#include <string>
|
||||
|
||||
using std::string;
|
||||
|
||||
TEST(EnvTest, SetAndGetEnv_ValueIsCorrect) {
|
||||
cpputils::setenv("my_key", "my_value");
|
||||
EXPECT_EQ(string("my_value"), string(std::getenv("my_key")));
|
||||
}
|
||||
|
||||
TEST(EnvTest, SetAndGetEnvWithSpacedValue_ValueIsCorrect) {
|
||||
cpputils::setenv("my_key", "my value with spaces");
|
||||
EXPECT_EQ(string("my value with spaces"), string(std::getenv("my_key")));
|
||||
}
|
||||
|
||||
TEST(EnvTest, UnsetAndGetEnv_ValueIsEmpty) {
|
||||
cpputils::setenv("my_key", "my_value");
|
||||
cpputils::unsetenv("my_key");
|
||||
EXPECT_EQ(nullptr, std::getenv("my_key"));
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
#include "testutils/CliTest.h"
|
||||
|
||||
#include <cpp-utils/system/env.h>
|
||||
|
||||
namespace bf = boost::filesystem;
|
||||
using ::testing::Values;
|
||||
using ::testing::WithParamInterface;
|
||||
@ -129,9 +131,9 @@ TEST_P(CliTest_WrongEnvironment, BaseDir_DoesntExist_Noninteractive) {
|
||||
// We can't set an EXPECT_CALL().Times(0), because this is a death test (i.e. it is forked) and gmock EXPECT_CALL in fork children don't report to parents.
|
||||
// So we set a default answer that shouldn't crash and check it's not called by checking that it crashes.
|
||||
ON_CALL(*console, askYesNo("Could not find base directory. Do you want to create it?", _)).WillByDefault(Return(true));
|
||||
::setenv("CRYFS_FRONTEND", "noninteractive", 1);
|
||||
cpputils::setenv("CRYFS_FRONTEND", "noninteractive");
|
||||
Test_Run_Error("Error: base directory not found", ErrorCode::InaccessibleBaseDir);
|
||||
::unsetenv("CRYFS_FRONTEND");
|
||||
cpputils::unsetenv("CRYFS_FRONTEND");
|
||||
}
|
||||
|
||||
TEST_P(CliTest_WrongEnvironment, BaseDir_DoesntExist_Create) {
|
||||
@ -187,9 +189,9 @@ TEST_P(CliTest_WrongEnvironment, MountDir_DoesntExist_Noninteractive) {
|
||||
// We can't set an EXPECT_CALL().Times(0), because this is a death test (i.e. it is forked) and gmock EXPECT_CALL in fork children don't report to parents.
|
||||
// So we set a default answer that shouldn't crash and check it's not called by checking that it crashes.
|
||||
ON_CALL(*console, askYesNo("Could not find base directory. Do you want to create it?", _)).WillByDefault(Return(true));
|
||||
::setenv("CRYFS_FRONTEND", "noninteractive", 1);
|
||||
cpputils::setenv("CRYFS_FRONTEND", "noninteractive");
|
||||
Test_Run_Error("mount directory not found", ErrorCode::InaccessibleMountDir);
|
||||
::unsetenv("CRYFS_FRONTEND");
|
||||
cpputils::unsetenv("CRYFS_FRONTEND");
|
||||
}
|
||||
|
||||
TEST_P(CliTest_WrongEnvironment, MountDir_DoesntExist_Create) {
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include <cryfs-cli/Environment.h>
|
||||
#include <boost/optional.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <cpp-utils/system/env.h>
|
||||
|
||||
using namespace cryfs;
|
||||
using std::string;
|
||||
@ -21,13 +22,13 @@ public:
|
||||
if (nullptr != oldValue) {
|
||||
_oldValue = string(oldValue);
|
||||
}
|
||||
::setenv(key.c_str(), value.c_str(), 1);
|
||||
cpputils::setenv(key.c_str(), value.c_str());
|
||||
}
|
||||
~WithEnv() {
|
||||
if (none == _oldValue) {
|
||||
::unsetenv(_key.c_str());
|
||||
cpputils::unsetenv(_key.c_str());
|
||||
} else {
|
||||
::setenv(_key.c_str(), _oldValue->c_str(), 1);
|
||||
cpputils::setenv(_key.c_str(), _oldValue->c_str());
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user