From acceac57268db00eef07401a4948f7506f466cc8 Mon Sep 17 00:00:00 2001 From: jiatolentinoDM Date: Fri, 6 Nov 2026 10:41:26 +0800 Subject: [PATCH] fix: report a move with a conflict as nothing moved `move_resources` moves everything or nothing and raises `ResourceMoveConflictError`. `dm resources move` reports that nothing moved, and `--check` reports that no resources would move. Requires datamasque-python 1.7.0.dev12. --- README.md | 5 +++-- pyproject.toml | 2 +- src/datamasque_cli/commands/resources.py | 21 ++++++++++----------- tests/commands/test_resources.py | 23 ++++++++--------------- uv.lock | 8 ++++---- 5 files changed, 26 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index bdb1867..aa67b4a 100644 --- a/README.md +++ b/README.md @@ -228,8 +228,9 @@ dm resources move --search payroll --to HR --check # Only list the ``` Resources move with their files and runs. `--project`, `--type`, `--search` and names combine; pass at least one. -A resource that clashes with the destination (same name, or a file in the way) stays where it is, the others move, -and the command exits with the `conflict` code, listing each one that stayed. +All of the resources move, or none do. A resource that clashes with the destination (same name, or a file in the way) +stops the move, and the command exits with the `conflict` code, listing each one. A resource already in the +destination stays there, and the others move. ### In-flight masking diff --git a/pyproject.toml b/pyproject.toml index afb5746..93b56aa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,7 +13,7 @@ dependencies = [ "typer>=0.16.0,<1", "rich>=13.8", "tomli-w>=1.0.0,<2", - "datamasque-python>=1.7.0.dev11,<2", + "datamasque-python>=1.7.0.dev12,<2", "pydantic>=2.5,<3", ] classifiers = [ diff --git a/src/datamasque_cli/commands/resources.py b/src/datamasque_cli/commands/resources.py index 7d1ab27..ef0c818 100644 --- a/src/datamasque_cli/commands/resources.py +++ b/src/datamasque_cli/commands/resources.py @@ -4,7 +4,7 @@ import typer from datamasque.client import DataMasqueClient -from datamasque.client.exceptions import DataMasqueApiError +from datamasque.client.exceptions import DataMasqueApiError, ResourceMoveConflictError from datamasque.client.models.project import Project from datamasque.client.models.resource import Resource, ResourceMoveConflict from datamasque.client.models.resource_type import ResourceTypeName @@ -16,8 +16,7 @@ app = typer.Typer(help="Work with resources of every type.", no_args_is_help=True) -MOVE_CONFLICT_MESSAGE = "Some resources did not move" -MOVE_CHECK_CONFLICT_MESSAGE = "Some resources would not move" +MOVE_CHECK_CONFLICT_MESSAGE = "No resources would move" def _describe_resource_count(count: int) -> str: @@ -85,7 +84,8 @@ def move_resources( Move resources to another project, with their files and runs. Select them by project, type, a name search or exact names; the filters combine. - A resource that clashes with the destination stays where it is, and the others move. + All of them move, or none do: a resource that clashes with the destination stops the move. + A resource already in the destination stays there, and the others move. Rulesets and configurations whose library imports resolve differently after the move become invalid. """ if not (names or sources or type_names or search): @@ -139,14 +139,13 @@ def move_resources( try: result = client.move_resources(selected, target) + except ResourceMoveConflictError as exc: + abort( + f"No resources moved to '{target.name}'", + code=ErrorCode.CONFLICT, + details=_get_conflict_details(exc.conflicts), + ) except DataMasqueApiError as exc: abort_api_error(f"Failed to move resources to '{target.name}'", exc) - if result.conflicts: - message = ( - f"{MOVE_CONFLICT_MESSAGE}; {result.moved} moved to '{target.name}'" - if result.moved - else f"No resources moved to '{target.name}'" - ) - abort(message, code=ErrorCode.CONFLICT, details=_get_conflict_details(result.conflicts)) print_success(f"Moved {_describe_resource_count(result.moved)} to '{target.name}'.") diff --git a/tests/commands/test_resources.py b/tests/commands/test_resources.py index 1693384..e4e9e61 100644 --- a/tests/commands/test_resources.py +++ b/tests/commands/test_resources.py @@ -4,6 +4,7 @@ from unittest.mock import MagicMock, patch import pytest +from datamasque.client.exceptions import ResourceMoveConflictError from datamasque.client.models.resource import ( Resource, ResourceMoveConflict, @@ -43,7 +44,7 @@ def client() -> Iterator[MagicMock]: ResourceTypeName.rulesets: [CUSTOMERS, ORDERS, ALREADY_THERE], ResourceTypeName.seeds: [BUILT_IN_SEED], } - client.move_resources.return_value = ResourceMoveResult(moved=2, conflicts=[]) + client.move_resources.return_value = ResourceMoveResult(moved=2) with patch(f"{MODULE}.get_client", return_value=client): yield client @@ -126,20 +127,12 @@ def test_move_with_nothing_outside_the_destination_is_not_found(client: MagicMoc client.move_resources.assert_not_called() -def test_move_that_leaves_resources_behind_names_them_and_fails(client: MagicMock, runner: CliRunner) -> None: - client.move_resources.return_value = ResourceMoveResult(moved=1, conflicts=[CUSTOMERS_CLASH]) - - result = runner.invoke(app, ["resources", "move", "--project", "finance", "--to", "default", "-y"]) - - assert result.exit_code == ExitCode.CONFLICT - assert result.stderr == ( - "Error: Some resources did not move; 1 moved to 'Default':\n Ruleset \"customers\" exists.\n" +def test_move_with_a_conflict_says_none_moved_and_names_it(client: MagicMock, runner: CliRunner) -> None: + response = MagicMock(status_code=409) + client.move_resources.side_effect = ResourceMoveConflictError( + "No resources moved.", response=response, conflicts=[CUSTOMERS_CLASH] ) - -def test_move_that_moves_nothing_says_none_moved(client: MagicMock, runner: CliRunner) -> None: - client.move_resources.return_value = ResourceMoveResult(moved=0, conflicts=[CUSTOMERS_CLASH]) - result = runner.invoke(app, ["resources", "move", "--project", "finance", "--to", "default", "-y"]) assert result.exit_code == ExitCode.CONFLICT @@ -153,7 +146,7 @@ def test_move_lists_a_detail_shared_by_several_conflicts_once(client: MagicMock, result = runner.invoke(app, ["resources", "move", "--project", "finance", "--to", "default", "--check"]) assert result.exit_code == ExitCode.CONFLICT - assert result.stderr == 'Error: Some resources would not move:\n Ruleset "customers" exists.\n' + assert result.stderr == 'Error: No resources would move:\n Ruleset "customers" exists.\n' def test_move_declined_at_the_prompt_moves_nothing(client: MagicMock, runner: CliRunner) -> None: @@ -170,7 +163,7 @@ def test_move_declined_at_the_prompt_moves_nothing(client: MagicMock, runner: Cl ( [CUSTOMERS_CLASH], ExitCode.CONFLICT, - 'Error: Some resources would not move:\n Ruleset "customers" exists.\n', + 'Error: No resources would move:\n Ruleset "customers" exists.\n', ), ], ids=["no-conflicts", "conflict"], diff --git a/uv.lock b/uv.lock index f42616e..7c94c5e 100644 --- a/uv.lock +++ b/uv.lock @@ -162,7 +162,7 @@ dev = [ [package.metadata] requires-dist = [ - { name = "datamasque-python", specifier = ">=1.7.0.dev11,<2", index = "https://test.pypi.org/simple/" }, + { name = "datamasque-python", specifier = ">=1.7.0.dev12,<2", index = "https://test.pypi.org/simple/" }, { name = "pydantic", specifier = ">=2.5,<3" }, { name = "rich", specifier = ">=13.8" }, { name = "tomli-w", specifier = ">=1.0.0,<2" }, @@ -180,15 +180,15 @@ dev = [ [[package]] name = "datamasque-python" -version = "1.7.0.dev11" +version = "1.7.0.dev12" source = { registry = "https://test.pypi.org/simple/" } dependencies = [ { name = "pydantic" }, { name = "requests" }, ] -sdist = { url = "https://test-files.pythonhosted.org/packages/de/04/b099330f0248a4bc932aa43b267c63f9069829f2daf9dd7c0b60c15df9df/datamasque_python-1.7.0.dev11.tar.gz", hash = "sha256:9d71161a70715ebba4099ed7188d2d3db02943a75815f00ce9e19e1b51731b3d", size = 247651, upload-time = "2026-11-03T05:25:25.579Z" } +sdist = { url = "https://test-files.pythonhosted.org/packages/87/f2/31b2cf352148b75340f2d7622645a53967f04b61da88bcfe003d9ed4c3fe/datamasque_python-1.7.0.dev12.tar.gz", hash = "sha256:2270db2591d0b5883ac1ebb271b7b1f1a37bc0a381b267f2bd27f832391785f0", size = 247499, upload-time = "2026-11-06T02:39:32.723Z" } wheels = [ - { url = "https://test-files.pythonhosted.org/packages/c4/0f/1965bc575726088729e525ffc0f16bfb2ffdec8822235a3c6b3f5479f9e4/datamasque_python-1.7.0.dev11-py3-none-any.whl", hash = "sha256:66e125e6373b94b81a8df7cc61ea9be0819448eda100e91cf8485d5355a3332b", size = 101133, upload-time = "2026-11-03T05:25:24.068Z" }, + { url = "https://test-files.pythonhosted.org/packages/b3/ed/aecad534c9f3e7473c41e55bebf9013a720011ef001bbf862b4dfc2f63ae/datamasque_python-1.7.0.dev12-py3-none-any.whl", hash = "sha256:ee5d7e32328ae61efd9f40ce7d8b4abcaa642a906bb4f933661948d85df259eb", size = 101448, upload-time = "2026-11-06T02:39:31.077Z" }, ] [[package]]