Skip to content

Commit

Permalink
Fix fatal error when GraphViz is missing
Browse files Browse the repository at this point in the history
Ref. eng/recordflux/RecordFlux#1499
  • Loading branch information
treiher committed Dec 22, 2023
1 parent 897a815 commit 12e8d25
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fatal error when comparing opaque fields (AdaCore/RecordFlux#1294, eng/recordflux/RecordFlux#1497)
- Fatal error when GraphViz is missing (eng/recordflux/RecordFlux#1499)
- Missing rejection of sequences of parameterized messages (eng/recordflux/RecordFlux#1439)

### Removed
Expand Down
18 changes: 16 additions & 2 deletions rflx/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
from pathlib import Path
from typing import Optional

from pydotplus import Dot, Edge, Node
from pydotplus import Dot, Edge, InvocationException, Node # type: ignore[attr-defined]

from rflx.error import RecordFluxError, Severity, Subsystem
from rflx.expression import TRUE, UNDEFINED
from rflx.identifier import ID
from rflx.model import FINAL_STATE, AbstractSession, Link, Message
Expand Down Expand Up @@ -48,7 +49,20 @@ def write_graph(graph: Dot, filename: Path, fmt: str = "svg") -> None:
log.info("Creating %s", filename)

with filename.open("wb") as f:
graph.write(f, format=fmt)
try:
graph.write(f, format=fmt)
except InvocationException as e:
RecordFluxError(
[
(e, Subsystem.GRAPH, Severity.ERROR, None),
(
"GraphViz is required for creating graphs",
Subsystem.GRAPH,
Severity.INFO,
None,
),
],
).propagate()


def create_message_graph(message: Message) -> Dot:
Expand Down
22 changes: 21 additions & 1 deletion tests/unit/graph_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from pathlib import Path

from pydotplus import Dot
import pytest
from pydotplus import Dot, InvocationException # type: ignore[attr-defined]

from rflx.error import RecordFluxError
from rflx.expression import FALSE, TRUE, Equal, Greater, Less, Number, Pow, Sub, Variable
from rflx.graph import create_message_graph, create_session_graph, write_graph
from rflx.identifier import ID
Expand Down Expand Up @@ -244,3 +246,21 @@ def test_session_graph(tmp_path: Path) -> None:
"""

assert_graph(create_session_graph(s, ignore=[r"^IGNORED_"]), expected_filtered, tmp_path)


def test_missing_graphviz(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
def write_mock(self: object, path: object, format: str = "") -> object: # noqa: ARG001, A002
raise InvocationException("GraphViz not found")

monkeypatch.setattr(Dot, "write", write_mock)

with pytest.raises(
RecordFluxError,
match=(
r"^"
r"graph: error: GraphViz not found\n"
r"graph: info: GraphViz is required for creating graphs"
r"$"
),
):
write_graph(Dot(""), tmp_path / "graph")

0 comments on commit 12e8d25

Please sign in to comment.