From 94bd810fdda9b46fb231c833ff73004451464a67 Mon Sep 17 00:00:00 2001 From: "Axel H." Date: Sat, 2 Nov 2024 16:33:51 +0100 Subject: [PATCH 1/2] fix(sync): ensure extras are preserved --- .../actions/sync_hooks.py | 4 ++- tests/test_actions/test_sync_hooks.py | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/sync_pre_commit_lock/actions/sync_hooks.py b/src/sync_pre_commit_lock/actions/sync_hooks.py index 6661022..10e7c92 100644 --- a/src/sync_pre_commit_lock/actions/sync_hooks.py +++ b/src/sync_pre_commit_lock/actions/sync_hooks.py @@ -4,6 +4,7 @@ from typing import TYPE_CHECKING, NamedTuple, Sequence from packaging.requirements import InvalidRequirement, Requirement +from packaging.specifiers import SpecifierSet from packaging.utils import canonicalize_name from sync_pre_commit_lock.db import DEPENDENCY_MAPPING, REPOSITORY_ALIASES, PackageRepoMapping @@ -162,7 +163,8 @@ def get_pre_commit_repo_hook_new_dependency(self, dependency: str) -> str: if not (locked_version := self.locked_packages.get(normalized_name)): self.printer.debug(f"Additional dependency {dependency} not found in the lockfile. Ignoring.") return dependency - return str(locked_version).replace(normalized_name, requirement.name) + requirement.specifier = SpecifierSet(f"=={locked_version.version}") + return str(requirement) def analyze_repos( self, diff --git a/tests/test_actions/test_sync_hooks.py b/tests/test_actions/test_sync_hooks.py index 73aa390..1293e1b 100644 --- a/tests/test_actions/test_sync_hooks.py +++ b/tests/test_actions/test_sync_hooks.py @@ -415,6 +415,33 @@ def test_analyze_repos_additional_dependencies() -> None: } +def test_analyze_repos_additional_dependencies_preserve_extras() -> None: + printer = MagicMock(spec=Printer) + pre_commit_config_file_path = MagicMock(spec=Path) + locked_packages: dict[str, GenericLockedPackage] = {"lib-name": GenericLockedPackage("lib-name", "2.0.0")} + plugin_config = SyncPreCommitLockConfig() + + syncer = SyncPreCommitHooksVersion( + printer=printer, + pre_commit_config_file_path=pre_commit_config_file_path, + locked_packages=locked_packages, + plugin_config=plugin_config, + ) + pre_commit_repo = PreCommitRepo( + "https://repo_url", "1.2.3", [PreCommitHook("hook", ["lib-name[with,extras]==1.2.2"])] + ) + pre_commit_repos = {pre_commit_repo} + syncer.mapping = {"lib-name": {"repo": "https://repo_url", "rev": "${rev}"}} + + to_fix, _ = syncer.analyze_repos(pre_commit_repos) + + assert to_fix == { + pre_commit_repo: PreCommitRepo( + "https://repo_url", "2.0.0", [PreCommitHook("hook", ["lib-name[extras,with]==2.0.0"])] + ) + } + + def test_analyze_repos_not_in_lock_but_additional_dependencies() -> None: printer = MagicMock(spec=Printer) pre_commit_config_file_path = MagicMock(spec=Path) From aca7357759a0c95333fb0a1bcfb205310ff185b1 Mon Sep 17 00:00:00 2001 From: "Axel H." Date: Sat, 2 Nov 2024 16:46:55 +0100 Subject: [PATCH 2/2] chore(sync): remove not used anymore method --- src/sync_pre_commit_lock/actions/sync_hooks.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/sync_pre_commit_lock/actions/sync_hooks.py b/src/sync_pre_commit_lock/actions/sync_hooks.py index 10e7c92..9934e45 100644 --- a/src/sync_pre_commit_lock/actions/sync_hooks.py +++ b/src/sync_pre_commit_lock/actions/sync_hooks.py @@ -22,9 +22,6 @@ class GenericLockedPackage(NamedTuple): version: str # Add original data here? - def __str__(self) -> str: - return f"{self.name}=={self.version}" - class SyncPreCommitHooksVersion: def __init__(