From cc89deb83150fbe0f7a568a6311e6d6ad20199d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Lamotte=20=28Klaim=29?= Date: Tue, 28 Mar 2023 19:30:55 +0200 Subject: [PATCH 01/12] Windows: Fixed environment variables not read as unicode. --- libmamba/src/core/environment.cpp | 33 +++++-------------------------- 1 file changed, 5 insertions(+), 28 deletions(-) diff --git a/libmamba/src/core/environment.cpp b/libmamba/src/core/environment.cpp index bbd429ab50..a0d8d72ac9 100644 --- a/libmamba/src/core/environment.cpp +++ b/libmamba/src/core/environment.cpp @@ -9,6 +9,7 @@ #ifdef _WIN32 #include "mamba/core/output.hpp" +#include "mamba/core/util_os.hpp" #endif namespace mamba @@ -18,31 +19,10 @@ namespace mamba std::optional get(const std::string& key) { #ifdef _WIN32 - const size_t initial_size = 1024; - std::unique_ptr temp_small = std::make_unique(initial_size); - std::size_t size = GetEnvironmentVariableA(key.c_str(), temp_small.get(), initial_size); - if (size == 0) // Error or empty/missing + const std::wstring unicode_key(key.begin(), key.end()); + if (const auto value = _wgetenv(unicode_key.c_str())) { - // Note that on Windows environment variables can never be empty, - // only missing. See https://stackoverflow.com/a/39095782 - auto last_err = GetLastError(); - if (last_err != ERROR_ENVVAR_NOT_FOUND) - { - LOG_ERROR << "Could not get environment variable: " << last_err; - } - return {}; - } - else if (size > initial_size) // Buffer too small - { - std::unique_ptr temp_large = std::make_unique(size); - GetEnvironmentVariableA(key.c_str(), temp_large.get(), size); - std::string res(temp_large.get()); - return res; - } - else // Success - { - std::string res(temp_small.get()); - return res; + return mamba::to_utf8(value); } #else const char* value = std::getenv(key.c_str()); @@ -50,11 +30,8 @@ namespace mamba { return value; } - else - { - return {}; - } #endif + return {}; } bool set(const std::string& key, const std::string& value) From e1a810bce8cfdc1a970baa7ef3a3daaec842df1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Lamotte=20=28Klaim=29?= Date: Wed, 29 Mar 2023 17:28:34 +0200 Subject: [PATCH 02/12] Use the safe version of getenv on Windows + prevent concurrecnt calls. --- libmamba/src/core/environment.cpp | 39 +++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/libmamba/src/core/environment.cpp b/libmamba/src/core/environment.cpp index a0d8d72ac9..293f9503e2 100644 --- a/libmamba/src/core/environment.cpp +++ b/libmamba/src/core/environment.cpp @@ -8,6 +8,7 @@ #include "mamba/core/util_string.hpp" #ifdef _WIN32 +#include #include "mamba/core/output.hpp" #include "mamba/core/util_os.hpp" #endif @@ -19,10 +20,43 @@ namespace mamba std::optional get(const std::string& key) { #ifdef _WIN32 + // See: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getenv-s-wgetenv-s?view=msvc-170 + static std::mutex call_mutex; + std::scoped_lock ready_to_execute{ call_mutex }; // Calls to getenv_s kinds of functions are not thread-safe, this is to prevent related issues. + + const auto on_failed = [&](errno_t error_code){ + LOG_ERROR << fmt::format( + "Failed to acquire environment variable '{}' : errcode = {}", + key, + error_code + ); + }; + const std::wstring unicode_key(key.begin(), key.end()); - if (const auto value = _wgetenv(unicode_key.c_str())) + size_t required_size = 0; + if (auto error_code = _wgetenv_s(&required_size, nullptr, 0, unicode_key.c_str()); + error_code == 0) + { + if (required_size == 0) // The value doesn't exist. + { + return {}; + } + + std::wstring value(required_size, L'?'); // Note: The required size implies a `\0` but basic_string doesn't. + if (error_code = _wgetenv_s(&required_size, value.data(), value.size(), unicode_key.c_str()); + error_code == 0) + { + value.pop_back(); // Remove the `\0` that was written in, otherwise any future concatenation will fail. + return mamba::to_utf8(value); + } + else + { + on_failed(error_code); + } + } + else { - return mamba::to_utf8(value); + on_failed(error_code); } #else const char* value = std::getenv(key.c_str()); @@ -37,6 +71,7 @@ namespace mamba bool set(const std::string& key, const std::string& value) { #ifdef _WIN32 + //_setenv_s auto res = SetEnvironmentVariableA(key.c_str(), value.c_str()); if (!res) { From 96245e25d2cab8279ffa079505524f51a15fa8cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Lamotte=20=28Klaim=29?= Date: Wed, 29 Mar 2023 18:27:52 +0200 Subject: [PATCH 03/12] Added `to_windows_unicode` to convert utf-8 strings to wstring with UTF-16, for specific usage with Windows APIs --- libmamba/include/mamba/core/util_os.hpp | 7 +++-- libmamba/src/core/util_os.cpp | 38 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/libmamba/include/mamba/core/util_os.hpp b/libmamba/include/mamba/core/util_os.hpp index e834b35308..6bfd86c1b4 100644 --- a/libmamba/include/mamba/core/util_os.hpp +++ b/libmamba/include/mamba/core/util_os.hpp @@ -40,9 +40,10 @@ namespace mamba void reset_console(); #ifdef _WIN32 - std::string to_utf8(const wchar_t* w, size_t s); - std::string to_utf8(const wchar_t* w); - std::string to_utf8(const std::wstring& s); + std::string to_utf8(const wchar_t* windows_unicode_text, size_t size); + std::string to_utf8(const wchar_t* windows_unicode_text); + std::string to_utf8(const std::wstring& windows_unicode_text); + std::wstring to_windows_unicode(const std::string_view utf8_text); #endif /* Test whether a given `std::ostream` object refers to a terminal. */ diff --git a/libmamba/src/core/util_os.cpp b/libmamba/src/core/util_os.cpp index c15ba674e1..fc3fed056e 100644 --- a/libmamba/src/core/util_os.cpp +++ b/libmamba/src/core/util_os.cpp @@ -543,6 +543,44 @@ namespace mamba { return to_utf8(s.data(), s.size()); } + + std::wstring to_windows_unicode(const std::string_view utf8_text) + { + std::wstring output; + if (!utf8_text.empty()) + { + assert(utf8_text.size() <= INT_MAX); + const int size = MultiByteToWideChar( + CP_UTF8, + 0, + utf8_text.data(), + utf8_text.size(), + nullptr, + 0 + ); + if (size <= 0) + { + unsigned long last_error = ::GetLastError(); + LOG_ERROR << "Failed to convert UTF-8 string to Windows Unicode (UTF-16)" + << std::system_category().message(static_cast(last_error)); + throw std::runtime_error("Failed to convert UTF-8 string to UTF-16"); + } + + output.resize(size); + int res_size = MultiByteToWideChar( + CP_UTF8, + 0, + utf8_text.data(), + utf8_text.size(), + output.data(), + output.size() + ); + assert(res_size == size); + } + + return output; + } + #endif /* From https://github.com/ikalnytskyi/termcolor From 309bd1f1d4695f57c9e8d2603c5ae79c749003ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Lamotte=20=28Klaim=29?= Date: Wed, 29 Mar 2023 18:28:29 +0200 Subject: [PATCH 04/12] Replaced set env variable implementation by safe unicode api on windows. --- libmamba/src/core/environment.cpp | 37 +++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/libmamba/src/core/environment.cpp b/libmamba/src/core/environment.cpp index 293f9503e2..02e0bc7eb3 100644 --- a/libmamba/src/core/environment.cpp +++ b/libmamba/src/core/environment.cpp @@ -22,7 +22,9 @@ namespace mamba #ifdef _WIN32 // See: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getenv-s-wgetenv-s?view=msvc-170 static std::mutex call_mutex; - std::scoped_lock ready_to_execute{ call_mutex }; // Calls to getenv_s kinds of functions are not thread-safe, this is to prevent related issues. + std::scoped_lock ready_to_execute{ call_mutex }; // Calls to getenv_s kinds of + // functions are not thread-safe, this + // is to prevent related issues. const auto on_failed = [&](errno_t error_code){ LOG_ERROR << fmt::format( @@ -32,7 +34,7 @@ namespace mamba ); }; - const std::wstring unicode_key(key.begin(), key.end()); + const std::wstring unicode_key = to_windows_unicode(key); size_t required_size = 0; if (auto error_code = _wgetenv_s(&required_size, nullptr, 0, unicode_key.c_str()); error_code == 0) @@ -42,11 +44,13 @@ namespace mamba return {}; } - std::wstring value(required_size, L'?'); // Note: The required size implies a `\0` but basic_string doesn't. + std::wstring value(required_size, L'?'); // Note: The required size implies a `\0` + // but basic_string doesn't. if (error_code = _wgetenv_s(&required_size, value.data(), value.size(), unicode_key.c_str()); error_code == 0) { - value.pop_back(); // Remove the `\0` that was written in, otherwise any future concatenation will fail. + value.pop_back(); // Remove the `\0` that was written in, otherwise any future + // concatenation will fail. return mamba::to_utf8(value); } else @@ -71,11 +75,24 @@ namespace mamba bool set(const std::string& key, const std::string& value) { #ifdef _WIN32 - //_setenv_s - auto res = SetEnvironmentVariableA(key.c_str(), value.c_str()); + // See: + // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getenv-s-wgetenv-s?view=msvc-170 + static std::mutex call_mutex; + std::scoped_lock ready_to_execute{ call_mutex }; // Calls to getenv_s kinds of + // functions are not thread-safe, this + // is to prevent related issues. + + const std::wstring unicode_key = to_windows_unicode(key); + const std::wstring unicode_value = to_windows_unicode(value); + auto res = _wputenv_s(unicode_key.c_str(), unicode_value.c_str()); if (!res) { - LOG_ERROR << "Could not set environment variable: " << GetLastError(); + LOG_ERROR << fmt::format( + "Could not set environment variable '{}' to '{}' : {}", + key, + value, + GetLastError() + ); } return res; #else @@ -86,11 +103,7 @@ namespace mamba void unset(const std::string& key) { #ifdef _WIN32 - auto res = SetEnvironmentVariableA(key.c_str(), NULL); - if (!res) - { - LOG_ERROR << "Could not unset environment variable: " << GetLastError(); - } + set(key, ""); #else unsetenv(key.c_str()); #endif From aef9041f10bf77674e7356c9f7296c42b0820137 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Lamotte=20=28Klaim=29?= Date: Wed, 29 Mar 2023 18:34:13 +0200 Subject: [PATCH 05/12] Formatting --- libmamba/src/core/environment.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/libmamba/src/core/environment.cpp b/libmamba/src/core/environment.cpp index 02e0bc7eb3..6e19aeb0ce 100644 --- a/libmamba/src/core/environment.cpp +++ b/libmamba/src/core/environment.cpp @@ -9,6 +9,7 @@ #ifdef _WIN32 #include + #include "mamba/core/output.hpp" #include "mamba/core/util_os.hpp" #endif @@ -20,13 +21,15 @@ namespace mamba std::optional get(const std::string& key) { #ifdef _WIN32 - // See: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getenv-s-wgetenv-s?view=msvc-170 + // See: + // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getenv-s-wgetenv-s?view=msvc-170 static std::mutex call_mutex; std::scoped_lock ready_to_execute{ call_mutex }; // Calls to getenv_s kinds of // functions are not thread-safe, this // is to prevent related issues. - const auto on_failed = [&](errno_t error_code){ + const auto on_failed = [&](errno_t error_code) + { LOG_ERROR << fmt::format( "Failed to acquire environment variable '{}' : errcode = {}", key, @@ -39,14 +42,19 @@ namespace mamba if (auto error_code = _wgetenv_s(&required_size, nullptr, 0, unicode_key.c_str()); error_code == 0) { - if (required_size == 0) // The value doesn't exist. + if (required_size == 0) // The value doesn't exist. { return {}; } std::wstring value(required_size, L'?'); // Note: The required size implies a `\0` // but basic_string doesn't. - if (error_code = _wgetenv_s(&required_size, value.data(), value.size(), unicode_key.c_str()); + if (error_code = _wgetenv_s( + &required_size, + value.data(), + value.size(), + unicode_key.c_str() + ); error_code == 0) { value.pop_back(); // Remove the `\0` that was written in, otherwise any future From d81d6690583c83e0af0e0afa33b29ca88798699d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Lamotte=20=28Klaim=29?= Date: Thu, 30 Mar 2023 16:06:16 +0200 Subject: [PATCH 06/12] Added basic tests for unicode conversion functions. --- libmamba/include/mamba/core/util_os.hpp | 5 ++++ libmamba/tests/CMakeLists.txt | 1 + libmamba/tests/src/core/test_util_os.cpp | 35 ++++++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 libmamba/tests/src/core/test_util_os.cpp diff --git a/libmamba/include/mamba/core/util_os.hpp b/libmamba/include/mamba/core/util_os.hpp index 6bfd86c1b4..58d17c25f5 100644 --- a/libmamba/include/mamba/core/util_os.hpp +++ b/libmamba/include/mamba/core/util_os.hpp @@ -12,6 +12,11 @@ #include "mamba/core/fsutil.hpp" +#ifdef _WIN32 +# define WIN32_LEAN_AND_MEAN +# include +#endif + namespace mamba { bool is_admin(); diff --git a/libmamba/tests/CMakeLists.txt b/libmamba/tests/CMakeLists.txt index 1640596305..13f2557b37 100644 --- a/libmamba/tests/CMakeLists.txt +++ b/libmamba/tests/CMakeLists.txt @@ -42,6 +42,7 @@ set(LIBMAMBA_TEST_SRCS src/core/test_virtual_packages.cpp src/core/test_util.cpp src/core/test_util_string.cpp + src/core/test_util_os.cpp src/core/test_env_lockfile.cpp src/core/test_execution.cpp src/core/test_invoke.cpp diff --git a/libmamba/tests/src/core/test_util_os.cpp b/libmamba/tests/src/core/test_util_os.cpp new file mode 100644 index 0000000000..0f4466a6b0 --- /dev/null +++ b/libmamba/tests/src/core/test_util_os.cpp @@ -0,0 +1,35 @@ +// Copyright (c) 2019, QuantStack and Mamba Contributors +// +// Distributed under the terms of the BSD 3-Clause License. +// +// The full license is in the file LICENSE, distributed with this software. + +#include + +#include + +#include "mamba/core/util_os.hpp" + + +#ifdef _WIN32 + +namespace +{ + const std::wstring text_utf16 = L"Hello, I am Joël. 私のにほんごわへたです"; + const std::string text_utf8 = u8"Hello, I am Joël. 私のにほんごわへたです"; +} + +TEST(to_utf8, basic_unicode_conversion) +{ + auto result = mamba::to_utf8(text_utf16); + EXPECT_EQ(text_utf8, result); +} + +TEST(to_windows_unicode, basic_unicode_conversion) +{ + auto result = mamba::to_windows_unicode(text_utf8); + EXPECT_EQ(text_utf16, result); +} + +#endif + From c94aa7d1d396be8a80e670ae34090fd567f18ee1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Lamotte=20=28Klaim=29?= Date: Thu, 30 Mar 2023 16:34:09 +0200 Subject: [PATCH 07/12] Added basic test for environment variable functions and fixed related issues. --- libmamba/include/mamba/core/util_random.hpp | 1 + libmamba/src/core/environment.cpp | 4 +- libmamba/tests/CMakeLists.txt | 1 + libmamba/tests/src/core/test_system_env.cpp | 47 +++++++++++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 libmamba/tests/src/core/test_system_env.cpp diff --git a/libmamba/include/mamba/core/util_random.hpp b/libmamba/include/mamba/core/util_random.hpp index 8f23cfaaef..ccf151ff9a 100644 --- a/libmamba/include/mamba/core/util_random.hpp +++ b/libmamba/include/mamba/core/util_random.hpp @@ -12,6 +12,7 @@ #include #include #include +#include namespace mamba { diff --git a/libmamba/src/core/environment.cpp b/libmamba/src/core/environment.cpp index 6e19aeb0ce..7941cd6198 100644 --- a/libmamba/src/core/environment.cpp +++ b/libmamba/src/core/environment.cpp @@ -93,7 +93,7 @@ namespace mamba const std::wstring unicode_key = to_windows_unicode(key); const std::wstring unicode_value = to_windows_unicode(value); auto res = _wputenv_s(unicode_key.c_str(), unicode_value.c_str()); - if (!res) + if (res != 0) { LOG_ERROR << fmt::format( "Could not set environment variable '{}' to '{}' : {}", @@ -102,7 +102,7 @@ namespace mamba GetLastError() ); } - return res; + return res == 0; #else return setenv(key.c_str(), value.c_str(), 1) == 0; #endif diff --git a/libmamba/tests/CMakeLists.txt b/libmamba/tests/CMakeLists.txt index 13f2557b37..04fe33407e 100644 --- a/libmamba/tests/CMakeLists.txt +++ b/libmamba/tests/CMakeLists.txt @@ -43,6 +43,7 @@ set(LIBMAMBA_TEST_SRCS src/core/test_util.cpp src/core/test_util_string.cpp src/core/test_util_os.cpp + src/core/test_system_env.cpp src/core/test_env_lockfile.cpp src/core/test_execution.cpp src/core/test_invoke.cpp diff --git a/libmamba/tests/src/core/test_system_env.cpp b/libmamba/tests/src/core/test_system_env.cpp new file mode 100644 index 0000000000..0a928da8de --- /dev/null +++ b/libmamba/tests/src/core/test_system_env.cpp @@ -0,0 +1,47 @@ +// Copyright (c) 2019, QuantStack and Mamba Contributors +// +// Distributed under the terms of the BSD 3-Clause License. +// +// The full license is in the file LICENSE, distributed with this software. + +#include + +#include + +#include "mamba/core/environment.hpp" +#include "mamba/core/util_random.hpp" + + +namespace +{ + void test_set_get_unset_env_variables(const std::string& key, const std::string& value) + { + const bool did_set = mamba::env::set(key, value); + ASSERT_TRUE(did_set); + + const auto result = mamba::env::get(key); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(*result, value); + + mamba::env::unset(key); + const auto result_empty = mamba::env::get(key); + ASSERT_FALSE(result_empty.has_value()); + + } +} + +TEST(system_env, set_get_unset_env_variables) +{ + const auto key = mamba::generate_random_alphanumeric_string(128); + const auto value = mamba::generate_random_alphanumeric_string(128); + + test_set_get_unset_env_variables(key, value); +} + +TEST(system_env, set_get_unset_variables_unicode) +{ + const std::string key = u8"Joël私のにほん"; + const std::string value = u8"Hello, I am Joël. 私のにほんごわへたです"; + + test_set_get_unset_env_variables(key, value); +} From 8d2b298a567e76b51ad7d60530aee2505eadb85c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Lamotte=20=28Klaim=29?= Date: Thu, 30 Mar 2023 16:41:49 +0200 Subject: [PATCH 08/12] Formatting fixes --- libmamba/include/mamba/core/util_os.hpp | 4 ++-- libmamba/include/mamba/core/util_random.hpp | 2 +- libmamba/tests/src/core/test_system_env.cpp | 5 ++--- libmamba/tests/src/core/test_util_os.cpp | 5 ++--- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/libmamba/include/mamba/core/util_os.hpp b/libmamba/include/mamba/core/util_os.hpp index 58d17c25f5..645dad1f07 100644 --- a/libmamba/include/mamba/core/util_os.hpp +++ b/libmamba/include/mamba/core/util_os.hpp @@ -13,8 +13,8 @@ #include "mamba/core/fsutil.hpp" #ifdef _WIN32 -# define WIN32_LEAN_AND_MEAN -# include +#define WIN32_LEAN_AND_MEAN +#include #endif namespace mamba diff --git a/libmamba/include/mamba/core/util_random.hpp b/libmamba/include/mamba/core/util_random.hpp index ccf151ff9a..a761063ac6 100644 --- a/libmamba/include/mamba/core/util_random.hpp +++ b/libmamba/include/mamba/core/util_random.hpp @@ -8,11 +8,11 @@ #define MAMBA_CORE_UTIL_RANDOM_HPP #include +#include #include #include #include #include -#include namespace mamba { diff --git a/libmamba/tests/src/core/test_system_env.cpp b/libmamba/tests/src/core/test_system_env.cpp index 0a928da8de..955deb90a9 100644 --- a/libmamba/tests/src/core/test_system_env.cpp +++ b/libmamba/tests/src/core/test_system_env.cpp @@ -4,10 +4,10 @@ // // The full license is in the file LICENSE, distributed with this software. -#include - #include +#include + #include "mamba/core/environment.hpp" #include "mamba/core/util_random.hpp" @@ -26,7 +26,6 @@ namespace mamba::env::unset(key); const auto result_empty = mamba::env::get(key); ASSERT_FALSE(result_empty.has_value()); - } } diff --git a/libmamba/tests/src/core/test_util_os.cpp b/libmamba/tests/src/core/test_util_os.cpp index 0f4466a6b0..61cc273adc 100644 --- a/libmamba/tests/src/core/test_util_os.cpp +++ b/libmamba/tests/src/core/test_util_os.cpp @@ -4,10 +4,10 @@ // // The full license is in the file LICENSE, distributed with this software. -#include - #include +#include + #include "mamba/core/util_os.hpp" @@ -32,4 +32,3 @@ TEST(to_windows_unicode, basic_unicode_conversion) } #endif - From b4758d732f40aab39eabe23b964af7888dd73145 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Lamotte=20=28Klaim=29?= Date: Thu, 30 Mar 2023 17:01:37 +0200 Subject: [PATCH 09/12] Added basic test for windows for getting environment variables already existing. This might also catch issues with home paths containing unicode. --- libmamba/tests/src/core/test_system_env.cpp | 22 +++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/libmamba/tests/src/core/test_system_env.cpp b/libmamba/tests/src/core/test_system_env.cpp index 955deb90a9..9bb3b3689f 100644 --- a/libmamba/tests/src/core/test_system_env.cpp +++ b/libmamba/tests/src/core/test_system_env.cpp @@ -44,3 +44,25 @@ TEST(system_env, set_get_unset_variables_unicode) test_set_get_unset_env_variables(key, value); } + +#ifdef _WIN32 + +TEST(system_env, get_predefined_env_variable) +{ + // We check that normal pre-defined variables are accessible and do not fail access even if some + // of these values have unicode. + const std::vector predefined_keys{ "PATH", "OS", "PATHEXT", + "ProgramData", "SystemRoot", "windir", + "APPDATA", "COMPUTERNAME", "TEMP", + "UserName", "USERPROFILE" }; + + for (const auto& key : predefined_keys) + { + const auto maybe_value = mamba::env::get(key); + ASSERT_TRUE(maybe_value.has_value()) << "key = " + key; + const auto value = *maybe_value; + EXPECT_FALSE(value.empty()) << "key = " + key; + } +} + +#endif From 3de46857f2c654f1dedb3c95c7ba4d3ceaec195a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Lamotte=20=28Klaim=29?= Date: Mon, 3 Apr 2023 17:29:21 +0200 Subject: [PATCH 10/12] Replace inclusion of Windows.h by just specifying the DWORD type (assuming it will not change for a long time in Windows) --- libmamba/include/mamba/core/util_os.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libmamba/include/mamba/core/util_os.hpp b/libmamba/include/mamba/core/util_os.hpp index 645dad1f07..0417c5cb1c 100644 --- a/libmamba/include/mamba/core/util_os.hpp +++ b/libmamba/include/mamba/core/util_os.hpp @@ -12,13 +12,13 @@ #include "mamba/core/fsutil.hpp" +namespace mamba +{ #ifdef _WIN32 -#define WIN32_LEAN_AND_MEAN -#include + // Intention is to avoid including `Windows.h`, while still using the basic Windows API types. + using DWORD = unsigned long; #endif -namespace mamba -{ bool is_admin(); fs::u8path get_self_exe_path(); From e6e938a995ff8184bb283cfcdaf3764b653741c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Lamotte=20=28Klaim=29?= Date: Mon, 3 Apr 2023 17:35:19 +0200 Subject: [PATCH 11/12] Updated modified file's copyright year. --- libmamba/include/mamba/core/environment.hpp | 2 +- libmamba/include/mamba/core/util_os.hpp | 2 +- libmamba/tests/src/core/test_system_env.cpp | 2 +- libmamba/tests/src/core/test_util_os.cpp | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libmamba/include/mamba/core/environment.hpp b/libmamba/include/mamba/core/environment.hpp index ba10c47523..7b36abee94 100644 --- a/libmamba/include/mamba/core/environment.hpp +++ b/libmamba/include/mamba/core/environment.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2019, QuantStack and Mamba Contributors +// Copyright (c) 2019-2023, QuantStack and Mamba Contributors // // Distributed under the terms of the BSD 3-Clause License. // diff --git a/libmamba/include/mamba/core/util_os.hpp b/libmamba/include/mamba/core/util_os.hpp index 0417c5cb1c..f7e8576f02 100644 --- a/libmamba/include/mamba/core/util_os.hpp +++ b/libmamba/include/mamba/core/util_os.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2019, QuantStack and Mamba Contributors +// Copyright (c) 2019-2023, QuantStack and Mamba Contributors // // Distributed under the terms of the BSD 3-Clause License. // diff --git a/libmamba/tests/src/core/test_system_env.cpp b/libmamba/tests/src/core/test_system_env.cpp index 9bb3b3689f..12e5c07554 100644 --- a/libmamba/tests/src/core/test_system_env.cpp +++ b/libmamba/tests/src/core/test_system_env.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2019, QuantStack and Mamba Contributors +// Copyright (c) 2023, QuantStack and Mamba Contributors // // Distributed under the terms of the BSD 3-Clause License. // diff --git a/libmamba/tests/src/core/test_util_os.cpp b/libmamba/tests/src/core/test_util_os.cpp index 61cc273adc..4bc1f801f8 100644 --- a/libmamba/tests/src/core/test_util_os.cpp +++ b/libmamba/tests/src/core/test_util_os.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2019, QuantStack and Mamba Contributors +// Copyright (c) 2023, QuantStack and Mamba Contributors // // Distributed under the terms of the BSD 3-Clause License. // From cd660dc553ed04bd9ba76a817d3580b671759a26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Lamotte=20=28Klaim=29?= Date: Mon, 3 Apr 2023 17:44:12 +0200 Subject: [PATCH 12/12] Added static check that our DWORD on Windows does not differs from the one in Windows.h --- libmamba/src/core/util_os.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libmamba/src/core/util_os.cpp b/libmamba/src/core/util_os.cpp index fc3fed056e..00400ca21c 100644 --- a/libmamba/src/core/util_os.cpp +++ b/libmamba/src/core/util_os.cpp @@ -36,6 +36,10 @@ #include "mamba/core/util_os.hpp" #include "mamba/core/util_string.hpp" +#ifdef _WIN32 +static_assert(std::is_same_v); +#endif + namespace mamba { // Heavily inspired by https://github.com/gpakosz/whereami/