Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport maintenance/2.17.x] [pyreverse] Bugfix: strip "/" at the end of the file #8528

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")