1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
#include "test-helpers.h"
#include "3rd-party/catch.hpp"
TEST_CASE("EnvVar object restores the environment variable to its original "
"state when the object is destroyed",
"[test-helpers]")
{
const char var[] = "nEwSb0a7-tEsT-eNvIroNm3Nt-v4rIabLe";
{
INFO("Checking that noone else is using that environment variable");
REQUIRE(::getenv(var) == nullptr);
}
const auto expected = std::string("let's try this out, shall we?");
const auto overwrite = true;
::setenv(var, expected.c_str(), overwrite);
REQUIRE(expected == ::getenv(var));
{
TestHelpers::EnvVar envVar(var);
REQUIRE(expected == ::getenv(var));
const auto newValue = std::string("totally new value");
::setenv(var, newValue.c_str(), overwrite);
REQUIRE_FALSE(expected == ::getenv(var));
}
REQUIRE(expected == ::getenv(var));
::unsetenv(var);
}
TEST_CASE("EnvVar::set() changes the current state of the environment variable",
"[test-helpers]")
{
const char var[] = "nEwSb0a7-tEsT-eNvIroNm3Nt-v4rIabLe";
{
INFO("Checking that noone else is using that environment variable");
REQUIRE(::getenv(var) == nullptr);
}
const auto expected = std::string("let's try this out, shall we?");
TestHelpers::EnvVar envVar(var);
envVar.set("absolutely new value");
REQUIRE_FALSE(expected == ::getenv(var));
envVar.set(expected);
REQUIRE(expected == ::getenv(var));
}
TEST_CASE("EnvVar::set() doesn't change the value to which the environment "
"variable is restored",
"[test-helpers]")
{
const char var[] = "nEwSb0a7-tEsT-eNvIroNm3Nt-v4rIabLe";
{
INFO("Checking that noone else is using that environment variable");
REQUIRE(::getenv(var) == nullptr);
}
const auto expected = std::string("let's try this out, shall we?");
const auto overwrite = true;
::setenv(var, expected.c_str(), overwrite);
REQUIRE(expected == ::getenv(var));
{
TestHelpers::EnvVar envVar(var);
envVar.set("some new value");
}
REQUIRE(expected == ::getenv(var));
::unsetenv(var);
}
TEST_CASE("EnvVar::set() runs a function (set by on_change()) after changing "
"the environment variable",
"[test-helpers]")
{
const char var[] = "nEwSb0a7-tEsT-eNvIroNm3Nt-v4rIabLe";
{
INFO("Checking that noone else is using that environment variable");
REQUIRE(::getenv(var) == nullptr);
}
const auto expected = std::string("let's try this out, shall we?");
const auto overwrite = true;
::setenv(var, expected.c_str(), overwrite);
SECTION("The function is ran *after* the change") {
// It's important to declare `newValue` before declaring `envVar`,
// because the string will be used in `on_change` function which is
// also ran on `envVar` destruction. If we declare `newValue` after
// `envVar`, it will be destroyed *before* `envVar`, and `on_change`
// function will try to access an already-freed memory.
const auto newValue = std::string("totally new value here");
auto valueChanged = false;
TestHelpers::EnvVar envVar(var);
envVar.on_change([&valueChanged, &newValue, &var](){
valueChanged = newValue == ::getenv(var);
});
envVar.set(newValue);
REQUIRE(valueChanged);
}
SECTION("The function is ran *once* per change") {
auto counter = unsigned{};
TestHelpers::EnvVar envVar(var);
envVar.on_change([&counter](){ counter++; });
REQUIRE(counter == 0);
envVar.set("new value");
REQUIRE(counter == 1);
envVar.set("some other value");
REQUIRE(counter == 2);
INFO("Same value as before, but function should still be called");
envVar.set("some other value");
REQUIRE(counter == 3);
}
::unsetenv(var);
}
TEST_CASE("EnvVar::unset() completely removes the variable from the environment",
"[test-helpers]")
{
const char var[] = "nEwSb0a7-tEsT-eNvIroNm3Nt-v4rIabLe";
{
INFO("Checking that noone else is using that environment variable");
REQUIRE(::getenv(var) == nullptr);
}
const auto expected = std::string("let's try this out, shall we?");
const auto overwrite = true;
::setenv(var, expected.c_str(), overwrite);
char* value = ::getenv(var);
REQUIRE_FALSE(value == nullptr);
REQUIRE(expected == value);
value = nullptr;
{
TestHelpers::EnvVar envVar(var);
value = ::getenv(var);
REQUIRE_FALSE(value == nullptr);
REQUIRE(expected == value);
value = nullptr;
envVar.unset();
value = ::getenv(var);
REQUIRE(value == nullptr);
value = nullptr;
}
::unsetenv(var);
}
TEST_CASE("EnvVar::unset() doesn't change the value to which the environment "
"variable is restored",
"[test-helpers]")
{
const char var[] = "nEwSb0a7-tEsT-eNvIroNm3Nt-v4rIabLe";
{
INFO("Checking that noone else is using that environment variable");
REQUIRE(::getenv(var) == nullptr);
}
const auto expected = std::string("let's try this out, shall we?");
const auto overwrite = true;
::setenv(var, expected.c_str(), overwrite);
char* value = ::getenv(var);
REQUIRE_FALSE(value == nullptr);
REQUIRE(expected == value);
value = nullptr;
{
TestHelpers::EnvVar envVar(var);
value = ::getenv(var);
REQUIRE_FALSE(value == nullptr);
REQUIRE(expected == value);
value = nullptr;
envVar.unset();
value = ::getenv(var);
REQUIRE(value == nullptr);
value = nullptr;
}
value = ::getenv(var);
REQUIRE_FALSE(value == nullptr);
REQUIRE(expected == value);
value = nullptr;
::unsetenv(var);
}
TEST_CASE("EnvVar::unset() runs a function (set by on_change()) after changing "
"the environment variable",
"[test-helpers]")
{
const char var[] = "nEwSb0a7-tEsT-eNvIroNm3Nt-v4rIabLe";
{
INFO("Checking that noone else is using that environment variable");
REQUIRE(::getenv(var) == nullptr);
}
const auto expected = std::string("let's try this out, shall we?");
const auto overwrite = true;
::setenv(var, expected.c_str(), overwrite);
char* value = ::getenv(var);
REQUIRE_FALSE(value == nullptr);
REQUIRE(expected == value);
value = nullptr;
{
auto counter = unsigned{};
auto as_expected = false;
TestHelpers::EnvVar envVar(var);
envVar.on_change([&counter, &as_expected, var]() {
as_expected = nullptr == ::getenv(var);
counter++;
});
value = ::getenv(var);
REQUIRE_FALSE(value == nullptr);
REQUIRE(expected == value);
value = nullptr;
envVar.unset();
REQUIRE(as_expected);
REQUIRE(counter == 1);
}
::unsetenv(var);
}
TEST_CASE("EnvVar's destructor runs a function (set by on_change()) after "
"restoring the varibale to its original state",
"[test-helpers]")
{
const char var[] = "nEwSb0a7-tEsT-eNvIroNm3Nt-v4rIabLe";
{
INFO("Checking that noone else is using that environment variable");
REQUIRE(::getenv(var) == nullptr);
}
const auto expected = std::string("let's try this out, shall we?");
SECTION("Variable wasn't even set") {
// Intentionally left empty.
//
// Catch framework runs each test case multiple times, each time
// entering some section it hasn't entered before. In our situation
// here, the test case will be ran twice: once with setenv() being
// called, and once without. As a result, EnvVar below will be created
// in two different situations: once when the environment variable is
// already present, and once when it's absent. The point of the test is
// that this wouldn't matter: EnvVar will behave the same regardless.
}
SECTION("Variable was set before EnvVar is created") {
const auto overwrite = true;
::setenv(var, expected.c_str(), overwrite);
}
auto counter = unsigned{};
{
TestHelpers::EnvVar envVar(var);
envVar.on_change([&counter](){ counter++; });
}
REQUIRE(counter == 1);
// It's a no-op if the variable is absent from the environment, so we don't
// need to put this inside a conditional to match SECTIONs above.
::unsetenv(var);
}
|