scoped_set_env_test.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2019 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifdef _WIN32
  15. #include <windows.h>
  16. #endif
  17. #include "gtest/gtest.h"
  18. #include "absl/base/internal/scoped_set_env.h"
  19. namespace {
  20. using absl::base_internal::ScopedSetEnv;
  21. std::string GetEnvVar(const char* name) {
  22. #ifdef _WIN32
  23. char buf[1024];
  24. auto get_res = GetEnvironmentVariableA(name, buf, sizeof(buf));
  25. if (get_res >= sizeof(buf)) {
  26. return "TOO_BIG";
  27. }
  28. if (get_res == 0) {
  29. return "UNSET";
  30. }
  31. return std::string(buf, get_res);
  32. #else
  33. const char* val = ::getenv(name);
  34. if (val == nullptr) {
  35. return "UNSET";
  36. }
  37. return val;
  38. #endif
  39. }
  40. TEST(ScopedSetEnvTest, SetNonExistingVarToString) {
  41. EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "UNSET");
  42. {
  43. ScopedSetEnv scoped_set("SCOPED_SET_ENV_TEST_VAR", "value");
  44. EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "value");
  45. }
  46. EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "UNSET");
  47. }
  48. TEST(ScopedSetEnvTest, SetNonExistingVarToNull) {
  49. EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "UNSET");
  50. {
  51. ScopedSetEnv scoped_set("SCOPED_SET_ENV_TEST_VAR", nullptr);
  52. EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "UNSET");
  53. }
  54. EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "UNSET");
  55. }
  56. TEST(ScopedSetEnvTest, SetExistingVarToString) {
  57. ScopedSetEnv scoped_set("SCOPED_SET_ENV_TEST_VAR", "value");
  58. EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "value");
  59. {
  60. ScopedSetEnv scoped_set("SCOPED_SET_ENV_TEST_VAR", "new_value");
  61. EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "new_value");
  62. }
  63. EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "value");
  64. }
  65. TEST(ScopedSetEnvTest, SetExistingVarToNull) {
  66. ScopedSetEnv scoped_set("SCOPED_SET_ENV_TEST_VAR", "value");
  67. EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "value");
  68. {
  69. ScopedSetEnv scoped_set("SCOPED_SET_ENV_TEST_VAR", nullptr);
  70. EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "UNSET");
  71. }
  72. EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "value");
  73. }
  74. } // namespace