-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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 <aprotyas@u.rochester.edu> * 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 <aprotyas@u.rochester.edu> * Change `get_env.hpp` header includes to `env.hpp` Signed-off-by: Abrar Rahman Protyasha <aprotyas@u.rochester.edu> * 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 <aprotyas@u.rochester.edu>
- Loading branch information
Showing
9 changed files
with
177 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) 2020, Open Source Robotics Foundation, Inc. | ||
// All rights reserved. | ||
// | ||
// Software License Agreement (BSD License 2.0) | ||
// | ||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions | ||
// are met: | ||
// | ||
// * Redistributions of source code must retain the above copyright | ||
// notice, this list of conditions and the following disclaimer. | ||
// * Redistributions in binary form must reproduce the above | ||
// copyright notice, this list of conditions and the following | ||
// disclaimer in the documentation and/or other materials provided | ||
// with the distribution. | ||
// * Neither the name of the copyright holders nor the names of its | ||
// contributors may be used to endorse or promote products derived | ||
// from this software without specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
// POSSIBILITY OF SUCH DAMAGE. | ||
|
||
#ifndef RCPPUTILS__ENV_HPP_ | ||
#define RCPPUTILS__ENV_HPP_ | ||
|
||
#include <string> | ||
|
||
#include "rcpputils/visibility_control.hpp" | ||
|
||
namespace rcpputils | ||
{ | ||
|
||
/// Retrieve the value of the given environment variable if it exists, or "". | ||
/* | ||
* \param[in] env_var the name of the environment variable | ||
* \return The value of the environment variable if it exists, or "". | ||
* \throws std::runtime_error on error | ||
*/ | ||
RCPPUTILS_PUBLIC | ||
std::string get_env_var(const char * env_var); | ||
|
||
/// Set/un-set a process-scoped environment variable. | ||
/* | ||
* \param[in] env_var The name of the environment variable. | ||
* \param[in] env_value Value to set the environment variable to, or `NULL` | ||
* to un-set. | ||
* \return Boolean representing whether the operation was successful. | ||
* \throws std::runtime_error if env_name is invalid/NULL, or if setting | ||
* the environment variable fails. | ||
* | ||
*/ | ||
RCPPUTILS_PUBLIC | ||
bool set_env_var(const char * env_var, const char * env_value); | ||
|
||
} // namespace rcpputils | ||
|
||
#endif // RCPPUTILS__ENV_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2020 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <rcpputils/env.hpp> | ||
|
||
#include <stdexcept> | ||
#include <string> | ||
|
||
|
||
/* Tests get_env_var. | ||
* | ||
* Expected environment variables must be set by the calling code: | ||
* | ||
* - EMPTY_TEST= | ||
* - NORMAL_TEST=foo | ||
* | ||
* These are set in the call to `ament_add_gtest()` in the `CMakeLists.txt`. | ||
*/ | ||
|
||
TEST(TestGetEnv, test_get_env) { | ||
std::string env; | ||
env = rcpputils::get_env_var("NORMAL_TEST"); | ||
EXPECT_STREQ("foo", env.c_str()); | ||
env = rcpputils::get_env_var("SHOULD_NOT_EXIST_TEST"); | ||
EXPECT_STREQ("", env.c_str()); | ||
env = rcpputils::get_env_var("EMPTY_TEST"); | ||
EXPECT_STREQ("", env.c_str()); | ||
} | ||
|
||
/* Tests set_env_var. */ | ||
|
||
TEST(TestSetEnv, test_set_env) { | ||
// Invalid cases: Environment variable name is invalid/NULL. | ||
EXPECT_THROW( | ||
rcpputils::set_env_var(nullptr, nullptr), | ||
std::runtime_error); | ||
EXPECT_THROW( | ||
rcpputils::set_env_var("=INVALID_ENV_VAR=", nullptr), | ||
std::runtime_error); | ||
EXPECT_THROW( | ||
rcpputils::set_env_var("=INVALID_ENV_VAR=", "InvalidEnvValue"), | ||
std::runtime_error); | ||
|
||
// Starting empty | ||
EXPECT_STREQ("", rcpputils::get_env_var("NEW_ENV_VAR").c_str()); | ||
|
||
// Simple set | ||
EXPECT_TRUE(rcpputils::set_env_var("NEW_ENV_VAR", "NewEnvValue")); | ||
EXPECT_STREQ( | ||
"NewEnvValue", | ||
rcpputils::get_env_var("NEW_ENV_VAR").c_str()); | ||
|
||
// Re-set | ||
EXPECT_TRUE(rcpputils::set_env_var("NEW_ENV_VAR", "DifferentEnvValue")); | ||
EXPECT_STREQ( | ||
"DifferentEnvValue", | ||
rcpputils::get_env_var("NEW_ENV_VAR").c_str()); | ||
|
||
// Un-set | ||
EXPECT_TRUE(rcpputils::set_env_var("NEW_ENV_VAR", nullptr)); | ||
EXPECT_STREQ( | ||
"", | ||
rcpputils::get_env_var("NEW_ENV_VAR").c_str()); | ||
|
||
// Un-set again | ||
EXPECT_TRUE(rcpputils::set_env_var("NEW_ENV_VAR", nullptr)); | ||
EXPECT_STREQ( | ||
"", | ||
rcpputils::get_env_var("NEW_ENV_VAR").c_str()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.