summaryrefslogtreecommitdiff
path: root/test/test_helpers/envvar.cpp
blob: 7d44b4864772f45b7bcc29c72a66ace96d45ee3c (plain) (blame)
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include "envvar.h"

#include "3rd-party/catch.hpp"

test_helpers::EnvVar::EnvVar(std::string name_)
	: EnvVar(name_, true)
{
	if (name_ == "TZ") {
		throw std::invalid_argument("Using EnvVar(\"TZ\") is discouraged. Try test_helpers::TzEnvVar instead.");
	} else if (name_ == "LC_CTYPE") {
		throw std::invalid_argument("Using EnvVar(\"LC_CTYPE\") is discouraged. Try test_helpers::LcCtypeEnvVar instead.");
	}
}

test_helpers::EnvVar::EnvVar(std::string name_, bool /* unused */)
	: name(std::move(name_))
{
	const char* original = ::getenv(name.c_str());
	was_set = original != nullptr;
	if (was_set) {
		value = std::string(original);
	}
}

test_helpers::EnvVar::~EnvVar()
{
	if (was_set) {
		set(value);
	} else {
		unset();
	}
}

void test_helpers::EnvVar::set(const std::string& new_value) const
{
	const auto overwrite = true;
	::setenv(name.c_str(), new_value.c_str(), overwrite);
	if (on_change_fn) {
		on_change_fn(new_value);
	}
}

void test_helpers::EnvVar::unset() const
{
	::unsetenv(name.c_str());
	if (on_change_fn) {
		on_change_fn(nonstd::nullopt);
	}
}

void test_helpers::EnvVar::on_change(
	std::function<void(nonstd::optional<std::string>)> fn)
{
	on_change_fn = std::move(fn);
}

test_helpers::TzEnvVar::TzEnvVar()
	: EnvVar("TZ", true)
{
	on_change([](nonstd::optional<std::string>) {
		::tzset();
	});
}

test_helpers::LcCtypeEnvVar::LcCtypeEnvVar()
	: EnvVar("LC_CTYPE", true)
{
	on_change([](nonstd::optional<std::string> new_charset) {
		if (new_charset.has_value()) {
			::setlocale(LC_CTYPE, new_charset.value().c_str());
		} else {
			::setlocale(LC_CTYPE, "");
		}
	});
}


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;

	REQUIRE(::setenv(var, expected.c_str(), overwrite) == 0);
	REQUIRE(expected == ::getenv(var));

	{
		test_helpers::EnvVar envVar(var);
		REQUIRE(expected == ::getenv(var));

		const auto newValue = std::string("totally new value");
		REQUIRE(::setenv(var, newValue.c_str(), overwrite) == 0);

		REQUIRE_FALSE(expected == ::getenv(var));
	}

	REQUIRE(expected == ::getenv(var));

	REQUIRE(::unsetenv(var) == 0);
}

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?");

	test_helpers::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;
	REQUIRE(::setenv(var, expected.c_str(), overwrite) == 0);
	REQUIRE(expected == ::getenv(var));

	{
		test_helpers::EnvVar envVar(var);
		envVar.set("some new value");
	}

	REQUIRE(expected == ::getenv(var));

	REQUIRE(::unsetenv(var) == 0);
}

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;
	REQUIRE(::setenv(var, expected.c_str(), overwrite) == 0);

	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;

		test_helpers::EnvVar envVar(var);
		envVar.on_change([&valueChanged,
		&var](nonstd::optional<std::string> new_value) {
			valueChanged = new_value.has_value() && (new_value.value() == ::getenv(var));
		});

		envVar.set(newValue);

		REQUIRE(valueChanged);
	}

	SECTION("The function is ran *once* per change") {
		auto counter = unsigned{};

		test_helpers::EnvVar envVar(var);
		envVar.on_change([&counter](nonstd::optional<std::string>) {
			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);
	}

	SECTION("Function is passed a non-empty `optional`") {
		const auto expected_new_value = std::string("test sentry");
		auto checks_ok = false;

		test_helpers::EnvVar envVar(var);
		envVar.on_change([&expected_new_value,
		&checks_ok](nonstd::optional<std::string> new_value) {
			checks_ok = new_value.has_value() && (new_value.value() == expected_new_value);
		});

		envVar.set(expected_new_value);

		REQUIRE(checks_ok);
	}

	REQUIRE(::unsetenv(var) == 0);
}

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;
	REQUIRE(::setenv(var, expected.c_str(), overwrite) == 0);

	char* value = ::getenv(var);
	REQUIRE_FALSE(value == nullptr);
	REQUIRE(expected == value);
	value = nullptr;

	{
		test_helpers::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;
	}

	REQUIRE(::unsetenv(var) == 0);
}

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;
	REQUIRE(::setenv(var, expected.c_str(), overwrite) == 0);

	char* value = ::getenv(var);
	REQUIRE_FALSE(value == nullptr);
	REQUIRE(expected == value);
	value = nullptr;

	{
		test_helpers::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;

	REQUIRE(::unsetenv(var) == 0);
}

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;
	REQUIRE(::setenv(var, expected.c_str(), overwrite) == 0);

	SECTION("The function is ran *after* the change") {
		auto value_unset = false;

		test_helpers::EnvVar envVar(var);
		envVar.on_change([&value_unset, &var](nonstd::optional<std::string> new_value) {
			value_unset = !new_value.has_value() && (nullptr == ::getenv(var));
		});

		envVar.unset();

		REQUIRE(value_unset);
	}

	SECTION("The function is run *once* per change") {
		auto counter = unsigned{};

		test_helpers::EnvVar envVar(var);
		envVar.on_change([&counter](nonstd::optional<std::string>) {
			counter++;
		});

		envVar.unset();

		REQUIRE(counter == 1);

		envVar.unset();

		REQUIRE(counter == 2);
	}

	SECTION("Function is passed `nullopt`") {
		auto checks_ok = false;

		test_helpers::EnvVar envVar(var);
		envVar.on_change([&checks_ok](nonstd::optional<std::string> new_value) {
			checks_ok = !new_value.has_value();
		});

		envVar.unset();

		REQUIRE(checks_ok);
	}


	REQUIRE(::unsetenv(var) == 0);
}

TEST_CASE("EnvVar's destructor runs a function (set by on_change()) after "
	"restoring the variable 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?");

	const auto check = [&]() {
		auto counter = unsigned{};

		{
			test_helpers::EnvVar envVar(var);
			envVar.on_change([&counter](nonstd::optional<std::string>) {
				counter++;
			});
		}

		REQUIRE(counter == 1);
	};

	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.

		check();
	}

	SECTION("Variable was set before EnvVar is created") {
		const auto overwrite = true;
		REQUIRE(::setenv(var, expected.c_str(), overwrite) == 0);

		check();
	}

	// 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.
	REQUIRE(::unsetenv(var) == 0);
}

TEST_CASE("EnvVar can't be constructed for TZ variable", "[test_helpers]")
{
	REQUIRE_THROWS_AS(test_helpers::EnvVar("TZ"), std::invalid_argument);
}

TEST_CASE("EnvVar can't be constructed for LC_CTYPE variable", "[test_helpers]")
{
	REQUIRE_THROWS_AS(test_helpers::EnvVar("LC_CTYPE"), std::invalid_argument);
}