Skip to content

Commit

Permalink
fix: remove print(), use Path everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
GabDug committed Jul 3, 2023
1 parent 8a5caf9 commit a8ef720
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
4 changes: 2 additions & 2 deletions src/sync_pre_commit_lock/actions/sync_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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[:]
Expand All @@ -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)


Expand Down
6 changes: 4 additions & 2 deletions src/sync_pre_commit_lock/config.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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", {})
Expand Down
1 change: 0 additions & 1 deletion src/sync_pre_commit_lock/pdm_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions src/sync_pre_commit_lock/poetry_plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import os
from pathlib import Path
from typing import TYPE_CHECKING, ClassVar

Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
5 changes: 1 addition & 4 deletions src/sync_pre_commit_lock/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_actions/test_pre_commit_config_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
14 changes: 8 additions & 6 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand All @@ -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})

0 comments on commit a8ef720

Please sign in to comment.