diff --git a/pyproject.toml b/pyproject.toml index 1f4678b..649fc40 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -66,7 +66,7 @@ plugins = [ [tool.ruff] line-length = 140 respect-gitignore = true -extend-select = ["Q000", "TCH", "I001", "S"] +extend-select = ["Q000", "TCH", "I001", "S", "T", "PTH"] extend-ignore =["S101"] target-version = "py310" diff --git a/src/sync_pre_commit_lock/actions/sync_hooks.py b/src/sync_pre_commit_lock/actions/sync_hooks.py index 2b0f385..bcf9a8b 100644 --- a/src/sync_pre_commit_lock/actions/sync_hooks.py +++ b/src/sync_pre_commit_lock/actions/sync_hooks.py @@ -63,7 +63,7 @@ def repos_normalized(self) -> set[PreCommitRepo]: return {PreCommitRepo(repo=normalize_git_url(repo.repo), rev=repo.rev) for repo in self.repos} def update_pre_commit_repo_versions(self, new_versions: dict[PreCommitRepo, str]) -> None: - """Fixes the pre-commit hooks to match the lockfile. Preserves comments and formatting as much as possible.""" + """Fix the pre-commit hooks to match the lockfile. Preserve comments and formatting as much as possible.""" original_lines = self.original_file_lines updated_lines = original_lines[:] @@ -82,7 +82,7 @@ def update_pre_commit_repo_versions(self, new_versions: dict[PreCommitRepo, str] if change_count == 0: return - with open(self.pre_commit_config_file_path, "w") as stream: + with self.pre_commit_config_file_path.open("w") as stream: stream.writelines(updated_lines) diff --git a/src/sync_pre_commit_lock/config.py b/src/sync_pre_commit_lock/config.py index f9abae1..e696f05 100644 --- a/src/sync_pre_commit_lock/config.py +++ b/src/sync_pre_commit_lock/config.py @@ -1,6 +1,7 @@ from __future__ import annotations from dataclasses import dataclass, field +from pathlib import Path from typing import TYPE_CHECKING, Any, TypeVar try: @@ -43,9 +44,10 @@ class SyncPreCommitLockConfig: dependency_mapping: PackageRepoMapping = field(default_factory=dict, metadata={"toml": "dependency-mapping"}) -def load_config() -> SyncPreCommitLockConfig: +def load_config(path: Path | None = None) -> SyncPreCommitLockConfig: # XXX We Should not hardcode this, and get the filename from PDM/Poetry/custom resolution - with open("pyproject.toml", "rb") as file: + path = path or Path("pyproject.toml") + with path.open("rb") as file: config_dict = toml.load(file) tool_dict = config_dict.get("tool", {}).get("sync-pre-commit-lock", {}) diff --git a/src/sync_pre_commit_lock/pdm_plugin.py b/src/sync_pre_commit_lock/pdm_plugin.py index 6fa306d..4b553b0 100644 --- a/src/sync_pre_commit_lock/pdm_plugin.py +++ b/src/sync_pre_commit_lock/pdm_plugin.py @@ -67,7 +67,6 @@ def on_pdm_install_setup_pre_commit( return None action = PDMSetupPreCommitHooks(printer, dry_run=dry_run) file_path = project.root / plugin_config.pre_commit_config_file - print(file_path) if not file_path.exists(): printer.info("No pre-commit config file found, skipping pre-commit hook check") return None diff --git a/src/sync_pre_commit_lock/poetry_plugin.py b/src/sync_pre_commit_lock/poetry_plugin.py index 4e58196..09cade7 100644 --- a/src/sync_pre_commit_lock/poetry_plugin.py +++ b/src/sync_pre_commit_lock/poetry_plugin.py @@ -1,6 +1,5 @@ from __future__ import annotations -import os from pathlib import Path from typing import TYPE_CHECKING, ClassVar @@ -63,8 +62,6 @@ def activate(self, application: Application) -> None: def _handle_post_command( self, event: ConsoleTerminateEvent | Event, event_name: str, dispatcher: EventDispatcher ) -> None: - print("SyncPreCommitLockPlugin handling post command") - assert isinstance(event, ConsoleTerminateEvent) if event.exit_code != 0: # The command failed, so the plugin shouldn't do anything @@ -91,7 +88,7 @@ def _handle_post_command( str(p.name): GenericLockedPackage(p.name, str(p.version)) for p in poetry_locked_packages } plugin_config = load_config() - file_path = Path(os.getcwd()) / plugin_config.pre_commit_config_file + file_path = Path().cwd() / plugin_config.pre_commit_config_file SyncPreCommitHooksVersion( printer, diff --git a/src/sync_pre_commit_lock/utils.py b/src/sync_pre_commit_lock/utils.py index 23b42b7..827bb30 100644 --- a/src/sync_pre_commit_lock/utils.py +++ b/src/sync_pre_commit_lock/utils.py @@ -18,10 +18,7 @@ def normalize_git_url(url: str) -> str: scheme = "https" # Lowercase the hostname and remove default port if it exists - if parsed_url.hostname: - netloc = parsed_url.hostname.lower() - else: - netloc = "" + netloc = parsed_url.hostname.lower() if parsed_url.hostname else "" # If netloc is empty (git, ssh URLs), then path contains it. if not netloc: diff --git a/tests/test_actions/test_pre_commit_config_file.py b/tests/test_actions/test_pre_commit_config_file.py index 10759d1..ac5b0cd 100644 --- a/tests/test_actions/test_pre_commit_config_file.py +++ b/tests/test_actions/test_pre_commit_config_file.py @@ -80,6 +80,7 @@ def test_repos_property() -> None: def test_update_pre_commit_repo_versions(mock_open_file: MagicMock, mock_re: MagicMock, mock_diff: MagicMock) -> None: data = {"repos": [{"repo": "repo1", "rev": "rev1"}]} mock_path = MagicMock(spec=Path) + mock_path.open = mock_open(read_data="dummy_stream") original_file_lines = ["repos:\n", " - repo: repo1\n", " rev: rev1\n"] config = PreCommitHookConfig(data, mock_path, original_file_lines=original_file_lines) @@ -91,7 +92,7 @@ def test_update_pre_commit_repo_versions(mock_open_file: MagicMock, mock_re: Mag config.update_pre_commit_repo_versions(new_versions) - mock_open_file.assert_called_once_with(mock_path, "w") # asserts the file is opened for writing + mock_path.open.assert_called_once_with("w") # asserts the file is opened for writing mock_re.sub.assert_called_once_with(r"(?<=rev: )\S*", "rev2", " rev: rev1\n") assert mock_diff.ndiff.called diff --git a/tests/test_config.py b/tests/test_config.py index c0e18ee..668c46c 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -42,11 +42,12 @@ def test_sync_pre_commit_lock_config() -> None: @patch("builtins.open", new_callable=MagicMock) def test_load_config_with_empty_tool_dict(mock_open: MagicMock, mock_load: MagicMock) -> None: expected_config = SyncPreCommitLockConfig() - - actual_config = load_config() + mock_path = MagicMock() + mock_path.open = mock_open(read_data="dummy_stream") + actual_config = load_config(mock_path) assert actual_config == expected_config - mock_open.assert_called_once_with("pyproject.toml", "rb") + mock_path.open.assert_called_once_with("rb") mock_load.assert_called_once() @@ -55,10 +56,11 @@ def test_load_config_with_empty_tool_dict(mock_open: MagicMock, mock_load: Magic @patch("sync_pre_commit_lock.config.from_toml", return_value=SyncPreCommitLockConfig(disable_sync_from_lock=True)) def test_load_config_with_data(mock_from_toml: MagicMock, mock_open: MagicMock, mock_load: MagicMock) -> None: expected_config = SyncPreCommitLockConfig(disable_sync_from_lock=True) - - actual_config = load_config() + mock_path = MagicMock() + mock_path.open = mock_open(read_data="dummy_stream") + actual_config = load_config(mock_path) assert actual_config == expected_config - mock_open.assert_called_once_with("pyproject.toml", "rb") + mock_path.open.assert_called_once_with("rb") mock_load.assert_called_once() mock_from_toml.assert_called_once_with({"disable": True})