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..8cb9e92 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.", ), + skip_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, skip_exact) Path("pyproject.toml").write_text(pyproject.dumps()) diff --git a/src/poetryup/pyproject.py b/src/poetryup/pyproject.py index c80a032..aafd701 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, + skip_exact: bool = False, + ) -> None: """Update dependencies and bump their version in pyproject Args: latest: Whether to update dependencies to their latest version + skip_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 skip_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..fee083e 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_skip_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: