Skip to content

Commit

Permalink
add _validate_module_names param
Browse files Browse the repository at this point in the history
  • Loading branch information
tsv1 committed Jan 24, 2024
1 parent 6e515c5 commit 0977cba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
1 change: 1 addition & 0 deletions tests/core/scenario_loader/test_scenario_file_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class Scenario(vedro.Scenario):
pass
'''))
loader = ScenarioFileLoader()
loader._validate_module_names = True

with when, raises(BaseException) as exc:
await loader.load(path)
Expand Down
25 changes: 17 additions & 8 deletions vedro/core/scenario_loader/_scenario_file_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ class ScenarioFileLoader(ScenarioLoader):
A class responsible for loading Vedro scenarios from a file.
"""

def __init__(self) -> None:
"""
Initialize the ScenarioFileLoader.
The loader is responsible for loading Vedro scenarios from a file.
"""
self._validate_module_names: bool = False

async def load(self, path: Path) -> List[Type[Scenario]]:
"""
Load Vedro scenarios from a module at the given path.
Expand Down Expand Up @@ -59,14 +67,15 @@ def _path_to_module_name(self, path: Path) -> str:
:raises ValueError: If any part of the path is not a valid Python identifier.
"""
parts = path.with_suffix("").parts
for part in parts:
if not self._is_valid_identifier(part):
raise ValueError(
f"The module name derived from the path '{path}' is invalid "
f"due to the segment '{part}'. A valid module name should "
"start with a letter or underscore, contain only letters, "
"digits, or underscores, and not be a Python keyword."
)
if self._validate_module_names:
for part in parts:
if not self._is_valid_identifier(part):
raise ValueError(
f"The module name derived from the path '{path}' is invalid "
f"due to the segment '{part}'. A valid module name should "
"start with a letter or underscore, contain only letters, "
"digits, or underscores, and not be a Python keyword."
)
return ".".join(parts)

def _is_valid_identifier(self, name: str) -> bool:
Expand Down

0 comments on commit 0977cba

Please sign in to comment.