Skip to content

Commit cb117a5

Browse files
authored
[env] Add set_env_var function (#150)
* [env] Add `set_env_var` function This commit adds the `set_env_var` function, which is essentially an `rcpputils` wrapper around `rcutils_set_env`. In doing so, also: - Rename `get_env.{hpp,cpp}` to `env.{hpp,cpp}` - Update FEATURES.md with new signature Signed-off-by: Abrar Rahman Protyasha <[email protected]> * Add tests for `set_env_var` function Also, renamed `test_get_env.cpp` to `test_env.cpp` to reflect the addition of a new function. Signed-off-by: Abrar Rahman Protyasha <[email protected]> * Change `get_env.hpp` header includes to `env.hpp` Signed-off-by: Abrar Rahman Protyasha <[email protected]> * Re-add (and deprecate) `get_env.hpp` It's better to deprecate the header on a tick-tock cycle rather than removing it outright, so this commit reverts the deletion and actually deprecates the header. Signed-off-by: Abrar Rahman Protyasha <[email protected]>
1 parent ce44a14 commit cb117a5

File tree

9 files changed

+177
-63
lines changed

9 files changed

+177
-63
lines changed

CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ add_library(${PROJECT_NAME}
3131
src/asserts.cpp
3232
src/filesystem_helper.cpp
3333
src/find_library.cpp
34-
src/get_env.cpp
34+
src/env.cpp
3535
src/shared_library.cpp)
3636
target_include_directories(${PROJECT_NAME} PUBLIC
3737
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
@@ -69,13 +69,13 @@ if(BUILD_TESTING)
6969

7070
ament_add_gtest(test_join test/test_join.cpp)
7171

72-
ament_add_gtest(test_get_env test/test_get_env.cpp
72+
ament_add_gtest(test_env test/test_env.cpp
7373
ENV
7474
EMPTY_TEST=
7575
NORMAL_TEST=foo
7676
)
77-
ament_target_dependencies(test_get_env rcutils)
78-
target_link_libraries(test_get_env ${PROJECT_NAME})
77+
ament_target_dependencies(test_env rcutils)
78+
target_link_libraries(test_env ${PROJECT_NAME})
7979

8080
ament_add_gtest(test_scope_exit test/test_scope_exit.cpp)
8181
target_link_libraries(test_scope_exit ${PROJECT_NAME})

docs/FEATURES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ The `rcpputils/process.hpp` header contains process utilities.
108108
Namely, this header provides the `rcpputils::get_executable_name()` function, which retrieves and returns the current program name as a string.
109109

110110
## Environment helpers {#environment-helpers}
111-
The `rcpputils/get_env.hpp` header provides functionality to lookup the value of a provided environment variable through the `rcpputils::get_env_var(const char *)` function.
111+
The `rcpputils/env.hpp` header provides functionality to lookup the value of a provided environment variable through the `rcpputils::get_env_var(const char *)` function and set/un-set the value of a named, process-scoped environment variable through the `rcpputils::set_env_var(const char *, const char *)` function.
112112

113113
## Scope guard support {#scope-guard-support}
114114
Support for a general-purpose scope guard is provided in the `rcpputils/scope_exit.hpp` header.

include/rcpputils/env.hpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) 2020, Open Source Robotics Foundation, Inc.
2+
// All rights reserved.
3+
//
4+
// Software License Agreement (BSD License 2.0)
5+
//
6+
// Redistribution and use in source and binary forms, with or without
7+
// modification, are permitted provided that the following conditions
8+
// are met:
9+
//
10+
// * Redistributions of source code must retain the above copyright
11+
// notice, this list of conditions and the following disclaimer.
12+
// * Redistributions in binary form must reproduce the above
13+
// copyright notice, this list of conditions and the following
14+
// disclaimer in the documentation and/or other materials provided
15+
// with the distribution.
16+
// * Neither the name of the copyright holders nor the names of its
17+
// contributors may be used to endorse or promote products derived
18+
// from this software without specific prior written permission.
19+
//
20+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23+
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24+
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25+
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26+
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27+
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28+
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29+
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30+
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31+
// POSSIBILITY OF SUCH DAMAGE.
32+
33+
#ifndef RCPPUTILS__ENV_HPP_
34+
#define RCPPUTILS__ENV_HPP_
35+
36+
#include <string>
37+
38+
#include "rcpputils/visibility_control.hpp"
39+
40+
namespace rcpputils
41+
{
42+
43+
/// Retrieve the value of the given environment variable if it exists, or "".
44+
/*
45+
* \param[in] env_var the name of the environment variable
46+
* \return The value of the environment variable if it exists, or "".
47+
* \throws std::runtime_error on error
48+
*/
49+
RCPPUTILS_PUBLIC
50+
std::string get_env_var(const char * env_var);
51+
52+
/// Set/un-set a process-scoped environment variable.
53+
/*
54+
* \param[in] env_var The name of the environment variable.
55+
* \param[in] env_value Value to set the environment variable to, or `NULL`
56+
* to un-set.
57+
* \return Boolean representing whether the operation was successful.
58+
* \throws std::runtime_error if env_name is invalid/NULL, or if setting
59+
* the environment variable fails.
60+
*
61+
*/
62+
RCPPUTILS_PUBLIC
63+
bool set_env_var(const char * env_var, const char * env_value);
64+
65+
} // namespace rcpputils
66+
67+
#endif // RCPPUTILS__ENV_HPP_

include/rcpputils/get_env.hpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,13 @@
3333
#ifndef RCPPUTILS__GET_ENV_HPP_
3434
#define RCPPUTILS__GET_ENV_HPP_
3535

36-
#include <string>
36+
#ifdef _MSC_VER
37+
#pragma message \
38+
("rcpputils/get_env.hpp has been deprecated, please include rcpputils/env.hpp instead")
39+
#else
40+
#warning rcpputils/get_env.hpp has been deprecated, please include rcpputils/env.hpp instead
41+
#endif
3742

38-
#include "rcpputils/visibility_control.hpp"
39-
40-
namespace rcpputils
41-
{
42-
43-
/// Retrieve the value of the given environment variable if it exists, or "".
44-
/*
45-
* \param[in] env_var the name of the environment variable
46-
* \return The value of the environment variable if it exists, or "".
47-
* \throws std::runtime_error on error
48-
*/
49-
RCPPUTILS_PUBLIC
50-
std::string get_env_var(const char * env_var);
51-
52-
} // namespace rcpputils
43+
#include "rcpputils/env.hpp"
5344

5445
#endif // RCPPUTILS__GET_ENV_HPP_

src/get_env.cpp renamed to src/env.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434
#include <string>
3535

3636
#include "rcutils/env.h"
37+
#include "rcutils/error_handling.h"
3738

38-
#include "rcpputils/get_env.hpp"
39+
#include "rcpputils/env.hpp"
3940

4041
namespace rcpputils
4142
{
@@ -50,4 +51,15 @@ std::string get_env_var(const char * env_var)
5051
return value ? value : "";
5152
}
5253

54+
bool set_env_var(const char * env_var, const char * env_value)
55+
{
56+
if (!rcutils_set_env(env_var, env_value)) {
57+
std::string err = rcutils_get_error_string().str;
58+
// Resetting the error state since error string has been extracted
59+
rcutils_reset_error();
60+
throw std::runtime_error(err);
61+
}
62+
return true;
63+
}
64+
5365
} // namespace rcpputils

src/find_library.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
#include "rcpputils/filesystem_helper.hpp"
2828
#include "rcpputils/split.hpp"
29-
#include "rcpputils/get_env.hpp"
29+
#include "rcpputils/env.hpp"
3030

3131
namespace rcpputils
3232
{

test/test_env.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2020 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <gtest/gtest.h>
16+
17+
#include <rcpputils/env.hpp>
18+
19+
#include <stdexcept>
20+
#include <string>
21+
22+
23+
/* Tests get_env_var.
24+
*
25+
* Expected environment variables must be set by the calling code:
26+
*
27+
* - EMPTY_TEST=
28+
* - NORMAL_TEST=foo
29+
*
30+
* These are set in the call to `ament_add_gtest()` in the `CMakeLists.txt`.
31+
*/
32+
33+
TEST(TestGetEnv, test_get_env) {
34+
std::string env;
35+
env = rcpputils::get_env_var("NORMAL_TEST");
36+
EXPECT_STREQ("foo", env.c_str());
37+
env = rcpputils::get_env_var("SHOULD_NOT_EXIST_TEST");
38+
EXPECT_STREQ("", env.c_str());
39+
env = rcpputils::get_env_var("EMPTY_TEST");
40+
EXPECT_STREQ("", env.c_str());
41+
}
42+
43+
/* Tests set_env_var. */
44+
45+
TEST(TestSetEnv, test_set_env) {
46+
// Invalid cases: Environment variable name is invalid/NULL.
47+
EXPECT_THROW(
48+
rcpputils::set_env_var(nullptr, nullptr),
49+
std::runtime_error);
50+
EXPECT_THROW(
51+
rcpputils::set_env_var("=INVALID_ENV_VAR=", nullptr),
52+
std::runtime_error);
53+
EXPECT_THROW(
54+
rcpputils::set_env_var("=INVALID_ENV_VAR=", "InvalidEnvValue"),
55+
std::runtime_error);
56+
57+
// Starting empty
58+
EXPECT_STREQ("", rcpputils::get_env_var("NEW_ENV_VAR").c_str());
59+
60+
// Simple set
61+
EXPECT_TRUE(rcpputils::set_env_var("NEW_ENV_VAR", "NewEnvValue"));
62+
EXPECT_STREQ(
63+
"NewEnvValue",
64+
rcpputils::get_env_var("NEW_ENV_VAR").c_str());
65+
66+
// Re-set
67+
EXPECT_TRUE(rcpputils::set_env_var("NEW_ENV_VAR", "DifferentEnvValue"));
68+
EXPECT_STREQ(
69+
"DifferentEnvValue",
70+
rcpputils::get_env_var("NEW_ENV_VAR").c_str());
71+
72+
// Un-set
73+
EXPECT_TRUE(rcpputils::set_env_var("NEW_ENV_VAR", nullptr));
74+
EXPECT_STREQ(
75+
"",
76+
rcpputils::get_env_var("NEW_ENV_VAR").c_str());
77+
78+
// Un-set again
79+
EXPECT_TRUE(rcpputils::set_env_var("NEW_ENV_VAR", nullptr));
80+
EXPECT_STREQ(
81+
"",
82+
rcpputils::get_env_var("NEW_ENV_VAR").c_str());
83+
}

test/test_filesystem_helper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <string>
1919

2020
#include "rcpputils/filesystem_helper.hpp"
21-
#include "rcpputils/get_env.hpp"
21+
#include "rcpputils/env.hpp"
2222

2323
#ifdef _WIN32
2424
static constexpr const bool is_win32 = true;

test/test_get_env.cpp

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)