Skip to content

Commit

Permalink
feat: add --preserve-wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
mzakharovcy committed Oct 17, 2023
1 parent 77b6ff4 commit ca9fcdf
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/poetry_plugin_up/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ class UpCommand(InstallerCommand):
description="Output bumped <comment>pyproject.toml</> but do not "
"execute anything.",
),
option(
long_name="preserve-wildcard",
short_name=None,
description="Do not bump wildcard dependencies "
"when updating to latest.",
),
]

def handle(self) -> int:
Expand All @@ -68,11 +74,18 @@ def handle(self) -> int:
no_install = self.option("no-install")
dry_run = self.option("dry-run")
exclude = self.option("exclude")
preserve_wildcard = self.option("preserve-wildcard")

if pinned and not latest:
self.line_error("'--pinned' specified without '--latest'")
raise Exception

if preserve_wildcard and not latest:
self.line_error(
"'--preserve-wildcard' specified without '--latest'"
)
raise Exception

selector = VersionSelector(self.poetry.pool)
pyproject_content = self.poetry.file.read()
original_pyproject_content = self.poetry.file.read()
Expand All @@ -87,6 +100,7 @@ def handle(self) -> int:
pyproject_content=pyproject_content,
selector=selector,
exclude=exclude,
preserve_wildcard=preserve_wildcard,
)

if dry_run:
Expand Down Expand Up @@ -125,6 +139,7 @@ def handle_dependency(
pyproject_content: TOMLDocument,
selector: VersionSelector,
exclude: List[str],
preserve_wildcard: bool,
) -> None:
"""Handles a dependency"""

Expand All @@ -134,6 +149,7 @@ def handle_dependency(
latest,
pinned,
exclude,
preserve_wildcard,
):
return

Expand Down Expand Up @@ -180,6 +196,7 @@ def is_bumpable(
latest: bool,
pinned: bool,
exclude: List[str],
preserve_wildcard: bool,
) -> bool:
"""Determines if a dependency can be bumped in pyproject.toml"""

Expand All @@ -193,6 +210,9 @@ def is_bumpable(
return False

constraint = dependency.pretty_constraint
if preserve_wildcard and constraint == "*":
return False

if not latest:
if is_pinned(constraint):
# pinned
Expand Down
37 changes: 37 additions & 0 deletions tests/e2e/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,40 @@ def test_command_with_exclude(
)
assert PyProjectTOML(tmp_pyproject_path).file.read() == expected
command_call.assert_called_once_with(name="update")


def test_command_preserve_wildcard(
app_tester: ApplicationTester,
packages: List[Package],
mocker: MockerFixture,
project_path: Path,
tmp_pyproject_path: Path,
) -> None:
command_call = mocker.patch(
"poetry.console.commands.command.Command.call",
return_value=0,
)
mocker.patch(
"poetry.version.version_selector.VersionSelector.find_best_candidate",
side_effect=packages,
)
mocker.patch(
"poetry.console.commands.installer_command.InstallerCommand.reset_poetry", # noqa: E501
return_value=None,
)

path = (
project_path
/ "expected_pyproject_with_latest_and_preserve_wildcard.toml"
)
expected = PyProjectTOML(path).file.read()

assert app_tester.execute("up --latest --preserve-wildcard") == 0
assert PyProjectTOML(tmp_pyproject_path).file.read() == expected
command_call.assert_called_once_with(name="update")


def test_preserve_wildcard_without_latest_fails(
app_tester: ApplicationTester,
) -> None:
assert app_tester.execute("up --preserve-wildcard") == 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[tool.poetry]
name = "simple-project"
version = "1.2.3"
description = "Some description."
authors = ["Mousa Zeid Baker"]
license = "MIT"

[tool.poetry.dependencies]
python = "^3.7"
foo = "^2.2.2"
bar = "^2.2.2"
baz = {version = "^2.2.2", extras = ["qux", "quux"]}
corge = {version = "^2.2.2", optional = true}
grault = {version = "^2.2.2", allow-prereleases = true}
garply = {path = "./"}
waldo = {git = "https://example.com/test/project.git"}

[tool.poetry.group.dev.dependencies]
fred = "1.1.1"
plugh = "^2.2.2"
xyzzy = "~2.2.2"
nacho = "^2.2.2"
thud = "^2.2.2"

[tool.poetry.group.docs.dependencies]
foobar = "^2.2.2"
foobaz = "^2.2.2"
fooqux = "^2.2.2"
fooquux = "*"
Foo_Corge = "^2.2.2"
43 changes: 43 additions & 0 deletions tests/integration/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def test_handle_dependency(
exclude=[],
pyproject_content=content,
selector=selector,
preserve_wildcard=False,
)

selector.find_best_candidate.assert_called_once_with(
Expand Down Expand Up @@ -87,6 +88,7 @@ def test_handle_dependency_with_latest(
exclude=[],
pyproject_content=content,
selector=selector,
preserve_wildcard=False,
)

selector.find_best_candidate.assert_called_once_with(
Expand Down Expand Up @@ -134,6 +136,7 @@ def test_handle_dependency_with_zero_caret(
exclude=[],
pyproject_content=content,
selector=selector,
preserve_wildcard=False,
)

selector.find_best_candidate.assert_called_once_with(
Expand Down Expand Up @@ -177,6 +180,46 @@ def test_handle_dependency_excluded(
exclude=["foo"],
pyproject_content=content,
selector=selector,
preserve_wildcard=False,
)

selector.find_best_candidate.assert_not_called()
bump_version_in_pyproject_content.assert_not_called()


def test_handle_dependency_preserve_wildcard(
up_cmd_tester: TestUpCommand,
mocker: MockerFixture,
) -> None:
dependency = Dependency(
name="foo",
constraint="*",
groups=["main"],
)
new_version = "2.0.0"
package = Package(
name=dependency.name,
version=new_version,
)

content = parse("")

selector = Mock()
selector.find_best_candidate = Mock(return_value=package)
bump_version_in_pyproject_content = mocker.patch(
"poetry_plugin_up.command.UpCommand.bump_version_in_pyproject_content",
return_value=None,
)

up_cmd_tester.handle_dependency(
dependency=dependency,
latest=True,
pinned=False,
only_packages=[],
exclude=[],
pyproject_content=content,
selector=selector,
preserve_wildcard=True,
)

selector.find_best_candidate.assert_not_called()
Expand Down
Loading

0 comments on commit ca9fcdf

Please sign in to comment.