From e2d29d97279e28601977db75ad849aa4356c16d9 Mon Sep 17 00:00:00 2001 From: Mousa Zeid Baker Date: Wed, 23 Mar 2022 21:30:53 +0100 Subject: [PATCH 1/2] feat: add option to skip dependencies with an exact version --- pyproject.toml | 2 +- src/poetryup/main.py | 6 +- src/poetryup/pyproject.py | 12 ++- ...project_with_exact_version_dependency.toml | 32 +++++++ ...project_with_exact_version_dependency.toml | 32 +++++++ tests/unit/test_pyproject.py | 86 +++++++++++++++++++ 6 files changed, 166 insertions(+), 4 deletions(-) create mode 100644 tests/unit/fixtures/expected_pyproject/pyproject_with_exact_version_dependency.toml create mode 100644 tests/unit/fixtures/input_pyproject/pyproject_with_exact_version_dependency.toml diff --git a/pyproject.toml b/pyproject.toml index 718a5b4..a5cf0bf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "poetryup" -version = "0.5.6" +version = "0.6.0" description = "Update dependencies and bump their version in the pyproject.toml file" authors = ["Mousa Zeid Baker"] packages = [ diff --git a/src/poetryup/main.py b/src/poetryup/main.py index 70598ac..d087dc6 100644 --- a/src/poetryup/main.py +++ b/src/poetryup/main.py @@ -19,6 +19,10 @@ def poetryup( default=False, help="Whether to update dependencies to their latest version.", ), + no_exact: bool = typer.Option( + default=False, + help="Whether to skip dependencies with an exact version.", + ), ): """Update dependencies and bump their version in pyproject.toml file""" @@ -30,7 +34,7 @@ def poetryup( ) pyproject = Pyproject(pyproject_str) - pyproject.update_dependencies(latest) + pyproject.update_dependencies(latest, no_exact) Path("pyproject.toml").write_text(pyproject.dumps()) diff --git a/src/poetryup/pyproject.py b/src/poetryup/pyproject.py index c80a032..da0c0c5 100644 --- a/src/poetryup/pyproject.py +++ b/src/poetryup/pyproject.py @@ -30,7 +30,7 @@ def constraint(self) -> str: elif type(self.version) is items.InlineTable: if self.version.get("version", "").startswith(("^", "~")): return self.version["version"][0] - return "" + return "" # dependencies with exact version or multiple versions class Pyproject: @@ -138,11 +138,16 @@ def list_lock_dependencies(self) -> List[Dependency]: return dependencies - def update_dependencies(self, latest: bool = False) -> None: + def update_dependencies( + self, + latest: bool = False, + no_exact: bool = False, + ) -> None: """Update dependencies and bump their version in pyproject Args: latest: Whether to update dependencies to their latest version + no_exact: Whether to skip dependencies with an exact version """ if latest: @@ -152,6 +157,9 @@ def update_dependencies(self, latest: bool = False) -> None: # other groups = {} for dependency in self.list_dependencies(): + if no_exact and dependency.constraint == "": + # skip dependencies with an exact version + continue if type(dependency.version) is items.String: groups[dependency.group] = groups.get( dependency.group, [] diff --git a/tests/unit/fixtures/expected_pyproject/pyproject_with_exact_version_dependency.toml b/tests/unit/fixtures/expected_pyproject/pyproject_with_exact_version_dependency.toml new file mode 100644 index 0000000..d753455 --- /dev/null +++ b/tests/unit/fixtures/expected_pyproject/pyproject_with_exact_version_dependency.toml @@ -0,0 +1,32 @@ +[tool.poetry] +name = "test-poetryup" +version = "0.1.0" +description = "Test PoetryUp" +authors = ["Mousa Zeid Baker"] +packages = [ + { include = "test_poetryup", from = "src" } +] +license = "MIT" +readme = "README.md" +homepage = "https://github.com/MousaZeidBaker/poetryup" +repository = "https://github.com/MousaZeidBaker/poetryup" +keywords=[ + "packaging", + "dependency", + "poetry", + "poetryup", +] +include = ["LICENSE"] + +[tool.poetry.dependencies] +python = "^3.6" +poetryup = "0.2.0" + +[tool.poetry.dev-dependencies] + +[tool.poetry.scripts] +poetryup = "src.test_poetryup.test_poetryup:main" + +[build-system] +requires = ["poetry-core>=1.0.0"] +build-backend = "poetry.core.masonry.api" diff --git a/tests/unit/fixtures/input_pyproject/pyproject_with_exact_version_dependency.toml b/tests/unit/fixtures/input_pyproject/pyproject_with_exact_version_dependency.toml new file mode 100644 index 0000000..fbcfe8e --- /dev/null +++ b/tests/unit/fixtures/input_pyproject/pyproject_with_exact_version_dependency.toml @@ -0,0 +1,32 @@ +[tool.poetry] +name = "test-poetryup" +version = "0.1.0" +description = "Test PoetryUp" +authors = ["Mousa Zeid Baker"] +packages = [ + { include = "test_poetryup", from = "src" } +] +license = "MIT" +readme = "README.md" +homepage = "https://github.com/MousaZeidBaker/poetryup" +repository = "https://github.com/MousaZeidBaker/poetryup" +keywords=[ + "packaging", + "dependency", + "poetry", + "poetryup", +] +include = ["LICENSE"] + +[tool.poetry.dependencies] +python = "^3.6" +poetryup = "0.1.0" + +[tool.poetry.dev-dependencies] + +[tool.poetry.scripts] +poetryup = "src.test_poetryup.test_poetryup:main" + +[build-system] +requires = ["poetry-core>=1.0.0"] +build-backend = "poetry.core.masonry.api" diff --git a/tests/unit/test_pyproject.py b/tests/unit/test_pyproject.py index 712c985..b0beecb 100644 --- a/tests/unit/test_pyproject.py +++ b/tests/unit/test_pyproject.py @@ -1,6 +1,8 @@ import os from pathlib import Path +from pytest_mock import MockerFixture + from poetryup.pyproject import Pyproject @@ -211,6 +213,90 @@ def test_update_dependencies_latest_with_dependency_groups( ) +def test_update_dependencies_with_exact_version_dependency( + mock_poetry_commands, +) -> None: + pyproject_str = Path( + os.path.join( + os.path.dirname(__file__), + "fixtures/input_pyproject/", + "pyproject_with_exact_version_dependency.toml", + ) + ).read_text() + expected_pyproject_str = Path( + os.path.join( + os.path.dirname(__file__), + "fixtures/expected_pyproject/", + "pyproject_with_exact_version_dependency.toml", + ) + ).read_text() + + pyproject = Pyproject(pyproject_str) + pyproject.update_dependencies() + assert pyproject.dumps() == expected_pyproject_str + assert ( + pyproject.pyproject["tool"]["poetry"]["dependencies"]["poetryup"] + == "0.2.0" + ) + + +def test_update_dependencies_latest_with_exact_version_dependency( + mock_poetry_commands, +) -> None: + pyproject_str = Path( + os.path.join( + os.path.dirname(__file__), + "fixtures/input_pyproject/", + "pyproject_with_exact_version_dependency.toml", + ) + ).read_text() + expected_pyproject_str = Path( + os.path.join( + os.path.dirname(__file__), + "fixtures/expected_pyproject/", + "pyproject_with_exact_version_dependency.toml", + ) + ).read_text() + + pyproject = Pyproject(pyproject_str) + pyproject.update_dependencies(latest=True) + assert pyproject.dumps() == expected_pyproject_str + assert ( + pyproject.pyproject["tool"]["poetry"]["dependencies"]["poetryup"] + == "0.2.0" + ) + + +def test_update_dependencies_latest_no_exact_with_exact_version_dependency( + mock_poetry_commands, + mocker: MockerFixture, +) -> None: + pyproject_str = Path( + os.path.join( + os.path.dirname(__file__), + "fixtures/input_pyproject/", + "pyproject_with_exact_version_dependency.toml", + ) + ).read_text() + + mocker.patch.object( + Pyproject, + "_Pyproject__run_poetry_show", + return_value=( + "poetryup 0.1.0 Update dependencies and bump their version in the " + "pyproject.toml file" + "\nā””ā”€ā”€ toml >=0.10.2,<0.11.0\n" + ), + ) + + pyproject = Pyproject(pyproject_str) + pyproject.update_dependencies(latest=True) + assert ( + pyproject.pyproject["tool"]["poetry"]["dependencies"]["poetryup"] + == "0.1.0" + ) + + def test_update_dependencies_with_git_dependency( mock_poetry_commands, ) -> None: From 283e7719b7bcfa4c7e8d979a23d97cf4621ed3d8 Mon Sep 17 00:00:00 2001 From: Mousa Zeid Baker Date: Wed, 23 Mar 2022 21:38:51 +0100 Subject: [PATCH 2/2] feat: rename no_exact option to skip_exact --- src/poetryup/main.py | 4 ++-- src/poetryup/pyproject.py | 6 +++--- tests/unit/test_pyproject.py | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/poetryup/main.py b/src/poetryup/main.py index d087dc6..8cb9e92 100644 --- a/src/poetryup/main.py +++ b/src/poetryup/main.py @@ -19,7 +19,7 @@ def poetryup( default=False, help="Whether to update dependencies to their latest version.", ), - no_exact: bool = typer.Option( + skip_exact: bool = typer.Option( default=False, help="Whether to skip dependencies with an exact version.", ), @@ -34,7 +34,7 @@ def poetryup( ) pyproject = Pyproject(pyproject_str) - pyproject.update_dependencies(latest, no_exact) + pyproject.update_dependencies(latest, skip_exact) Path("pyproject.toml").write_text(pyproject.dumps()) diff --git a/src/poetryup/pyproject.py b/src/poetryup/pyproject.py index da0c0c5..aafd701 100644 --- a/src/poetryup/pyproject.py +++ b/src/poetryup/pyproject.py @@ -141,13 +141,13 @@ def list_lock_dependencies(self) -> List[Dependency]: def update_dependencies( self, latest: bool = False, - no_exact: bool = False, + skip_exact: bool = False, ) -> None: """Update dependencies and bump their version in pyproject Args: latest: Whether to update dependencies to their latest version - no_exact: Whether to skip dependencies with an exact version + skip_exact: Whether to skip dependencies with an exact version """ if latest: @@ -157,7 +157,7 @@ def update_dependencies( # other groups = {} for dependency in self.list_dependencies(): - if no_exact and dependency.constraint == "": + if skip_exact and dependency.constraint == "": # skip dependencies with an exact version continue if type(dependency.version) is items.String: diff --git a/tests/unit/test_pyproject.py b/tests/unit/test_pyproject.py index b0beecb..fee083e 100644 --- a/tests/unit/test_pyproject.py +++ b/tests/unit/test_pyproject.py @@ -267,7 +267,7 @@ def test_update_dependencies_latest_with_exact_version_dependency( ) -def test_update_dependencies_latest_no_exact_with_exact_version_dependency( +def test_update_dependencies_latest_skip_exact_with_exact_version_dependency( mock_poetry_commands, mocker: MockerFixture, ) -> None: