summaryrefslogtreecommitdiff
path: root/test/testenv.cc
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2026-01-02 22:42:31 +0100
committerJoel Klinghed <the_jk@spawned.biz>2026-01-02 22:42:31 +0100
commit6ed8f5151719fbc14ec0ac6d28a346d1f74cf2ca (patch)
treeebe7588e89e1aa2ae5376acf85f3a3a7b2ec7e10 /test/testenv.cc
Initial commitHEADmain
Diffstat (limited to 'test/testenv.cc')
-rw-r--r--test/testenv.cc44
1 files changed, 44 insertions, 0 deletions
diff --git a/test/testenv.cc b/test/testenv.cc
new file mode 100644
index 0000000..56701a4
--- /dev/null
+++ b/test/testenv.cc
@@ -0,0 +1,44 @@
+#include "testenv.hh"
+
+#include <cstdlib>
+#include <optional>
+#include <string>
+
+void TestEnv::setenv(std::string const& name, std::string const& value) {
+ saveenv(name);
+
+ // NOLINTNEXTLINE(misc-include-cleaner)
+ ::setenv(name.c_str(), value.c_str(), 1);
+}
+
+void TestEnv::unsetenv(std::string const& name) {
+ saveenv(name);
+
+ // NOLINTNEXTLINE(misc-include-cleaner)
+ ::unsetenv(name.c_str());
+}
+
+void TestEnv::TearDown() {
+ for (auto const& pair : env_) {
+ if (pair.second.has_value()) {
+ // NOLINTNEXTLINE(misc-include-cleaner)
+ ::setenv(pair.first.c_str(), pair.second->c_str(), 1);
+ } else {
+ // NOLINTNEXTLINE(misc-include-cleaner)
+ ::unsetenv(pair.first.c_str());
+ }
+ }
+}
+
+void TestEnv::saveenv(std::string const& name) {
+ auto it = env_.find(name);
+ if (it != env_.end())
+ return;
+
+ auto* str = getenv(name.c_str());
+ if (str == nullptr) {
+ env_.emplace(name, std::nullopt);
+ } else {
+ env_.emplace(name, str);
+ }
+}