Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifdef _WIN32
00016 #include <windows.h>
00017 #endif
00018
00019 #include "gtest/gtest.h"
00020 #include "absl/base/internal/scoped_set_env.h"
00021
00022 namespace {
00023
00024 using absl::base_internal::ScopedSetEnv;
00025
00026 std::string GetEnvVar(const char* name) {
00027 #ifdef _WIN32
00028 char buf[1024];
00029 auto get_res = GetEnvironmentVariableA(name, buf, sizeof(buf));
00030 if (get_res >= sizeof(buf)) {
00031 return "TOO_BIG";
00032 }
00033
00034 if (get_res == 0) {
00035 return "UNSET";
00036 }
00037
00038 return std::string(buf, get_res);
00039 #else
00040 const char* val = ::getenv(name);
00041 if (val == nullptr) {
00042 return "UNSET";
00043 }
00044
00045 return val;
00046 #endif
00047 }
00048
00049 TEST(ScopedSetEnvTest, SetNonExistingVarToString) {
00050 EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "UNSET");
00051
00052 {
00053 ScopedSetEnv scoped_set("SCOPED_SET_ENV_TEST_VAR", "value");
00054
00055 EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "value");
00056 }
00057
00058 EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "UNSET");
00059 }
00060
00061 TEST(ScopedSetEnvTest, SetNonExistingVarToNull) {
00062 EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "UNSET");
00063
00064 {
00065 ScopedSetEnv scoped_set("SCOPED_SET_ENV_TEST_VAR", nullptr);
00066
00067 EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "UNSET");
00068 }
00069
00070 EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "UNSET");
00071 }
00072
00073 TEST(ScopedSetEnvTest, SetExistingVarToString) {
00074 ScopedSetEnv scoped_set("SCOPED_SET_ENV_TEST_VAR", "value");
00075 EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "value");
00076
00077 {
00078 ScopedSetEnv scoped_set("SCOPED_SET_ENV_TEST_VAR", "new_value");
00079
00080 EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "new_value");
00081 }
00082
00083 EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "value");
00084 }
00085
00086 TEST(ScopedSetEnvTest, SetExistingVarToNull) {
00087 ScopedSetEnv scoped_set("SCOPED_SET_ENV_TEST_VAR", "value");
00088 EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "value");
00089
00090 {
00091 ScopedSetEnv scoped_set("SCOPED_SET_ENV_TEST_VAR", nullptr);
00092
00093 EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "UNSET");
00094 }
00095
00096 EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "value");
00097 }
00098
00099 }