22import os
33import sys
44from typing import Iterator
5+ from unittest import mock
56
67import pytest
78
89from pylint .lint import fix_import_path
10+ from pylint .pyreverse import main
911
1012TEST_DATA_DIR = os .path .abspath (os .path .join (os .path .dirname (__file__ ), ".." , "data" ))
1113PROJECT_ROOT_DIR = os .path .abspath (os .path .join (TEST_DATA_DIR , ".." ))
1214
1315
16+ @pytest .fixture (name = "mock_subprocess" )
17+ def mock_utils_subprocess ():
18+ with mock .patch ("pylint.pyreverse.utils.subprocess" ) as mock_subprocess :
19+ yield mock_subprocess
20+
21+
22+ @pytest .fixture
23+ def mock_graphviz (mock_subprocess ):
24+ mock_subprocess .run .return_value = mock .Mock (
25+ stderr = (
26+ 'Format: "XYZ" not recognized. Use one of: '
27+ "bmp canon cgimage cmap cmapx cmapx_np dot dot_json eps exr fig gd "
28+ "gd2 gif gv icns ico imap imap_np ismap jp2 jpe jpeg jpg json json0 "
29+ "mp pct pdf pic pict plain plain-ext png pov ps ps2 psd sgi svg svgz "
30+ "tga tif tiff tk vdx vml vmlz vrml wbmp webp xdot xdot1.2 xdot1.4 xdot_json"
31+ )
32+ )
33+ with mock .patch ("pylint.pyreverse.utils.shutil" ) as mock_shutil :
34+ mock_shutil .which .return_value = "/usr/bin/dot"
35+ yield
36+
37+
1438@pytest .fixture (params = [PROJECT_ROOT_DIR , TEST_DATA_DIR ])
1539def setup_path (request ) -> Iterator :
1640 current_sys_path = list (sys .path )
@@ -29,3 +53,64 @@ def test_project_root_in_sys_path():
2953 """
3054 with fix_import_path ([TEST_DATA_DIR ]):
3155 assert sys .path == [PROJECT_ROOT_DIR ]
56+
57+
58+ @mock .patch ("pylint.pyreverse.main.Linker" , new = mock .MagicMock ())
59+ @mock .patch ("pylint.pyreverse.main.DiadefsHandler" , new = mock .MagicMock ())
60+ @mock .patch ("pylint.pyreverse.main.writer" )
61+ @pytest .mark .usefixtures ("mock_graphviz" )
62+ def test_graphviz_supported_image_format (mock_writer , capsys ):
63+ """Test that Graphviz is used if the image format is supported."""
64+ with pytest .raises (SystemExit ) as wrapped_sysexit :
65+ # we have to catch the SystemExit so the test execution does not stop
66+ main .Run (["-o" , "png" , TEST_DATA_DIR ])
67+ # Check that the right info message is shown to the user
68+ assert (
69+ "Format png is not supported natively. Pyreverse will try to generate it using Graphviz..."
70+ in capsys .readouterr ().out
71+ )
72+ # Check that pyreverse actually made the call to create the diagram and we exit cleanly
73+ mock_writer .DiagramWriter ().write .assert_called_once ()
74+ assert wrapped_sysexit .value .code == 0
75+
76+
77+ @mock .patch ("pylint.pyreverse.main.Linker" , new = mock .MagicMock ())
78+ @mock .patch ("pylint.pyreverse.main.DiadefsHandler" , new = mock .MagicMock ())
79+ @mock .patch ("pylint.pyreverse.main.writer" )
80+ @pytest .mark .usefixtures ("mock_graphviz" )
81+ def test_graphviz_cant_determine_supported_formats (
82+ mock_writer , mock_subprocess , capsys
83+ ):
84+ """Test that Graphviz is used if the image format is supported."""
85+ mock_subprocess .run .return_value .stderr = "..."
86+ with pytest .raises (SystemExit ) as wrapped_sysexit :
87+ # we have to catch the SystemExit so the test execution does not stop
88+ main .Run (["-o" , "png" , TEST_DATA_DIR ])
89+ # Check that the right info message is shown to the user
90+ assert (
91+ "Unable to determine Graphviz supported output formats."
92+ in capsys .readouterr ().out
93+ )
94+ # Check that pyreverse actually made the call to create the diagram and we exit cleanly
95+ mock_writer .DiagramWriter ().write .assert_called_once ()
96+ assert wrapped_sysexit .value .code == 0
97+
98+
99+ @mock .patch ("pylint.pyreverse.main.Linker" , new = mock .MagicMock ())
100+ @mock .patch ("pylint.pyreverse.main.DiadefsHandler" , new = mock .MagicMock ())
101+ @mock .patch ("pylint.pyreverse.main.writer" , new = mock .MagicMock ())
102+ @pytest .mark .usefixtures ("mock_graphviz" )
103+ def test_graphviz_unsupported_image_format (capsys ):
104+ """Test that Graphviz is used if the image format is supported."""
105+ with pytest .raises (SystemExit ) as wrapped_sysexit :
106+ # we have to catch the SystemExit so the test execution does not stop
107+ main .Run (["-o" , "somethingElse" , TEST_DATA_DIR ])
108+ # Check that the right info messages are shown to the user
109+ stdout = capsys .readouterr ().out
110+ assert (
111+ "Format somethingElse is not supported natively. Pyreverse will try to generate it using Graphviz..."
112+ in stdout
113+ )
114+ assert "Format somethingElse is not supported by Graphviz. It supports:" in stdout
115+ # Check that we exited with the expected error code
116+ assert wrapped_sysexit .value .code == 32
0 commit comments