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>
diff --git a/env.cpp b/env.cpp
index 09dd985..6841987 100644
--- a/env.cpp
+++ b/env.cpp
@@ -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(