From 79853a8ce13f25eb867dfa8edc9d26483869616c Mon Sep 17 00:00:00 2001 From: Shengting Cui Date: Fri, 19 Jul 2024 20:37:40 +0000 Subject: [PATCH] Generalize to include the case no output_root in realization --- .../catchment/Formulation_Manager.hpp | 25 ++++++++++--------- .../realizations/Formulation_Manager_Test.cpp | 5 ++-- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/include/realizations/catchment/Formulation_Manager.hpp b/include/realizations/catchment/Formulation_Manager.hpp index ceebcf8858..26c628e4cf 100644 --- a/include/realizations/catchment/Formulation_Manager.hpp +++ b/include/realizations/catchment/Formulation_Manager.hpp @@ -295,27 +295,28 @@ namespace realization { */ std::string get_output_root() const { const auto output_root = this->tree.get_optional("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 "./"; } /** diff --git a/test/realizations/Formulation_Manager_Test.cpp b/test/realizations/Formulation_Manager_Test.cpp index 323ef670d1..ddfd3c1cdf 100644 --- a/test/realizations/Formulation_Manager_Test.cpp +++ b/test/realizations/Formulation_Manager_Test.cpp @@ -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\": [ " "{" @@ -264,6 +263,7 @@ const std::string EXAMPLE_1 = "{ " "} " "}"; const std::string EXAMPLE_2 = "{ " + "\"output_root\": \"./output_dir/\"," "\"global\": { " "\"formulations\": [ " "{" @@ -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) { @@ -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) {