Skip to content

Commit

Permalink
Add unsetenv and also setenv macros and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sirknightj committed Feb 6, 2025
1 parent c82534e commit 78ee209
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,15 @@ extern PUBLIC_API atomicXor globalAtomicXor;
//
// Environment variables
//
#define GETENV getenv
#if defined __WINDOWS_BUILD__
#define GETENV GetEnvironmentVariable
#define SETENV(var, value) SetEnvironmentVariable(var, value)
#define UNSETENV(var) SetEnvironmentVariable(var, NULL)
#else
#define GETENV getenv
#define SETENV(var, value) setenv(var, value, 1)
#define UNSETENV unsetenv
#endif

//
// Empty string definition
Expand Down
52 changes: 52 additions & 0 deletions tst/common/CommonDefsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,55 @@ TEST(CommonDefsTest, GetEnvWorks)
{
EXPECT_NE(nullptr, GETENV("PATH")) << "PATH environment variable is unexpectedly unset, or GETENV doesn't work";
}

TEST(CommonDefsTest, SetEnvWorks)
{
const char* test_var = "MY_TEST_VAR";
const char* test_value = "test_value";

// Save the original value of the environment variable, if it exists
const char* original_value = GETENV(test_var);

// Set the environment variable to a new value
SETENV(test_var, test_value);

// Verify if the environment variable was set correctly
EXPECT_STREQ(test_value, GETENV(test_var)) << "Failed to set environment variable " << test_var;

// Restore the original environment variable value (if any)
if (original_value) {
SETENV(test_var, original_value);
} else {
// If it was not set, unset the variable
UNSETENV(test_var);
}
}

TEST(CommonDefsTest, UnsetEnvWorks)
{
const char* test_var = "MY_TEST_VAR";
const char* test_value = "test_value";

// Save the original value of the environment variable, if it exists
const char* original_value = GETENV(test_var);

// Set the environment variable to a new value
SETENV(test_var, test_value);

// Verify if the environment variable was set correctly
EXPECT_STREQ(test_value, GETENV(test_var)) << "Failed to set environment variable " << test_var;

// Unset the environment variable
UNSETENV(test_var);

// Verify if the environment variable is properly unset
EXPECT_EQ(nullptr, GETENV(test_var)) << "Failed to unset environment variable " << test_var;

// Restore the original environment variable value (if any)
if (original_value) {
SETENV(test_var, original_value);
} else {
// If it was not set, unset the variable
UNSETENV(test_var);
}
}

0 comments on commit 78ee209

Please sign in to comment.