Skip to content

Commit

Permalink
Repository version user-entered options order fix
Browse files Browse the repository at this point in the history
The order in which the user enters options (--version, --repository, ..) no longer matters for
'pulp <plugin> repository version' commands, since all API calls are now deferred to after the
options' callbacks are processed.

closes pulp#650
  • Loading branch information
MichalPysik committed Feb 19, 2024
1 parent bd1c96f commit 6c12872
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGES/650.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
User-entered order of parameters no longer matters for repository version commands.
11 changes: 11 additions & 0 deletions pulp-glue/pulp_glue/common/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,17 @@ def scope(self) -> Dict[str, Any]:
else:
return {self.repository_ctx.HREF: self.repository_ctx.pulp_href}

@PulpEntityContext.entity.getter
def entity(self) -> EntityDefinition:
if (
self._entity is None
and self._entity_lookup
and "number" in self._entity_lookup.keys()
and self._entity_lookup.get("number") is None
):
self.pulp_href = self.repository_ctx.entity["latest_version_href"]
return super().entity

def repair(self, href: Optional[str] = None) -> Any:
"""
Trigger a repair task for this repository version.
Expand Down
11 changes: 3 additions & 8 deletions pulpcore/cli/common/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,14 +414,9 @@ def _href_callback(
def _version_callback(
ctx: click.Context, param: click.Parameter, value: t.Optional[int]
) -> t.Optional[int]:
entity_ctx = ctx.find_object(PulpEntityContext)
assert entity_ctx is not None
repository_ctx = ctx.find_object(PulpRepositoryContext)
assert repository_ctx is not None
if value is not None:
entity_ctx.pulp_href = f"{repository_ctx.entity['versions_href']}{value}/"
else:
entity_ctx.pulp_href = repository_ctx.entity["latest_version_href"]
repository_version_ctx = ctx.find_object(PulpRepositoryVersionContext)
assert repository_version_ctx is not None
repository_version_ctx.entity = {"number": value}
return value


Expand Down
2 changes: 1 addition & 1 deletion tests/scripts/pulp_file/test_sync.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ expect_succ pulp file repository version repair --repository "cli_test_file_repo
test "$(echo "$OUTPUT" | jq -r '.state')" = "completed"

# Delete version again
expect_succ pulp file repository version destroy --repository "cli_test_file_repository" --version 1
expect_succ pulp file repository version destroy --version 1 --repository "cli_test_file_repository"

# Test autopublish
expect_succ pulp file repository create --name "$autopublish_repo" --remote "cli_test_file_remote" --autopublish
Expand Down
44 changes: 44 additions & 0 deletions tests/test_help_pages.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import typing as t
from functools import reduce

import click
import pytest
Expand Down Expand Up @@ -57,3 +58,46 @@ def test_access_help(no_api: None, subtests: SubTests) -> None:
assert result.stdout.startswith("Usage:") or result.stdout.startswith(
"DeprecationWarning:"
)


@pytest.mark.parametrize(
"command,options",
[
(
[
"file",
"repository",
"show",
],
[
"--repository",
"dummy",
],
),
pytest.param(
[
"file",
"repository",
"version",
"show",
],
[
"--repository",
"dummy",
"--version",
"42",
],
),
],
)
def test_deferred_context(
monkeypatch: pytest.MonkeyPatch, no_api: None, command: t.List[str], options: t.List[str]
) -> None:
monkeypatch.setattr(
reduce(lambda com, sub: com.commands[sub], command, main),
"callback",
lambda: None,
)
runner = CliRunner()
result = runner.invoke(main, command + options)
assert result.exit_code == 0

0 comments on commit 6c12872

Please sign in to comment.