Skip to content

Commit

Permalink
Generalize to include the case no output_root in realization
Browse files Browse the repository at this point in the history
  • Loading branch information
stcui007 committed Jul 19, 2024
1 parent ed97ff8 commit 79853a8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
25 changes: 13 additions & 12 deletions include/realizations/catchment/Formulation_Manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,27 +295,28 @@ namespace realization {
*/
std::string get_output_root() const {
const auto output_root = this->tree.get_optional<std::string>("output_root");
std::string str;
if (output_root != boost::none && *output_root != "") {
// Check if the path ends with a trailing slash,
// otherwise add it.
str = output_root->back() == '/'
std::string str = output_root->back() == '/'
? *output_root
: *output_root + "/";
}

const char* dir = str.c_str();
const char* dir = str.c_str();

//use C++ system function to check if there is a dir match that defined in realization
struct stat sb;
if (stat(dir, &sb) == 0 && S_ISDIR(sb.st_mode))
return dir;
else {
//throw std::runtime_error("output_root directory does not exist, please create one matching that in realization");
mkdir(dir, 0755);
//use C++ system function to check if there is a dir match that defined in realization
struct stat sb;
if (stat(dir, &sb) == 0 && S_ISDIR(sb.st_mode))
return dir;
else {
//throw std::runtime_error("output_root directory does not exist, please create one matching that in realization");
mkdir(dir, 0755);
return dir;
}
}

return dir;
//for case where there is no output_root in the realization file
return "./";
}

/**
Expand Down
5 changes: 3 additions & 2 deletions test/realizations/Formulation_Manager_Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ class Formulation_Manager_Test : public ::testing::Test {
const double EPSILON = 0.0000001;

const std::string EXAMPLE_1 = "{ "
"\"output_root\": \"./output_dir/\","
"\"global\": { "
"\"formulations\": [ "
"{"
Expand Down Expand Up @@ -264,6 +263,7 @@ const std::string EXAMPLE_1 = "{ "
"} "
"}";
const std::string EXAMPLE_2 = "{ "
"\"output_root\": \"./output_dir/\","
"\"global\": { "
"\"formulations\": [ "
"{"
Expand Down Expand Up @@ -719,7 +719,7 @@ TEST_F(Formulation_Manager_Test, basic_reading_1) {

ASSERT_TRUE(manager.contains("cat-52"));
ASSERT_TRUE(manager.contains("cat-67"));
ASSERT_EQ(manager.get_output_root(), "./output_dir/");
ASSERT_EQ(manager.get_output_root(), "./");
}

TEST_F(Formulation_Manager_Test, basic_reading_2) {
Expand All @@ -742,6 +742,7 @@ TEST_F(Formulation_Manager_Test, basic_reading_2) {

ASSERT_TRUE(manager.contains("cat-52"));
ASSERT_TRUE(manager.contains("cat-67"));
ASSERT_EQ(manager.get_output_root(), "./output_dir/");
}

TEST_F(Formulation_Manager_Test, basic_run_1) {
Expand Down

0 comments on commit 79853a8

Please sign in to comment.