diff --git a/src/config/config.cc b/src/config/config.cc index 39346cd5..e331e08c 100644 --- a/src/config/config.cc +++ b/src/config/config.cc @@ -16,6 +16,7 @@ * limitations under the License. */ #include "config.h" +#include namespace clanguml { namespace config { @@ -25,6 +26,12 @@ config load(const std::string &config_file) try { YAML::Node doc = YAML::LoadFile(config_file); + // Store the parent path of the config_file to properly resolve + // the include files paths + auto config_file_path = std::filesystem::path{config_file}; + doc.force_insert( + "__parent_path", config_file_path.parent_path().string()); + return doc.as(); } catch (YAML::BadFile &e) { @@ -416,8 +423,11 @@ template <> struct convert { std::shared_ptr diagram_config{}; if (has_key(d.second, "include!")) { - YAML::Node node = - YAML::LoadFile(d.second["include!"].as()); + auto parent_path = node["__parent_path"].as(); + auto include_path = std::filesystem::path{parent_path}; + include_path /= d.second["include!"].as(); + + YAML::Node node = YAML::LoadFile(include_path.string()); diagram_config = parse_diagram_config(node); } diff --git a/tests/test_config.cc b/tests/test_config.cc index 93546ef9..159ca2be 100644 --- a/tests/test_config.cc +++ b/tests/test_config.cc @@ -51,4 +51,24 @@ TEST_CASE("Test config inherited", "[unit-test]") CHECK(cus.glob()[0] == "src/main.cc"); CHECK(clanguml::util::contains(cus.using_namespace(), "clanguml::ns1")); CHECK(cus.include_relations_also_as_members()); +} + +TEST_CASE("Test config includes", "[unit-test]") +{ + auto cfg = clanguml::config::load("./test_config_data/includes.yml"); + + CHECK(cfg.diagrams.size() == 2); + auto &def = *cfg.diagrams["class_1"]; + CHECK(def.type() == clanguml::config::diagram_type::class_diagram); + CHECK(def.glob().size() == 2); + CHECK(def.glob()[0] == "src/**/*.cc"); + CHECK(def.glob()[1] == "src/**/*.h"); + CHECK(clanguml::util::contains(def.using_namespace(), "clanguml")); + + auto &cus = *cfg.diagrams["class_2"]; + CHECK(cus.type() == clanguml::config::diagram_type::class_diagram); + CHECK(cus.glob().size() == 1); + CHECK(cus.glob()[0] == "src/main.cc"); + CHECK(clanguml::util::contains(cus.using_namespace(), "clanguml::ns1")); + CHECK(cus.include_relations_also_as_members()); } \ No newline at end of file diff --git a/tests/test_config_data/includes.yml b/tests/test_config_data/includes.yml new file mode 100644 index 00000000..edbf64e4 --- /dev/null +++ b/tests/test_config_data/includes.yml @@ -0,0 +1,18 @@ +compilation_database_dir: debug +output_directory: output +include_relations_also_as_members: false +using_namespace: + - clanguml +include: + namespaces: + - clanguml + - ABCD +glob: + - src/**/*.cc + - src/**/*.h + +diagrams: + class_1: + include!: includes1.yml + class_2: + include!: includes2.yml diff --git a/tests/test_config_data/includes1.yml b/tests/test_config_data/includes1.yml new file mode 100644 index 00000000..ed1b5e31 --- /dev/null +++ b/tests/test_config_data/includes1.yml @@ -0,0 +1 @@ +type: class \ No newline at end of file diff --git a/tests/test_config_data/includes2.yml b/tests/test_config_data/includes2.yml new file mode 100644 index 00000000..623edbd5 --- /dev/null +++ b/tests/test_config_data/includes2.yml @@ -0,0 +1,6 @@ +type: class +using_namespace: + - clanguml::ns1 +include_relations_also_as_members: true +glob: + - src/main.cc