Skip to content

Commit 7759809

Browse files
committed
Add convenient 'GetEnv' and 'SetEnv' implementations back (#91)
It's almost a copy of 'util/system/env.h' (removed now by PR #117). Only now we're using std::getenv with some nullptr-check instead of the old GetEnv. Since this testing library actively uses these functions (the rest of the whole library hardly uses them), it seems reasonable to have them as a part of 'cpp-testing-common' library.
1 parent cd57ef9 commit 7759809

File tree

7 files changed

+83
-0
lines changed

7 files changed

+83
-0
lines changed

library/cpp/testing/common/CMakeLists.darwin-arm64.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ target_link_libraries(cpp-testing-common PUBLIC
1515
)
1616
target_sources(cpp-testing-common PRIVATE
1717
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/env.cpp
18+
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/env_var.cpp
1819
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/network.cpp
1920
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/probe.cpp
2021
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/scope.cpp

library/cpp/testing/common/CMakeLists.darwin-x86_64.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ target_link_libraries(cpp-testing-common PUBLIC
1515
)
1616
target_sources(cpp-testing-common PRIVATE
1717
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/env.cpp
18+
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/env_var.cpp
1819
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/network.cpp
1920
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/probe.cpp
2021
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/scope.cpp

library/cpp/testing/common/CMakeLists.linux-aarch64.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ target_link_libraries(cpp-testing-common PUBLIC
1515
)
1616
target_sources(cpp-testing-common PRIVATE
1717
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/env.cpp
18+
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/env_var.cpp
1819
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/network.cpp
1920
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/probe.cpp
2021
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/scope.cpp

library/cpp/testing/common/CMakeLists.linux-x86_64.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ target_link_libraries(cpp-testing-common PUBLIC
1515
)
1616
target_sources(cpp-testing-common PRIVATE
1717
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/env.cpp
18+
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/env_var.cpp
1819
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/network.cpp
1920
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/probe.cpp
2021
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/scope.cpp

library/cpp/testing/common/CMakeLists.windows-x86_64.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ target_link_libraries(cpp-testing-common PUBLIC
1515
)
1616
target_sources(cpp-testing-common PRIVATE
1717
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/env.cpp
18+
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/env_var.cpp
1819
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/network.cpp
1920
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/probe.cpp
2021
${CMAKE_SOURCE_DIR}/library/cpp/testing/common/scope.cpp
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "env_var.h"
2+
3+
#include <util/generic/yexception.h>
4+
5+
#include <util/system/platform.h>
6+
7+
#if defined(_win_)
8+
#include <util/system/wininit.h>
9+
#else
10+
#include <cerrno>
11+
#endif
12+
13+
#include <cstdlib>
14+
15+
namespace NUtils {
16+
17+
std::string GetEnv(std::string_view key, std::string_view def) {
18+
const char* str_ptr = std::getenv(key.data());
19+
return std::string(str_ptr == nullptr ? def : str_ptr);
20+
}
21+
22+
void SetEnv(std::string_view key, std::string_view new_value) {
23+
bool is_ok = false;
24+
int error_code = 0;
25+
#if defined(_win_)
26+
is_ok = SetEnvironmentVariable(key.data(), new_value.data()) == 0;
27+
if (!is_ok) {
28+
error_code = GetLastError();
29+
}
30+
#else
31+
is_ok = setenv(key.data(), new_value.data(), /* overwrite */ true) == 0;
32+
if (!is_ok) {
33+
error_code = errno;
34+
}
35+
#endif
36+
Y_ENSURE_EX(
37+
is_ok,
38+
TSystemError() << "failed to SetEnv with error-code " << error_code
39+
);
40+
}
41+
42+
} // namespace NUtils

library/cpp/testing/common/env_var.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <string_view>
5+
6+
namespace NUtils {
7+
8+
/**
9+
* Search the environment list provided by the host environment for associated variable.
10+
*
11+
* @param key String identifying the name of the environmental variable to look for
12+
* @param def String that returns if environmental variable not found by key
13+
*
14+
* @return String that is associated with the matched environment variable or empty string
15+
* if such variable is missing.
16+
*
17+
* @note Use it only in pair with `SetEnv` as there may be inconsistency in their
18+
* behaviour otherwise.
19+
* @note Calls to `GetEnv` and `SetEnv` from different threads must be synchronized.
20+
* @see SetEnv
21+
*/
22+
std::string GetEnv(std::string_view key, std::string_view def = "");
23+
24+
/**
25+
* Add or change environment variable provided by the host environment.
26+
*
27+
* @param key String identifying the name of the environment variable to set or change
28+
* @param new_value Value to assign
29+
* @note Use it only in pair with `GetEnv` as there may be inconsistency in their
30+
* behaviour otherwise.
31+
* @note Calls to `GetEnv` and `SetEnv` from different threads must be synchronized.
32+
* @see GetEnv
33+
*/
34+
void SetEnv(std::string_view key, std::string_view new_value);
35+
36+
} // namespace NUtils

0 commit comments

Comments
 (0)