diff options
author | Patrick Venture <venture@google.com> | 2018-04-17 19:38:06 -0700 |
---|---|---|
committer | Patrick Venture <venture@google.com> | 2018-04-18 14:25:30 -0700 |
commit | a24c8808d5c3b13b849e79115a6969b5a8faf8b0 (patch) | |
tree | 2a015c72bf15c74c4ec85b238e36270e093d88c3 | |
parent | 7a5285de708b2f46d47f37920c784314b4ad55aa (diff) | |
download | phosphor-hwmon-a24c8808d5c3b13b849e79115a6969b5a8faf8b0.tar.gz phosphor-hwmon-a24c8808d5c3b13b849e79115a6969b5a8faf8b0.zip |
move calls to cstdlib::getenv to env::getEnv
Wrapped the cstdlib::getenv() call into env::getEnv
so that it can be tested by mocking the env namespace,
by just dropping in test_env.cpp which implements those
methods and tying them a singleton upon which we can set
expectations.
Note: regardless of the approach taken to mock this, wrapping
this method is a good fix.
Change-Id: I65d22da08fc3dd76e6860f728c54e6c847ed07de
Signed-off-by: Patrick Venture <venture@google.com>
-rw-r--r-- | env.cpp | 14 | ||||
-rw-r--r-- | env.hpp | 10 | ||||
-rw-r--r-- | mainloop.cpp | 10 | ||||
-rw-r--r-- | targets.hpp | 4 |
4 files changed, 24 insertions, 14 deletions
@@ -22,23 +22,23 @@ namespace env { +std::string getEnv(const char* key) +{ + auto value = std::getenv(key); + return (value) ? std::string(value) : std::string(); +} + std::string getEnv( const char* prefix, const SensorSet::key_type& sensor) { std::string key; - std::string value; key.assign(prefix); key.append(1, '_'); key.append(sensor.first); key.append(sensor.second); - auto env = getenv(key.c_str()); - if (env) - { - value.assign(env); - } - return value; + return getEnv(key.c_str()); } std::string getEnv( @@ -8,6 +8,16 @@ namespace env { /** @brief Reads an environment variable * + * Reads the environment for that key + * + * @param[in] key - the key + * + * @return string - the env var value + */ +std::string getEnv(const char* key); + +/** @brief Reads an environment variable + * * Reads <prefix>_<sensor.first><sensor.second> * * @param[in] prefix - the variable prefix diff --git a/mainloop.cpp b/mainloop.cpp index 99c8020..37c2316 100644 --- a/mainloop.cpp +++ b/mainloop.cpp @@ -287,8 +287,8 @@ void MainLoop::getObject(SensorSet::container_t::const_reference sensor) { // Get list of return codes for removing sensors on device std::string deviceRmRCs; - auto devRmRCs = getenv("REMOVERCS"); - if (devRmRCs) + auto devRmRCs = env::getEnv("REMOVERCS"); + if (!devRmRCs.empty()) { deviceRmRCs.assign(devRmRCs); } @@ -534,10 +534,10 @@ void MainLoop::init() } { - auto interval = getenv("INTERVAL"); - if (interval) + auto interval = env::getEnv("INTERVAL"); + if (!interval.empty()) { - _interval = strtoull(interval, NULL, 10); + _interval = strtoull(interval.c_str(), NULL, 10); } } } diff --git a/targets.hpp b/targets.hpp index fe256a3..02bdd4f 100644 --- a/targets.hpp +++ b/targets.hpp @@ -103,8 +103,8 @@ std::shared_ptr<T> addTarget(const SensorSet::key_type& sensor, if (fs::exists(sysfsFullPath)) { auto useTarget = true; - auto tmEnv = getenv("TARGET_MODE"); - if (tmEnv) + auto tmEnv = env::getEnv("TARGET_MODE"); + if (!tmEnv.empty()) { std::string mode{tmEnv}; std::transform(mode.begin(), mode.end(), mode.begin(), toupper); |