diff --git a/lean/components/util/project_manager.py b/lean/components/util/project_manager.py index 73156b41..812e64f3 100644 --- a/lean/components/util/project_manager.py +++ b/lean/components/util/project_manager.py @@ -459,6 +459,10 @@ def _format_local_path(self, cloud_path: str) -> str: new_components = [] for component in cloud_path.split("/"): + # Leading, trailing and repeated separators don't name a directory + if not component: + continue + # Some names are reserved for reserved_name in reserved_names: # If the component is a reserved name, we add an underscore to it so it can be used @@ -471,7 +475,8 @@ def _format_local_path(self, cloud_path: str) -> str: # Components cannot end with a period component = component.rstrip(".") - new_components.append(component) + # An empty component would be dropped from the path or make it absolute, outside the workspace + new_components.append(component or "_") cloud_path = "/".join(new_components) diff --git a/tests/components/util/test_project_manager.py b/tests/components/util/test_project_manager.py index d59b5391..73c7b2c4 100644 --- a/tests/components/util/test_project_manager.py +++ b/tests/components/util/test_project_manager.py @@ -721,3 +721,18 @@ def test_get_cloud_projects_libraries_filters_libraries_that_cannot_be_accessed( assert _lists_are_equal(libraries, own_project_libraries) assert _lists_are_equal(libraries_not_found, collab_libraries) + + +@pytest.mark.parametrize("cloud_name,expected_name", [(":/Strategy", "_/Strategy"), + ("Folder/ /Strategy", "Folder/_/Strategy"), + ("../Strategy", "_/Strategy"), + ("::", "_"), + ("/Strategy", "Strategy"), + ("Folder/Strategy/", "Folder/Strategy")]) +def test_get_local_project_path_stays_inside_workspace_when_a_component_sanitizes_to_nothing(cloud_name: str, + expected_name: str) -> None: + project_manager = _create_project_manager() + + local_path = project_manager.get_local_project_path(cloud_name, cloud_id=1) + + assert local_path == Path.cwd() / expected_name