aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.posix/test.getenv.cc
diff options
context:
space:
mode:
authorLexi Winter <lexi@le-fay.org>2025-06-28 20:40:25 +0100
committerLexi Winter <lexi@le-fay.org>2025-06-28 20:40:25 +0100
commitc54ff48ac3abb62a40eb1a438da8e3e7ef139797 (patch)
tree4a78c556c7cbc6d3d7e364ca0c52c57ac0f5094b /nihil.posix/test.getenv.cc
parenta2d7181700ac64b8e7a4472ec26dfa253b38f188 (diff)
downloadnihil-c54ff48ac3abb62a40eb1a438da8e3e7ef139797.tar.gz
nihil-c54ff48ac3abb62a40eb1a438da8e3e7ef139797.tar.bz2
posix: add tempfile()
Diffstat (limited to 'nihil.posix/test.getenv.cc')
-rw-r--r--nihil.posix/test.getenv.cc49
1 files changed, 49 insertions, 0 deletions
diff --git a/nihil.posix/test.getenv.cc b/nihil.posix/test.getenv.cc
new file mode 100644
index 0000000..fdb5277
--- /dev/null
+++ b/nihil.posix/test.getenv.cc
@@ -0,0 +1,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);
+}