proxygen
EnvUtilTest.cpp File Reference
#include <folly/experimental/EnvUtil.h>
#include <boost/algorithm/string.hpp>
#include <folly/Memory.h>
#include <folly/Subprocess.h>
#include <folly/portability/Fcntl.h>
#include <folly/portability/GTest.h>
#include <folly/portability/Stdlib.h>
#include <glog/logging.h>
#include <spawn.h>
#include <system_error>

Go to the source code of this file.

Functions

 DEFINE_string (env_util_subprocess_binary,"./env_util_subprocess","Location of the `env_util_subprocess` test helper program")
 
 TEST (EnvVarSaverTest, ExampleNew)
 
 TEST (EnvVarSaverTest, ExampleExisting)
 
 TEST (EnvVarSaverTest, Movable)
 
 TEST (EnvironmentStateTest, FailOnEmptyString)
 
 TEST (EnvironmentStateTest, MovableAndCopyable)
 
 TEST (EnvironmentStateTest, FailOnDuplicate)
 
 TEST (EnvironmentStateTest, Separation)
 
 TEST (EnvironmentStateTest, Update)
 
 TEST (EnvironmentStateTest, forSubprocess)
 
 TEST (EnvironmentStateTest, forC)
 
 TEST (EnvVarSaverTest, ExampleDeleting)
 

Function Documentation

DEFINE_string ( env_util_subprocess_binary  ,
"./env_util_subprocess"  ,
"Location of the `env_util_subprocess` test helper program"   
)
TEST ( EnvVarSaverTest  ,
ExampleNew   
)

Definition at line 39 of file EnvUtilTest.cpp.

References EXPECT_EQ, and EXPECT_STREQ.

39  {
40  auto key = "hahahahaha";
41  EXPECT_EQ(nullptr, getenv(key));
42 
43  PCHECK(0 == setenv(key, "", true));
44  EXPECT_STREQ("", getenv(key));
45  PCHECK(0 == unsetenv(key));
46  EXPECT_EQ(nullptr, getenv(key));
47 
48  auto saver = std::make_unique<EnvVarSaver>();
49  PCHECK(0 == setenv(key, "blah", true));
50  EXPECT_STREQ("blah", getenv(key));
51  saver = nullptr;
52  EXPECT_EQ(nullptr, getenv(key));
53 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:1995
TEST ( EnvVarSaverTest  ,
ExampleExisting   
)

Definition at line 55 of file EnvUtilTest.cpp.

References EXPECT_EQ, EXPECT_NE, EXPECT_STREQ, string, and folly::value().

55  {
56  auto key = "PATH";
57  EXPECT_NE(nullptr, getenv(key));
58  auto value = std::string{getenv(key)};
59 
60  auto saver = std::make_unique<EnvVarSaver>();
61  PCHECK(0 == setenv(key, "blah", true));
62  EXPECT_STREQ("blah", getenv(key));
63  saver = nullptr;
64  EXPECT_EQ(value, getenv(key));
65 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:1995
static const char *const value
Definition: Conv.cpp:50
const char * string
Definition: Conv.cpp:212
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
TEST ( EnvVarSaverTest  ,
Movable   
)

Definition at line 67 of file EnvUtilTest.cpp.

References folly::Optional< Value >::clear(), folly::Optional< Value >::emplace(), EXPECT_EQ, EXPECT_NE, EXPECT_STREQ, folly::gen::move, string, and folly::value().

67  {
68  Optional<EnvVarSaver> pSaver1;
69  pSaver1.emplace();
70  auto key = "PATH";
71  EXPECT_NE(nullptr, getenv(key));
72  auto value = std::string{getenv(key)};
73  Optional<EnvVarSaver> pSaver2;
74  pSaver2.emplace(std::move(*pSaver1));
75  pSaver1.clear();
76  PCHECK(0 == setenv(key, "blah", true));
77  EXPECT_STREQ("blah", getenv(key));
78  pSaver2.clear();
79  EXPECT_EQ(value, getenv(key));
80 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:1995
static const char *const value
Definition: Conv.cpp:50
Value & emplace(Args &&...args)
Definition: Optional.h:231
const char * string
Definition: Conv.cpp:212
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
void clear() noexcept
Definition: Optional.h:251
TEST ( EnvironmentStateTest  ,
FailOnEmptyString   
)

Definition at line 82 of file EnvUtilTest.cpp.

References EXPECT_THROW.

82  {
83  EnvVarSaver saver{};
84  char test[4] = "A=B";
85  PCHECK(0 == putenv(test));
86  auto okState = EnvironmentState::fromCurrentEnvironment();
87  test[0] = 0;
89  EnvironmentState::fromCurrentEnvironment(), MalformedEnvironment);
90 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
TEST ( EnvironmentStateTest  ,
MovableAndCopyable   
)

Definition at line 92 of file EnvUtilTest.cpp.

References folly::empty(), EXPECT_EQ, and folly::gen::move.

92  {
93  auto initialState = EnvironmentState::fromCurrentEnvironment();
94  auto copiedState1 = EnvironmentState::empty();
95  copiedState1.operator=(initialState);
96  EnvironmentState copiedState2{initialState};
97  EXPECT_EQ(*initialState, *copiedState1);
98  EXPECT_EQ(*initialState, *copiedState2);
99  (*initialState)["foo"] = "bar";
100  EXPECT_EQ(0, copiedState1->count("foo"));
101  EXPECT_EQ(0, copiedState2->count("foo"));
102  auto movedState1 = EnvironmentState::empty();
103  movedState1.operator=(std::move(copiedState1));
104  EnvironmentState movedState2{std::move(copiedState2)};
105  EXPECT_EQ(0, movedState1->count("foo"));
106  EXPECT_EQ(0, movedState2->count("foo"));
107  initialState->erase("foo");
108  EXPECT_EQ(*initialState, *movedState1);
109  EXPECT_EQ(*initialState, *movedState2);
110 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
constexpr auto empty(C const &c) -> decltype(c.empty())
Definition: Access.h:55
TEST ( EnvironmentStateTest  ,
FailOnDuplicate   
)

Definition at line 112 of file EnvUtilTest.cpp.

References EXPECT_THROW.

112  {
113  EnvVarSaver saver{};
114  char test[7] = "PATG=B";
115  PCHECK(0 == putenv(test));
116  auto okState = EnvironmentState::fromCurrentEnvironment();
117  test[3] = 'H';
118  EXPECT_THROW(
119  EnvironmentState::fromCurrentEnvironment(), MalformedEnvironment);
120 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
TEST ( EnvironmentStateTest  ,
Separation   
)

Definition at line 122 of file EnvUtilTest.cpp.

References EXPECT_EQ, and EXPECT_STREQ.

122  {
123  EnvVarSaver saver{};
124  auto initialState = EnvironmentState::fromCurrentEnvironment();
125  PCHECK(0 == setenv("spork", "foon", true));
126  auto updatedState = EnvironmentState::fromCurrentEnvironment();
127  EXPECT_EQ(0, initialState->count("spork"));
128  EXPECT_EQ(1, updatedState->count("spork"));
129  EXPECT_EQ("foon", (*updatedState)["spork"]);
130  updatedState->erase("spork");
131  EXPECT_EQ(0, updatedState->count("spork"));
132  EXPECT_STREQ("foon", getenv("spork"));
133 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:1995
TEST ( EnvironmentStateTest  ,
Update   
)

Definition at line 135 of file EnvUtilTest.cpp.

References EXPECT_EQ, and EXPECT_STREQ.

135  {
136  EnvVarSaver saver{};
137  auto env = EnvironmentState::fromCurrentEnvironment();
138  EXPECT_EQ(nullptr, getenv("spork"));
139  (*env)["spork"] = "foon";
140  EXPECT_EQ(nullptr, getenv("spork"));
141  env.setAsCurrentEnvironment();
142  EXPECT_STREQ("foon", getenv("spork"));
143 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:1995
TEST ( EnvironmentStateTest  ,
forSubprocess   
)

Definition at line 145 of file EnvUtilTest.cpp.

References folly::empty(), and EXPECT_EQ.

145  {
146  auto env = EnvironmentState::empty();
147  (*env)["spork"] = "foon";
148  std::vector<std::string> expected = {"spork=foon"};
149  auto vec = env.toVector();
150  EXPECT_EQ(expected, vec);
151  Subprocess subProcess{{fLS::FLAGS_env_util_subprocess_binary},
152  {},
153  fLS::FLAGS_env_util_subprocess_binary.c_str(),
154  &vec};
155  EXPECT_EQ(0, subProcess.wait().exitStatus());
156 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr auto empty(C const &c) -> decltype(c.empty())
Definition: Access.h:55
Definition: Traits.h:588
TEST ( EnvironmentStateTest  ,
forC   
)

Definition at line 158 of file EnvUtilTest.cpp.

References folly::empty(), EXPECT_EQ, and EXPECT_STREQ.

158  {
159  auto env = EnvironmentState::empty();
160  (*env)["spork"] = "foon";
161  EXPECT_STREQ("spork=foon", env.toPointerArray().get()[0]);
162  EXPECT_EQ(nullptr, env.toPointerArray().get()[1]);
163  char const* program = fLS::FLAGS_env_util_subprocess_binary.c_str();
164  pid_t pid;
165  PCHECK(
166  0 ==
167  posix_spawn(
168  &pid,
169  program,
170  nullptr,
171  nullptr,
172  nullptr,
173  env.toPointerArray().get()));
174  int result;
175  PCHECK(pid == waitpid(pid, &result, 0));
176  EXPECT_EQ(0, result);
177 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:1995
constexpr auto empty(C const &c) -> decltype(c.empty())
Definition: Access.h:55
TEST ( EnvVarSaverTest  ,
ExampleDeleting   
)

Definition at line 179 of file EnvUtilTest.cpp.

References EXPECT_EQ, EXPECT_NE, EXPECT_TRUE, string, and folly::value().

179  {
180  auto key = "PATH";
181  EXPECT_NE(nullptr, getenv(key));
182  auto value = std::string{getenv(key)};
183 
184  auto saver = std::make_unique<EnvVarSaver>();
185  PCHECK(0 == unsetenv(key));
186  EXPECT_EQ(nullptr, getenv(key));
187  saver = nullptr;
188  EXPECT_TRUE(value == getenv(key));
189 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static const char *const value
Definition: Conv.cpp:50
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
const char * string
Definition: Conv.cpp:212
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926