Skip to content

Commit

Permalink
Remove calls to std::getenv
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoinePrv committed Oct 31, 2023
1 parent afd56fe commit c0aa5b3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion libmamba/src/api/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ namespace mamba
bool use_fallback = config.at("use_target_prefix_fallback").value<bool>();
if (use_fallback)
{
prefix = std::getenv("CONDA_PREFIX") ? std::getenv("CONDA_PREFIX") : "";
prefix = util::get_env("CONDA_PREFIX").value_or("");
}
}

Expand Down
3 changes: 1 addition & 2 deletions libmamba/src/api/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)";
}
Expand Down
20 changes: 10 additions & 10 deletions libmamba/src/core/download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 == "<system>" && util::on_linux)
{
Expand Down Expand Up @@ -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");

Expand Down
4 changes: 2 additions & 2 deletions libmamba/src/core/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
17 changes: 8 additions & 9 deletions libmamba/tests/src/specs/test_repo_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <nlohmann/json.hpp>

#include "mamba/specs/repo_data.hpp"
#include "mamba/util/environment.hpp"

using namespace mamba::specs;
namespace nl = nlohmann;
Expand All @@ -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);
Expand Down Expand Up @@ -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<RepoData>();
// 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<RepoData>();
// Serialize
const nl::json json = std::move(data);
}
}

0 comments on commit c0aa5b3

Please sign in to comment.