Skip to content

Commit b3f600c

Browse files
authored
fix(cli): insert the current working directory at the beginning of the PYTHON_PATH to ease module resolution (#82)
1 parent 0fb1681 commit b3f600c

File tree

8 files changed

+48
-12
lines changed

8 files changed

+48
-12
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ poetry run pytest -v --cov=py2puml --cov-branch --cov-report term-missing --cov-
202202

203203
# Changelog
204204

205-
* `upcoming`: fix url to PlantUML logo
205+
* `0.9.1`: improved 0.7.2 by adding the current working directory at the beginning of the sys.path to resolve the module paths of the project being inspected.
206+
Fix url to PlantUML logo on the README.md page
206207
* `0.9.0`: add classes defined in `__init__.py` files to plantuml output; replaced yapf by the ruff formatter
207208
* `0.8.1`: delegated the grouping of nested namespaces (see `0.7.0`) to the PlantUML binary, which handles it natively
208209
* `0.8.0`: added support for union types, and github actions (pre-commit hooks + automated tests)

py2puml/cli.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99

1010

1111
def run():
12-
# adds the current working directory to the system path so that py2puml can import them
12+
# adds the current working directory to the system path in the first place
13+
# to ease module resolution when py2puml imports them
1314
current_working_directory = str(Path.cwd().resolve())
14-
if current_working_directory not in path:
15-
path.append(current_working_directory)
15+
path.insert(0, current_working_directory)
1616

1717
argparser = ArgumentParser(description='Generate PlantUML class diagrams to document your Python application.')
1818

19-
argparser.add_argument('-v', '--version', action='version', version='py2puml 0.9.0')
19+
argparser.add_argument('-v', '--version', action='version', version='py2puml 0.9.1')
2020
argparser.add_argument('path', metavar='path', type=str, help='the filepath to the domain')
2121
argparser.add_argument('module', metavar='module', type=str, help='the module name of the domain', default=None)
2222

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "py2puml"
3-
version = "0.9.0"
3+
version = "0.9.1"
44
description = "Generate PlantUML class diagrams to document your Python application."
55
keywords = ["class diagram", "PlantUML", "documentation", "inspection", "AST"]
66
readme = "README.md"

tests/modules/withconfusingrootpackage/test/__init__.py

Whitespace-only changes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
When attempting to resolve test.range, the python interpreter will search:
3+
- in the current working directory (and should succeed)
4+
- in the test package of the Python standard library (and should fail)
5+
6+
This non-regression test ensures that the current working directory is
7+
in the first position of the paths of PYTHON_PATH (sys.path) so that
8+
module resolution is attempted first in the inspected codebase.
9+
"""
10+
from datetime import datetime
11+
12+
13+
class Range:
14+
def __init__(self, start: datetime, stop: datetime):
15+
self.start = start
16+
self.stop = stop

tests/puml_files/test.puml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@startuml test
2+
!pragma useIntermediatePackages false
3+
4+
class test.range.Range {
5+
start: datetime
6+
stop: datetime
7+
}
8+
footer Generated by //py2puml//
9+
@enduml

tests/py2puml/test__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# Ensures the library version is modified in the pyproject.toml file when upgrading it (pull request)
55
def test_version():
6-
assert __version__ == '0.9.0'
6+
assert __version__ == '0.9.1'
77

88

99
# Description also output in the CLI

tests/py2puml/test_cli.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,21 @@ def test_cli_consistency_with_the_default_configuration(entrypoint: List[str]):
2020
assert ''.join(puml_content).strip() == cli_stdout.strip()
2121

2222

23-
def test_cli_on_specific_working_directory():
24-
command = ['py2puml', 'withrootnotincwd', 'withrootnotincwd']
25-
cli_process = run(command, stdout=PIPE, stderr=PIPE, text=True, check=True, cwd='tests/modules')
26-
27-
with open(TESTS_PATH / 'puml_files' / 'withrootnotincwd.puml', 'r', encoding='utf8') as expected_puml_file:
23+
@mark.parametrize(
24+
['command', 'current_working_directory', 'expected_puml_contents_file'],
25+
[
26+
(['python', '-m', 'py2puml', 'withrootnotincwd', 'withrootnotincwd'], 'tests/modules', 'withrootnotincwd.puml'),
27+
(['py2puml', 'withrootnotincwd', 'withrootnotincwd'], 'tests/modules', 'withrootnotincwd.puml'),
28+
(['python', '-m', 'py2puml', 'test', 'test'], 'tests/modules/withconfusingrootpackage', 'test.puml'),
29+
(['py2puml', 'test', 'test'], 'tests/modules/withconfusingrootpackage', 'test.puml'),
30+
],
31+
)
32+
def test_cli_on_specific_working_directory(
33+
command: List[str], current_working_directory: str, expected_puml_contents_file: str
34+
):
35+
cli_process = run(command, stdout=PIPE, stderr=PIPE, text=True, check=True, cwd=current_working_directory)
36+
37+
with open(TESTS_PATH / 'puml_files' / expected_puml_contents_file, 'r', encoding='utf8') as expected_puml_file:
2838
assert_multilines(
2939
# removes the last return carriage added by the stdout
3040
list(StringIO(cli_process.stdout))[:-1],

0 commit comments

Comments
 (0)