From ef512b81ec6e8d9c9fbc9ba2ce1c761379c3d564 Mon Sep 17 00:00:00 2001 From: yutianjiang39 <245600294+yutianjiang39@users.noreply.github.com> Date: Mon, 30 Nov 2026 01:19:18 +0000 Subject: [PATCH] fix: exit nonzero when a cloud push fails --- lean/commands/cloud/push.py | 7 +++-- lean/components/cloud/push_manager.py | 5 +-- tests/commands/cloud/test_push.py | 45 ++++++++++++++++++++++++--- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/lean/commands/cloud/push.py b/lean/commands/cloud/push.py index b097098c..8ce496d5 100644 --- a/lean/commands/cloud/push.py +++ b/lean/commands/cloud/push.py @@ -66,9 +66,12 @@ def push(project: Optional[Path], encrypt: Optional[bool], decrypt: Optional[boo from lean.components.util.encryption_helper import validate_encryption_key_registered_with_cloud validate_encryption_key_registered_with_cloud(key, container.organization_manager, container.api_client) - push_manager.push_project(project, encryption_action, key, force) + all_pushed = push_manager.push_project(project, encryption_action, key, force) else: if key is not None: raise RuntimeError(f"Encryption key can only be specified when pushing a single project.") projects_to_push = [p.parent for p in Path.cwd().rglob(PROJECT_CONFIG_FILE_NAME)] - push_manager.push_projects(projects_to_push, [], encryption_action, key, force) + all_pushed = push_manager.push_projects(projects_to_push, [], encryption_action, key, force) + + if not all_pushed: + raise RuntimeError("One or more projects could not be pushed") diff --git a/lean/components/cloud/push_manager.py b/lean/components/cloud/push_manager.py index 5c47c7d1..18a3e66f 100644 --- a/lean/components/cloud/push_manager.py +++ b/lean/components/cloud/push_manager.py @@ -54,15 +54,16 @@ def __init__(self, # Where each pushed project is after its push, which renames directories whose name is not valid locally or in the cloud self.pushed_project_paths: Dict[Path, Path] = {} - def push_project(self, project: Path, encryption_action: Optional[ActionType]=None, encryption_key: Optional[Path]=None, force: Optional[bool]=False) -> None: + def push_project(self, project: Path, encryption_action: Optional[ActionType]=None, encryption_key: Optional[Path]=None, force: Optional[bool]=False) -> bool: """Pushes the given project from the local drive to the cloud. It will also push every library referenced by the project and add or remove references. :param project: path to the directory containing the local project that needs to be pushed + :return: True if the project and all its referenced libraries were pushed """ libraries = self._project_manager.get_project_libraries(project) - self.push_projects([project], libraries, encryption_action, encryption_key, force) + return self.push_projects([project], libraries, encryption_action, encryption_key, force) def push_projects(self, projects_to_push: List[Path], associated_libraries_to_push: Optional[List[Path]]=[], encryption_action: Optional[ActionType]=None, encryption_key: Optional[Path]=None, force: Optional[bool]=False) -> bool: """Pushes the given projects from the local drive to the cloud. diff --git a/tests/commands/cloud/test_push.py b/tests/commands/cloud/test_push.py index c77927a0..46f49038 100644 --- a/tests/commands/cloud/test_push.py +++ b/tests/commands/cloud/test_push.py @@ -87,6 +87,41 @@ def test_cloud_push_pushes_single_project_when_project_option_given() -> None: push_manager.push_project.assert_called_once_with(Path.cwd() / "Python Project", None, None, False) +@pytest.mark.parametrize("project_names, options", [ + (["Python Project"], ["--project", "Python Project"]), + (["Library/Python Library", "Python Project"], ["--project", "Python Project"]), + (["Python Project", "CSharp Project", "Library/Python Library", "Library/CSharp Library"], []), +], ids=["single-project", "referenced-library", "all-projects"]) +def test_cloud_push_exits_nonzero_on_write_permission_failure(project_names, options) -> None: + create_fake_lean_cli_directory() + api_client = mock.Mock() + init_container(api_client_to_use=api_client) + + cloud_projects = {} + for project_id, name in enumerate(project_names, start=1): + container.project_config_manager.get_project_config(Path.cwd() / name).set("cloud-id", project_id) + cloud_projects[project_id] = create_api_project(project_id, name) + if options and len(project_names) > 1: + container.library_manager.add_lean_library_reference_to_project(Path.cwd() / "Python Project", + Path.cwd() / "Library/Python Library") + + api_client.projects.get.side_effect = lambda project_id, organization_id: cloud_projects[project_id] + locked = RequestFailedError(mock.Mock(status_code=403), "Another collaborator controls the write permission") + api_client.projects.update.side_effect = [locked] + [None] * (len(project_names) - 1) + + result = CliRunner().invoke(lean, ["cloud", "push", *options]) + + updated = [call.kwargs["name"] for call in api_client.projects.update.call_args_list] + assert sorted(updated) == sorted(project_names) + if options: + assert updated == project_names # Referenced libraries are attempted before the project. + output = " ".join(result.output.split()) + assert f"Cannot push '{Path(updated[0])}': {locked}" in output + assert "Please pull any required changes and push with --force" in output + assert result.exit_code != 0 + assert isinstance(result.exception, RuntimeError) + + def test_cloud_push_aborts_when_given_directory_is_not_lean_project() -> None: create_fake_lean_cli_directory() @@ -338,7 +373,7 @@ def test_cloud_push_writes_no_files_when_a_too_large_push_turns_on_encryption() result = CliRunner().invoke(lean, ["cloud", "push", "--project", project_path, "--encrypt", "--key", encryption_file_path]) - assert result.exit_code == 0 + assert result.exit_code != 0 # The cloud may still refuse the key, so the files must not be pushed one by one before it is accepted api_client.projects.update.assert_called_once() api_client.files.create.assert_not_called() @@ -411,7 +446,7 @@ def test_cloud_push_aborts_sending_encrypted_files_when_local_file_encrypted_wit result = CliRunner().invoke(lean, ["cloud", "push", "--project", project_path, "--encrypt", "--key", encryption_file_path_y]) - assert result.exit_code == 0 + assert result.exit_code != 0 api_client.projects.update.assert_not_called() def test_cloud_push_aborts_when_local_files_encrypted_with_key_x_and_new_cloud_project_given_key_y() -> None: @@ -443,7 +478,7 @@ def test_cloud_push_aborts_when_local_files_encrypted_with_key_x_and_new_cloud_p result = CliRunner().invoke(lean, ["cloud", "push", "--project", project_path, "--encrypt", "--key", encryption_file_path_y]) - assert result.exit_code == 0 + assert result.exit_code != 0 assert "is not encrypted with the encryption key" in " ".join(result.output.split()) api_client.projects.update.assert_not_called() @@ -631,7 +666,7 @@ def test_cloud_push_aborts_when_local_files_in_encrypted_state_with_key_x_and_cl result = CliRunner().invoke(lean, ["cloud", "push", "--project", project_path]) - assert result.exit_code == 0 + assert result.exit_code != 0 api_client.projects.update.assert_not_called() @@ -671,7 +706,7 @@ def test_cloud_push_aborts_without_updating_cloud_project_when_project_pulled_en result = CliRunner().invoke(lean, ["cloud", "push", "--project", "Python Project"]) - assert result.exit_code == 0 + assert result.exit_code != 0 assert "its files are encrypted" in result.output api_client.projects.update.assert_not_called() api_client.projects.create.assert_not_called()