diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/testing/common/scope.h | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/testing/common/scope.h')
-rw-r--r-- | library/cpp/testing/common/scope.h | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/library/cpp/testing/common/scope.h b/library/cpp/testing/common/scope.h new file mode 100644 index 0000000000..a2ca0e77e4 --- /dev/null +++ b/library/cpp/testing/common/scope.h @@ -0,0 +1,39 @@ +#pragma once + +#include <util/generic/string.h> +#include <util/generic/vector.h> +#include <util/system/env.h> + +#include <utility> + +namespace NTesting { + // @brief Assigns new values to the given environment variables and restores old values upon destruction. + // @note if there was no env variable with given name, it will be set to empty string upon destruction IGNIETFERRO-1486 + struct TScopedEnvironment { + TScopedEnvironment(const TString& name, const TString& value) + : PreviousState{1, {name, ::GetEnv(name)}} + { + ::SetEnv(name, value); + } + + TScopedEnvironment(const TVector<std::pair<TString, TString>>& vars) + : PreviousState(Reserve(vars.size())) + { + for (const auto& [k, v] : vars) { + PreviousState.emplace_back(k, ::GetEnv(k)); + ::SetEnv(k, v); + } + } + + ~TScopedEnvironment() { + for (const auto& [k, v] : PreviousState) { + ::SetEnv(k, v); + } + } + + TScopedEnvironment(const TScopedEnvironment&) = delete; + TScopedEnvironment& operator=(const TScopedEnvironment&) = delete; + private: + TVector<std::pair<TString, TString>> PreviousState; + }; +} |