diff options
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); + } +} |
