diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2025-10-07 19:58:28 +0200 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2025-10-19 00:13:47 +0200 |
| commit | 4f6e76493fb74f5385d5a14dce3a01c9901802ed (patch) | |
| tree | a38722ec832fd44ad34257730e075e8b07825bd0 /test/testenv.cc | |
| parent | c87f9627efc8b612eb9b000acfcc6731cad15765 (diff) | |
paths: New module
Path utilities (doh)
Diffstat (limited to 'test/testenv.cc')
| -rw-r--r-- | test/testenv.cc | 44 |
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); + } +} |
