Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

Commit

Permalink
(maint) Merge up 11a942a to master
Browse files Browse the repository at this point in the history
Generated by CI

* commit '11a942a82ee7fecdf1b9313d2776ee4a08321e6e':
  (maint) Fix boost::nowide::getenv on OS X
  (FACT-1919) Add environment::get_int
  • Loading branch information
puppetlabs-jenkins committed Aug 26, 2019
2 parents 2a4b3de + 11a942a commit 83ad3f7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
8 changes: 8 additions & 0 deletions util/inc/leatherman/util/environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ namespace leatherman { namespace util {
*/
struct environment
{
/**
* Gets an environment variable as integer.
* @param name The name of the environment variable to get.
* @param default_value The value to return if the variable is absent or invalid.
* @return Returns the environment variable if present and valid or the default value if not.
*/
static int get_int(std::string const& name, int default_value);

/**
* Gets an environment variable.
* @param name The name of the environment variable to get.
Expand Down
15 changes: 15 additions & 0 deletions util/src/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ using namespace std;

namespace leatherman { namespace util {

int environment::get_int(string const& name, int default_value)
{
auto variable = boost::nowide::getenv(name.c_str());
if (!variable) {
return default_value;
}

try {
return stoi(variable);
}
catch (invalid_argument&) {
return default_value;
}
}

bool environment::get(string const& name, string& value)
{
auto variable = boost::nowide::getenv(name.c_str());
Expand Down
30 changes: 30 additions & 0 deletions util/tests/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,39 @@ SCENARIO("getting an environment variable") {
boost::nowide::unsetenv("ENVTEST");
value = "";
REQUIRE_FALSE(environment::get("ENVTEST", value));
REQUIRE(environment::get_int("ENVTEST", 100) == 100);
REQUIRE(value.empty());
}

SCENARIO("getting an int environment variable") {
boost::nowide::setenv("INTVAR", "100", 1);
boost::nowide::setenv("STRVAR", "BAR", 1);
boost::nowide::setenv("EMPTYVAR", "", 1);
GIVEN("an int variable") {
THEN("should return the converted value as int") {
REQUIRE(environment::get_int("INTVAR", 1) == 100);
}
boost::nowide::unsetenv("INTVAR");
}
GIVEN("a string variable") {
THEN("should return the default value") {
REQUIRE(environment::get_int("STRVAR", 123) == 123);
}
boost::nowide::unsetenv("STRVAR");
}
GIVEN("an empty variable") {
THEN("should return the default value") {
REQUIRE(environment::get_int("EMPTYVAR", 123) == 123);
}
boost::nowide::unsetenv("EMPTYVAR");
}
GIVEN("a non-existent variable") {
THEN("should return the default value") {
REQUIRE(environment::get_int("NOVAR", 123) == 123);
}
}
}

SCENARIO("setting an environment variable") {
REQUIRE_FALSE(boost::nowide::getenv(""));
GIVEN("a non-empty value") {
Expand Down

0 comments on commit 83ad3f7

Please sign in to comment.