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 22, 2024
1 parent cb91ef3 commit 807416a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 12 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.
20 changes: 17 additions & 3 deletions pulp-glue/pulp_glue/common/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,9 +675,8 @@ def __init__(
self.pulp_ctx.needs_plugin(plugin_requirement)

self._entity = None
if pulp_href is None:
self._entity_lookup = entity or {}
else:
self._entity_lookup = entity or {}
if pulp_href is not None:
self.pulp_href = pulp_href

def call(
Expand Down Expand Up @@ -1160,6 +1159,21 @@ def scope(self) -> Dict[str, Any]:
else:
return {self.repository_ctx.HREF: self.repository_ctx.pulp_href}

@property
def entity(self) -> EntityDefinition:
if (
self._entity is None
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

@entity.setter
def entity(self, value: Optional[EntityDefinition]) -> None:
# ignore needed due to a bug regarding overriding property setter in mypy
PulpEntityContext.entity.fset(self, value) # type: ignore[attr-defined]

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
51 changes: 51 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,53 @@ 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] if isinstance(com, click.Group) else pytest.fail(),
command,
t.cast(click.Command, main),
),
"callback",
lambda: None,
)
runner = CliRunner()
result = runner.invoke(main, command + options)
assert result.exit_code == 0

0 comments on commit 807416a

Please sign in to comment.