From c0aa5b32fc38d907dda200b136e327d949eb3d5e Mon Sep 17 00:00:00 2001 From: AntoinePrv Date: Tue, 31 Oct 2023 12:09:52 +0100 Subject: [PATCH] Remove calls to std::getenv --- libmamba/src/api/configuration.cpp | 2 +- libmamba/src/api/info.cpp | 3 +-- libmamba/src/core/download.cpp | 20 ++++++++++---------- libmamba/src/core/util.cpp | 4 ++-- libmamba/tests/src/specs/test_repo_data.cpp | 17 ++++++++--------- 5 files changed, 22 insertions(+), 24 deletions(-) diff --git a/libmamba/src/api/configuration.cpp b/libmamba/src/api/configuration.cpp index e49d62c942..17d0f18fa3 100644 --- a/libmamba/src/api/configuration.cpp +++ b/libmamba/src/api/configuration.cpp @@ -591,7 +591,7 @@ namespace mamba bool use_fallback = config.at("use_target_prefix_fallback").value(); if (use_fallback) { - prefix = std::getenv("CONDA_PREFIX") ? std::getenv("CONDA_PREFIX") : ""; + prefix = util::get_env("CONDA_PREFIX").value_or(""); } } diff --git a/libmamba/src/api/info.cpp b/libmamba/src/api/info.cpp index 3538d9e25a..b17297c3c8 100644 --- a/libmamba/src/api/info.cpp +++ b/libmamba/src/api/info.cpp @@ -118,8 +118,7 @@ namespace mamba location = "-"; } - if (std::getenv("CONDA_PREFIX") - && (std::getenv("CONDA_PREFIX") == ctx.prefix_params.target_prefix)) + if (auto prefix = util::get_env("CONDA_PREFIX"); prefix == ctx.prefix_params.target_prefix) { name += " (active)"; } diff --git a/libmamba/src/core/download.cpp b/libmamba/src/core/download.cpp index 3887d33280..fb9cba04b3 100644 --- a/libmamba/src/core/download.cpp +++ b/libmamba/src/core/download.cpp @@ -4,6 +4,7 @@ #include "mamba/core/util.hpp" #include "mamba/core/util_scope.hpp" #include "mamba/util/build.hpp" +#include "mamba/util/environment.hpp" #include "mamba/util/iterator.hpp" #include "mamba/util/string.hpp" #include "mamba/util/url.hpp" @@ -60,11 +61,13 @@ namespace mamba } #endif - if (!remote_fetch_params.ssl_verify.size() - && std::getenv("REQUESTS_CA_BUNDLE") != nullptr) + if (!remote_fetch_params.ssl_verify.size()) { - remote_fetch_params.ssl_verify = std::getenv("REQUESTS_CA_BUNDLE"); - LOG_INFO << "Using REQUESTS_CA_BUNDLE " << remote_fetch_params.ssl_verify; + if (auto ca = util::get_env("REQUESTS_CA_BUNDLE"); ca.has_value()) + { + remote_fetch_params.ssl_verify = ca.value(); + LOG_INFO << "Using REQUESTS_CA_BUNDLE " << remote_fetch_params.ssl_verify; + } } else if (remote_fetch_params.ssl_verify == "" && util::on_linux) { @@ -101,14 +104,11 @@ namespace mamba { // TODO: we should probably store set_low_speed_limit and set_ssl_no_revoke in // RemoteFetchParams if the request is slower than 30b/s for 60 seconds, cancel. - const std::string no_low_speed_limit = std::getenv("MAMBA_NO_LOW_SPEED_LIMIT") - ? std::getenv("MAMBA_NO_LOW_SPEED_LIMIT") - : "0"; + const std::string no_low_speed_limit = util::get_env("MAMBA_NO_LOW_SPEED_LIMIT") + .value_or("0"); const bool set_low_speed_opt = (no_low_speed_limit == "0"); - const std::string ssl_no_revoke_env = std::getenv("MAMBA_SSL_NO_REVOKE") - ? std::getenv("MAMBA_SSL_NO_REVOKE") - : "0"; + const std::string ssl_no_revoke_env = util::get_env("MAMBA_SSL_NO_REVOKE").value_or("0"); const bool set_ssl_no_revoke = context.remote_fetch_params.ssl_no_revoke || (ssl_no_revoke_env != "0"); diff --git a/libmamba/src/core/util.cpp b/libmamba/src/core/util.cpp index 944a80451f..bb732634f8 100644 --- a/libmamba/src/core/util.cpp +++ b/libmamba/src/core/util.cpp @@ -1454,9 +1454,9 @@ namespace mamba } else { - if (std::getenv("CONDA_EXE")) + if (auto exe = util::get_env("CONDA_EXE")) { - shebang = std::getenv("CONDA_EXE"); + shebang = exe.value(); } else { diff --git a/libmamba/tests/src/specs/test_repo_data.cpp b/libmamba/tests/src/specs/test_repo_data.cpp index 5799ba1aa8..52792f5e79 100644 --- a/libmamba/tests/src/specs/test_repo_data.cpp +++ b/libmamba/tests/src/specs/test_repo_data.cpp @@ -10,6 +10,7 @@ #include #include "mamba/specs/repo_data.hpp" +#include "mamba/util/environment.hpp" using namespace mamba::specs; namespace nl = nlohmann; @@ -27,7 +28,7 @@ TEST_SUITE("repo_data") p.md5 = "ffsd"; p.noarch = NoArchType::Python; - nl::json const j = p; + const nl::json j = p; CHECK_EQ(j.at("name"), p.name); CHECK_EQ(j.at("version"), p.version.str()); CHECK_EQ(j.at("build"), p.build_string); @@ -146,15 +147,13 @@ TEST_SUITE("repo_data") // ``repodata.json`` of interest are very large files. Should we check them in in VCS? // Download them in CMake? Do a specific integration test? // Could be downloaded in the tests, but we would like to keep these tests Context-free. - const char* repodata_file_path = std::getenv("MAMBA_REPODATA_JSON"); - if (repodata_file_path == nullptr) + if (auto repodata_file_path = mamba::util::get_env("MAMBA_REPODATA_JSON")) { - return; + auto repodata_file = std::ifstream(repodata_file_path.value()); + // Deserialize + auto data = nl::json::parse(repodata_file).get(); + // Serialize + const nl::json json = std::move(data); } - auto repodata_file = std::ifstream(repodata_file_path); - // Deserialize - auto data = nl::json::parse(repodata_file).get(); - // Serialize - const nl::json json = std::move(data); } }