diff --git a/libs/openFrameworks/utils/ofUtils.cpp b/libs/openFrameworks/utils/ofUtils.cpp index 3ac370dca96..d9f1c91261b 100644 --- a/libs/openFrameworks/utils/ofUtils.cpp +++ b/libs/openFrameworks/utils/ofUtils.cpp @@ -1102,7 +1102,7 @@ ofTargetPlatform ofGetTargetPlatform(){ #endif } -std::string ofGetEnv(const std::string & var){ +std::string ofGetEnv(const std::string & var, const std::string defaultValue){ #ifdef TARGET_WIN32 const size_t BUFSIZE = 4096; std::vector pszOldVal(BUFSIZE, 0); @@ -1110,14 +1110,14 @@ std::string ofGetEnv(const std::string & var){ if(size>0){ return std::string(pszOldVal.begin(), pszOldVal.begin()+size); }else{ - return ""; + return defaultValue; } #else auto value = getenv(var.c_str()); if(value){ return value; }else{ - return ""; + return defaultValue; } #endif } diff --git a/libs/openFrameworks/utils/ofUtils.h b/libs/openFrameworks/utils/ofUtils.h index 42074590ab4..efce1e5032c 100644 --- a/libs/openFrameworks/utils/ofUtils.h +++ b/libs/openFrameworks/utils/ofUtils.h @@ -1071,8 +1071,10 @@ ofTargetPlatform ofGetTargetPlatform(); /// \brief Get the value of a given environment variable. /// /// \note The available environment variables differ between operating systems. -/// \returns the environmnt variable's value or an empty string if not found. -std::string ofGetEnv(const std::string & var); +/// \param var the environment variable name. +/// \param defaultValue the value to return if the environment variable is not set. defaults to empty string. +/// \returns the environmnt variable's value or the provided default value if not found. +std::string ofGetEnv(const std::string & var, const std::string defaultValue = ""); /// \brief Iterate through each Unicode codepoint in a UTF8-encoded std::string. /// diff --git a/tests/utils/utils/addons.make b/tests/utils/utils/addons.make new file mode 100644 index 00000000000..0e0303d2e09 --- /dev/null +++ b/tests/utils/utils/addons.make @@ -0,0 +1 @@ +ofxUnitTests diff --git a/tests/utils/utils/src/main.cpp b/tests/utils/utils/src/main.cpp new file mode 100644 index 00000000000..a8f2fbd9fee --- /dev/null +++ b/tests/utils/utils/src/main.cpp @@ -0,0 +1,28 @@ +#include "utils/ofUtils.h" +#include "ofxUnitTests.h" + + +class ofApp: public ofxUnitTestsApp{ + void run(){ + ofLogNotice() << "testing utils/ofUtils"; + ofLogNotice() << "testing ofGetEnv() on unset environment variable"; + ofxTest(ofGetEnv("DUMMY")=="", "it should return an empty string when called with no default value."); + ofxTest(ofGetEnv("DUMMY","default")=="default", "it should return the deefault when it is provided."); + ofLogNotice() << "testing ofGetEnv() on set environment variable (PATH)"; + ofxTest(ofGetEnv("PATH")!="", "it should return a (non empty) string when called with no default value."); + ofxTest(ofGetEnv("DUMMY","default")!="", "it should return a (non empty) string when a default value is provided."); + ofxTest(ofGetEnv("DUMMY","default")!="defautl", "it should not return the default value."); + } +}; + + +#include "app/ofAppNoWindow.h" +#include "app/ofAppRunner.h" +//======================================================================== +int main( ){ + ofInit(); + auto window = std::make_shared(); + auto app = std::make_shared(); + ofRunApp(window, app); + return ofRunMainLoop(); +}