scoped_set_env_test.cc
Go to the documentation of this file.
00001 // Copyright 2019 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
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 }  // namespace


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15