Added default diagram generation error for empty diagrams (#246)

This commit is contained in:
Bartek Kryza 2024-03-04 19:55:55 +01:00
parent cb44c3ded4
commit baef768f6c
No known key found for this signature in database
GPG Key ID: 241B25F44E85B4D7
27 changed files with 315 additions and 10 deletions

View File

@ -249,6 +249,11 @@ void diagram::remove_redundant_dependencies()
}
}
bool diagram::is_empty() const
{
return element_view<class_>::is_empty() &&
element_view<enum_>::is_empty() && element_view<concept_>::is_empty();
}
} // namespace clanguml::class_diagram::model
namespace clanguml::common::model {

View File

@ -249,6 +249,13 @@ public:
*/
inja::json context() const override;
/**
* @brief Check whether the diagram is empty
*
* @return True, if diagram is empty
*/
bool is_empty() const override;
private:
template <typename ElementT>
bool add_with_namespace_path(std::unique_ptr<ElementT> &&e);

View File

@ -107,6 +107,8 @@ cli_flow_t cli_handler::parse(int argc, const char **argv)
"Query the specific compiler driver to extract system paths and add to "
"compile commands (e.g. arm-none-eabi-g++)");
#endif
app.add_flag("--allow-empty-diagrams", allow_empty_diagrams,
"Do not raise an error when generated diagram model is empty");
app.add_option(
"--add-class-diagram", add_class_diagram, "Add class diagram config");
app.add_option("--add-sequence-diagram", add_sequence_diagram,
@ -302,6 +304,10 @@ cli_flow_t cli_handler::handle_post_config_options()
LOG_INFO("Loaded clang-uml config from {}", config_path);
if (allow_empty_diagrams) {
config.allow_empty_diagrams.set(true);
}
//
// Override selected config options from command line
//

View File

@ -165,6 +165,7 @@ public:
bool list_diagrams{false};
bool quiet{false};
bool initialize{false};
bool allow_empty_diagrams{false};
std::optional<std::vector<std::string>> add_compile_flag;
std::optional<std::vector<std::string>> remove_compile_flag;
#if !defined(_WIN32)

View File

@ -95,11 +95,17 @@ void generate_diagram_select_generator(const std::string &od,
using diagram_generator =
typename diagram_generator_t<DiagramConfig, GeneratorTag>::type;
std::stringstream buffer;
buffer << diagram_generator(
dynamic_cast<DiagramConfig &>(*diagram), *model);
// Only open the file after the diagram has been generated successfully
// in order not to overwrite previous diagram in case of failure
auto path = std::filesystem::path{od} /
fmt::format("{}.{}", name, GeneratorTag::extension);
std::ofstream ofs;
ofs.open(path, std::ofstream::out | std::ofstream::trunc);
ofs << diagram_generator(dynamic_cast<DiagramConfig &>(*diagram), *model);
ofs << buffer.str();
ofs.close();
@ -258,11 +264,12 @@ void generate_diagrams(const std::vector<std::string> &diagram_names,
if (indicator)
indicator->complete(name);
}
catch (std::exception &e) {
catch (const std::exception &e) {
if (indicator)
indicator->fail(name);
LOG_ERROR(e.what());
LOG_ERROR(
"ERROR: Failed to generate diagram {}: {}", name, e.what());
}
};

View File

@ -107,11 +107,19 @@ std::ostream &operator<<(
template <typename C, typename D>
void generator<C, D>::generate(std::ostream &ostr) const
{
const auto &config = generators::generator<C, D>::config();
const auto &model = generators::generator<C, D>::model();
if (!config.allow_empty_diagrams() && model.is_empty()) {
throw clanguml::error::empty_diagram_error{
"Diagram configuration resulted in empty diagram."};
}
nlohmann::json j;
j["name"] = generators::generator<C, D>::model().name();
j["diagram_type"] = to_string(generators::generator<C, D>::model().type());
if (generators::generator<C, D>::config().title) {
j["title"] = generators::generator<C, D>::config().title();
j["name"] = model.name();
j["diagram_type"] = to_string(model.type());
if (config.title) {
j["title"] = config.title();
}
generate_diagram(j);

View File

@ -250,6 +250,13 @@ template <typename C, typename D>
void generator<C, D>::generate(std::ostream &ostr) const
{
const auto &config = generators::generator<C, D>::config();
const auto &model = generators::generator<C, D>::model();
if (!config.allow_empty_diagrams() && model.is_empty() &&
config.mermaid().before.empty() && config.mermaid().after.empty()) {
throw clanguml::error::empty_diagram_error{
"Diagram configuration resulted in empty diagram."};
}
update_context();

View File

@ -21,6 +21,7 @@
#include "common/model/diagram_filter.h"
#include "common/model/relationship.h"
#include "config/config.h"
#include "error.h"
#include "util/error.h"
#include "util/util.h"
#include "version.h"
@ -265,9 +266,16 @@ template <typename C, typename D>
void generator<C, D>::generate(std::ostream &ostr) const
{
const auto &config = generators::generator<C, D>::config();
const auto &model = generators::generator<C, D>::model();
update_context();
if (!config.allow_empty_diagrams() && model.is_empty() &&
config.puml().before.empty() && config.puml().after.empty()) {
throw clanguml::error::empty_diagram_error{
"Diagram configuration resulted in empty diagram."};
}
ostr << "@startuml" << '\n';
generate_title(ostr);

View File

@ -164,6 +164,13 @@ public:
*/
virtual inja::json context() const = 0;
/**
* @brief Check whether the diagram is empty
*
* @return True, if diagram is empty
*/
virtual bool is_empty() const = 0;
private:
std::string name_;
std::unique_ptr<diagram_filter> filter_;

View File

@ -69,6 +69,13 @@ public:
return {};
}
/**
* @brief Check whether the element view is empty
*
* @return True, if the view does not contain any elements
*/
bool is_empty() const { return elements_.empty(); }
private:
reference_vector<T> elements_;
};

View File

@ -237,6 +237,7 @@ void inheritable_diagram_options::inherit(
parent.generate_condition_statements);
debug_mode.override(parent.debug_mode);
generate_metadata.override(parent.generate_metadata);
allow_empty_diagrams.override(parent.allow_empty_diagrams);
type_aliases.override(parent.type_aliases);
}

View File

@ -577,6 +577,7 @@ struct inheritable_diagram_options {
"message_comment_width", clanguml::util::kDefaultMessageCommentWidth};
option<bool> debug_mode{"debug_mode", false};
option<bool> generate_metadata{"generate_metadata", true};
option<bool> allow_empty_diagrams{"allow_empty_diagrams", false};
protected:
// This is the relative path with respect to the `base_directory`,

View File

@ -306,6 +306,7 @@ root:
query_driver: !optional string
add_compile_flags: !optional [string]
remove_compile_flags: !optional [string]
allow_empty_diagrams: !optional bool
diagram_templates: !optional diagram_templates_t
diagrams: !required map_t<string;diagram_t>
#

View File

@ -820,6 +820,7 @@ template <> struct convert<config> {
get_option(node, rhs.using_module);
get_option(node, rhs.output_directory);
get_option(node, rhs.query_driver);
get_option(node, rhs.allow_empty_diagrams);
get_option(node, rhs.compilation_database_dir);
get_option(node, rhs.add_compile_flags);
get_option(node, rhs.remove_compile_flags);

View File

@ -151,6 +151,9 @@ inja::json diagram::context() const
return ctx;
}
bool diagram::is_empty() const { return element_view<source_file>::is_empty(); }
} // namespace clanguml::include_diagram::model
namespace clanguml::common::model {

View File

@ -131,6 +131,13 @@ public:
const common::model::namespace_ &ns) const override;
inja::json context() const override;
/**
* @brief Check whether the diagram is empty
*
* @return True, if diagram is empty
*/
bool is_empty() const override;
};
template <typename ElementT>

View File

@ -73,6 +73,8 @@ inja::json diagram::context() const
return ctx;
}
bool diagram::is_empty() const { return element_view<package>::is_empty(); }
} // namespace clanguml::package_diagram::model
namespace clanguml::common::model {

View File

@ -157,6 +157,13 @@ public:
*/
inja::json context() const override;
/**
* @brief Check whether the diagram is empty
*
* @return True, if diagram is empty
*/
bool is_empty() const override;
private:
/**
* @brief Add element using module as diagram path

View File

@ -399,6 +399,11 @@ std::vector<message_chain_t> diagram::get_all_from_to_message_chains(
return message_chains_unique;
}
bool diagram::is_empty() const
{
return sequences_.empty() || participants_.empty();
}
void diagram::print() const
{
LOG_TRACE(" --- Participants ---");

View File

@ -280,6 +280,13 @@ public:
*/
void finalize() override;
/**
* @brief Check whether the diagram is empty
*
* @return True, if diagram is empty
*/
bool is_empty() const override;
private:
/**
* This method checks the last messages in sequence (current_messages),

View File

@ -36,4 +36,8 @@ class compilation_database_error : public std::runtime_error {
using std::runtime_error::runtime_error;
};
class empty_diagram_error : public std::runtime_error {
using std::runtime_error::runtime_error;
};
} // namespace clanguml::error

View File

@ -1,3 +1,4 @@
allow_empty_diagrams: true
diagrams:
t90000_class:
type: class

24
tests/t90001/.clang-uml Normal file
View File

@ -0,0 +1,24 @@
diagrams:
t90001_class:
type: class
include:
namespaces:
- no_such_namespace
t90001_sequence:
type: sequence
include:
namespaces:
- no_such_namespace
from:
- function: "nowhere"
t90001_include:
type: include
include:
namespaces:
- no_such_namespace
t90001_package:
type: package
include:
namespaces:
- no_such_namespace

88
tests/t90001/t90001.cc Normal file
View File

@ -0,0 +1,88 @@
#include <vector>
namespace clanguml {
namespace t90001 {
namespace ns1 {
/// \brief This is class A
class A {
public:
/// Abstract foo_a
virtual void foo_a() = 0;
/// Abstract foo_c
virtual void foo_c() = 0;
};
/// \brief This is class B
class B : public A {
public:
virtual void foo_a() override { }
};
/// @brief This is class C - class C has a long comment
///
/// Vivamus integer non suscipit taciti mus etiam at primis tempor sagittis sit,
/// euismod libero facilisi aptent elementum felis blandit cursus gravida sociis
/// erat ante, eleifend lectus nullam dapibus netus feugiat curae curabitur est
/// ad.
class C : public A {
public:
/// Do nothing unless override is provided
virtual void foo_c() override { }
};
/// This is class D
/// which is a little like B
/// and a little like C
class D : public B, public C {
public:
/**
* Forward foo_a
*/
void foo_a() override
{
for (auto a : as)
a->foo_a();
}
/**
* Forward foo_c
*/
void foo_c() override
{
for (auto a : as)
a->foo_c();
}
private:
/// All the A pointers
std::vector<A *> as;
};
class E : virtual public B, public virtual C {
public:
///
/// Forward foo_a
///
void foo_a() override
{
for (auto a : as)
a->foo_a();
}
///
/// Forward foo_c
///
void foo_c() override
{
for (auto a : as)
a->foo_c();
}
private:
/// All the A pointers
std::vector<A *> as;
};
} // namespace ns1
} // namespace t90001
} // namespace clanguml

92
tests/t90001/test_case.h Normal file
View File

@ -0,0 +1,92 @@
/**
* tests/t90001/test_case.cc
*
* Copyright (c) 2021-2024 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
TEST_CASE("t90001", "[test-case][config]")
{
using clanguml::error::empty_diagram_error;
auto [config, db] = load_config("t90001");
{
auto diagram = config.diagrams["t90001_class"];
auto model = generate_class_diagram(*db, diagram);
REQUIRE(model->is_empty());
REQUIRE(model->name() == "t90001_class");
REQUIRE_THROWS_AS(
generate_class_puml(diagram, *model), empty_diagram_error);
REQUIRE_THROWS_AS(
generate_class_json(diagram, *model), empty_diagram_error);
REQUIRE_THROWS_AS(
generate_class_mermaid(diagram, *model), empty_diagram_error);
}
{
auto diagram = config.diagrams["t90001_sequence"];
auto model = generate_sequence_diagram(*db, diagram);
REQUIRE(model->is_empty());
REQUIRE(model->name() == "t90001_sequence");
REQUIRE_THROWS_AS(
generate_sequence_puml(diagram, *model), empty_diagram_error);
REQUIRE_THROWS_AS(
generate_sequence_json(diagram, *model), empty_diagram_error);
REQUIRE_THROWS_AS(
generate_sequence_mermaid(diagram, *model), empty_diagram_error);
}
{
auto diagram = config.diagrams["t90001_package"];
auto model = generate_package_diagram(*db, diagram);
REQUIRE(model->is_empty());
REQUIRE(model->name() == "t90001_package");
REQUIRE_THROWS_AS(
generate_package_puml(diagram, *model), empty_diagram_error);
REQUIRE_THROWS_AS(
generate_package_json(diagram, *model), empty_diagram_error);
REQUIRE_THROWS_AS(
generate_package_mermaid(diagram, *model), empty_diagram_error);
}
{
auto diagram = config.diagrams["t90001_include"];
auto model = generate_include_diagram(*db, diagram);
REQUIRE(model->is_empty());
REQUIRE(model->name() == "t90001_include");
REQUIRE_THROWS_AS(
generate_include_puml(diagram, *model), empty_diagram_error);
REQUIRE_THROWS_AS(
generate_include_json(diagram, *model), empty_diagram_error);
REQUIRE_THROWS_AS(
generate_include_mermaid(diagram, *model), empty_diagram_error);
}
}

View File

@ -501,6 +501,7 @@ using namespace clanguml::test::matchers;
/// Other tests (e.g. configuration file)
///
#include "t90000/test_case.h"
#include "t90001/test_case.h"
///
/// Main test function

View File

@ -12,6 +12,3 @@ include:
exclude:
access: [ public, protected, private ]
relationships: [ dependency ]
plantuml:
before:
- title clang-uml top level architecture - AST visitors