diff options
| author | YDBot <[email protected]> | 2025-11-03 00:52:38 +0000 |
|---|---|---|
| committer | YDBot <[email protected]> | 2025-11-03 00:52:38 +0000 |
| commit | 2d490611a0e3097c111366eb9ea9e74d0a576209 (patch) | |
| tree | f7ab5825bb803bad02cf9acb6a63a3ed2ebcf042 /library/cpp | |
| parent | a3150c48bbea4bec90b1927434917736d248b4c9 (diff) | |
| parent | 73672db4e0da266a2d32be8e83983f1b556eb68d (diff) | |
Sync branches 251103-0051
Diffstat (limited to 'library/cpp')
| -rw-r--r-- | library/cpp/yt/system/env.cpp | 42 | ||||
| -rw-r--r-- | library/cpp/yt/system/env.h | 22 | ||||
| -rw-r--r-- | library/cpp/yt/system/thread_id.h | 2 | ||||
| -rw-r--r-- | library/cpp/yt/system/unittests/env_ut.cpp | 52 | ||||
| -rw-r--r-- | library/cpp/yt/system/unittests/ya.make | 16 | ||||
| -rw-r--r-- | library/cpp/yt/system/ya.make | 5 |
6 files changed, 138 insertions, 1 deletions
diff --git a/library/cpp/yt/system/env.cpp b/library/cpp/yt/system/env.cpp new file mode 100644 index 00000000000..ff5eaeb8545 --- /dev/null +++ b/library/cpp/yt/system/env.cpp @@ -0,0 +1,42 @@ +#include "env.h" + +#include <util/system/platform.h> + +#ifdef _darwin_ + #include <crt_externs.h> + #define environ (*_NSGetEnviron()) +#endif + +#ifdef _linux_ + #include <unistd.h> +#endif + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +#if defined(_linux_) || defined(_darwin_) + +std::vector<std::string> GetEnvironNameValuePairs() +{ + std::vector<std::string> result; + for (char** envIt = environ; *envIt; ++envIt) { + result.emplace_back(*envIt); + } + return result; +} + +#endif + +std::pair<TStringBuf, std::optional<TStringBuf>> ParseEnvironNameValuePair(TStringBuf pair) +{ + if (auto pos = pair.find('='); pos != std::string::npos) { + return {pair.substr(0, pos), pair.substr(pos + 1)}; + } else { + return {pair, std::nullopt}; + } +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT::NThreading diff --git a/library/cpp/yt/system/env.h b/library/cpp/yt/system/env.h new file mode 100644 index 00000000000..77bc933a8ec --- /dev/null +++ b/library/cpp/yt/system/env.h @@ -0,0 +1,22 @@ +#pragma once + +#include <util/generic/strbuf.h> + +#include <optional> +#include <string> +#include <vector> + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +// NB: Currently not supported on other platforms. +#if defined(_linux_) || defined(_darwin_) +std::vector<std::string> GetEnvironNameValuePairs(); +#endif + +std::pair<TStringBuf, std::optional<TStringBuf>> ParseEnvironNameValuePair(TStringBuf pair); + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT diff --git a/library/cpp/yt/system/thread_id.h b/library/cpp/yt/system/thread_id.h index 5dc06925c0b..1f75067015d 100644 --- a/library/cpp/yt/system/thread_id.h +++ b/library/cpp/yt/system/thread_id.h @@ -18,7 +18,7 @@ TSequentialThreadId GetSequentialThreadId(); //////////////////////////////////////////////////////////////////////////////// -} // namespace NYT::NThreading +} // namespace NYT #define THREAD_ID_INL_H_ #include "thread_id-inl.h" diff --git a/library/cpp/yt/system/unittests/env_ut.cpp b/library/cpp/yt/system/unittests/env_ut.cpp new file mode 100644 index 00000000000..21183a12390 --- /dev/null +++ b/library/cpp/yt/system/unittests/env_ut.cpp @@ -0,0 +1,52 @@ +#include <library/cpp/testing/gtest/gtest.h> + +#include <library/cpp/yt/system/env.h> + +#include <library/cpp/yt/misc/guid.h> + +#include <library/cpp/yt/string/format.h> + +#include <util/system/env.h> + +namespace NYT { +namespace { + +//////////////////////////////////////////////////////////////////////////////// + +TEST(TParseEnvironNameValuePairTest, NonNull) +{ + auto pair = ParseEnvironNameValuePair("var=value"); + EXPECT_EQ(pair.first, "var"); + EXPECT_EQ(pair.second, "value"); +} + +TEST(TParseEnvironNameValuePairTest, Null) +{ + auto pair = ParseEnvironNameValuePair("some"); + EXPECT_EQ(pair.first, "some"); + EXPECT_EQ(pair.second, std::nullopt); +} + +//////////////////////////////////////////////////////////////////////////////// + +#if defined(_linux_) || defined(_darwin_) + +TEST(TGetEnvironNameValuePairsTest, Simple) +{ + auto key = Format("var%v", TGuid::Create()); + auto value = TString("value"); + auto pair = Format("%v=%v", key, value); + SetEnv(key, value); + auto pairs = GetEnvironNameValuePairs(); + EXPECT_TRUE(std::ranges::find(pairs, pair) != pairs.end()); + UnsetEnv(key); +} + +#endif + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace +} // namespace NYT + + diff --git a/library/cpp/yt/system/unittests/ya.make b/library/cpp/yt/system/unittests/ya.make new file mode 100644 index 00000000000..e3f0849b4d3 --- /dev/null +++ b/library/cpp/yt/system/unittests/ya.make @@ -0,0 +1,16 @@ +GTEST() + +INCLUDE(${ARCADIA_ROOT}/library/cpp/yt/ya_cpp.make.inc) + +SRCS( + env_ut.cpp +) + +PEERDIR( + library/cpp/yt/misc + library/cpp/yt/string + library/cpp/yt/system + library/cpp/testing/gtest +) + +END() diff --git a/library/cpp/yt/system/ya.make b/library/cpp/yt/system/ya.make index c60a940dbf2..eb8509ce4f4 100644 --- a/library/cpp/yt/system/ya.make +++ b/library/cpp/yt/system/ya.make @@ -3,6 +3,7 @@ LIBRARY() INCLUDE(${ARCADIA_ROOT}/library/cpp/yt/ya_cpp.make.inc) SRCS( + env.cpp exit.cpp thread_id.cpp ) @@ -12,3 +13,7 @@ PEERDIR( ) END() + +RECURSE_FOR_TESTS( + unittests +) |
