diff --git a/include/aura.h b/include/aura.h index 14b28cb..84e0505 100644 --- a/include/aura.h +++ b/include/aura.h @@ -65,6 +65,13 @@ namespace Nickvision::Aura * @return The environment variable if found, else empty string */ static std::string getEnvVar(const std::string& key); + /** + * @brief Sets a system environment variable. + * @param key The environment variable to set + * @param value The value for the environment variable + * @return True if set, else false + */ + static bool setEnvVar(const std::string& key, const std::string& value); private: static std::unique_ptr m_instance; diff --git a/src/aura.cpp b/src/aura.cpp index 5e99855..7c84c55 100644 --- a/src/aura.cpp +++ b/src/aura.cpp @@ -1,6 +1,9 @@ #include "aura.h" #include #include +#ifdef __linux__ +#include +#endif namespace Nickvision::Aura { @@ -44,4 +47,14 @@ namespace Nickvision::Aura } return ""; } + + bool Aura::setEnvVar(const std::string& key, const std::string& value) + { +#ifdef _WIN32 + std::string var{ key + "=" + value }; + return _putenv(var.c_str()) == 0; +#elif defined(__linux__) + return setenv(key.c_str(), value.c_str(), true) == 0; +#endif + } } \ No newline at end of file diff --git a/tests/networktests.cpp b/tests/networktests.cpp index 9ca2475..80cbd39 100644 --- a/tests/networktests.cpp +++ b/tests/networktests.cpp @@ -1,9 +1,8 @@ #include +#include "aura.h" #include "network/networkmonitor.h" -#ifdef __linux__ -#include -#endif +using namespace Nickvision::Aura; using namespace Nickvision::Aura::Network; TEST(NetworkTests, ConnectedGlobal) @@ -18,11 +17,7 @@ TEST(NetworkTests, ConnectedGlobal) TEST(NetworkTests, DisableNetCheck) { -#ifdef _WIN32 - ASSERT_EQ(_putenv("AURA_DISABLE_NETCHECK=true"), 0); -#elif defined(__linux__) - ASSERT_EQ(setenv("AURA_DISABLE_NETCHECK", "true", true), 0); -#endif + ASSERT_TRUE(Aura::setEnvVar("AURA_DISABLE_NETCHECK", "true")); NetworkMonitor netmon; netmon.stateChanged() += [](const NetworkStateChangedEventArgs& e) {