Skip to content

Commit

Permalink
[pyreverse] Bugfix: strip "/" at the end of the file (#8517) (#8528)
Browse files Browse the repository at this point in the history
Signed-off-by: Alvaro Frias Garay <[email protected]>
Co-authored-by: Pierre Sassoulas <[email protected]>
(cherry picked from commit 6ad17fb)

Co-authored-by: Alvaro Frias <[email protected]>
  • Loading branch information
github-actions[bot] and qequ authored Apr 2, 2023
1 parent 538c41b commit 6dda042
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/8504.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a crash in pyreverse when "/" characters are used in the output filename e.g pyreverse -o png -p name/ path/to/project.

Closes #8504
2 changes: 1 addition & 1 deletion pylint/pyreverse/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self, config: argparse.Namespace) -> None:
def write(self, diadefs: Iterable[ClassDiagram | PackageDiagram]) -> None:
"""Write files for <project> according to <diadefs>."""
for diagram in diadefs:
basename = diagram.title.strip().replace(" ", "_")
basename = diagram.title.strip().replace("/", "_").replace(" ", "_")
file_name = f"{basename}.{self.config.output_format}"
if os.path.exists(self.config.output_directory):
file_name = os.path.join(self.config.output_directory, file_name)
Expand Down
17 changes: 17 additions & 0 deletions tests/pyreverse/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,20 @@ def test_color_for_stdlib_module(default_config: PyreverseConfig) -> None:
obj.node = Mock()
obj.node.qname.return_value = "collections"
assert writer.get_shape_color(obj) == "grey"


def test_package_name_with_slash(default_config: PyreverseConfig) -> None:
"""Test to check the names of the generated files are corrected
when using an incorrect character like "/" in the package name.
"""
writer = DiagramWriter(default_config)
obj = Mock()

obj.objects = []
obj.get_relationships.return_value = []
obj.title = "test/package/name/with/slash/"
writer.write([obj])

assert os.path.exists("test_package_name_with_slash_.dot")
# remove the generated file
os.remove("test_package_name_with_slash_.dot")

0 comments on commit 6dda042

Please sign in to comment.