Skip to content

Commit

Permalink
Aura - Add Aura::setEnvVar() Function
Browse files Browse the repository at this point in the history
  • Loading branch information
nlogozzo committed Dec 25, 2023
1 parent b6a6195 commit 82c2502
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
7 changes: 7 additions & 0 deletions include/aura.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Aura> m_instance;
Expand Down
13 changes: 13 additions & 0 deletions src/aura.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "aura.h"
#include <cstdlib>
#include <stdexcept>
#ifdef __linux__
#include <stdlib.h>
#endif

namespace Nickvision::Aura
{
Expand Down Expand Up @@ -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
}
}
11 changes: 3 additions & 8 deletions tests/networktests.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include <gtest/gtest.h>
#include "aura.h"
#include "network/networkmonitor.h"
#ifdef __linux__
#include <stdlib.h>
#endif

using namespace Nickvision::Aura;
using namespace Nickvision::Aura::Network;

TEST(NetworkTests, ConnectedGlobal)
Expand All @@ -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)
{
Expand Down

0 comments on commit 82c2502

Please sign in to comment.