diff --git a/lean/components/util/project_manager.py b/lean/components/util/project_manager.py index 73156b41..75f3cb88 100644 --- a/lean/components/util/project_manager.py +++ b/lean/components/util/project_manager.py @@ -442,7 +442,7 @@ def _format_local_path(self, cloud_path: str) -> str: """Converts the given cloud path into a local path which is valid for the current operating system. :param cloud_path: the path of the project in the cloud - :return: the converted cloud_path so that it is valid locally + :return: a valid relative local path, or "_" if the cloud path names no components """ # Remove forbidden characters and OS-specific path separator that are not path separators on QuantConnect # Windows, \":*?"<>| are forbidden @@ -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,11 +475,10 @@ def _format_local_path(self, cloud_path: str) -> str: # Components cannot end with a period component = component.rstrip(".") - new_components.append(component) - - cloud_path = "/".join(new_components) + # An empty component would be dropped from the path or make it absolute, outside the workspace + new_components.append(component or "_") - return cloud_path + return "/".join(new_components) or "_" def _generate_python_library_projects_config(self) -> None: diff --git a/tests/components/util/test_project_manager.py b/tests/components/util/test_project_manager.py index d59b5391..c8d7a48a 100644 --- a/tests/components/util/test_project_manager.py +++ b/tests/components/util/test_project_manager.py @@ -721,3 +721,20 @@ 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