aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.posix/test_getenv.cc
blob: fdb5277fee990583d3b4ed139daf21380e65ea38 (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
/*
 * This source code is released into the public domain.
 */

#include <ranges>
#include <string>
#include <system_error>

#include <unistd.h>

#include <catch2/catch_test_macros.hpp>

import nihil.posix;

TEST_CASE("getenv: existing value", "[getenv]")
{
	auto constexpr *name = "NIHIL_TEST_VAR";
	auto constexpr *value = "test is a test";

	REQUIRE(::setenv(name, value, 1) == 0);

	auto const s = nihil::getenv(name);
	REQUIRE(s);
	REQUIRE(*s == value);
}

TEST_CASE("getenv: non-existing value", "[getenv]")
{
	auto constexpr *name = "NIHIL_TEST_VAR";

	REQUIRE(::unsetenv(name) == 0);

	auto const s = nihil::getenv(name);
	REQUIRE(!s);
	REQUIRE(s.error() == std::errc::no_such_file_or_directory);
}

// Force the call to getenv_r() to reallocate.
TEST_CASE("getenv: long value")
{
	auto constexpr *name = "NIHIL_TEST_VAR";
	auto const value = std::string(4096, 'a');

	REQUIRE(::setenv(name, value.c_str(), 1) == 0);

	auto const s = nihil::getenv(name);
	REQUIRE(s);
	REQUIRE(*s == value);
}